plugins/admin-flatfile/admin-overrides.sp
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 444 0c718808b5a2
child 908 cd411e8827c4
permissions -rw-r--r--
removed this potential feature, not useful enough
     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 
    21 #define OVERRIDE_STATE_NONE			0
    22 #define OVERRIDE_STATE_LEVELS		1
    23 #define OVERRIDE_STATE_OVERRIDES	2
    24 
    25 static Handle:g_hOverrideParser = INVALID_HANDLE;
    26 static g_OverrideState = OVERRIDE_STATE_NONE;
    27 
    28 public SMCResult:ReadOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    29 {
    30 	if (g_IgnoreLevel)
    31 	{
    32 		g_IgnoreLevel++;
    33 		return SMCParse_Continue;
    34 	}
    35 	
    36 	if (g_OverrideState == OVERRIDE_STATE_NONE)
    37 	{
    38 		if (StrEqual(name, "Levels"))
    39 		{
    40 			g_OverrideState = OVERRIDE_STATE_LEVELS;
    41 		} else {
    42 			g_IgnoreLevel++;
    43 		}
    44 	} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
    45 		if (StrEqual(name, "Overrides"))
    46 		{
    47 			g_OverrideState = OVERRIDE_STATE_OVERRIDES;
    48 		} else {
    49 			g_IgnoreLevel++;
    50 		}
    51 	} else {
    52 		g_IgnoreLevel++;
    53 	}
    54 	
    55 	return SMCParse_Continue;
    56 }
    57 
    58 public SMCResult:ReadOverrides_KeyValue(Handle:smc, 
    59 										const String:key[], 
    60 										const String:value[], 
    61 										bool:key_quotes, 
    62 										bool:value_quotes)
    63 {
    64 	if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
    65 		|| g_IgnoreLevel)
    66 	{
    67 		return SMCParse_Continue;
    68 	}
    69 	
    70 	new AdminFlag:array[AdminFlags_TOTAL];
    71 	new flags_total;
    72 	
    73 	new len = strlen(value);
    74 	for (new i=0; i<len; i++)
    75 	{
    76 		if (value[i] < 'a' || value[i] > 'z')
    77 		{
    78 			ParseError("Invalid flag detected: %c", value[i]);
    79 			continue;
    80 		}
    81 		new val = value[i] - 'a';
    82 		if (!g_FlagsSet[val])
    83 		{
    84 			ParseError("Invalid flag detected: %c", value[i]);
    85 			continue;
    86 		}
    87 		array[flags_total++] = g_FlagLetters[val];
    88 	}
    89 	
    90 	new flags = FlagArrayToBits(array, flags_total);
    91 	
    92 	if (key[0] == '@')
    93 	{
    94 		AddCommandOverride(key[1], Override_CommandGroup, flags);
    95 	} else {
    96 		AddCommandOverride(key, Override_Command, flags);
    97 	}
    98 	
    99 	return SMCParse_Continue;
   100 }
   101 
   102 public SMCResult:ReadOverrides_EndSection(Handle:smc)
   103 {
   104 	/* If we're ignoring, skip out */
   105 	if (g_IgnoreLevel)
   106 	{
   107 		g_IgnoreLevel--;
   108 		return SMCParse_Continue;
   109 	}
   110 	
   111 	if (g_OverrideState == OVERRIDE_STATE_LEVELS)
   112 	{
   113 		g_OverrideState = OVERRIDE_STATE_NONE;
   114 	} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
   115 		/* We're totally done parsing */
   116 		g_OverrideState = OVERRIDE_STATE_LEVELS;
   117 		return SMCParse_Halt;
   118 	}
   119 	
   120 	return SMCParse_Continue;
   121 }
   122 
   123 static InitializeOverrideParser()
   124 {
   125 	if (g_hOverrideParser == INVALID_HANDLE)
   126 	{
   127 		g_hOverrideParser = SMC_CreateParser();
   128 		SMC_SetReaders(g_hOverrideParser,
   129 					   ReadOverrides_NewSection,
   130 					   ReadOverrides_KeyValue,
   131 					   ReadOverrides_EndSection);
   132 	}
   133 }
   134 
   135 ReadOverrides()
   136 {
   137 	InitializeOverrideParser();
   138 	
   139 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_levels.cfg");
   140 	
   141 	/* Set states */
   142 	InitGlobalStates();
   143 	g_OverrideState = OVERRIDE_STATE_NONE;
   144 		
   145 	new SMCError:err = SMC_ParseFile(g_hOverrideParser, g_Filename);
   146 	if (err != SMCError_Okay)
   147 	{
   148 		decl String:buffer[64];
   149 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   150 		{
   151 			ParseError("%s", buffer);
   152 		} else {
   153 			ParseError("Fatal parse error");
   154 		}
   155 	}
   156 }