Added support for Dark Messiah engine and game. (no bug, r=me).
The development of this feature would not be possible without the support of the following people from the game's community:
Dylan Riggs, Carl Pettengill, Ed Moreland, and Christian.
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 || defined PLATFORM_APPLE
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 SOURCE_ENGINE >= SE_ORANGEBOX
65 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
66 void Hook_ExecDispatchPre(const CCommand &cmd)
67 #elif SOURCE_ENGINE == SE_DARKMESSIAH
68 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
69 void Hook_ExecDispatchPre()
71 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
72 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
73 void Hook_ExecDispatchPre()
76 #if SOURCE_ENGINE <= SE_DARKMESSIAH
80 const char *arg = cmd.Arg(1);
84 && strcmp(arg, g_ServerCfgFile->GetString()) == 0)
90 #if SOURCE_ENGINE >= SE_ORANGEBOX
91 void Hook_ExecDispatchPost(const CCommand &cmd)
93 void Hook_ExecDispatchPost()
98 g_bGotTrigger = false;
99 g_bServerExecd = true;
100 CheckAndFinalizeConfigs();
104 void CheckAndFinalizeConfigs()
106 if ((g_bServerExecd || g_ServerCfgFile == NULL)
107 && g_bGotServerStart)
109 #if SOURCE_ENGINE >= SE_ORANGEBOX
110 g_PendingInternalPush = true;
112 SM_InternalCmdTrigger();
117 void CoreConfig::OnSourceModAllInitialized()
119 g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
120 g_pOnServerCfg = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
121 g_pOnConfigsExecuted = g_Forwards.CreateForward("OnConfigsExecuted", ET_Ignore, 0, NULL);
122 g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL);
125 void CoreConfig::OnSourceModShutdown()
127 g_RootMenu.RemoveRootConsoleCommand("config", this);
128 g_Forwards.ReleaseForward(g_pOnServerCfg);
129 g_Forwards.ReleaseForward(g_pOnConfigsExecuted);
130 g_Forwards.ReleaseForward(g_pOnAutoConfigsBuffered);
132 if (g_pExecPtr != NULL)
134 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPre, false);
135 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPost, true);
140 void CoreConfig::OnSourceModLevelChange(const char *mapName)
142 static bool already_checked = false;
144 if (!already_checked)
146 if (engine->IsDedicatedServer())
148 g_ServerCfgFile = icvar->FindVar("servercfgfile");
152 g_ServerCfgFile = icvar->FindVar("lservercfgfile");
155 if (g_ServerCfgFile != NULL)
157 g_pExecPtr = FindCommand("exec");
158 if (g_pExecPtr != NULL)
160 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPre, false);
161 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_pExecPtr, Hook_ExecDispatchPost, true);
165 g_ServerCfgFile = NULL;
168 already_checked = true;
171 g_bConfigsExecd = false;
172 g_bServerExecd = false;
173 g_bGotServerStart = false;
174 g_bGotTrigger = false;
177 void CoreConfig::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
179 int argcount = command.ArgC();
182 const char *option = command.Arg(2);
183 const char *value = command.Arg(3);
187 ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
189 if (res == ConfigResult_Reject)
191 g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
192 } else if (res == ConfigResult_Ignore) {
193 g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
195 g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
201 g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
204 void CoreConfig::Initialize()
207 char filePath[PLATFORM_MAX_PATH];
209 /* Try to get command line value of core config convar */
210 const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
212 /* If sm_corecfgfile not specified on command line, use default value */
215 corecfg = sm_corecfgfile.GetDefault();
218 /* Format path to config file */
219 g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
221 /* Parse config file */
222 if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
224 /* :TODO: This won't actually log or print anything :( - So fix that somehow */
225 const char *error = textparsers->GetSMCErrorString(err);
226 g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
230 SMCResult CoreConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
233 ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
235 if (err == ConfigResult_Reject)
237 /* This is a fatal error */
238 g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
241 return SMCResult_Continue;
244 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
249 SMGlobalClass *pBase = SMGlobalClass::head;
252 if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
256 pBase = pBase->m_pGlobalClassNext;
259 return ConfigResult_Ignore;
262 bool SM_AreConfigsExecuted()
264 return g_bConfigsExecd;
267 inline bool IsPathSepChar(char c)
269 #if defined PLATFORM_WINDOWS
270 return (c == '\\' || c == '/');
271 #elif defined PLATFORM_LINUX || defined PLATFORM_POSIX
276 bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
278 bool will_create = false;
280 /* See if we should be creating */
281 if (can_create && cfg->create)
285 /* If the folder does not exist, attempt to create it.
286 * We're awfully nice.
288 const char *folder = cfg->folder.c_str();
289 char path[PLATFORM_MAX_PATH];
290 char build[PLATFORM_MAX_PATH];
292 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
294 if (!g_LibSys.IsPathDirectory(path))
296 char *cur_ptr = path;
299 g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
300 len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
304 /* Find next suitable path */
305 char *next_ptr = cur_ptr;
306 while (*next_ptr != '\0')
308 if (IsPathSepChar(*next_ptr))
316 if (*next_ptr == '\0')
320 len += g_LibSys.PathFormat(&build[len],
324 if (!g_LibSys.CreateFolder(build))
333 /* Check if the file exists. */
334 char file[PLATFORM_MAX_PATH];
335 char local[PLATFORM_MAX_PATH];
337 if (cfg->folder.size())
339 g_LibSys.PathFormat(local,
343 cfg->autocfg.c_str());
345 g_LibSys.PathFormat(local,
348 cfg->autocfg.c_str());
351 g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
353 bool file_exists = g_LibSys.IsPathFile(file);
354 if (!file_exists && will_create)
356 List<const ConVar *> *convars = NULL;
357 if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
359 /* Attempt to create it */
360 FILE *fp = fopen(file, "wt");
363 fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SVN_FULL_VERSION);
364 fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
367 List<const ConVar *>::iterator iter;
369 for (iter = convars->begin(); iter != convars->end(); iter++)
371 const ConVar *cvar = (*iter);
373 if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
381 /* Print comments until there is no more */
382 strncopy(descr, cvar->GetHelpText(), sizeof(descr));
383 while (*dptr != '\0')
385 /* Find the next line */
386 char *next_ptr = dptr;
387 while (*next_ptr != '\0')
389 if (*next_ptr == '\n')
397 fprintf(fp, "// %s\n", dptr);
401 fprintf(fp, "// -\n");
402 fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
405 fprintf(fp, "// Minimum: \"%02f\"\n", x);
409 fprintf(fp, "// Maximum: \"%02f\"\n", x);
411 fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
426 UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
427 engine->ServerCommand(cmd);
433 void SM_DoSingleExecFwds(IPluginContext *ctx)
437 if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
442 if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
448 void SM_ConfigsExecuted_Plugin(unsigned int serial)
450 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
451 while (iter->MorePlugins())
453 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
454 if (plugin->GetSerial() == serial)
456 SM_DoSingleExecFwds(plugin->GetBaseContext());
464 void SM_ExecuteForPlugin(IPluginContext *ctx)
466 CPlugin *plugin = (CPlugin *)g_PluginSys.GetPluginByCtx(ctx->GetContext());
468 unsigned int num = plugin->GetConfigCount();
471 SM_DoSingleExecFwds(ctx);
475 bool can_create = true;
476 for (unsigned int i=0; i<num; i++)
478 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
481 UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
482 engine->ServerCommand(cmd);
486 void SM_ExecuteAllConfigs()
488 if (g_bGotServerStart)
493 engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
495 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
496 while (iter->MorePlugins())
498 CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
499 unsigned int num = plugin->GetConfigCount();
500 bool can_create = true;
501 for (unsigned int i=0; i<num; i++)
503 can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
509 g_bGotServerStart = true;
510 CheckAndFinalizeConfigs();
513 void SM_ConfigsExecuted_Global()
520 g_bConfigsExecd = true;
522 g_pOnServerCfg->Execute(NULL);
523 g_pOnConfigsExecuted->Execute(NULL);
526 void SM_InternalCmdTrigger()
528 /* Order is important here. We need to buffer things before we send the command out. */
529 g_pOnAutoConfigsBuffered->Execute(NULL);
530 engine->ServerCommand("sm internal 1\n");
531 g_PendingInternalPush = false;