core/CoreConfig.cpp
author Asher Baker <asherkin@gmail.com>
Thu May 16 14:58:14 2013 +0100 (2013-05-16)
changeset 3920 e4fa6b583893
parent 3899 fd4ec50a0d6c
child 3942 7aaa00382627
permissions -rw-r--r--
Fixed copy-paste error in 116256b519b5.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <ITextParsers.h>
    33 #include "CoreConfig.h"
    34 #include "sourcemod.h"
    35 #include "sourcemm_api.h"
    36 #include "sm_srvcmds.h"
    37 #include <sourcemod_version.h>
    38 #include "sm_stringutil.h"
    39 #include "LibrarySys.h"
    40 #include "Logger.h"
    41 #include "ForwardSys.h"
    42 #include "frame_hooks.h"
    43 #include "logic_bridge.h"
    44 
    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");
    49 #endif
    50 
    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;
    61 
    62 void CheckAndFinalizeConfigs();
    63 
    64 #if SOURCE_ENGINE == SE_DOTA
    65 SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, void *, const CCommand &);
    66 void Hook_ExecDispatchPre(void *pUnknown, const CCommand &cmd)
    67 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    68 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    69 void Hook_ExecDispatchPre(const CCommand &cmd)
    70 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    71 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    72 void Hook_ExecDispatchPre()
    73 #else
    74 # if SH_IMPL_VERSION >= 4
    75  extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    76 # else
    77  extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    78 # endif
    79 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    80 void Hook_ExecDispatchPre()
    81 #endif
    82 {
    83 #if SOURCE_ENGINE <= SE_DARKMESSIAH
    84 	CCommand cmd;
    85 #endif
    86 
    87 	const char *arg = cmd.Arg(1);
    88 
    89 	if (!g_bServerExecd && arg != NULL && strcmp(arg, g_ServerCfgFile->GetString()) == 0)
    90 	{
    91 		g_bGotTrigger = true;
    92 	}
    93 }
    94 
    95 #if SOURCE_ENGINE == SE_DOTA
    96 void Hook_ExecDispatchPost(void *pUnknown, const CCommand &cmd)
    97 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    98 void Hook_ExecDispatchPost(const CCommand &cmd)
    99 #else
   100 void Hook_ExecDispatchPost()
   101 #endif
   102 {
   103 	if (g_bGotTrigger)
   104 	{
   105 		g_bGotTrigger = false;
   106 		g_bServerExecd = true;
   107 		CheckAndFinalizeConfigs();
   108 	}
   109 }
   110 
   111 void CheckAndFinalizeConfigs()
   112 {
   113 	if ((g_bServerExecd || g_ServerCfgFile == NULL) && g_bGotServerStart)
   114 	{
   115         g_PendingInternalPush = true;
   116 	}
   117 }
   118 
   119 void CoreConfig::OnSourceModAllInitialized()
   120 {
   121 	g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
   122 	g_pOnServerCfg = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
   123 	g_pOnConfigsExecuted = g_Forwards.CreateForward("OnConfigsExecuted", ET_Ignore, 0, NULL);
   124 	g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL);
   125 }
   126 
   127 CoreConfig::CoreConfig() : m_Strings(512)
   128 {
   129 }
   130 
   131 CoreConfig::~CoreConfig()
   132 {
   133 }
   134 
   135 void CoreConfig::OnSourceModShutdown()
   136 {
   137 	g_RootMenu.RemoveRootConsoleCommand("config", this);
   138 	g_Forwards.ReleaseForward(g_pOnServerCfg);
   139 	g_Forwards.ReleaseForward(g_pOnConfigsExecuted);
   140 	g_Forwards.ReleaseForward(g_pOnAutoConfigsBuffered);
   141 
   142 	if (g_pExecPtr != NULL)
   143 	{
   144 		SH_REMOVE_HOOK(ConCommand, Dispatch, g_pExecPtr, SH_STATIC(Hook_ExecDispatchPre), false);
   145 		SH_REMOVE_HOOK(ConCommand, Dispatch, g_pExecPtr, SH_STATIC(Hook_ExecDispatchPost), true);
   146 		g_pExecPtr = NULL;
   147 	}
   148 }
   149 
   150 void CoreConfig::OnSourceModLevelChange(const char *mapName)
   151 {
   152 	static bool already_checked = false;
   153 
   154 	if (!already_checked)
   155 	{
   156 		if (engine->IsDedicatedServer())
   157 		{
   158 			g_ServerCfgFile = icvar->FindVar("servercfgfile");
   159 		}
   160 		else
   161 		{
   162 			g_ServerCfgFile = icvar->FindVar("lservercfgfile");
   163 		}
   164 
   165 		if (g_ServerCfgFile != NULL)
   166 		{
   167 			g_pExecPtr = FindCommand("exec");
   168 			if (g_pExecPtr != NULL)
   169 			{
   170 				SH_ADD_HOOK(ConCommand, Dispatch, g_pExecPtr, SH_STATIC(Hook_ExecDispatchPre), false);
   171 				SH_ADD_HOOK(ConCommand, Dispatch, g_pExecPtr, SH_STATIC(Hook_ExecDispatchPost), true);
   172 			}
   173 			else
   174 			{
   175 				g_ServerCfgFile = NULL;
   176 			}
   177 		}
   178 		already_checked = true;
   179 	}
   180 
   181 	g_bConfigsExecd = false;
   182 	g_bServerExecd = false;
   183 	g_bGotServerStart = false;
   184 	g_bGotTrigger = false;
   185 }
   186 
   187 void CoreConfig::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   188 {
   189 	int argcount = command.ArgC();
   190 	if (argcount >= 4)
   191 	{
   192 		const char *option = command.Arg(2);
   193 		const char *value = command.Arg(3);
   194 
   195 		char error[255];
   196 
   197 		ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
   198 
   199 		if (res == ConfigResult_Reject)
   200 		{
   201 			g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\". (%s)", option, value, error);
   202 		} else if (res == ConfigResult_Ignore) {
   203 			g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
   204 		} else {
   205 			g_RootMenu.ConsolePrint("[SM] Config option \"%s\" successfully set to \"%s\".", option, value);
   206 		}
   207 
   208 		return;
   209 	} else if (argcount >= 3) {
   210 		const char *option = command.Arg(2);
   211 		
   212 		const char *value = GetCoreConfigValue(option);
   213 		
   214 		if (value == NULL)
   215 		{
   216 			g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
   217 		} else {
   218 			g_RootMenu.ConsolePrint("[SM] Config option \"%s\" is set to \"%s\".", option, value);
   219 		}
   220 		
   221 		return;
   222 	}
   223 
   224 	g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> [value]");
   225 }
   226 
   227 void CoreConfig::Initialize()
   228 {
   229 	SMCError err;
   230 	char filePath[PLATFORM_MAX_PATH];
   231 
   232 	/* Try to get command line value of core config convar */
   233 	const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
   234 
   235 	/* If sm_corecfgfile is on the command line, use that
   236 	 * If sm_corecfgfile isn't there, check sm_basepath on the command line and build the path off that
   237 	 * If sm_basepath isn't there, just use the default path for the cfg
   238 	 */
   239 	if (corecfg)
   240 	{
   241 		g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
   242 	}
   243 	else
   244 	{
   245 		const char *basepath = icvar->GetCommandLineValue("sm_basepath");
   246 
   247 		/* Format path to config file */
   248 		if (basepath)
   249 		{
   250 			g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s/%s", g_SourceMod.GetGamePath(), basepath, "configs/core.cfg");
   251 		}
   252 		else
   253 		{
   254 			g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), sm_corecfgfile.GetDefault());
   255 		}
   256 	}
   257 
   258 	/* Reset cached key values */
   259 	m_KeyValues.clear();
   260 	m_Strings.Reset();
   261 
   262 	/* Parse config file */
   263 	if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
   264 	{
   265  		/* :TODO: This won't actually log or print anything :( - So fix that somehow */
   266 		const char *error = textparsers->GetSMCErrorString(err);
   267 		g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
   268 	}
   269 }
   270 
   271 SMCResult CoreConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
   272 {
   273 	char error[255];
   274 	ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
   275 
   276 	if (err == ConfigResult_Reject)
   277 	{
   278 		/* This is a fatal error */
   279 		g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
   280 	}
   281 
   282 	return SMCResult_Continue;
   283 }
   284 
   285 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
   286 {
   287 	ConfigResult result;
   288 
   289 	/* Notify! */
   290 	SMGlobalClass *pBase = SMGlobalClass::head;
   291 	while (pBase)
   292 	{
   293 		if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
   294 		{
   295 			return result;
   296 		}
   297 		pBase = pBase->m_pGlobalClassNext;
   298 	}
   299 
   300 	m_KeyValues.replace(option, m_Strings.AddString(value));
   301 
   302 	return ConfigResult_Ignore;
   303 }
   304 
   305 const char *CoreConfig::GetCoreConfigValue(const char *key)
   306 {
   307 	int *pKey = m_KeyValues.retrieve(key);
   308 	if (pKey == NULL)
   309 	{
   310 		return NULL;
   311 	}
   312 	return m_Strings.GetString(*pKey);
   313 }
   314 
   315 bool SM_AreConfigsExecuted()
   316 {
   317 	return g_bConfigsExecd;
   318 }
   319 
   320 inline bool IsPathSepChar(char c)
   321 {
   322 #if defined PLATFORM_WINDOWS
   323 	return (c == '\\' || c == '/');
   324 #elif defined PLATFORM_LINUX || defined PLATFORM_POSIX
   325 	return (c == '/');
   326 #endif
   327 }
   328 
   329 bool SM_ExecuteConfig(IPlugin *pl, AutoConfig *cfg, bool can_create)
   330 {
   331 	bool will_create = false;
   332 	
   333 	/* See if we should be creating */
   334 	if (can_create && cfg->create)
   335 	{
   336 		will_create = true;
   337 		
   338 		/* If the folder does not exist, attempt to create it.  
   339 		 * We're awfully nice.
   340 		 */
   341 		const char *folder = cfg->folder.c_str();
   342 		char path[PLATFORM_MAX_PATH];
   343 		char build[PLATFORM_MAX_PATH];
   344 
   345 		g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
   346 
   347 		if (!g_LibSys.IsPathDirectory(path))
   348 		{
   349 			char *cur_ptr = path;
   350 			size_t len;
   351 			
   352 			g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
   353 			len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
   354 
   355 			do
   356 			{
   357 				/* Find next suitable path */
   358 				char *next_ptr = cur_ptr;
   359 				while (*next_ptr != '\0')
   360 				{
   361 					if (IsPathSepChar(*next_ptr))
   362 					{
   363 						*next_ptr = '\0';
   364 						next_ptr++;
   365 						break;
   366 					}
   367 					next_ptr++;
   368 				}
   369 				if (*next_ptr == '\0')
   370 				{
   371 					next_ptr = NULL;
   372 				}
   373 				len += g_LibSys.PathFormat(&build[len], 
   374 					sizeof(build)-len,
   375 					"/%s",
   376 					cur_ptr);
   377 				if (!g_LibSys.CreateFolder(build))
   378 				{
   379 					break;
   380 				}
   381 				cur_ptr = next_ptr;
   382 			} while (cur_ptr);
   383 		}
   384 	}
   385 
   386 	/* Check if the file exists. */
   387 	char file[PLATFORM_MAX_PATH];
   388 	char local[PLATFORM_MAX_PATH];
   389 
   390 	if (cfg->folder.size())
   391 	{
   392 		g_LibSys.PathFormat(local, 
   393 			sizeof(local), 
   394 			"%s/%s.cfg",
   395 			cfg->folder.c_str(),
   396 			cfg->autocfg.c_str());
   397 	} else {
   398 		g_LibSys.PathFormat(local,
   399 			sizeof(local),
   400 			"%s.cfg",
   401 			cfg->autocfg.c_str());
   402 	}
   403 	
   404 	g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
   405 
   406 	bool file_exists = g_LibSys.IsPathFile(file);
   407 	if (!file_exists && will_create)
   408 	{
   409 		List<const ConVar *> *convars = NULL;
   410 		if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
   411 		{
   412 			/* Attempt to create it */
   413 			FILE *fp = fopen(file, "wt");
   414 			if (fp)
   415 			{
   416 				fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SM_VERSION_STRING);
   417 				fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
   418 				fprintf(fp, "\n\n");
   419 
   420 				List<const ConVar *>::iterator iter;
   421 				float x;
   422 				for (iter = convars->begin(); iter != convars->end(); iter++)
   423 				{
   424 					const ConVar *cvar = (*iter);
   425 
   426 					if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
   427 					{
   428 						continue;
   429 					}
   430 
   431 					char descr[255];
   432 					char *dptr = descr;
   433 
   434 					/* Print comments until there is no more */
   435 					strncopy(descr, cvar->GetHelpText(), sizeof(descr));
   436 					while (*dptr != '\0')
   437 					{
   438 						/* Find the next line */
   439 						char *next_ptr = dptr;
   440 						while (*next_ptr != '\0')
   441 						{
   442 							if (*next_ptr == '\n')
   443 							{
   444 								*next_ptr = '\0';
   445 								next_ptr++;
   446 								break;
   447 							}
   448 							next_ptr++;
   449 						}
   450 						fprintf(fp, "// %s\n", dptr);
   451 						dptr = next_ptr;
   452 					}
   453 
   454 					fprintf(fp, "// -\n");
   455 					fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
   456 					if (cvar->GetMin(x))
   457 					{
   458 						fprintf(fp, "// Minimum: \"%02f\"\n", x);
   459 					}
   460 					if (cvar->GetMax(x))
   461 					{
   462 						fprintf(fp, "// Maximum: \"%02f\"\n", x);
   463 					}
   464 					fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
   465 					fprintf(fp, "\n");
   466 				}
   467 				fprintf(fp, "\n");
   468 
   469 				file_exists = true;
   470 				can_create = false;
   471 				fclose(fp);
   472 			}
   473 			else
   474 			{
   475 				g_Logger.LogError("Failed to auto generate config for %s, make sure the directory has write permission.", pl->GetFilename());
   476 				return can_create;
   477 			}
   478 		}
   479 	}
   480 
   481 	if (file_exists)
   482 	{
   483 		char cmd[255];
   484 		UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
   485 		engine->ServerCommand(cmd);
   486 	}
   487 
   488 	return can_create;
   489 }
   490 
   491 void SM_DoSingleExecFwds(IPluginContext *ctx)
   492 {
   493 	IPluginFunction *pf;
   494 
   495 	if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
   496 	{
   497 		pf->Execute(NULL);
   498 	}
   499 
   500 	if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
   501 	{
   502 		pf->Execute(NULL);
   503 	}
   504 }
   505 
   506 void SM_ConfigsExecuted_Plugin(unsigned int serial)
   507 {
   508 	IPluginIterator *iter = scripts->GetPluginIterator();
   509 	while (iter->MorePlugins())
   510 	{
   511 		IPlugin *plugin = iter->GetPlugin();
   512 		if (plugin->GetSerial() == serial)
   513 		{
   514 			SM_DoSingleExecFwds(plugin->GetBaseContext());
   515 			break;
   516 		}
   517 		iter->NextPlugin();
   518 	}
   519 	iter->Release();
   520 }
   521 
   522 void SM_ExecuteForPlugin(IPluginContext *ctx)
   523 {
   524 	SMPlugin *plugin = scripts->FindPluginByContext(ctx->GetContext());
   525 
   526 	unsigned int num = plugin->GetConfigCount();
   527 	if (!num)
   528 	{
   529 		SM_DoSingleExecFwds(ctx);
   530 	}
   531 	else
   532 	{
   533 		bool can_create = true;
   534 		for (unsigned int i=0; i<num; i++)
   535 		{
   536 			can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
   537 		}
   538 		char cmd[255];
   539 		UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
   540 		engine->ServerCommand(cmd);
   541 	}
   542 }
   543 
   544 void SM_ExecuteAllConfigs()
   545 {
   546 	if (g_bGotServerStart)
   547 	{
   548 		return;
   549 	}
   550 
   551 	engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
   552 
   553 	AutoPluginList plugins(scripts);
   554 	for (size_t i = 0; i < plugins->size(); i++)
   555 	{
   556 		SMPlugin *plugin = plugins->at(i);
   557 		unsigned int num = plugin->GetConfigCount();
   558 		bool can_create = true;
   559 		for (unsigned int i=0; i<num; i++)
   560 		{
   561 			can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
   562 		}
   563 	}
   564 
   565 	g_bGotServerStart = true;
   566 	CheckAndFinalizeConfigs();
   567 }
   568 
   569 void SM_ConfigsExecuted_Global()
   570 {
   571 	if (g_bConfigsExecd)
   572 	{
   573 		return;
   574 	}
   575 
   576 	g_bConfigsExecd = true;
   577 
   578 	g_pOnServerCfg->Execute(NULL);
   579 	g_pOnConfigsExecuted->Execute(NULL);
   580 }
   581 
   582 void SM_InternalCmdTrigger()
   583 {
   584 	/* Order is important here.  We need to buffer things before we send the command out. */
   585 	g_pOnAutoConfigsBuffered->Execute(NULL);
   586 	engine->ServerCommand("sm internal 1\n");
   587     g_PendingInternalPush = false;
   588 }
   589