ignores bad groups safely now

cleaned up code a bit, removed redundancy

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40448
This commit is contained in:
David Anderson 2007-02-06 23:30:50 +00:00
parent 9969243266
commit d2a3c7f0f7
4 changed files with 121 additions and 87 deletions

View File

@ -10,11 +10,13 @@ public Plugin:myinfo =
url = "http://www.sourcemod.net/" url = "http://www.sourcemod.net/"
}; };
/** Various globals */ /** Various parsing globals */
new bool:g_FlagsSet[26]; /* Maps whether flags are set */ new bool:g_FlagsSet[26]; /* Maps whether flags are set */
new AdminFlag:g_FlagLetters[26]; /* Maps the flag letters */ new AdminFlag:g_FlagLetters[26]; /* Maps the flag letters */
new bool:g_LoggedFileName = false; /* Whether or not the file name has been logged */ new bool:g_LoggedFileName = false; /* Whether or not the file name has been logged */
new g_ErrorCount = 0; new g_ErrorCount = 0; /* Current error count */
new g_IgnoreLevel = 0; /* Nested ignored section count, so users can screw up files safely */
new String:g_Filename[PLATFORM_MAX_PATH]; /* Used for error messages */
#include "admin-levels.sp" #include "admin-levels.sp"
#include "admin-overrides.sp" #include "admin-overrides.sp"
@ -31,3 +33,25 @@ public OnRebuildAdminCache(AdminCachePart:part)
ReadGroups(); ReadGroups();
} }
} }
ParseError(const String:format[], {Handle,String,Float,_}:...)
{
decl String:buffer[512];
if (!g_LoggedFileName)
{
LogError("Error(s) detected parsing admin_levels.cfg:");
g_LoggedFileName = true;
}
VFormat(buffer, sizeof(buffer), format, 2);
LogError(" (%d) %s", ++g_ErrorCount, buffer);
}
InitGlobalStates()
{
g_ErrorCount = 0;
g_IgnoreLevel = 0;
g_LoggedFileName = false;
}

View File

@ -11,32 +11,23 @@ static g_GroupState = GROUP_STATE_NONE;
static g_GroupPass = 0 static g_GroupPass = 0
static bool:g_NeedReparse = false; static bool:g_NeedReparse = false;
//:TODO: immunity needs to self-check itself for redundancy in core
//:TODO: invalidating groups internally needs to check immunities
//:TODO: reparsing //:TODO: reparsing
static LogGroupError(const String:fmt[], {Handle,String,Float,_}:...)
{
decl String:buffer[512];
if (!g_LoggedFileName)
{
LogError("Error(s) detected parsing admin_groups.cfg:");
g_LoggedFileName = true;
}
VFormat(buffer, sizeof(buffer), fmt, 2);
LogError(" (%d) %s", ++g_ErrorCount, buffer);
}
public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes) public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{ {
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_NONE) if (g_GroupState == GROUP_STATE_NONE)
{ {
if (StrEqual(name, "Groups")) if (StrEqual(name, "Groups"))
{ {
g_GroupState = GROUP_STATE_GROUPS; g_GroupState = GROUP_STATE_GROUPS;
} else {
g_IgnoreLevel++;
} }
} else if (g_GroupState == GROUP_STATE_GROUPS) { } else if (g_GroupState == GROUP_STATE_GROUPS) {
if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID) if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
@ -48,7 +39,11 @@ public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt
if (StrEqual(name, "Overrides")) if (StrEqual(name, "Overrides"))
{ {
g_GroupState = GROUP_STATE_OVERRIDES; g_GroupState = GROUP_STATE_OVERRIDES;
} else {
g_IgnoreLevel++;
} }
} else {
g_IgnoreLevel++;
} }
return SMCParse_Continue; return SMCParse_Continue;
@ -60,7 +55,8 @@ public SMCResult:ReadGroups_KeyValue(Handle:smc,
bool:key_quotes, bool:key_quotes,
bool:value_quotes) bool:value_quotes)
{ {
if (g_CurGrp == INVALID_GROUP_ID) if (g_CurGrp == INVALID_GROUP_ID
|| g_IgnoreLevel)
{ {
return SMCParse_Continue; return SMCParse_Continue;
} }
@ -130,7 +126,7 @@ public SMCResult:ReadGroups_KeyValue(Handle:smc,
{ {
SetAdmGroupImmuneFrom(g_CurGrp, id); SetAdmGroupImmuneFrom(g_CurGrp, id);
} else { } else {
LogGroupError("Unable to find group: \"%s\"", value); ParseError("Unable to find group: \"%s\"", value);
} }
} }
} }
@ -140,6 +136,13 @@ public SMCResult:ReadGroups_KeyValue(Handle:smc,
public SMCResult:ReadGroups_EndSection(Handle:smc) public SMCResult:ReadGroups_EndSection(Handle:smc)
{ {
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_OVERRIDES) if (g_GroupState == GROUP_STATE_OVERRIDES)
{ {
g_GroupState = GROUP_STATE_INGROUP; g_GroupState = GROUP_STATE_INGROUP;
@ -165,20 +168,13 @@ static InitializeGroupParser()
} }
} }
ReadGroups() InternalReadGroups(const String:path[], pass)
{ {
new String:path[PLATFORM_MAX_PATH];
InitializeGroupParser();
BuildPath(Path_SM, path, sizeof(path), "configs/admin_groups.cfg");
/* Set states */ /* Set states */
InitGlobalStates();
g_GroupState = GROUP_STATE_NONE; g_GroupState = GROUP_STATE_NONE;
g_LoggedFileName = false;
g_ErrorCount = 0;
g_CurGrp = INVALID_GROUP_ID; g_CurGrp = INVALID_GROUP_ID;
g_GroupPass = GROUP_PASS_FIRST; g_GroupPass = pass;
g_NeedReparse = false; g_NeedReparse = false;
new SMCError:err = SMC_ParseFile(g_hGroupParser, path); new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
@ -187,10 +183,23 @@ ReadGroups()
decl String:buffer[64]; decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer))) if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{ {
LogGroupError("%s", buffer); ParseError("%s", buffer);
} else { } else {
LogGroupError("Fatal parse error"); ParseError("Fatal parse error");
} }
} }
} }
ReadGroups()
{
InitializeGroupParser();
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
if (g_NeedReparse)
{
InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
}
}

View File

@ -36,34 +36,31 @@ LoadDefaultLetters()
g_FlagLetters['z'-'a'] = Admin_Root; g_FlagLetters['z'-'a'] = Admin_Root;
} }
static LogLevelError(const String:format[], {Handle,String,Float,_}:...)
{
decl String:buffer[512];
if (!g_LoggedFileName)
{
LogError("Error(s) detected parsing admin_levels.cfg:");
g_LoggedFileName = true;
}
VFormat(buffer, sizeof(buffer), format, 2);
LogError(" (%d) %s", ++g_ErrorCount, buffer);
}
public SMCResult:ReadLevels_NewSection(Handle:smc, const String:name[], bool:opt_quotes) public SMCResult:ReadLevels_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{ {
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_LevelState == LEVEL_STATE_NONE) if (g_LevelState == LEVEL_STATE_NONE)
{ {
if (StrEqual(name, "Levels")) if (StrEqual(name, "Levels"))
{ {
g_LevelState = LEVEL_STATE_LEVELS; g_LevelState = LEVEL_STATE_LEVELS;
} else {
g_IgnoreLevel++;
} }
} else if (g_LevelState == LEVEL_STATE_LEVELS) { } else if (g_LevelState == LEVEL_STATE_LEVELS) {
if (StrEqual(name, "Flags")) if (StrEqual(name, "Flags"))
{ {
g_LevelState = LEVEL_STATE_FLAGS; g_LevelState = LEVEL_STATE_FLAGS;
} else {
g_IgnoreLevel++;
} }
} else {
g_IgnoreLevel++;
} }
return SMCParse_Continue; return SMCParse_Continue;
@ -71,13 +68,13 @@ public SMCResult:ReadLevels_NewSection(Handle:smc, const String:name[], bool:opt
public SMCResult:ReadLevels_KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes) public SMCResult:ReadLevels_KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes)
{ {
if (g_LevelState == LEVEL_STATE_FLAGS) if (g_LevelState == LEVEL_STATE_FLAGS && !g_IgnoreLevel)
{ {
new chr = value[0]; new chr = value[0];
if (chr < 'a' || chr > 'z') if (chr < 'a' || chr > 'z')
{ {
LogLevelError("Unrecognized character: \"%s\"", value); ParseError("Unrecognized character: \"%s\"", value);
return SMCParse_Continue; return SMCParse_Continue;
} }
@ -127,7 +124,7 @@ public SMCResult:ReadLevels_KeyValue(Handle:smc, const String:key[], const Strin
} else if (StrEqual(key, "custom6")) { } else if (StrEqual(key, "custom6")) {
flag = Admin_Custom6; flag = Admin_Custom6;
} else { } else {
LogLevelError("Unrecognized flag type: %s", key); ParseError("Unrecognized flag type: %s", key);
} }
g_FlagLetters[chr] = flag; g_FlagLetters[chr] = flag;
@ -139,6 +136,13 @@ public SMCResult:ReadLevels_KeyValue(Handle:smc, const String:key[], const Strin
public SMCResult:ReadLevels_EndSection(Handle:smc) public SMCResult:ReadLevels_EndSection(Handle:smc)
{ {
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_LevelState == LEVEL_STATE_FLAGS) if (g_LevelState == LEVEL_STATE_FLAGS)
{ {
/* We're totally done parsing */ /* We're totally done parsing */
@ -165,27 +169,24 @@ static InitializeLevelParser()
RefreshLevels() RefreshLevels()
{ {
new String:path[PLATFORM_MAX_PATH];
LoadDefaultLetters(); LoadDefaultLetters();
InitializeLevelParser(); InitializeLevelParser();
BuildPath(Path_SM, path, sizeof(path), "configs/admin_levels.cfg"); BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_levels.cfg");
/* Set states */ /* Set states */
InitGlobalStates();
g_LevelState = LEVEL_STATE_NONE; g_LevelState = LEVEL_STATE_NONE;
g_LoggedFileName = false;
g_ErrorCount = 0;
new SMCError:err = SMC_ParseFile(g_hLevelParser, path); new SMCError:err = SMC_ParseFile(g_hLevelParser, g_Filename);
if (err != SMCError_Okay) if (err != SMCError_Okay)
{ {
decl String:buffer[64]; decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer))) if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{ {
LogLevelError("%s", buffer); ParseError("%s", buffer);
} else { } else {
LogLevelError("Fatal parse error"); ParseError("Fatal parse error");
} }
} }
} }

View File

@ -5,35 +5,31 @@
static Handle:g_hOverrideParser = INVALID_HANDLE; static Handle:g_hOverrideParser = INVALID_HANDLE;
static g_OverrideState = OVERRIDE_STATE_NONE; static g_OverrideState = OVERRIDE_STATE_NONE;
static LogOverrideError(const String:format[], {Handle,String,Float,_}:...)
{
decl String:buffer[512];
if (!g_LoggedFileName)
{
LogError("Error(s) detected parsing admin_levels.cfg:");
g_LoggedFileName = true;
}
VFormat(buffer, sizeof(buffer), format, 2);
LogError(" (%d) %s", ++g_ErrorCount, buffer);
}
public SMCResult:ReadOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes) public SMCResult:ReadOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{ {
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_NONE) if (g_OverrideState == OVERRIDE_STATE_NONE)
{ {
if (StrEqual(name, "Levels")) if (StrEqual(name, "Levels"))
{ {
g_OverrideState = OVERRIDE_STATE_LEVELS; g_OverrideState = OVERRIDE_STATE_LEVELS;
} else {
g_IgnoreLevel++;
} }
} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) { } else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
if (StrEqual(name, "Overrides")) if (StrEqual(name, "Overrides"))
{ {
g_OverrideState = OVERRIDE_STATE_OVERRIDES; g_OverrideState = OVERRIDE_STATE_OVERRIDES;
} else {
g_IgnoreLevel++;
} }
} else {
g_IgnoreLevel++;
} }
return SMCParse_Continue; return SMCParse_Continue;
@ -45,7 +41,8 @@ public SMCResult:ReadOverrides_KeyValue(Handle:smc,
bool:key_quotes, bool:key_quotes,
bool:value_quotes) bool:value_quotes)
{ {
if (g_OverrideState != OVERRIDE_STATE_OVERRIDES) if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
|| g_IgnoreLevel)
{ {
return SMCParse_Continue; return SMCParse_Continue;
} }
@ -58,13 +55,13 @@ public SMCResult:ReadOverrides_KeyValue(Handle:smc,
{ {
if (value[i] < 'a' || value[i] > 'z') if (value[i] < 'a' || value[i] > 'z')
{ {
LogOverrideError("Invalid flag detected: %c", value[i]); ParseError("Invalid flag detected: %c", value[i]);
continue; continue;
} }
new val = value[i] - 'a'; new val = value[i] - 'a';
if (!g_FlagsSet[val]) if (!g_FlagsSet[val])
{ {
LogOverrideError("Invalid flag detected: %c", value[i]); ParseError("Invalid flag detected: %c", value[i]);
continue; continue;
} }
array[flags_total++] = g_FlagLetters[val]; array[flags_total++] = g_FlagLetters[val];
@ -84,6 +81,13 @@ public SMCResult:ReadOverrides_KeyValue(Handle:smc,
public SMCResult:ReadOverrides_EndSection(Handle:smc) public SMCResult:ReadOverrides_EndSection(Handle:smc)
{ {
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_LEVELS) if (g_OverrideState == OVERRIDE_STATE_LEVELS)
{ {
g_OverrideState = OVERRIDE_STATE_NONE; g_OverrideState = OVERRIDE_STATE_NONE;
@ -110,27 +114,23 @@ static InitializeOverrideParser()
ReadOverrides() ReadOverrides()
{ {
new String:path[PLATFORM_MAX_PATH];
InitializeOverrideParser(); InitializeOverrideParser();
BuildPath(Path_SM, path, sizeof(path), "configs/admin_levels.cfg"); BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_levels.cfg");
/* Set states */ /* Set states */
InitGlobalStates();
g_OverrideState = OVERRIDE_STATE_NONE; g_OverrideState = OVERRIDE_STATE_NONE;
g_LoggedFileName = false;
g_ErrorCount = 0;
new SMCError:err = SMC_ParseFile(g_hOverrideParser, path); new SMCError:err = SMC_ParseFile(g_hOverrideParser, g_Filename);
if (err != SMCError_Okay) if (err != SMCError_Okay)
{ {
decl String:buffer[64]; decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer))) if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{ {
LogOverrideError("%s", buffer); ParseError("%s", buffer);
} else { } else {
LogOverrideError("Fatal parse error"); ParseError("Fatal parse error");
} }
} }
} }