core/CoreConfig.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Jul 04 19:04:33 2008 +0000 (2008-07-04)
changeset 2155 3f1fd7ab3d86
parent 2065 e2e5445a2f9b
child 2166 b2594457bad1
permissions -rw-r--r--
fixed amb1801, IsFakeClient() wasn't working for TF2 causing false results and crashes
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <ITextParsers.h>
ds@1952
    33
#include "CoreConfig.h"
ds@1952
    34
#include "sourcemod.h"
ds@1952
    35
#include "sourcemm_api.h"
ds@1952
    36
#include "sm_srvcmds.h"
ds@1952
    37
#include "sm_version.h"
ds@1952
    38
#include "sm_stringutil.h"
ds@1952
    39
#include "LibrarySys.h"
ds@1952
    40
#include "Logger.h"
ds@1952
    41
#include "PluginSys.h"
ds@1952
    42
#include "ForwardSys.h"
ds@1952
    43
ds@1952
    44
#ifdef PLATFORM_WINDOWS
ds@1952
    45
ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
ds@1952
    46
#elif defined PLATFORM_LINUX
ds@1952
    47
ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
ds@1952
    48
#endif
ds@1952
    49
ds@1952
    50
IForward *g_pOnServerCfg = NULL;
ds@1952
    51
IForward *g_pOnConfigsExecuted = NULL;
dvander@2065
    52
IForward *g_pOnAutoConfigsBuffered = NULL;
ds@1952
    53
CoreConfig g_CoreConfig;
ds@1952
    54
bool g_bConfigsExecd = false;
ds@1952
    55
ds@1952
    56
void CoreConfig::OnSourceModAllInitialized()
ds@1952
    57
{
ds@1952
    58
	g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
ds@1952
    59
	g_pOnServerCfg = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
ds@1952
    60
	g_pOnConfigsExecuted = g_Forwards.CreateForward("OnConfigsExecuted", ET_Ignore, 0, NULL);
dvander@2065
    61
	g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL);
ds@1952
    62
}
ds@1952
    63
ds@1952
    64
void CoreConfig::OnSourceModShutdown()
ds@1952
    65
{
ds@1952
    66
	g_RootMenu.RemoveRootConsoleCommand("config", this);
ds@1952
    67
	g_Forwards.ReleaseForward(g_pOnServerCfg);
ds@1952
    68
	g_Forwards.ReleaseForward(g_pOnConfigsExecuted);
dvander@2065
    69
	g_Forwards.ReleaseForward(g_pOnAutoConfigsBuffered);
ds@1952
    70
}
ds@1952
    71
ds@1952
    72
void CoreConfig::OnSourceModLevelChange(const char *mapName)
ds@1952
    73
{
ds@1952
    74
	g_bConfigsExecd = false;
ds@1952
    75
}
ds@1952
    76
ds@1952
    77
void CoreConfig::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@1952
    78
{
ds@1952
    79
	int argcount = command.ArgC();
ds@1952
    80
	if (argcount >= 4)
ds@1952
    81
	{
ds@1952
    82
		const char *option = command.Arg(2);
ds@1952
    83
		const char *value = command.Arg(3);
ds@1952
    84
ds@1952
    85
		char error[255];
ds@1952
    86
ds@1952
    87
		ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
ds@1952
    88
ds@1952
    89
		if (res == ConfigResult_Reject)
ds@1952
    90
		{
ds@1952
    91
			g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
ds@1952
    92
		} else if (res == ConfigResult_Ignore) {
ds@1952
    93
			g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
ds@1952
    94
		} else {
ds@1952
    95
			g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
ds@1952
    96
		}
ds@1952
    97
ds@1952
    98
		return;
ds@1952
    99
	}
ds@1952
   100
ds@1952
   101
	g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
ds@1952
   102
}
ds@1952
   103
ds@1952
   104
void CoreConfig::Initialize()
ds@1952
   105
{
ds@1952
   106
	SMCError err;
ds@1952
   107
	char filePath[PLATFORM_MAX_PATH];
ds@1952
   108
ds@1952
   109
	/* Try to get command line value of core config convar */
ds@1952
   110
	const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
ds@1952
   111
ds@1952
   112
	/* If sm_corecfgfile not specified on command line, use default value */
ds@1952
   113
	if (!corecfg)
ds@1952
   114
	{
ds@1952
   115
		corecfg = sm_corecfgfile.GetDefault();
ds@1952
   116
	}
ds@1952
   117
ds@1952
   118
	/* Format path to config file */
ds@1952
   119
	g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
ds@1952
   120
ds@1952
   121
	/* Parse config file */
ds@1952
   122
	if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
ds@1952
   123
	{
ds@1952
   124
 		/* :TODO: This won't actually log or print anything :( - So fix that somehow */
ds@1952
   125
		const char *error = textparsers->GetSMCErrorString(err);
ds@1952
   126
		g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
ds@1952
   127
	}
ds@1952
   128
}
ds@1952
   129
ds@1952
   130
SMCResult CoreConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1952
   131
{
ds@1952
   132
	char error[255];
ds@1952
   133
	ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
ds@1952
   134
ds@1952
   135
	if (err == ConfigResult_Reject)
ds@1952
   136
	{
ds@1952
   137
		/* This is a fatal error */
ds@1952
   138
		g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
ds@1952
   139
	}
ds@1952
   140
ds@1952
   141
	return SMCResult_Continue;
ds@1952
   142
}
ds@1952
   143
ds@1952
   144
ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
ds@1952
   145
{
ds@1952
   146
	ConfigResult result;
ds@1952
   147
ds@1952
   148
	/* Notify! */
ds@1952
   149
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   150
	while (pBase)
ds@1952
   151
	{
ds@1952
   152
		if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
ds@1952
   153
		{
ds@1952
   154
			return result;
ds@1952
   155
		}
ds@1952
   156
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   157
	}
ds@1952
   158
ds@1952
   159
	return ConfigResult_Ignore;
ds@1952
   160
}
ds@1952
   161
ds@1952
   162
bool SM_AreConfigsExecuted()
ds@1952
   163
{
ds@1952
   164
	return g_bConfigsExecd;
ds@1952
   165
}
ds@1952
   166
ds@1952
   167
inline bool IsPathSepChar(char c)
ds@1952
   168
{
ds@1952
   169
#if defined PLATFORM_WINDOWS
ds@1952
   170
	return (c == '\\' || c == '/');
ds@1952
   171
#elif defined PLATFORM_LINUX
ds@1952
   172
	return (c == '/');
ds@1952
   173
#endif
ds@1952
   174
}
ds@1952
   175
ds@1952
   176
bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
ds@1952
   177
{
ds@1952
   178
	bool will_create = false;
ds@1952
   179
	
ds@1952
   180
	/* See if we should be creating */
ds@1952
   181
	if (can_create && cfg->create)
ds@1952
   182
	{
ds@1952
   183
		will_create = true;
ds@1952
   184
		
ds@1952
   185
		/* If the folder does not exist, attempt to create it.  
ds@1952
   186
		 * We're awfully nice.
ds@1952
   187
		 */
ds@1952
   188
		const char *folder = cfg->folder.c_str();
ds@1952
   189
		char path[PLATFORM_MAX_PATH];
ds@1952
   190
		char build[PLATFORM_MAX_PATH];
ds@1952
   191
ds@1952
   192
		g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);
ds@1952
   193
ds@1952
   194
		if (!g_LibSys.IsPathDirectory(path))
ds@1952
   195
		{
ds@1952
   196
			char *cur_ptr = path;
ds@1952
   197
			size_t len;
ds@1952
   198
			
ds@1952
   199
			g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
ds@1952
   200
			len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");
ds@1952
   201
ds@1952
   202
			do
ds@1952
   203
			{
ds@1952
   204
				/* Find next suitable path */
ds@1952
   205
				char *next_ptr = cur_ptr;
ds@1952
   206
				while (*next_ptr != '\0')
ds@1952
   207
				{
ds@1952
   208
					if (IsPathSepChar(*next_ptr))
ds@1952
   209
					{
ds@1952
   210
						*next_ptr = '\0';
ds@1952
   211
						next_ptr++;
ds@1952
   212
						break;
ds@1952
   213
					}
ds@1952
   214
					next_ptr++;
ds@1952
   215
				}
ds@1952
   216
				if (*next_ptr == '\0')
ds@1952
   217
				{
ds@1952
   218
					next_ptr = NULL;
ds@1952
   219
				}
ds@1952
   220
				len += g_LibSys.PathFormat(&build[len], 
ds@1952
   221
					sizeof(build)-len,
ds@1952
   222
					"/%s",
ds@1952
   223
					cur_ptr);
ds@1952
   224
				if (!g_LibSys.CreateFolder(build))
ds@1952
   225
				{
ds@1952
   226
					break;
ds@1952
   227
				}
ds@1952
   228
				cur_ptr = next_ptr;
ds@1952
   229
			} while (cur_ptr);
ds@1952
   230
		}
ds@1952
   231
	}
ds@1952
   232
ds@1952
   233
	/* Check if the file exists. */
ds@1952
   234
	char file[PLATFORM_MAX_PATH];
ds@1952
   235
	char local[PLATFORM_MAX_PATH];
ds@1952
   236
ds@1952
   237
	if (cfg->folder.size())
ds@1952
   238
	{
ds@1952
   239
		g_LibSys.PathFormat(local, 
ds@1952
   240
			sizeof(local), 
ds@1952
   241
			"%s/%s.cfg",
ds@1952
   242
			cfg->folder.c_str(),
ds@1952
   243
			cfg->autocfg.c_str());
ds@1952
   244
	} else {
ds@1952
   245
		g_LibSys.PathFormat(local,
ds@1952
   246
			sizeof(local),
ds@1952
   247
			"%s.cfg",
ds@1952
   248
			cfg->autocfg.c_str());
ds@1952
   249
	}
ds@1952
   250
	
ds@1952
   251
	g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);
ds@1952
   252
ds@1952
   253
	bool file_exists = g_LibSys.IsPathFile(file);
ds@1952
   254
	if (!file_exists && will_create)
ds@1952
   255
	{
ds@1952
   256
		List<const ConVar *> *convars = NULL;
ds@1952
   257
		if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
ds@1952
   258
		{
ds@1952
   259
			/* Attempt to create it */
ds@1952
   260
			FILE *fp = fopen(file, "wt");
ds@1952
   261
			if (fp)
ds@1952
   262
			{
ds@1952
   263
				fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SVN_FULL_VERSION);
ds@1952
   264
				fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
ds@1952
   265
				fprintf(fp, "\n\n");
ds@1952
   266
ds@1952
   267
				List<const ConVar *>::iterator iter;
ds@1952
   268
				float x;
ds@1952
   269
				for (iter = convars->begin(); iter != convars->end(); iter++)
ds@1952
   270
				{
ds@1952
   271
					const ConVar *cvar = (*iter);
ds@1952
   272
ds@1952
   273
					if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
ds@1952
   274
					{
ds@1952
   275
						continue;
ds@1952
   276
					}
ds@1952
   277
ds@1952
   278
					char descr[255];
ds@1952
   279
					char *dptr = descr;
ds@1952
   280
ds@1952
   281
					/* Print comments until there is no more */
ds@1952
   282
					strncopy(descr, cvar->GetHelpText(), sizeof(descr));
ds@1952
   283
					while (*dptr != '\0')
ds@1952
   284
					{
ds@1952
   285
						/* Find the next line */
ds@1952
   286
						char *next_ptr = dptr;
ds@1952
   287
						while (*next_ptr != '\0')
ds@1952
   288
						{
ds@1952
   289
							if (*next_ptr == '\n')
ds@1952
   290
							{
ds@1952
   291
								*next_ptr = '\0';
ds@1952
   292
								next_ptr++;
ds@1952
   293
								break;
ds@1952
   294
							}
ds@1952
   295
							next_ptr++;
ds@1952
   296
						}
ds@1952
   297
						fprintf(fp, "// %s\n", dptr);
ds@1952
   298
						dptr = next_ptr;
ds@1952
   299
					}
ds@1952
   300
ds@1952
   301
					fprintf(fp, "// -\n");
ds@1952
   302
					fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
ds@1952
   303
					if (cvar->GetMin(x))
ds@1952
   304
					{
ds@1952
   305
						fprintf(fp, "// Minimum: \"%02f\"\n", x);
ds@1952
   306
					}
ds@1952
   307
					if (cvar->GetMax(x))
ds@1952
   308
					{
ds@1952
   309
						fprintf(fp, "// Maximum: \"%02f\"\n", x);
ds@1952
   310
					}
ds@1952
   311
					fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
ds@1952
   312
					fprintf(fp, "\n");
ds@1952
   313
				}
ds@1952
   314
				fprintf(fp, "\n");
ds@1952
   315
ds@1952
   316
				file_exists = true;
ds@1952
   317
				can_create = false;
ds@1952
   318
				fclose(fp);
ds@1952
   319
			}
ds@1952
   320
		}
ds@1952
   321
	}
ds@1952
   322
ds@1952
   323
	if (file_exists)
ds@1952
   324
	{
ds@1952
   325
		char cmd[255];
ds@1952
   326
		UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
ds@1952
   327
		engine->ServerCommand(cmd);
ds@1952
   328
	}
ds@1952
   329
ds@1952
   330
	return can_create;
ds@1952
   331
}
ds@1952
   332
ds@1952
   333
void SM_DoSingleExecFwds(IPluginContext *ctx)
ds@1952
   334
{
ds@1952
   335
	IPluginFunction *pf;
ds@1952
   336
ds@1952
   337
	if ((pf = ctx->GetFunctionByName("OnServerCfg")) != NULL)
ds@1952
   338
	{
ds@1952
   339
		pf->Execute(NULL);
ds@1952
   340
	}
ds@1952
   341
ds@1952
   342
	if ((pf = ctx->GetFunctionByName("OnConfigsExecuted")) != NULL)
ds@1952
   343
	{
ds@1952
   344
		pf->Execute(NULL);
ds@1952
   345
	}
ds@1952
   346
}
ds@1952
   347
ds@1952
   348
void SM_ConfigsExecuted_Plugin(unsigned int serial)
ds@1952
   349
{
ds@1952
   350
	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
ds@1952
   351
	while (iter->MorePlugins())
ds@1952
   352
	{
ds@1952
   353
		CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
ds@1952
   354
		if (plugin->GetSerial() == serial)
ds@1952
   355
		{
ds@1952
   356
			SM_DoSingleExecFwds(plugin->GetBaseContext());
ds@1952
   357
			break;
ds@1952
   358
		}
ds@1952
   359
		iter->NextPlugin();
ds@1952
   360
	}
ds@1952
   361
	iter->Release();
ds@1952
   362
}
ds@1952
   363
ds@1952
   364
void SM_ExecuteForPlugin(IPluginContext *ctx)
ds@1952
   365
{
ds@1952
   366
	CPlugin *plugin = (CPlugin *)g_PluginSys.GetPluginByCtx(ctx->GetContext());
ds@1952
   367
ds@1952
   368
	unsigned int num = plugin->GetConfigCount();
ds@1952
   369
	if (!num)
ds@1952
   370
	{
ds@1952
   371
		SM_DoSingleExecFwds(ctx);
ds@1952
   372
	}
ds@1952
   373
	else
ds@1952
   374
	{
ds@1952
   375
		bool can_create = true;
ds@1952
   376
		for (unsigned int i=0; i<num; i++)
ds@1952
   377
		{
ds@1952
   378
			can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
ds@1952
   379
		}
ds@1952
   380
		char cmd[255];
ds@1952
   381
		UTIL_Format(cmd, sizeof(cmd), "sm internal 2 %d\n", plugin->GetSerial());
ds@1952
   382
		engine->ServerCommand(cmd);
ds@1952
   383
	}
ds@1952
   384
}
ds@1952
   385
ds@1952
   386
void SM_ExecuteAllConfigs()
ds@1952
   387
{
ds@1952
   388
	if (g_bConfigsExecd)
ds@1952
   389
	{
ds@1952
   390
		return;
ds@1952
   391
	}
ds@1952
   392
ds@1952
   393
	engine->ServerCommand("exec sourcemod/sourcemod.cfg\n");
ds@1952
   394
ds@1952
   395
	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
ds@1952
   396
	while (iter->MorePlugins())
ds@1952
   397
	{
ds@1952
   398
		CPlugin *plugin = (CPlugin *)(iter->GetPlugin());
ds@1952
   399
		unsigned int num = plugin->GetConfigCount();
ds@1952
   400
		bool can_create = true;
ds@1952
   401
		for (unsigned int i=0; i<num; i++)
ds@1952
   402
		{
ds@1952
   403
			can_create = SM_ExecuteConfig(plugin, plugin->GetConfig(i), can_create);
ds@1952
   404
		}
ds@1952
   405
		iter->NextPlugin();
ds@1952
   406
	}
ds@1952
   407
	iter->Release();
ds@1952
   408
dvander@2065
   409
	g_pOnAutoConfigsBuffered->Execute(NULL);
dvander@2065
   410
ds@1952
   411
	engine->ServerCommand("sm internal 1\n");
ds@1952
   412
}
ds@1952
   413
ds@1952
   414
void SM_ConfigsExecuted_Global()
ds@1952
   415
{
ds@1952
   416
	if (g_bConfigsExecd)
ds@1952
   417
	{
ds@1952
   418
		return;
ds@1952
   419
	}
ds@1952
   420
ds@1952
   421
	g_bConfigsExecd = true;
ds@1952
   422
ds@1952
   423
	g_pOnServerCfg->Execute(NULL);
ds@1952
   424
	g_pOnConfigsExecuted->Execute(NULL);
ds@1952
   425
}