plugins/admin-flatfile/admin-overrides.sp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/admin-flatfile/admin-overrides.sp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,213 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Admin File Reader Plugin
     1.8 + * Reads overrides from the admin_levels.cfg file.  Do not compile
     1.9 + * this directly.
    1.10 + *
    1.11 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
    1.12 + * =============================================================================
    1.13 + *
    1.14 + * This program is free software; you can redistribute it and/or modify it under
    1.15 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.16 + * Free Software Foundation.
    1.17 + * 
    1.18 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.19 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.20 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.21 + * details.
    1.22 + *
    1.23 + * You should have received a copy of the GNU General Public License along with
    1.24 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.25 + *
    1.26 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.27 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.28 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.29 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.30 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.31 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.32 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.33 + * or <http://www.sourcemod.net/license.php>.
    1.34 + *
    1.35 + * Version: $Id$
    1.36 + */
    1.37 +
    1.38 +#define OVERRIDE_STATE_NONE			0
    1.39 +#define OVERRIDE_STATE_LEVELS		1
    1.40 +#define OVERRIDE_STATE_OVERRIDES	2
    1.41 +
    1.42 +static Handle:g_hOldOverrideParser = INVALID_HANDLE;
    1.43 +static Handle:g_hNewOverrideParser = INVALID_HANDLE;
    1.44 +static g_OverrideState = OVERRIDE_STATE_NONE;
    1.45 +
    1.46 +public SMCResult:ReadOldOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.47 +{
    1.48 +	if (g_IgnoreLevel)
    1.49 +	{
    1.50 +		g_IgnoreLevel++;
    1.51 +		return SMCParse_Continue;
    1.52 +	}
    1.53 +	
    1.54 +	if (g_OverrideState == OVERRIDE_STATE_NONE)
    1.55 +	{
    1.56 +		if (StrEqual(name, "Levels"))
    1.57 +		{
    1.58 +			g_OverrideState = OVERRIDE_STATE_LEVELS;
    1.59 +		} else {
    1.60 +			g_IgnoreLevel++;
    1.61 +		}
    1.62 +	} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
    1.63 +		if (StrEqual(name, "Overrides"))
    1.64 +		{
    1.65 +			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    1.66 +		} else {
    1.67 +			g_IgnoreLevel++;
    1.68 +		}
    1.69 +	} else {
    1.70 +		g_IgnoreLevel++;
    1.71 +	}
    1.72 +	
    1.73 +	return SMCParse_Continue;
    1.74 +}
    1.75 +
    1.76 +public SMCResult:ReadNewOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.77 +{
    1.78 +	if (g_IgnoreLevel)
    1.79 +	{
    1.80 +		g_IgnoreLevel++;
    1.81 +		return SMCParse_Continue;
    1.82 +	}
    1.83 +	
    1.84 +	if (g_OverrideState == OVERRIDE_STATE_NONE)
    1.85 +	{
    1.86 +		if (StrEqual(name, "Overrides"))
    1.87 +		{
    1.88 +			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    1.89 +		} else {
    1.90 +			g_IgnoreLevel++;
    1.91 +		}
    1.92 +	} else {
    1.93 +		g_IgnoreLevel++;
    1.94 +	}
    1.95 +	
    1.96 +	return SMCParse_Continue;
    1.97 +}
    1.98 +
    1.99 +public SMCResult:ReadOverrides_KeyValue(Handle:smc, 
   1.100 +										const String:key[], 
   1.101 +										const String:value[], 
   1.102 +										bool:key_quotes, 
   1.103 +										bool:value_quotes)
   1.104 +{
   1.105 +	if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
   1.106 +		|| g_IgnoreLevel)
   1.107 +	{
   1.108 +		return SMCParse_Continue;
   1.109 +	}
   1.110 +	
   1.111 +	new flags = ReadFlagString(value);
   1.112 +	
   1.113 +	if (key[0] == '@')
   1.114 +	{
   1.115 +		AddCommandOverride(key[1], Override_CommandGroup, flags);
   1.116 +	} else {
   1.117 +		AddCommandOverride(key, Override_Command, flags);
   1.118 +	}
   1.119 +	
   1.120 +	return SMCParse_Continue;
   1.121 +}
   1.122 +
   1.123 +public SMCResult:ReadOldOverrides_EndSection(Handle:smc)
   1.124 +{
   1.125 +	/* If we're ignoring, skip out */
   1.126 +	if (g_IgnoreLevel)
   1.127 +	{
   1.128 +		g_IgnoreLevel--;
   1.129 +		return SMCParse_Continue;
   1.130 +	}
   1.131 +	
   1.132 +	if (g_OverrideState == OVERRIDE_STATE_LEVELS)
   1.133 +	{
   1.134 +		g_OverrideState = OVERRIDE_STATE_NONE;
   1.135 +	} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
   1.136 +		/* We're totally done parsing */
   1.137 +		g_OverrideState = OVERRIDE_STATE_LEVELS;
   1.138 +		return SMCParse_Halt;
   1.139 +	}
   1.140 +	
   1.141 +	return SMCParse_Continue;
   1.142 +}
   1.143 +
   1.144 +public SMCResult:ReadNewOverrides_EndSection(Handle:smc)
   1.145 +{
   1.146 +	/* If we're ignoring, skip out */
   1.147 +	if (g_IgnoreLevel)
   1.148 +	{
   1.149 +		g_IgnoreLevel--;
   1.150 +		return SMCParse_Continue;
   1.151 +	}
   1.152 +	
   1.153 +	if (g_OverrideState == OVERRIDE_STATE_OVERRIDES)
   1.154 +	{
   1.155 +		g_OverrideState = OVERRIDE_STATE_NONE;
   1.156 +	}
   1.157 +	
   1.158 +	return SMCParse_Continue;
   1.159 +}
   1.160 +
   1.161 +public SMCResult:ReadOverrides_CurrentLine(Handle:smc, const String:line[], lineno)
   1.162 +{
   1.163 +	g_CurrentLine = lineno;
   1.164 +	
   1.165 +	return SMCParse_Continue;
   1.166 +}
   1.167 +
   1.168 +static InitializeOverrideParsers()
   1.169 +{
   1.170 +	if (g_hOldOverrideParser == INVALID_HANDLE)
   1.171 +	{
   1.172 +		g_hOldOverrideParser = SMC_CreateParser();
   1.173 +		SMC_SetReaders(g_hOldOverrideParser,
   1.174 +					   ReadOldOverrides_NewSection,
   1.175 +					   ReadOverrides_KeyValue,
   1.176 +					   ReadOldOverrides_EndSection);
   1.177 +		SMC_SetRawLine(g_hOldOverrideParser, ReadOverrides_CurrentLine);
   1.178 +	}
   1.179 +	if (g_hNewOverrideParser == INVALID_HANDLE)
   1.180 +	{
   1.181 +		g_hNewOverrideParser = SMC_CreateParser();
   1.182 +		SMC_SetReaders(g_hNewOverrideParser,
   1.183 +					   ReadNewOverrides_NewSection,
   1.184 +					   ReadOverrides_KeyValue,
   1.185 +					   ReadNewOverrides_EndSection);
   1.186 +		SMC_SetRawLine(g_hNewOverrideParser, ReadOverrides_CurrentLine);
   1.187 +	}
   1.188 +}
   1.189 +
   1.190 +InternalReadOverrides(Handle:parser, const String:file[])
   1.191 +{
   1.192 +	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), file);
   1.193 +	
   1.194 +	/* Set states */
   1.195 +	InitGlobalStates();
   1.196 +	g_OverrideState = OVERRIDE_STATE_NONE;
   1.197 +		
   1.198 +	new SMCError:err = SMC_ParseFile(parser, g_Filename);
   1.199 +	if (err != SMCError_Okay)
   1.200 +	{
   1.201 +		decl String:buffer[64];
   1.202 +		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   1.203 +		{
   1.204 +			ParseError("%s", buffer);
   1.205 +		} else {
   1.206 +			ParseError("Fatal parse error");
   1.207 +		}
   1.208 +	}
   1.209 +}
   1.210 +
   1.211 +ReadOverrides()
   1.212 +{
   1.213 +	InitializeOverrideParsers();
   1.214 +	InternalReadOverrides(g_hOldOverrideParser, "configs/admin_levels.cfg");
   1.215 +	InternalReadOverrides(g_hNewOverrideParser, "configs/admin_overrides.cfg");
   1.216 +}