plugins/admin-flatfile/admin-overrides.sp
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 18:24:02 2007 +0000 (2007-07-07)
changeset 1050 cdae545c8c92
parent 908 cd411e8827c4
child 1298 9cddbed74581
permissions -rw-r--r--
fixed bug amb569
     1 /* 
     2  * admin-overrides.sp
     3  * Reads overrides from the admin_levels.cfg file.  Do not compile this directly.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20   * Version: $Id$
    21  */
    22 
    23 #define OVERRIDE_STATE_NONE			0
    24 #define OVERRIDE_STATE_LEVELS		1
    25 #define OVERRIDE_STATE_OVERRIDES	2
    26 
    27 static Handle:g_hOverrideParser = INVALID_HANDLE;
    28 static g_OverrideState = OVERRIDE_STATE_NONE;
    29 
    30 public SMCResult:ReadOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    31 {
    32 	if (g_IgnoreLevel)
    33 	{
    34 		g_IgnoreLevel++;
    35 		return SMCParse_Continue;
    36 	}
    37 	
    38 	if (g_OverrideState == OVERRIDE_STATE_NONE)
    39 	{
    40 		if (StrEqual(name, "Levels"))
    41 		{
    42 			g_OverrideState = OVERRIDE_STATE_LEVELS;
    43 		} else {
    44 			g_IgnoreLevel++;
    45 		}
    46 	} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
    47 		if (StrEqual(name, "Overrides"))
    48 		{
    49 			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    50 		} else {
    51 			g_IgnoreLevel++;
    52 		}
    53 	} else {
    54 		g_IgnoreLevel++;
    55 	}
    56 	
    57 	return SMCParse_Continue;
    58 }
    59 
    60 public SMCResult:ReadOverrides_KeyValue(Handle:smc, 
    61 										const String:key[], 
    62 										const String:value[], 
    63 										bool:key_quotes, 
    64 										bool:value_quotes)
    65 {
    66 	if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
    67 		|| g_IgnoreLevel)
    68 	{
    69 		return SMCParse_Continue;
    70 	}
    71 	
    72 	new AdminFlag:array[AdminFlags_TOTAL];
    73 	new flags_total;
    74 	
    75 	new len = strlen(value);
    76 	for (new i=0; i<len; i++)
    77 	{
    78 		if (value[i] < 'a' || value[i] > 'z')
    79 		{
    80 			ParseError("Invalid flag detected: %c", value[i]);
    81 			continue;
    82 		}
    83 		new val = value[i] - 'a';
    84 		if (!g_FlagsSet[val])
    85 		{
    86 			ParseError("Invalid flag detected: %c", value[i]);
    87 			continue;
    88 		}
    89 		array[flags_total++] = g_FlagLetters[val];
    90 	}
    91 	
    92 	new flags = FlagArrayToBits(array, flags_total);
    93 	
    94 	if (key[0] == '@')
    95 	{
    96 		AddCommandOverride(key[1], Override_CommandGroup, flags);
    97 	} else {
    98 		AddCommandOverride(key, Override_Command, flags);
    99 	}
   100 	
   101 	return SMCParse_Continue;
   102 }
   103 
   104 public SMCResult:ReadOverrides_EndSection(Handle:smc)
   105 {
   106 	/* If we're ignoring, skip out */
   107 	if (g_IgnoreLevel)
   108 	{
   109 		g_IgnoreLevel--;
   110 		return SMCParse_Continue;
   111 	}
   112 	
   113 	if (g_OverrideState == OVERRIDE_STATE_LEVELS)
   114 	{
   115 		g_OverrideState = OVERRIDE_STATE_NONE;
   116 	} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
   117 		/* We're totally done parsing */
   118 		g_OverrideState = OVERRIDE_STATE_LEVELS;
   119 		return SMCParse_Halt;
   120 	}
   121 	
   122 	return SMCParse_Continue;
   123 }
   124 
   125 static InitializeOverrideParser()
   126 {
   127 	if (g_hOverrideParser == INVALID_HANDLE)
   128 	{
   129 		g_hOverrideParser = SMC_CreateParser();
   130 		SMC_SetReaders(g_hOverrideParser,
   131 					   ReadOverrides_NewSection,
   132 					   ReadOverrides_KeyValue,
   133 					   ReadOverrides_EndSection);
   134 	}
   135 }
   136 
   137 ReadOverrides()
   138 {
   139 	InitializeOverrideParser();
   140 	
   141 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_levels.cfg");
   142 	
   143 	/* Set states */
   144 	InitGlobalStates();
   145 	g_OverrideState = OVERRIDE_STATE_NONE;
   146 		
   147 	new SMCError:err = SMC_ParseFile(g_hOverrideParser, g_Filename);
   148 	if (err != SMCError_Okay)
   149 	{
   150 		decl String:buffer[64];
   151 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   152 		{
   153 			ParseError("%s", buffer);
   154 		} else {
   155 			ParseError("Fatal parse error");
   156 		}
   157 	}
   158 }