Convert admin-flatfile to use AdminId/GroupId methodmaps and newdecls.

This commit is contained in:
Nicholas Hastings 2015-02-17 08:24:46 -08:00
parent e4ee52e1ac
commit 63d1fa2d8e
5 changed files with 158 additions and 143 deletions

View File

@ -36,7 +36,7 @@
#include <sourcemod>
public Plugin:myinfo =
public Plugin myinfo =
{
name = "Admin File Reader",
author = "AlliedModders LLC",
@ -46,18 +46,18 @@ public Plugin:myinfo =
};
/** Various parsing globals */
new bool:g_LoggedFileName = false; /* Whether or not the file name has been logged */
new g_ErrorCount = 0; /* Current error count */
new g_IgnoreLevel = 0; /* Nested ignored section count, so users can screw up files safely */
new g_CurrentLine = 0; /* Current line we're on */
new String:g_Filename[PLATFORM_MAX_PATH]; /* Used for error messages */
bool g_LoggedFileName = false; /* Whether or not the file name has been logged */
int g_ErrorCount = 0; /* Current error count */
int g_IgnoreLevel = 0; /* Nested ignored section count, so users can screw up files safely */
int g_CurrentLine = 0; /* Current line we're on */
char g_Filename[PLATFORM_MAX_PATH]; /* Used for error messages */
#include "admin-overrides.sp"
#include "admin-groups.sp"
#include "admin-users.sp"
#include "admin-simple.sp"
public OnRebuildAdminCache(AdminCachePart:part)
public void OnRebuildAdminCache(AdminCachePart part)
{
if (part == AdminCache_Overrides)
{
@ -70,9 +70,9 @@ public OnRebuildAdminCache(AdminCachePart:part)
}
}
ParseError(const String:format[], any:...)
void ParseError(const char[] format, any ...)
{
decl String:buffer[512];
char buffer[512];
if (!g_LoggedFileName)
{
@ -87,7 +87,7 @@ ParseError(const String:format[], any:...)
g_ErrorCount++;
}
InitGlobalStates()
void InitGlobalStates()
{
g_ErrorCount = 0;
g_IgnoreLevel = 0;

View File

@ -31,18 +31,26 @@
* Version: $Id$
*/
#define GROUP_STATE_NONE 0
#define GROUP_STATE_GROUPS 1
#define GROUP_STATE_INGROUP 2
#define GROUP_STATE_OVERRIDES 3
#define GROUP_PASS_FIRST 1
#define GROUP_PASS_SECOND 2
enum GroupState
{
GroupState_None,
GroupState_Groups,
GroupState_InGroup,
GroupState_Overrides,
}
enum GroupPass
{
GroupPass_Invalid,
GroupPass_First,
GroupPass_Second,
}
static SMCParser g_hGroupParser;
static GroupId:g_CurGrp = INVALID_GROUP_ID;
static g_GroupState = GROUP_STATE_NONE;
static g_GroupPass = 0;
static bool:g_NeedReparse = false;
static GroupId g_CurGrp = INVALID_GROUP_ID;
static GroupState g_GroupState = GroupState_None;
static GroupPass g_GroupPass = GroupPass_Invalid;
static bool g_NeedReparse = false;
public SMCResult ReadGroups_NewSection(SMCParser smc, const char[] name, bool opt_quotes)
{
@ -52,24 +60,24 @@ public SMCResult ReadGroups_NewSection(SMCParser smc, const char[] name, bool op
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_NONE)
if (g_GroupState == GroupState_None)
{
if (StrEqual(name, "Groups"))
{
g_GroupState = GROUP_STATE_GROUPS;
g_GroupState = GroupState_Groups;
} else {
g_IgnoreLevel++;
}
} else if (g_GroupState == GROUP_STATE_GROUPS) {
} else if (g_GroupState == GroupState_Groups) {
if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
{
g_CurGrp = FindAdmGroup(name);
}
g_GroupState = GROUP_STATE_INGROUP;
} else if (g_GroupState == GROUP_STATE_INGROUP) {
g_GroupState = GroupState_InGroup;
} else if (g_GroupState == GroupState_InGroup) {
if (StrEqual(name, "Overrides"))
{
g_GroupState = GROUP_STATE_OVERRIDES;
g_GroupState = GroupState_Overrides;
} else {
g_IgnoreLevel++;
}
@ -91,28 +99,28 @@ public SMCResult ReadGroups_KeyValue(SMCParser smc,
return SMCParse_Continue;
}
new AdminFlag:flag;
AdminFlag flag;
if (g_GroupPass == GROUP_PASS_FIRST)
if (g_GroupPass == GroupPass_First)
{
if (g_GroupState == GROUP_STATE_INGROUP)
if (g_GroupState == GroupState_InGroup)
{
if (StrEqual(key, "flags"))
{
new len = strlen(value);
for (new i=0; i<len; i++)
int len = strlen(value);
for (int i=0; i<len; i++)
{
if (!FindFlagByChar(value[i], flag))
{
continue;
}
SetAdmGroupAddFlag(g_CurGrp, flag, true);
g_CurGrp.SetFlag(flag, true);
}
} else if (StrEqual(key, "immunity")) {
g_NeedReparse = true;
}
} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
new OverrideRule:rule = Command_Deny;
} else if (g_GroupState == GroupState_Overrides) {
OverrideRule rule = Command_Deny;
if (StrEqual(value, "allow", false))
{
@ -121,29 +129,29 @@ public SMCResult ReadGroups_KeyValue(SMCParser smc,
if (key[0] == '@')
{
AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
g_CurGrp.AddCommandOverride(key[1], Override_CommandGroup, rule);
} else {
AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
g_CurGrp.AddCommandOverride(key, Override_Command, rule);
}
}
} else if (g_GroupPass == GROUP_PASS_SECOND
&& g_GroupState == GROUP_STATE_INGROUP) {
} else if (g_GroupPass == GroupPass_Second
&& g_GroupState == GroupState_InGroup) {
/* Check for immunity again, core should handle double inserts */
if (StrEqual(key, "immunity"))
{
/* If it's a value we know about, use it */
if (StrEqual(value, "*"))
{
SetAdmGroupImmunityLevel(g_CurGrp, 2);
g_CurGrp.ImmunityLevel = 2;
} else if (StrEqual(value, "$")) {
SetAdmGroupImmunityLevel(g_CurGrp, 1);
g_CurGrp.ImmunityLevel = 1;
} else {
new level;
int level;
if (StringToIntEx(value, level))
{
SetAdmGroupImmunityLevel(g_CurGrp, level);
g_CurGrp.ImmunityLevel = level;
} else {
new GroupId:id;
GroupId id;
if (value[0] == '@')
{
id = FindAdmGroup(value[1]);
@ -152,7 +160,7 @@ public SMCResult ReadGroups_KeyValue(SMCParser smc,
}
if (id != INVALID_GROUP_ID)
{
SetAdmGroupImmuneFrom(g_CurGrp, id);
g_CurGrp.AddGroupImmunity(id);
} else {
ParseError("Unable to find group: \"%s\"", value);
}
@ -173,14 +181,14 @@ public SMCResult ReadGroups_EndSection(SMCParser smc)
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_OVERRIDES)
if (g_GroupState == GroupState_Overrides)
{
g_GroupState = GROUP_STATE_INGROUP;
} else if (g_GroupState == GROUP_STATE_INGROUP) {
g_GroupState = GROUP_STATE_GROUPS;
g_GroupState = GroupState_InGroup;
} else if (g_GroupState == GroupState_InGroup) {
g_GroupState = GroupState_Groups;
g_CurGrp = INVALID_GROUP_ID;
} else if (g_GroupState == GROUP_STATE_GROUPS) {
g_GroupState = GROUP_STATE_NONE;
} else if (g_GroupState == GroupState_Groups) {
g_GroupState = GroupState_None;
}
return SMCParse_Continue;
@ -193,7 +201,7 @@ public SMCResult ReadGroups_CurrentLine(SMCParser smc, const char[] line, int li
return SMCParse_Continue;
}
static InitializeGroupParser()
static void InitializeGroupParser()
{
if (!g_hGroupParser)
{
@ -205,11 +213,11 @@ static InitializeGroupParser()
}
}
static InternalReadGroups(const String:path[], pass)
static void InternalReadGroups(const char[] path, GroupPass pass)
{
/* Set states */
InitGlobalStates();
g_GroupState = GROUP_STATE_NONE;
g_GroupState = GroupState_None;
g_CurGrp = INVALID_GROUP_ID;
g_GroupPass = pass;
g_NeedReparse = false;
@ -227,16 +235,16 @@ static InternalReadGroups(const String:path[], pass)
}
}
ReadGroups()
void ReadGroups()
{
InitializeGroupParser();
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
InternalReadGroups(g_Filename, GroupPass_First);
if (g_NeedReparse)
{
InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
InternalReadGroups(g_Filename, GroupPass_Second);
}
}

View File

@ -32,13 +32,16 @@
* Version: $Id$
*/
#define OVERRIDE_STATE_NONE 0
#define OVERRIDE_STATE_LEVELS 1
#define OVERRIDE_STATE_OVERRIDES 2
enum OverrideState
{
OverrideState_None,
OverrideState_Levels,
OverrideState_Overrides,
}
static SMCParser g_hOldOverrideParser;
static SMCParser g_hNewOverrideParser;
static g_OverrideState = OVERRIDE_STATE_NONE;
static OverrideState g_OverrideState = OverrideState_None;
public SMCResult ReadOldOverrides_NewSection(SMCParser smc, const char[] name, bool opt_quotes)
{
@ -48,18 +51,18 @@ public SMCResult ReadOldOverrides_NewSection(SMCParser smc, const char[] name, b
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_NONE)
if (g_OverrideState == OverrideState_None)
{
if (StrEqual(name, "Levels"))
{
g_OverrideState = OVERRIDE_STATE_LEVELS;
g_OverrideState = OverrideState_Levels;
} else {
g_IgnoreLevel++;
}
} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
} else if (g_OverrideState == OverrideState_Levels) {
if (StrEqual(name, "Overrides"))
{
g_OverrideState = OVERRIDE_STATE_OVERRIDES;
g_OverrideState = OverrideState_Overrides;
} else {
g_IgnoreLevel++;
}
@ -78,11 +81,11 @@ public SMCResult ReadNewOverrides_NewSection(SMCParser smc, const char[] name, b
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_NONE)
if (g_OverrideState == OverrideState_None)
{
if (StrEqual(name, "Overrides"))
{
g_OverrideState = OVERRIDE_STATE_OVERRIDES;
g_OverrideState = OverrideState_Overrides;
} else {
g_IgnoreLevel++;
}
@ -99,7 +102,7 @@ public SMCResult ReadOverrides_KeyValue(SMCParser smc,
bool key_quotes,
bool value_quotes)
{
if (g_OverrideState != OVERRIDE_STATE_OVERRIDES || g_IgnoreLevel)
if (g_OverrideState != OverrideState_Overrides || g_IgnoreLevel)
{
return SMCParse_Continue;
}
@ -125,12 +128,12 @@ public SMCResult ReadOldOverrides_EndSection(SMCParser smc)
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_LEVELS)
if (g_OverrideState == OverrideState_Levels)
{
g_OverrideState = OVERRIDE_STATE_NONE;
} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
g_OverrideState = OverrideState_None;
} else if (g_OverrideState == OverrideState_Overrides) {
/* We're totally done parsing */
g_OverrideState = OVERRIDE_STATE_LEVELS;
g_OverrideState = OverrideState_Levels;
return SMCParse_Halt;
}
@ -146,9 +149,9 @@ public SMCResult ReadNewOverrides_EndSection(SMCParser smc)
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_OVERRIDES)
if (g_OverrideState == OverrideState_Overrides)
{
g_OverrideState = OVERRIDE_STATE_NONE;
g_OverrideState = OverrideState_None;
}
return SMCParse_Continue;
@ -161,7 +164,7 @@ public SMCResult ReadOverrides_CurrentLine(SMCParser smc, const char[] line, int
return SMCParse_Continue;
}
static InitializeOverrideParsers()
static void InitializeOverrideParsers()
{
if (!g_hOldOverrideParser)
{
@ -181,13 +184,13 @@ static InitializeOverrideParsers()
}
}
InternalReadOverrides(SMCParser parser, const char[] file)
void InternalReadOverrides(SMCParser parser, const char[] file)
{
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), file);
/* Set states */
InitGlobalStates();
g_OverrideState = OVERRIDE_STATE_NONE;
g_OverrideState = OverrideState_None;
SMCError err = parser.ParseFile(g_Filename);
if (err != SMCError_Okay)
@ -202,7 +205,7 @@ InternalReadOverrides(SMCParser parser, const char[] file)
}
}
ReadOverrides()
void ReadOverrides()
{
InitializeOverrideParsers();
InternalReadOverrides(g_hOldOverrideParser, "configs/admin_levels.cfg");

View File

@ -31,7 +31,7 @@
* Version: $Id$
*/
public ReadSimpleUsers()
public void ReadSimpleUsers()
{
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
@ -90,7 +90,7 @@ public ReadSimpleUsers()
DecodeAuthMethod(const String:auth[], String:method[32], &offset)
void DecodeAuthMethod(const char[] auth, char method[32], int &offset)
{
if ((StrContains(auth, "STEAM_") == 0) || (strncmp("0:", auth, 2) == 0) || (strncmp("1:", auth, 2) == 0))
{
@ -119,13 +119,13 @@ DecodeAuthMethod(const String:auth[], String:method[32], &offset)
}
}
ReadAdminLine(const String:line[])
void ReadAdminLine(const char[] line)
{
new bool:is_bound;
new AdminId:admin;
new String:auth[64];
decl String:auth_method[32];
new idx, cur_idx, auth_offset;
bool is_bound;
AdminId admin;
char auth[64];
char auth_method[32];
int idx, cur_idx, auth_offset;
if ((cur_idx = BreakString(line, auth, sizeof(auth))) == -1)
{
@ -148,16 +148,16 @@ ReadAdminLine(const String:line[])
}
/* Read flags */
new String:flags[64];
char flags[64];
cur_idx = BreakString(line[idx], flags, sizeof(flags));
idx += cur_idx;
/* Read immunity level, if any */
new level, flag_idx;
int level, flag_idx;
if ((flag_idx = StringToIntEx(flags, level)) > 0)
{
SetAdminImmunityLevel(admin, level);
admin.ImmunityLevel = level;
if (flags[flag_idx] == ':')
{
flag_idx++;
@ -166,41 +166,41 @@ ReadAdminLine(const String:line[])
if (flags[flag_idx] == '@')
{
new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
GroupId gid = FindAdmGroup(flags[flag_idx + 1]);
if (gid == INVALID_GROUP_ID)
{
ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
return;
}
AdminInheritGroup(admin, gid);
admin.InheritGroup(gid);
}
else
{
new len = strlen(flags[flag_idx]);
new bool:is_default = false;
for (new i=0; i<len; i++)
int len = strlen(flags[flag_idx]);
bool is_default = false;
for (int i=0; i<len; i++)
{
if (!level && flags[flag_idx + i] == '$')
{
SetAdminImmunityLevel(admin, 1);
admin.ImmunityLevel = 1;
} else {
new AdminFlag:flag;
AdminFlag flag;
if (!FindFlagByChar(flags[flag_idx + i], flag))
{
ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
continue;
}
SetAdminFlag(admin, flag, true);
admin.SetFlag(flag, true);
}
}
if (is_default)
{
new GroupId:gid = FindAdmGroup("Default");
GroupId gid = FindAdmGroup("Default");
if (gid != INVALID_GROUP_ID)
{
AdminInheritGroup(admin, gid);
admin.InheritGroup(gid);
}
}
}
@ -208,18 +208,18 @@ ReadAdminLine(const String:line[])
/* Lastly, is there a password? */
if (cur_idx != -1)
{
decl String:password[64];
char password[64];
BreakString(line[idx], password, sizeof(password));
SetAdminPassword(admin, password);
admin.SetPassword(password);
}
/* Now, bind the identity to something */
if (!is_bound)
{
if (!BindAdminIdentity(admin, auth_method, auth[auth_offset]))
if (!admin.BindIdentity(auth_method, auth[auth_offset]))
{
/* We should never reach here */
RemoveAdmin(admin);
admin.Purge();
ParseError("Failed to bind identity %s (method %s)", auth[auth_offset], auth_method);
}
}

View File

@ -31,21 +31,24 @@
* Version: $Id$
*/
#define USER_STATE_NONE 0
#define USER_STATE_ADMINS 1
#define USER_STATE_INADMIN 2
enum UserState
{
UserState_None,
UserState_Admins,
UserState_InAdmin,
}
static SMCParser g_hUserParser;
static g_UserState = USER_STATE_NONE;
static String:g_CurAuth[64];
static String:g_CurIdent[64];
static String:g_CurName[64];
static String:g_CurPass[64];
static Handle:g_GroupArray;
static g_CurFlags;
static g_CurImmunity;
static UserState g_UserState = UserState_None;
static char g_CurAuth[64];
static char g_CurIdent[64];
static char g_CurName[64];
static char g_CurPass[64];
static ArrayList g_GroupArray;
static int g_CurFlags;
static int g_CurImmunity;
public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
public SMCResult ReadUsers_NewSection(SMCParser smc, const char[] name, bool opt_quotes)
{
if (g_IgnoreLevel)
{
@ -53,25 +56,25 @@ public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_
return SMCParse_Continue;
}
if (g_UserState == USER_STATE_NONE)
if (g_UserState == UserState_None)
{
if (StrEqual(name, "Admins"))
{
g_UserState = USER_STATE_ADMINS;
g_UserState = UserState_Admins;
}
else
{
g_IgnoreLevel++;
}
}
else if (g_UserState == USER_STATE_ADMINS)
else if (g_UserState == UserState_Admins)
{
g_UserState = USER_STATE_INADMIN;
g_UserState = UserState_InAdmin;
strcopy(g_CurName, sizeof(g_CurName), name);
g_CurAuth[0] = '\0';
g_CurIdent[0] = '\0';
g_CurPass[0] = '\0';
ClearArray(g_GroupArray);
g_GroupArray.Clear();
g_CurFlags = 0;
g_CurImmunity = 0;
}
@ -83,13 +86,13 @@ public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_
return SMCParse_Continue;
}
public SMCResult:ReadUsers_KeyValue(Handle:smc,
const String:key[],
const String:value[],
bool:key_quotes,
bool:value_quotes)
public SMCResult ReadUsers_KeyValue(SMCParser smc,
const char[] key,
const char[] value,
bool key_quotes,
bool value_quotes)
{
if (g_UserState != USER_STATE_INADMIN || g_IgnoreLevel)
if (g_UserState != UserState_InAdmin || g_IgnoreLevel)
{
return SMCParse_Continue;
}
@ -108,20 +111,20 @@ public SMCResult:ReadUsers_KeyValue(Handle:smc,
}
else if (StrEqual(key, "group"))
{
new GroupId:id = FindAdmGroup(value);
GroupId id = FindAdmGroup(value);
if (id == INVALID_GROUP_ID)
{
ParseError("Unknown group \"%s\"", value);
}
PushArrayCell(g_GroupArray, id);
g_GroupArray.Push(id);
}
else if (StrEqual(key, "flags"))
{
new len = strlen(value);
new AdminFlag:flag;
int len = strlen(value);
AdminFlag flag;
for (new i = 0; i < len; i++)
for (int i = 0; i < len; i++)
{
if (!FindFlagByChar(value[i], flag))
{
@ -141,7 +144,7 @@ public SMCResult:ReadUsers_KeyValue(Handle:smc,
return SMCParse_Continue;
}
public SMCResult:ReadUsers_EndSection(Handle:smc)
public SMCResult ReadUsers_EndSection(SMCParser smc)
{
if (g_IgnoreLevel)
{
@ -149,13 +152,14 @@ public SMCResult:ReadUsers_EndSection(Handle:smc)
return SMCParse_Continue;
}
if (g_UserState == USER_STATE_INADMIN)
if (g_UserState == UserState_InAdmin)
{
/* Dump this user to memory */
if (g_CurIdent[0] != '\0' && g_CurAuth[0] != '\0')
{
decl AdminFlag:flags[26];
new AdminId:id, i, num_groups, num_flags;
AdminFlag flags[26];
AdminId id;
int i, num_groups, num_flags;
if ((id = FindAdminByIdentity(g_CurAuth, g_CurIdent)) == INVALID_ADMIN_ID)
{
@ -168,10 +172,10 @@ public SMCResult:ReadUsers_EndSection(Handle:smc)
}
}
num_groups = GetArraySize(g_GroupArray);
num_groups = g_GroupArray.Length;
for (i = 0; i < num_groups; i++)
{
AdminInheritGroup(id, GetArrayCell(g_GroupArray, i));
AdminInheritGroup(id, g_GroupArray.Get(i));
}
SetAdminPassword(id, g_CurPass);
@ -191,24 +195,24 @@ public SMCResult:ReadUsers_EndSection(Handle:smc)
ParseError("Failed to create admin: did you forget either the auth or identity properties?");
}
g_UserState = USER_STATE_ADMINS;
g_UserState = UserState_Admins;
}
else if (g_UserState == USER_STATE_ADMINS)
else if (g_UserState == UserState_Admins)
{
g_UserState = USER_STATE_NONE;
g_UserState = UserState_None;
}
return SMCParse_Continue;
}
public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
public SMCResult ReadUsers_CurrentLine(SMCParser smc, const char[] line, int lineno)
{
g_CurrentLine = lineno;
return SMCParse_Continue;
}
static InitializeUserParser()
static void InitializeUserParser()
{
if (!g_hUserParser)
{
@ -222,7 +226,7 @@ static InitializeUserParser()
}
}
ReadUsers()
void ReadUsers()
{
InitializeUserParser();
@ -230,7 +234,7 @@ ReadUsers()
/* Set states */
InitGlobalStates();
g_UserState = USER_STATE_NONE;
g_UserState = UserState_None;
SMCError err = g_hUserParser.ParseFile(g_Filename);
if (err != SMCError_Okay)