3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "CoreConfig.h"
16 #include "sourcemod.h"
17 #include "sourcemm_api.h"
18 #include "sm_srvcmds.h"
19 #include "LibrarySys.h"
20 #include "TextParsers.h"
23 #ifdef PLATFORM_WINDOWS
24 ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
25 #elif defined PLATFORM_LINUX
26 ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
29 CoreConfig g_CoreConfig;
31 void CoreConfig::OnSourceModAllInitialized()
33 g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
36 void CoreConfig::OnSourceModShutdown()
38 g_RootMenu.RemoveRootConsoleCommand("config", this);
41 void CoreConfig::OnRootConsoleCommand(const char *command, unsigned int argcount)
45 const char *option = engine->Cmd_Argv(2);
46 const char *value = engine->Cmd_Argv(3);
50 ConfigResult err = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
52 if (err == ConfigResult_Reject)
54 g_RootMenu.ConsolePrint("Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
55 } else if (err == ConfigResult_Ignore) {
56 g_RootMenu.ConsolePrint("No such config option \"%s\" exists.", option);
58 g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
64 g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
67 void CoreConfig::Initialize()
70 char filePath[PLATFORM_MAX_PATH];
72 /* Try to get command line value of core config convar */
73 const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
75 /* If sm_corecfgfile not specified on command line, use default value */
78 corecfg = sm_corecfgfile.GetDefault();
81 /* Format path to config file */
82 g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetModPath(), corecfg);
84 /* Parse config file */
85 if ((err=g_TextParser.ParseFile_SMC(filePath, this, NULL, NULL))
88 /* :TODO: This won't actually log or print anything :( - So fix that somehow */
89 const char *error = g_TextParser.GetSMCErrorString(err);
90 g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
94 SMCParseResult CoreConfig::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
97 ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
99 if (err == ConfigResult_Reject)
101 /* This is a fatal error */
102 g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
105 return SMCParse_Continue;
108 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
113 SMGlobalClass *pBase = SMGlobalClass::head;
116 if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
120 pBase = pBase->m_pGlobalClassNext;
123 return ConfigResult_Ignore;