core/CoreConfig.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu May 03 03:45:53 2007 +0000 (2007-05-03)
changeset 729 110c14c99545
parent 669 ce01c2eb020c
child 978 54782a83a1ef
permissions -rw-r--r--
Various pointless things of no importance:
- Renamed ISourceMod::GetModPath() to GetGamePath()
- Renamed ISourceMod::GetModFolderName() to GetGameFolderName()
- Various spelling corrections (why did I do this?)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "CoreConfig.h"
    16 #include "sourcemod.h"
    17 #include "sourcemm_api.h"
    18 #include "sm_srvcmds.h"
    19 #include "LibrarySys.h"
    20 #include "TextParsers.h"
    21 #include "Logger.h"
    22 
    23 #ifdef PLATFORM_WINDOWS
    24 ConVar sm_corecfgfile("sm_corecfgfile", "addons\\sourcemod\\configs\\core.cfg", 0, "SourceMod core configuration file");
    25 #elif defined PLATFORM_LINUX
    26 ConVar sm_corecfgfile("sm_corecfgfile", "addons/sourcemod/configs/core.cfg", 0, "SourceMod core configuration file");
    27 #endif
    28 
    29 CoreConfig g_CoreConfig;
    30 
    31 void CoreConfig::OnSourceModAllInitialized()
    32 {
    33 	g_RootMenu.AddRootConsoleCommand("config", "Set core configuration options", this);
    34 }
    35 
    36 void CoreConfig::OnSourceModShutdown()
    37 {
    38 	g_RootMenu.RemoveRootConsoleCommand("config", this);
    39 }
    40 
    41 void CoreConfig::OnRootConsoleCommand(const char *command, unsigned int argcount)
    42 {
    43 	if (argcount >= 4)
    44 	{
    45 		const char *option = engine->Cmd_Argv(2);
    46 		const char *value = engine->Cmd_Argv(3);
    47 
    48 		char error[255];
    49 
    50 		ConfigResult res = SetConfigOption(option, value, ConfigSource_Console, error, sizeof(error));
    51 
    52 		if (res == ConfigResult_Reject)
    53 		{
    54 			g_RootMenu.ConsolePrint("[SM] Could not set config option \"%s\" to \"%s\" (%s)", option, value, error);
    55 		} else if (res == ConfigResult_Ignore) {
    56 			g_RootMenu.ConsolePrint("[SM] No such config option \"%s\" exists.", option);
    57 		} else {
    58 			g_RootMenu.ConsolePrint("Config option \"%s\" successfully set to \"%s.\"", option, value);
    59 		}
    60 
    61 		return;
    62 	}
    63 
    64 	g_RootMenu.ConsolePrint("[SM] Usage: sm config <option> <value>");
    65 }
    66 
    67 void CoreConfig::Initialize()
    68 {
    69 	SMCParseError err;
    70 	char filePath[PLATFORM_MAX_PATH];
    71 
    72 	/* Try to get command line value of core config convar */
    73 	const char *corecfg = icvar->GetCommandLineValue("sm_corecfgfile");
    74 
    75 	/* If sm_corecfgfile not specified on command line, use default value */
    76 	if (!corecfg)
    77 	{
    78 		corecfg = sm_corecfgfile.GetDefault();
    79 	}
    80 
    81 	/* Format path to config file */
    82 	g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
    83 
    84 	/* Parse config file */
    85 	if ((err=g_TextParser.ParseFile_SMC(filePath, this, NULL, NULL)) != SMCParse_Okay)
    86 	{
    87  		/* :TODO: This won't actually log or print anything :( - So fix that somehow */
    88 		const char *error = g_TextParser.GetSMCErrorString(err);
    89 		g_Logger.LogFatal("[SM] Error encountered parsing core config file: %s", error ? error : "");
    90 	}
    91 }
    92 
    93 SMCParseResult CoreConfig::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
    94 {
    95 	char error[255];
    96 	ConfigResult err = SetConfigOption(key, value, ConfigSource_File, error, sizeof(error));
    97 
    98 	if (err == ConfigResult_Reject)
    99 	{
   100 		/* This is a fatal error */
   101 		g_Logger.LogFatal("Config error (key: %s) (value: %s) %s", key, value, error);
   102 	}
   103 
   104 	return SMCParse_Continue;
   105 }
   106 
   107 ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value, ConfigSource source, char *error, size_t maxlength)
   108 {
   109 	ConfigResult result;
   110 
   111 	/* Notify! */
   112 	SMGlobalClass *pBase = SMGlobalClass::head;
   113 	while (pBase)
   114 	{
   115 		if ((result = pBase->OnSourceModConfigChanged(option, value, source, error, maxlength)) != ConfigResult_Ignore)
   116 		{
   117 			return result;
   118 		}
   119 		pBase = pBase->m_pGlobalClassNext;
   120 	}
   121 
   122 	return ConfigResult_Ignore;
   123 }