plugins/admin-flatfile/admin-overrides.sp
author Matt Woodrow <pred@alliedmods.net>
Sat May 24 04:08:29 2008 +0000 (2008-05-24)
changeset 2069 80d56f1d58fe
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Added a load-block for incorrect mods to TF2 and cstrike extensions.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin File Reader Plugin
     5  * Reads overrides from the admin_levels.cfg file.  Do not compile
     6  * this directly.
     7  *
     8  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     9  * =============================================================================
    10  *
    11  * This program is free software; you can redistribute it and/or modify it under
    12  * the terms of the GNU General Public License, version 3.0, as published by the
    13  * Free Software Foundation.
    14  * 
    15  * This program is distributed in the hope that it will be useful, but WITHOUT
    16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    18  * details.
    19  *
    20  * You should have received a copy of the GNU General Public License along with
    21  * this program.  If not, see <http://www.gnu.org/licenses/>.
    22  *
    23  * As a special exception, AlliedModders LLC gives you permission to link the
    24  * code of this program (as well as its derivative works) to "Half-Life 2," the
    25  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    26  * by the Valve Corporation.  You must obey the GNU General Public License in
    27  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    28  * this exception to all derivative works.  AlliedModders LLC defines further
    29  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    30  * or <http://www.sourcemod.net/license.php>.
    31  *
    32  * Version: $Id$
    33  */
    34 
    35 #define OVERRIDE_STATE_NONE			0
    36 #define OVERRIDE_STATE_LEVELS		1
    37 #define OVERRIDE_STATE_OVERRIDES	2
    38 
    39 static Handle:g_hOldOverrideParser = INVALID_HANDLE;
    40 static Handle:g_hNewOverrideParser = INVALID_HANDLE;
    41 static g_OverrideState = OVERRIDE_STATE_NONE;
    42 
    43 public SMCResult:ReadOldOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    44 {
    45 	if (g_IgnoreLevel)
    46 	{
    47 		g_IgnoreLevel++;
    48 		return SMCParse_Continue;
    49 	}
    50 	
    51 	if (g_OverrideState == OVERRIDE_STATE_NONE)
    52 	{
    53 		if (StrEqual(name, "Levels"))
    54 		{
    55 			g_OverrideState = OVERRIDE_STATE_LEVELS;
    56 		} else {
    57 			g_IgnoreLevel++;
    58 		}
    59 	} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
    60 		if (StrEqual(name, "Overrides"))
    61 		{
    62 			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    63 		} else {
    64 			g_IgnoreLevel++;
    65 		}
    66 	} else {
    67 		g_IgnoreLevel++;
    68 	}
    69 	
    70 	return SMCParse_Continue;
    71 }
    72 
    73 public SMCResult:ReadNewOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    74 {
    75 	if (g_IgnoreLevel)
    76 	{
    77 		g_IgnoreLevel++;
    78 		return SMCParse_Continue;
    79 	}
    80 	
    81 	if (g_OverrideState == OVERRIDE_STATE_NONE)
    82 	{
    83 		if (StrEqual(name, "Overrides"))
    84 		{
    85 			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    86 		} else {
    87 			g_IgnoreLevel++;
    88 		}
    89 	} else {
    90 		g_IgnoreLevel++;
    91 	}
    92 	
    93 	return SMCParse_Continue;
    94 }
    95 
    96 public SMCResult:ReadOverrides_KeyValue(Handle:smc, 
    97 										const String:key[], 
    98 										const String:value[], 
    99 										bool:key_quotes, 
   100 										bool:value_quotes)
   101 {
   102 	if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
   103 		|| g_IgnoreLevel)
   104 	{
   105 		return SMCParse_Continue;
   106 	}
   107 	
   108 	new flags = ReadFlagString(value);
   109 	
   110 	if (key[0] == '@')
   111 	{
   112 		AddCommandOverride(key[1], Override_CommandGroup, flags);
   113 	} else {
   114 		AddCommandOverride(key, Override_Command, flags);
   115 	}
   116 	
   117 	return SMCParse_Continue;
   118 }
   119 
   120 public SMCResult:ReadOldOverrides_EndSection(Handle:smc)
   121 {
   122 	/* If we're ignoring, skip out */
   123 	if (g_IgnoreLevel)
   124 	{
   125 		g_IgnoreLevel--;
   126 		return SMCParse_Continue;
   127 	}
   128 	
   129 	if (g_OverrideState == OVERRIDE_STATE_LEVELS)
   130 	{
   131 		g_OverrideState = OVERRIDE_STATE_NONE;
   132 	} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
   133 		/* We're totally done parsing */
   134 		g_OverrideState = OVERRIDE_STATE_LEVELS;
   135 		return SMCParse_Halt;
   136 	}
   137 	
   138 	return SMCParse_Continue;
   139 }
   140 
   141 public SMCResult:ReadNewOverrides_EndSection(Handle:smc)
   142 {
   143 	/* If we're ignoring, skip out */
   144 	if (g_IgnoreLevel)
   145 	{
   146 		g_IgnoreLevel--;
   147 		return SMCParse_Continue;
   148 	}
   149 	
   150 	if (g_OverrideState == OVERRIDE_STATE_OVERRIDES)
   151 	{
   152 		g_OverrideState = OVERRIDE_STATE_NONE;
   153 	}
   154 	
   155 	return SMCParse_Continue;
   156 }
   157 
   158 public SMCResult:ReadOverrides_CurrentLine(Handle:smc, const String:line[], lineno)
   159 {
   160 	g_CurrentLine = lineno;
   161 	
   162 	return SMCParse_Continue;
   163 }
   164 
   165 static InitializeOverrideParsers()
   166 {
   167 	if (g_hOldOverrideParser == INVALID_HANDLE)
   168 	{
   169 		g_hOldOverrideParser = SMC_CreateParser();
   170 		SMC_SetReaders(g_hOldOverrideParser,
   171 					   ReadOldOverrides_NewSection,
   172 					   ReadOverrides_KeyValue,
   173 					   ReadOldOverrides_EndSection);
   174 		SMC_SetRawLine(g_hOldOverrideParser, ReadOverrides_CurrentLine);
   175 	}
   176 	if (g_hNewOverrideParser == INVALID_HANDLE)
   177 	{
   178 		g_hNewOverrideParser = SMC_CreateParser();
   179 		SMC_SetReaders(g_hNewOverrideParser,
   180 					   ReadNewOverrides_NewSection,
   181 					   ReadOverrides_KeyValue,
   182 					   ReadNewOverrides_EndSection);
   183 		SMC_SetRawLine(g_hNewOverrideParser, ReadOverrides_CurrentLine);
   184 	}
   185 }
   186 
   187 InternalReadOverrides(Handle:parser, const String:file[])
   188 {
   189 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), file);
   190 	
   191 	/* Set states */
   192 	InitGlobalStates();
   193 	g_OverrideState = OVERRIDE_STATE_NONE;
   194 		
   195 	new SMCError:err = SMC_ParseFile(parser, g_Filename);
   196 	if (err != SMCError_Okay)
   197 	{
   198 		decl String:buffer[64];
   199 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   200 		{
   201 			ParseError("%s", buffer);
   202 		} else {
   203 			ParseError("Fatal parse error");
   204 		}
   205 	}
   206 }
   207 
   208 ReadOverrides()
   209 {
   210 	InitializeOverrideParsers();
   211 	InternalReadOverrides(g_hOldOverrideParser, "configs/admin_levels.cfg");
   212 	InternalReadOverrides(g_hNewOverrideParser, "configs/admin_overrides.cfg");
   213 }