1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/CoreConfig.cpp Thu Apr 05 03:02:00 2007 +0000
1.3 @@ -0,0 +1,133 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ===============================================================
1.7 + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
1.8 + * ===============================================================
1.9 + *
1.10 + * This file is not open source and may not be copied without explicit
1.11 + * written permission of AlliedModders LLC. This file may not be redistributed
1.12 + * in whole or significant part.
1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
1.14 + *
1.15 + * Version: $Id$
1.16 + */
1.17 +
1.18 +#include "CoreConfig.h"
1.19 +#include "sourcemod.h"
1.20 +#include "sourcemm_api.h"
1.21 +#include "sm_srvcmds.h"
1.22 +#include "LibrarySys.h"
1.23 +#include "TextParsers.h"
1.24 +#include "Logger.h"
1.25 +
1.26 +#ifdef PLATFORM_WINDOWS
1.27 +ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
1.28 +#else
1.29 +ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
1.30 +#endif
1.31 +
1.32 +CoreConfig g_CoreConfig;
1.33 +
1.34 +void CoreConfig::OnSourceModAllInitialized()
1.35 +{
1.36 + g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
1.37 +}
1.38 +
1.39 +void CoreConfig::OnSourceModShutdown()
1.40 +{
1.41 + g_RootMenu.RemoveRootConsoleCommand("config", this);
1.42 +}
1.43 +
1.44 +void CoreConfig::OnRootConsoleCommand(const char *command, unsigned int argcount)
1.45 +{
1.46 + if (argcount >= 4)
1.47 + {
1.48 + const char *option = engine->Cmd_Argv(2);
1.49 + const char *value = engine->Cmd_Argv(3);
1.50 +
1.51 + CoreConfigErr err = SetConfigOption(option, value);
1.52 +
1.53 + switch (err)
1.54 + {
1.55 + case CoreConfig_NoRuntime:
1.56 + g_RootMenu.ConsolePrint("[SM] Cannot set \"%s\" while SourceMod is running.", option);
1.57 + break;
1.58 + case CoreConfig_InvalidValue:
1.59 + g_RootMenu.ConsolePrint("[SM] Invalid value \"%s\" specified for configuration option \"%s\"", value, option);
1.60 + break;
1.61 + case CoreConfig_InvalidOption:
1.62 + g_RootMenu.ConsolePrint("[SM] Invalid configuration option specified: %s", option);
1.63 + break;
1.64 + default:
1.65 + break;
1.66 + }
1.67 +
1.68 + return;
1.69 + }
1.70 +
1.71 + g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
1.72 +}
1.73 +
1.74 +void CoreConfig::Initialize()
1.75 +{
1.76 + SMCParseError err;
1.77 + char filePath[PLATFORM_MAX_PATH];
1.78 +
1.79 + /* Try to get command line value of core config convar */
1.80 + const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
1.81 +
1.82 + /* If sm_corecfgfile not specified on command line, use default value */
1.83 + if (!corecfg)
1.84 + {
1.85 + corecfg = sm_corecfgfile.GetDefault();
1.86 + }
1.87 +
1.88 + /* Format path to config file */
1.89 + g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetModPath(), corecfg);
1.90 +
1.91 + /* Parse config file */
1.92 + if ((err=g_TextParser.ParseFile_SMC(filePath, this, NULL, NULL))
1.93 + != SMCParse_Okay)
1.94 + {
1.95 + /* :TODO: This won't actually log or print anything :( - So fix that somehow */
1.96 + g_Logger.LogError("[SM] Error encountered parsing core config file: %s", g_TextParser.GetSMCErrorString(err));
1.97 + }
1.98 +}
1.99 +
1.100 +SMCParseResult CoreConfig::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
1.101 +{
1.102 + CoreConfigErr err = SetConfigOption(key, value);
1.103 +
1.104 + if (err == CoreConfig_InvalidOption)
1.105 + {
1.106 + g_Logger.LogError("[SM] Warning: Ignoring invalid option \"%s\" in configuration file.", key);
1.107 + } else if (err == CoreConfig_InvalidValue) {
1.108 + g_Logger.LogError("[SM] Warning encountered parsing core configuration file.");
1.109 + g_Logger.LogError("[SM] Invalid value \"%s\" specified for option \"%s\"", value, key);
1.110 + }
1.111 +
1.112 + return SMCParse_Continue;
1.113 +}
1.114 +
1.115 +CoreConfigErr CoreConfig::SetConfigOption(const char *option, const char *value)
1.116 +{
1.117 + CoreConfigErr err = CoreConfig_TOTAL;
1.118 + CoreConfigErr currentErr;
1.119 +
1.120 + /* Notify! */
1.121 + SMGlobalClass *pBase = SMGlobalClass::head;
1.122 + while (pBase)
1.123 + {
1.124 + currentErr = pBase->OnSourceModConfigChanged(option, value);
1.125 +
1.126 + /* Lowest error code has priority */
1.127 + if (currentErr < err)
1.128 + {
1.129 + err = currentErr;
1.130 + }
1.131 +
1.132 + pBase = pBase->m_pGlobalClassNext;
1.133 + }
1.134 +
1.135 + return err;
1.136 +}