3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include <ITextParsers.h>
33 #include "CoreConfig.h"
34 #include "sourcemod.h"
35 #include "sourcemm_api.h"
36 #include "sm_srvcmds.h"
37 #include "sm_version.h"
38 #include "sm_stringutil.h"
39 #include "LibrarySys.h"
41 #include "PluginSys.h"
42 #include "ForwardSys.h"
44 #ifdef PLATFORM_WINDOWS
45 ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
46 #elif defined PLATFORM_LINUX
47 ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
50 IForward *g_pOnServerCfg = NULL;
51 IForward *g_pOnConfigsExecuted = NULL;
52 CoreConfig g_CoreConfig;
53 bool g_bConfigsExecd = false;
55 void CoreConfig::OnSourceModAllInitialized()
57 g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
58 g_pOnServerCfg = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
59 g_pOnConfigsExecuted = g_Forwards.CreateForward("OnConfigsExecuted", ET_Ignore, 0, NULL);
62 void CoreConfig::OnSourceModShutdown()
64 g_RootMenu.RemoveRootConsoleCommand("config", this);
65 g_Forwards.ReleaseForward(g_pOnServerCfg);
66 g_Forwards.ReleaseForward(g_pOnConfigsExecuted);
69 void CoreConfig::OnSourceModLevelChange(const char *mapName)
71 g_bConfigsExecd = false;
74 void CoreConfig::OnRootConsoleCommand(const char *command, unsigned int argcount)
78 const char *option = engine->Cmd_Argv(2);
79 const char *value = engine->Cmd_Argv(3);
83 ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
85 if (res == ConfigResult_Reject)
87 g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
88 } else if (res == ConfigResult_Ignore) {
89 g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
91 g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
97 g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
100 void CoreConfig::Initialize()
103 char filePath[PLATFORM_MAX_PATH];
105 /* Try to get command line value of core config convar */
106 const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
108 /* If sm_corecfgfile not specified on command line, use default value */
111 corecfg = sm_corecfgfile.GetDefault();
114 /* Format path to config file */
115 g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
117 /* Parse config file */
118 if ((err=textparsers->ParseFile_SMC(filePath, this, NULL, NULL)) != SMCParse_Okay)
120 /* :TODO: This won't actually log or print anything :( - So fix that somehow */
121 const char *error = textparsers->GetSMCErrorString(err);
122 g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
126 SMCParseResult CoreConfig::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
129 ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
131 if (err == ConfigResult_Reject)
133 /* This is a fatal error */
134 g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
137 return SMCParse_Continue;
140 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
145 SMGlobalClass *pBase = SMGlobalClass::head;
148 if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
152 pBase = pBase->m_pGlobalClassNext;
155 return ConfigResult_Ignore;
158 bool SM_AreConfigsExecuted()
160 return g_bConfigsExecd;
163 inline bool IsPathSepChar(char c)
165 #if defined PLATFORM_WINDOWS
166 return (c == '\\' || c == '/');
167 #elif defined PLATFORM_LINUX
172 bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
174 bool will_create = false;
176 /* See if we should be creating */
177 if (can_create && cfg->create)
181 /* If the folder does not exist, attempt to create it.
182 * We're awfully nice.
184 const char *folder = cfg->folder.c_str();
185 char path[PLATFORM_MAX_PATH];
186 char build[PLATFORM_MAX_PATH];
188 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
190 if (!g_LibSys.IsPathDirectory(path))
192 char *cur_ptr = path;
195 g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
196 len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
200 /* Find next suitable path */
201 char *next_ptr = cur_ptr;
202 while (*next_ptr != '\0')
204 if (IsPathSepChar(*next_ptr))
212 if (*next_ptr == '\0')
216 len += g_LibSys.PathFormat(&build[len],
220 if (!g_LibSys.CreateFolder(build))
229 /* Check if the file exists. */
230 char file[PLATFORM_MAX_PATH];
231 char local[PLATFORM_MAX_PATH];
233 if (cfg->folder.size())
235 g_LibSys.PathFormat(local,
239 cfg->autocfg.c_str());
241 g_LibSys.PathFormat(local,
244 cfg->autocfg.c_str());
247 g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
249 bool file_exists = g_LibSys.IsPathFile(file);
250 if (!file_exists && will_create)
252 List<const ConVar *> *convars = NULL;
253 if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
255 /* Attempt to create it */
256 FILE *fp = fopen(file, "wt");
259 fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SVN_FULL_VERSION);
260 fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
263 List<const ConVar *>::iterator iter;
265 for (iter = convars->begin(); iter != convars->end(); iter++)
267 const ConVar *cvar = (*iter);
269 if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
277 /* Print comments until there is no more */
278 strncopy(descr, cvar->GetHelpText(), sizeof(descr));
279 while (*dptr != '\0')
281 /* Find the next line */
282 char *next_ptr = dptr;
283 while (*next_ptr != '\0')
285 if (*next_ptr == '\n')
293 fprintf(fp, "// %s\n", dptr);
297 fprintf(fp, "// -\n");
298 fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
301 fprintf(fp, "// Minimum: \"%02f\"\n", x);
305 fprintf(fp, "// Maximum: \"%02f\"\n", x);
307 fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
322 UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
323 engine->ServerCommand(cmd);
329 void SM_DoSingleExecFwds(IPluginContext *ctx)
333 if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
338 if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
344 void SM_ConfigsExecuted_Plugin(unsigned int serial)
346 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
347 while (iter->MorePlugins())
349 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
350 if (plugin->GetSerial() == serial)
352 SM_DoSingleExecFwds(plugin->GetBaseContext());
360 void SM_ExecuteForPlugin(IPluginContext *ctx)
362 CPlugin *plugin = (CPlugin *)g_PluginSys.GetPluginByCtx(ctx->GetContext());
364 unsigned int num = plugin->GetConfigCount();
367 SM_DoSingleExecFwds(ctx);
369 bool can_create = true;
370 for (unsigned int i=0; i<num; i++)
372 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
375 UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
376 engine->ServerCommand(cmd);
380 void SM_ExecuteAllConfigs()
387 engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
389 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
390 while (iter->MorePlugins())
392 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
393 unsigned int num = plugin->GetConfigCount();
394 bool can_create = true;
395 for (unsigned int i=0; i<num; i++)
397 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
403 engine->ServerCommand("sm internal 1\n");
406 void SM_ConfigsExecuted_Global()
413 g_bConfigsExecd = true;
415 g_pOnServerCfg->Execute(NULL);
416 g_pOnConfigsExecuted->Execute(NULL);