Removed pointless "systems" folder from core.
3 * =============================================================================
5 * Copyright (C) 2004-2008 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"
43 #include "frame_hooks.h"
45 #ifdef PLATFORM_WINDOWS
46 ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
47 #elif defined PLATFORM_LINUX
48 ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
51 IForward *g_pOnServerCfg = NULL;
52 IForward *g_pOnConfigsExecuted = NULL;
53 IForward *g_pOnAutoConfigsBuffered = NULL;
54 CoreConfig g_CoreConfig;
55 bool g_bConfigsExecd = false;
56 bool g_bServerExecd = false;
57 bool g_bGotServerStart = false;
58 bool g_bGotTrigger = false;
59 ConCommand *g_pExecPtr = NULL;
60 ConVar *g_ServerCfgFile = NULL;
62 void CheckAndFinalizeConfigs();
64 #if defined ORANGEBOX_BUILD
65 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
66 void Hook_ExecDispatchPre(const CCommand &cmd)
68 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
69 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
70 void Hook_ExecDispatchPre()
73 #if !defined ORANGEBOX_BUILD
77 const char *arg = cmd.Arg(1);
81 && strcmp(arg, g_ServerCfgFile->GetString()) == 0)
87 #if defined ORANGEBOX_BUILD
88 void Hook_ExecDispatchPost(const CCommand &cmd)
90 void Hook_ExecDispatchPost()
95 g_bGotTrigger = false;
96 g_bServerExecd = true;
97 CheckAndFinalizeConfigs();
101 void CheckAndFinalizeConfigs()
103 if ((g_bServerExecd || g_ServerCfgFile == NULL)
104 && g_bGotServerStart)
106 #if defined ORANGEBOX_BUILD
107 g_PendingInternalPush = true;
109 SM_InternalCmdTrigger();
114 void CoreConfig::OnSourceModAllInitialized()
116 g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
117 g_pOnServerCfg = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
118 g_pOnConfigsExecuted = g_Forwards.CreateForward("OnConfigsExecuted", ET_Ignore, 0, NULL);
119 g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL);
122 void CoreConfig::OnSourceModShutdown()
124 g_RootMenu.RemoveRootConsoleCommand("config", this);
125 g_Forwards.ReleaseForward(g_pOnServerCfg);
126 g_Forwards.ReleaseForward(g_pOnConfigsExecuted);
127 g_Forwards.ReleaseForward(g_pOnAutoConfigsBuffered);
129 if (g_pExecPtr != NULL)
131 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPre, false);
132 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPost, true);
137 void CoreConfig::OnSourceModLevelChange(const char *mapName)
139 static bool already_checked = false;
141 if (!already_checked)
143 if (engine->IsDedicatedServer())
145 g_ServerCfgFile = icvar->FindVar("servercfgfile");
149 g_ServerCfgFile = icvar->FindVar("lservercfgfile");
152 if (g_ServerCfgFile != NULL)
154 ConCommandBase *pBase = icvar->GetCommands();
155 while (pBase != NULL)
157 if (pBase->IsCommand() && strcmp(pBase->GetName(), "exec") == 0)
161 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
164 g_pExecPtr = (ConCommand *)pBase;
165 if (g_pExecPtr != NULL)
167 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPre, false);
168 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPost, true);
172 g_ServerCfgFile = NULL;
175 already_checked = true;
178 g_bConfigsExecd = false;
179 g_bServerExecd = false;
180 g_bGotServerStart = false;
181 g_bGotTrigger = false;
184 void CoreConfig::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
186 int argcount = command.ArgC();
189 const char *option = command.Arg(2);
190 const char *value = command.Arg(3);
194 ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
196 if (res == ConfigResult_Reject)
198 g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
199 } else if (res == ConfigResult_Ignore) {
200 g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
202 g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
208 g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
211 void CoreConfig::Initialize()
214 char filePath[PLATFORM_MAX_PATH];
216 /* Try to get command line value of core config convar */
217 const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
219 /* If sm_corecfgfile not specified on command line, use default value */
222 corecfg = sm_corecfgfile.GetDefault();
225 /* Format path to config file */
226 g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
228 /* Parse config file */
229 if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
231 /* :TODO: This won't actually log or print anything :( - So fix that somehow */
232 const char *error = textparsers->GetSMCErrorString(err);
233 g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
237 SMCResult CoreConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
240 ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
242 if (err == ConfigResult_Reject)
244 /* This is a fatal error */
245 g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
248 return SMCResult_Continue;
251 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
256 SMGlobalClass *pBase = SMGlobalClass::head;
259 if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
263 pBase = pBase->m_pGlobalClassNext;
266 return ConfigResult_Ignore;
269 bool SM_AreConfigsExecuted()
271 return g_bConfigsExecd;
274 inline bool IsPathSepChar(char c)
276 #if defined PLATFORM_WINDOWS
277 return (c == '\\' || c == '/');
278 #elif defined PLATFORM_LINUX
283 bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
285 bool will_create = false;
287 /* See if we should be creating */
288 if (can_create && cfg->create)
292 /* If the folder does not exist, attempt to create it.
293 * We're awfully nice.
295 const char *folder = cfg->folder.c_str();
296 char path[PLATFORM_MAX_PATH];
297 char build[PLATFORM_MAX_PATH];
299 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
301 if (!g_LibSys.IsPathDirectory(path))
303 char *cur_ptr = path;
306 g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
307 len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
311 /* Find next suitable path */
312 char *next_ptr = cur_ptr;
313 while (*next_ptr != '\0')
315 if (IsPathSepChar(*next_ptr))
323 if (*next_ptr == '\0')
327 len += g_LibSys.PathFormat(&build[len],
331 if (!g_LibSys.CreateFolder(build))
340 /* Check if the file exists. */
341 char file[PLATFORM_MAX_PATH];
342 char local[PLATFORM_MAX_PATH];
344 if (cfg->folder.size())
346 g_LibSys.PathFormat(local,
350 cfg->autocfg.c_str());
352 g_LibSys.PathFormat(local,
355 cfg->autocfg.c_str());
358 g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
360 bool file_exists = g_LibSys.IsPathFile(file);
361 if (!file_exists && will_create)
363 List<const ConVar *> *convars = NULL;
364 if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
366 /* Attempt to create it */
367 FILE *fp = fopen(file, "wt");
370 fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SVN_FULL_VERSION);
371 fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
374 List<const ConVar *>::iterator iter;
376 for (iter = convars->begin(); iter != convars->end(); iter++)
378 const ConVar *cvar = (*iter);
380 if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
388 /* Print comments until there is no more */
389 strncopy(descr, cvar->GetHelpText(), sizeof(descr));
390 while (*dptr != '\0')
392 /* Find the next line */
393 char *next_ptr = dptr;
394 while (*next_ptr != '\0')
396 if (*next_ptr == '\n')
404 fprintf(fp, "// %s\n", dptr);
408 fprintf(fp, "// -\n");
409 fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
412 fprintf(fp, "// Minimum: \"%02f\"\n", x);
416 fprintf(fp, "// Maximum: \"%02f\"\n", x);
418 fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
433 UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
434 engine->ServerCommand(cmd);
440 void SM_DoSingleExecFwds(IPluginContext *ctx)
444 if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
449 if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
455 void SM_ConfigsExecuted_Plugin(unsigned int serial)
457 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
458 while (iter->MorePlugins())
460 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
461 if (plugin->GetSerial() == serial)
463 SM_DoSingleExecFwds(plugin->GetBaseContext());
471 void SM_ExecuteForPlugin(IPluginContext *ctx)
473 CPlugin *plugin = (CPlugin *)g_PluginSys.GetPluginByCtx(ctx->GetContext());
475 unsigned int num = plugin->GetConfigCount();
478 SM_DoSingleExecFwds(ctx);
482 bool can_create = true;
483 for (unsigned int i=0; i<num; i++)
485 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
488 UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
489 engine->ServerCommand(cmd);
493 void SM_ExecuteAllConfigs()
495 if (g_bGotServerStart)
500 engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
502 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
503 while (iter->MorePlugins())
505 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
506 unsigned int num = plugin->GetConfigCount();
507 bool can_create = true;
508 for (unsigned int i=0; i<num; i++)
510 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
516 g_bGotServerStart = true;
517 CheckAndFinalizeConfigs();
520 void SM_ConfigsExecuted_Global()
527 g_bConfigsExecd = true;
529 g_pOnServerCfg->Execute(NULL);
530 g_pOnConfigsExecuted->Execute(NULL);
533 void SM_InternalCmdTrigger()
535 /* Order is important here. We need to buffer things before we send the command out. */
536 g_pOnAutoConfigsBuffered->Execute(NULL);
537 engine->ServerCommand("sm internal 1\n");
538 g_PendingInternalPush = false;