From c68a770456b519a802d687a770a90328588d060d Mon Sep 17 00:00:00 2001 From: nwrl Date: Fri, 15 Aug 2025 15:51:14 -0500 Subject: Replace sourcemod logging functions with helper doSimpleLog function --- nameblocker.sp | 100 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 37 deletions(-) diff --git a/nameblocker.sp b/nameblocker.sp index f125638..982b3e4 100644 --- a/nameblocker.sp +++ b/nameblocker.sp @@ -42,7 +42,7 @@ #include #include -#define DEBUG_304142124110815 1 +#define _SMNB__DEBUG_304142124110815__VERBOSE 1 public Plugin myinfo = { name = "SM Name Blocker", @@ -95,13 +95,40 @@ ConVar gcvarBanTime; static const char BANTIMENAME[] = "nameb ConVar gcvarKickMsg; static const char KICKMSGNAME[] = "nameblock_KickMessage"; static const char DEFAULTKICKMSG[] = "Failed name check"; ConVar gcvarBanMsg; static const char BANMSGNAME[] = "nameblock_BanMessage"; static const char DEFAULTBANMESSAGE[] = "Failed name check"; -// Logs and throws an error with the same message of upto 2048 characters -void logAndFail(const char[] format, any ...) { - char buf[2048 + 1]; - VFormat(buf, sizeof(buf), format, 2); +enum LOGMODE { + LM_MSG = (1<<0), + LM_ERROR = (1<<1), + LM_ACTION = (1<<2), // Unused, may implement later + LM_TOFILE = (1<<3), // Unused, may implement later +}; + +void doSimpleLog(LOGMODE modeflags, bool fail=false, const char[] fmt, any ...) { + if(modeflags < LM_MSG || modeflags > (LM_MSG | LM_ERROR)) { + doSimpleLog(LM_ERROR, false, " Mode oob. Expected: [%d, %d], Got: %d", LM_MSG, (LM_MSG | LM_ERROR), modeflags); + return; + } + + /* Got fed up enough to look at sourcemod's code as to how they deal with Format type functions and they just make a 2048 char + // long buffer instead of just using vsnprintf twice to automatically determine the size of the buffer, which is fucking lame + // as hell. Literally, just make a copy of the va_args via va_copy(), run vsnprintf() to get the number of + // characters to be written, then run it again with the copy to actually put it into a buffer. I'm sure there's a better way + // to reimplement asprintf, but it's easy as balls and takes 2 seconds. Instead we get this, and they don't even offer the + // same functionality where VFormat determines the number of bytes to be written, so I have to settle for some bullshit */ + + // This language sucks fuck dude + + char buf[2048]; + VFormat(buf, sizeof(buf), fmt, 3); - LogError(buf); - SetFailState(buf); + if(modeflags & LM_MSG) {LogMessage(buf);} + if(modeflags & LM_ERROR) {LogError(buf);} + + #if defined _SMNB__DEBUG_304142124110815__VERBOSE + PrintToServer(buf); + #endif + + if(fail) SetFailState(buf); + return; } // Concatenate arguments into a single buffer @@ -117,8 +144,8 @@ int concatArgs(char[] buf, int maxbuflen, int maxarglen, int end, int start=1) { Format(tmp, maxbuflen, "%s %s", tmp, arg); } - #if defined DEBUG_304142124110815 - PrintToServer(" Preformed concat: %s", tmp); + #if defined _SMNB__DEBUG_304142124110815__VERBOSE + doSimpleLog(LM_MSG, false, " Preformed concat: %s", tmp); #endif return strcopy(buf, maxbuflen, tmp); } @@ -135,9 +162,9 @@ int registerIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[ return 0; } -// `registerIntConVar` wrapper. Calls `logAndFail` if the convar couldn't be registered +// `registerIntConVar` wrapper. Calls `doSimpleLog` if the convar couldn't be registered void xRegisterIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[] desc) { - if(registerIntConVar(cv, defaultVal, name, desc)) logAndFail("Could not register cvar \"%s\"", name); + if(registerIntConVar(cv, defaultVal, name, desc)) doSimpleLog(LM_ERROR, true, " Error: could not register cvar \"%s\"", name); } @@ -148,7 +175,7 @@ void initDatabase() { bool err = false; db = SQLite_UseDatabase("sourcemod-local", sqlerr, sizeof(sqlerr)); - if(db == null) logAndFail("Could not connect to sql database: %s", sqlerr); + if(db == null) doSimpleLog(LM_ERROR, true, " Error: Could not connect to sql database: %s", sqlerr); // Prep table SQL_LockDatabase(db); @@ -158,8 +185,7 @@ void initDatabase() { } SQL_UnlockDatabase(db); - if(err) logAndFail("Could not create table: %s", sqlerr); - + if(err) doSimpleLog(LM_ERROR, true, " Error: Could not create table: %s", sqlerr); return; } @@ -182,10 +208,10 @@ void initPrepStatements() { SQL_UnlockDatabase(db); switch(err) { - case 1: {logAndFail("Could not prepare insert statement: %s", sqlerr);} // Doesn't recognize "163687013_SMNameBlocker" as a valid token - case 2: {logAndFail("Could not prepare delete statement: %s", sqlerr);} - case 3: {logAndFail("Could not prepare replace statement: %s", sqlerr);} - case 4: {logAndFail("Could not prepare populate statement: %s", sqlerr);} + case 1: {doSimpleLog(LM_ERROR, true, " Error: Could not prepare insert statement: %s", sqlerr);} // Doesn't recognize "163687013_SMNameBlocker" as a valid token + case 2: {doSimpleLog(LM_ERROR, true, " Error: Could not prepare delete statement: %s", sqlerr);} + case 3: {doSimpleLog(LM_ERROR, true, " Error: Could not prepare replace statement: %s", sqlerr);} + case 4: {doSimpleLog(LM_ERROR, true, " Error: Could not prepare populate statement: %s", sqlerr);} } return; @@ -194,10 +220,10 @@ void initPrepStatements() { void loadFromDatabase() { // Initialize and populate datatypes regexlist = new ArrayList(ByteCountToCells(HANDLE_SIZE)); - if(regexlist == null) logAndFail("Could not initialize regexlist ArrayList"); + if(regexlist == null) doSimpleLog(LM_ERROR, true, " Error: Could not initialize regexlist ArrayList"); patternlist = new ArrayList(ByteCountToCells(PATTERN_MAX_LEN)); - if(patternlist == null) logAndFail("Could not initialize patternlist ArrayList"); + if(patternlist == null) doSimpleLog(LM_ERROR, true, " Error: Could not initialize patternlist ArrayList"); // Initialize table if it doesn't exist int err = 0; @@ -213,8 +239,8 @@ void loadFromDatabase() { SQL_UnlockDatabase(db); switch(err) { - case 1: {logAndFail("Could not initialize nameblocker table: %s", sqlerr);} - case 2: {logAndFail("Population query failed: %s", sqlerr);} + case 1: {doSimpleLog(LM_ERROR, true, " Error: Could not initialize nameblocker table: %s", sqlerr);} + case 2: {doSimpleLog(LM_ERROR, true, " Error: Population query failed: %s", sqlerr);} } @@ -224,12 +250,12 @@ void loadFromDatabase() { SQL_FetchString(dbPopulate, 1, pattern, sizeof(pattern)); cur = CompileRegex(pattern, gcvarRegexCompFlags, reerr, sizeof(reerr), reenum); if(cur == null) { - LogError("Could not compile regex pattern \"%s\": %s (%d)", pattern, reerr, reenum); + doSimpleLog(LM_ERROR, false, " Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, reerr, reenum); continue; } if(aModPattern(MM_INSERT, regexlist.Length, pattern)) { - LogError("Couldn't add regex \"%s\" to arraylists, continuing", pattern); + doSimpleLog(LM_ERROR, false, " Error: Couldn't add regex \"%s\" to arraylists, continuing", pattern); CloseHandle(cur); } } @@ -252,10 +278,10 @@ public void OnAllPluginsLoaded() { xRegisterIntConVar(gcvarBanTime, DEFAULTBANTIME, BANTIMENAME, "Time (in minutes) to ban players when operating in ban mode. 0 for permaban"); gcvarKickMsg = CreateConVar(KICKMSGNAME, DEFAULTKICKMSG, "Default kick message"); - if(gcvarKickMsg == null) logAndFail("Could not init convar \"gcvarKickMsg\""); + if(gcvarKickMsg == null) doSimpleLog(LM_ERROR, true, " Error: Could not init convar \"gcvarKickMsg\""); gcvarBanMsg = CreateConVar(BANMSGNAME, DEFAULTBANMESSAGE, "Default ban message"); - if(gcvarBanMsg == null) logAndFail("Could not init convar \"gcvarBanMsg\""); + if(gcvarBanMsg == null) doSimpleLog(LM_ERROR, true, " Error: Could not init convar \"gcvarBanMsg\""); AutoExecConfig(true, "nameblocker_cvars"); @@ -370,7 +396,7 @@ int __modPattern__insert(int index, char[] pattern, int patternlen, int client) bool flag = false; if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { - LogError("Could not get client \"%N\"'s steamid64", client); + doSimpleLog(LM_ERROR, false, " Error: Could not get client \"%N\"'s steamid64", client); return -1; } @@ -381,7 +407,7 @@ int __modPattern__insert(int index, char[] pattern, int patternlen, int client) SQL_BindParamString(dbInsert, 1, steamid64, false); if(!SQL_Execute(dbInsert)) { SQL_GetError(dbInsert, sqlerr, sizeof(sqlerr)); - LogError("Could not insert values into database: %s", sqlerr); + doSimpleLog(LM_ERROR, false, " Error: Could not insert values into database: %s", sqlerr); flag = true; } @@ -406,7 +432,7 @@ int __modPattern__delete(int index) { SQL_BindParamString(dbDelete, 0, pattern, false); if(!SQL_Execute(dbDelete)) { SQL_GetError(dbDelete, sqlerr, sizeof(sqlerr)); - LogError("Could not delete from database: %s", sqlerr); + doSimpleLog(LM_ERROR, false, " Error: Could not delete from database: %s", sqlerr); flag = true; } @@ -429,7 +455,7 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { - LogError("Could not get \"%N\"'s steamid64"); + doSimpleLog(LM_ERROR, false, " Error: Could not get \"%N\"'s steamid64"); return -1; } @@ -441,7 +467,7 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) if(!SQL_Execute(dbReplace)) { SQL_GetError(dbReplace, sqlerr, sizeof(sqlerr)); - LogError("Could not replace in database: %s", sqlerr); + doSimpleLog(LM_ERROR, false, " Error: Could not replace in database: %s", sqlerr); flag = true; } @@ -463,7 +489,7 @@ int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=-1, i case MM_DELETE: {return __modPattern__delete(index);} case MM_REPLACE: {return __modPattern__replace(index, pattern, patternlen, client);} default: { - LogError("Given invalid DBMOD_MODE"); + doSimpleLog(LM_ERROR, false, " Error: Given invalid DBMOD_MODE"); return -1; } } @@ -479,7 +505,7 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { char errstr[512]; RegexError reerr; res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); if(res == null) { - LogError("Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr); + doSimpleLog(LM_ERROR, false, " Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr); return -1; } } @@ -518,7 +544,7 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { default: { - logAndFail("Got impossible state"); + doSimpleLog(LM_ERROR, true, " Error: Got impossible state"); } } @@ -536,7 +562,7 @@ int checkName(int client) { char name[64 + 1]; if(getName(client, name, sizeof(name)) <= 0) { - LogError("Tried to get a client's name for a name check, but could not"); + doSimpleLog(LM_ERROR, false, " Error: Tried to get a client's name for a name check, but could not"); return -1; } @@ -588,13 +614,13 @@ int handleNameHit(int client) { return 0; } default: { - LogError("%L failed a name check, but the operating mode in an invalid state", client); + doSimpleLog(LM_ERROR, false, " Error: %L failed a name check, but the operating mode in an invalid state", client); return -1; } } } int handleFailedRegex(int client) { - LogError("Ran into regex error when trying to check user %L's name", client); + doSimpleLog(LM_ERROR, false, " Error: Ran into regex error when trying to check user %L's name", client); return 0; } \ No newline at end of file -- cgit v1.2.3