From 6b6ead7eee9a97b467f927d4336fac2aa6ff99a7 Mon Sep 17 00:00:00 2001 From: nwrl Date: Wed, 6 Aug 2025 20:10:40 -0500 Subject: Work on sql --- nameblocker.sp | 56 +++++++++++++++++++++++--------------------------------- orderchecker.sp | 29 ----------------------------- 2 files changed, 23 insertions(+), 62 deletions(-) delete mode 100644 orderchecker.sp diff --git a/nameblocker.sp b/nameblocker.sp index 3d5498c..b9f1e7a 100644 --- a/nameblocker.sp +++ b/nameblocker.sp @@ -1,10 +1,14 @@ // Notes: // Memory allocated via the "new" keyword IS garbage collected. Handles are not. Close handles when you're done with them - // Handles are closed when the plugin is unloaded, so if the lifetime of a handle is the plugin's lifetime, don't worry about closing it - // The contents of ArrayList objects are lost on map transition / server restart. Any persistent data should be stored in a - // key-value file OR in a database + + // Handles are closed when the plugin is unloaded, so if the lifetime of a handle is the plugin's lifetime, don't worry about + // closing it + + // The contents of ArrayList objects are lost on SERVER RESTART ONLY. Any persistent data should be stored in a key-value file + // OR in a database // Basically every string function will add a null terminator, so add `+ 1` to the end of any defined size string definition + // It is possible to throw an error with the ThrowError function, but anything that would bother should probably disable the // plugin entirely, so SetFailState is better @@ -16,13 +20,18 @@ // The database should contain: // The string used to compile a regex pattern, sanitized to avoid sql injection attacks // The steamid of the admin who added a regex pattern - // The time a regex pattern was added + // The date (YYYY-MM-DD) a regex pattern was added + // The time (HH:MM:SS.SSS) a regex pattern was added // In the future, this may change to include regex compilation flags, kick/ban modes, etc. . Depends on how many features // I want to cram into this given how fundamentally shitty SourcePawn as a language is. I'm not trying to keep track of 17 // different arrays and make sure that the indicies between them never get out of date. Maybe enum structs will solve my // concerns, maybe not + // Plugins are loaded/unloaded on server start/end, and on map changes IF the plugin has been modified. This means, because I'm + // first writing to the database and then updating the lists, there realistically shouldn't be any weird memory problems. + // That makes this a whole lot easier + #pragma newdecls required #pragma semicolon 1 @@ -54,7 +63,9 @@ enum OperatingMode { ArrayList regexlist; ArrayList patternlist; + Database db; +static const char TABLENAME[] = "163687013_SMNameBlocker"; ConVar gcvarOperMode; static const char OPERMODENAME[] = "nameblock_OperatingMode"; const OperatingMode DEFAULTOPERMODE = OP_KICK; ConVar gcvarAdmCmdFlag; static const char ADMCMDFLAGNAME[] = "nameblock_AdminCommandFlag"; const int DEFAULTADMCMDFLAG = ADMFLAG_BAN; @@ -125,34 +136,6 @@ public void OnAllPluginsLoaded() { RegAdminCmd("nb_listpatterns", cmdListPatterns, gcvarAdmCmdFlag.IntValue, "List current regex patterns and their indicies"); } -public void OnConfigsExecuted() { - // I'm not sure when this is executed in relation to when OnClientPostAdminCheck is. Sourcemod's API reference says it's ran - // once after OnMapStart, but idk if "Map Start" is a sufficient enough game state for players to join - - // Let me illustrate my concern: - // Server starts normally, plugin is working - // Players join. Nothing weird happens because the plugin loaded everything before a player could have joined - // Players/Server initiates a map change WHILE players are still connected. If OnMapStart is when players can start - // connecting, and is before OnConfigsExecuted, the following scenarios could happen: - - // 1: OnMapStart fires, players may join - // 2: Player joins. This fires OnClientPostAdminCheck - // 3: OnClientPostAdminCheck fires checkName, which tries to querry the array lists - // 4: ArrayLists are empty or null, causing unexpected behavior - // 4.1: ArrayLists are empty, nothing major happens, but a player with an invalid name gets through. Not ideal, but - // also not the end of the world - // 4.2: ArrayLists are null, trying to use them causes a null pointer dereference, either crashing the plugin &/or - // server, or resulting in some other undefined behavior for sourcemod to deal with - - // If OnMapStart doesn't let players join, or rather OnConfigsExecuted fires before players can join, then there's no - // problem. Alternatively, I can write something to kick/retry players until OnConfigsExecuted fires - - // As of now, I will simply leave this as a note. No need to go making weird systems if they may not be necessary - - if(loadFromDatabase()) logAndFail(DATABASE_FAIL_MSG); - -} - public void OnClientPostAdminCheck(int client) { checkName(client); @@ -305,7 +288,12 @@ int loadFromDatabase() { if(db == null) logAndFail("Could not connect to sql database: %s", sqlerr); // Initialize table if it doesn't exist - + char sqlcbuf[256 + 1]; + Format(sqlcbuf, sizeof(sqlcbuf), "CREATE TABLE IF NOT EXISTS \"%s\" (id INTEGER NOT NULL, regexstr TEXT NOT NULL ON CONFLICT IGNORE, steamid64 TEXT NOT NULL ON CONFLICT IGNORE, dateof TEXT DEFAULT CURRENT_DATE, timeof TEXT DEFAULT CURRENT_TIME, PRIMARY KEY (id), UNIQUE (regexstr));", TABLENAME); + if(!SQL_FastQuery(db, sqlcbuf)) { + SQL_GetError(db, sqlerr, sizeof(sqlerr)); + logAndFail("Could not initialize nameblocker table: %s", sqlerr); + } // Initialize and populate datatypes regexlist = new ArrayList(ByteCountToCells(HANDLE_SIZE)); @@ -315,6 +303,8 @@ int loadFromDatabase() { if(patternlist == null) logAndFail("Could not initialize patternlist ArrayList"); // select patterns from nameblock table/database + + // compile each pattern // insert pattern & regex into respective lists diff --git a/orderchecker.sp b/orderchecker.sp deleted file mode 100644 index a80ac4d..0000000 --- a/orderchecker.sp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma newdecls required -#pragma semicolon 1 - -#include - -public Plugin myinfo = { - name = "orderchecker", - description = "logs a message in chat when a callback is fired", - author = "NW/RL", - version = "alpha-0.1", - url = "" -}; - -public void OnAllPluginsLoaded() { - PrintToChatAll("OnAllPluginsLoaded"); -} - -public void OnConfigsExecuted() { - PrintToChatAll("OnConfigsExecuted"); -} - -public void OnClientPostAdminCheck(int client) { - PrintToChatAll("OnClientPostAdminCheck on %N", client); -} - -public void OnClientSettingsChanged(int client) { - PrintToChatAll("OnClientSettingsChanged on %N", client); -} - -- cgit v1.2.3