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 *cmdname, const CCommand &command)
76 int argcount = command.ArgC();
79 const char *option = command.Arg(2);
80 const char *value = command.Arg(3);
84 ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
86 if (res == ConfigResult_Reject)
88 g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
89 } else if (res == ConfigResult_Ignore) {
90 g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
92 g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
98 g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
101 void CoreConfig::Initialize()
104 char filePath[PLATFORM_MAX_PATH];
106 /* Try to get command line value of core config convar */
107 const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
109 /* If sm_corecfgfile not specified on command line, use default value */
112 corecfg = sm_corecfgfile.GetDefault();
115 /* Format path to config file */
116 g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
118 /* Parse config file */
119 if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
121 /* :TODO: This won't actually log or print anything :( - So fix that somehow */
122 const char *error = textparsers->GetSMCErrorString(err);
123 g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
127 SMCResult CoreConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
130 ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
132 if (err == ConfigResult_Reject)
134 /* This is a fatal error */
135 g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
138 return SMCResult_Continue;
141 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
146 SMGlobalClass *pBase = SMGlobalClass::head;
149 if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
153 pBase = pBase->m_pGlobalClassNext;
156 return ConfigResult_Ignore;
159 bool SM_AreConfigsExecuted()
161 return g_bConfigsExecd;
164 inline bool IsPathSepChar(char c)
166 #if defined PLATFORM_WINDOWS
167 return (c == '\\' || c == '/');
168 #elif defined PLATFORM_LINUX
173 bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
175 bool will_create = false;
177 /* See if we should be creating */
178 if (can_create && cfg->create)
182 /* If the folder does not exist, attempt to create it.
183 * We're awfully nice.
185 const char *folder = cfg->folder.c_str();
186 char path[PLATFORM_MAX_PATH];
187 char build[PLATFORM_MAX_PATH];
189 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
191 if (!g_LibSys.IsPathDirectory(path))
193 char *cur_ptr = path;
196 g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
197 len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
201 /* Find next suitable path */
202 char *next_ptr = cur_ptr;
203 while (*next_ptr != '\0')
205 if (IsPathSepChar(*next_ptr))
213 if (*next_ptr == '\0')
217 len += g_LibSys.PathFormat(&build[len],
221 if (!g_LibSys.CreateFolder(build))
230 /* Check if the file exists. */
231 char file[PLATFORM_MAX_PATH];
232 char local[PLATFORM_MAX_PATH];
234 if (cfg->folder.size())
236 g_LibSys.PathFormat(local,
240 cfg->autocfg.c_str());
242 g_LibSys.PathFormat(local,
245 cfg->autocfg.c_str());
248 g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
250 bool file_exists = g_LibSys.IsPathFile(file);
251 if (!file_exists && will_create)
253 List<const ConVar *> *convars = NULL;
254 if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
256 /* Attempt to create it */
257 FILE *fp = fopen(file, "wt");
260 fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SVN_FULL_VERSION);
261 fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
264 List<const ConVar *>::iterator iter;
266 for (iter = convars->begin(); iter != convars->end(); iter++)
268 const ConVar *cvar = (*iter);
270 if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
278 /* Print comments until there is no more */
279 strncopy(descr, cvar->GetHelpText(), sizeof(descr));
280 while (*dptr != '\0')
282 /* Find the next line */
283 char *next_ptr = dptr;
284 while (*next_ptr != '\0')
286 if (*next_ptr == '\n')
294 fprintf(fp, "// %s\n", dptr);
298 fprintf(fp, "// -\n");
299 fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
302 fprintf(fp, "// Minimum: \"%02f\"\n", x);
306 fprintf(fp, "// Maximum: \"%02f\"\n", x);
308 fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
323 UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
324 engine->ServerCommand(cmd);
330 void SM_DoSingleExecFwds(IPluginContext *ctx)
334 if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
339 if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
345 void SM_ConfigsExecuted_Plugin(unsigned int serial)
347 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
348 while (iter->MorePlugins())
350 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
351 if (plugin->GetSerial() == serial)
353 SM_DoSingleExecFwds(plugin->GetBaseContext());
361 void SM_ExecuteForPlugin(IPluginContext *ctx)
363 CPlugin *plugin = (CPlugin *)g_PluginSys.GetPluginByCtx(ctx->GetContext());
365 unsigned int num = plugin->GetConfigCount();
368 SM_DoSingleExecFwds(ctx);
372 bool can_create = true;
373 for (unsigned int i=0; i<num; i++)
375 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
378 UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
379 engine->ServerCommand(cmd);
383 void SM_ExecuteAllConfigs()
390 engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
392 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
393 while (iter->MorePlugins())
395 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
396 unsigned int num = plugin->GetConfigCount();
397 bool can_create = true;
398 for (unsigned int i=0; i<num; i++)
400 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
406 engine->ServerCommand("sm internal 1\n");
409 void SM_ConfigsExecuted_Global()
416 g_bConfigsExecd = true;
418 g_pOnServerCfg->Execute(NULL);
419 g_pOnConfigsExecuted->Execute(NULL);