From 06c88e4cd38334298c6c5e91731578f819aab6aa Mon Sep 17 00:00:00 2001 From: Rushaway Date: Sat, 12 Jul 2025 08:06:21 +0200 Subject: [PATCH 1/2] feat!(cookies): optimize cookie storage --- addons/sourcemod/scripting/ZLeader.sp | 82 +++++++++++++------ .../sourcemod/scripting/include/zleader.inc | 4 +- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/addons/sourcemod/scripting/ZLeader.sp b/addons/sourcemod/scripting/ZLeader.sp index dac2a78..45cee96 100644 --- a/addons/sourcemod/scripting/ZLeader.sp +++ b/addons/sourcemod/scripting/ZLeader.sp @@ -86,9 +86,7 @@ char g_sEntityTypes[ENTITIES_PER_MK][32] = { "prop_dynamic", "light_dynamic", "e float g_fPos[3], g_fMarkerTime; -Handle g_hShortcut = INVALID_HANDLE, - g_hPingSound = INVALID_HANDLE, - g_hMarkerPos = INVALID_HANDLE, +Handle g_hZLeaderSettings = INVALID_HANDLE, g_hSetClientLeaderForward = INVALID_HANDLE, g_hRemoveClientLeaderForward = INVALID_HANDLE; @@ -230,9 +228,7 @@ public void OnPluginStart() { /* COOKIES */ SetCookieMenuItem(ZLeaderCookieHandler, 0, "ZLeader Settings"); - g_hMarkerPos = RegClientCookie("zleader_makerpos", "ZLeader Marker Position", CookieAccess_Protected); - g_hShortcut = RegClientCookie("zleader_shortcut", "ZLeader ShortCut", CookieAccess_Protected); - g_hPingSound = RegClientCookie("zleader_pingsound", "ZLeader Ping Sound", CookieAccess_Protected); + g_hZLeaderSettings = RegClientCookie("zleader_settings", "ZLeader Settings", CookieAccess_Protected); /* ADD FILTERS */ AddMultiTargetFilter("@leaders", Filter_Leaders, "Possible Leaders", false); @@ -418,9 +414,7 @@ public void OnPluginEnd() { RemoveCommandListener(QuickLeaderMenuCommand, "+lookatweapon"); RemoveCommandListener(QuickMarkerMenuCommand, "-lookatweapon"); - CloseHandle(g_hShortcut); - CloseHandle(g_hPingSound); - CloseHandle(g_hMarkerPos); + CloseHandle(g_hZLeaderSettings); CloseHandle(g_hSetClientLeaderForward); CloseHandle(g_hRemoveClientLeaderForward); } @@ -520,29 +514,71 @@ public void OnClientCookiesCached(int client) { ReadClientCookies(client); } +void ParseClientCookie(int client, bool &shortcut, bool &pingSound, int &markerPos) { + char buffer[64]; + GetClientCookie(client, g_hZLeaderSettings, buffer, sizeof(buffer)); + + // Parse chain format: shortcut|pingsound|markerpos + if (buffer[0] != '\0') { + char parts[3][16]; + int count = ExplodeString(buffer, "|", parts, 3, sizeof(parts[])); + + if (count >= 1) { + shortcut = view_as(StringToInt(parts[0])); + } + if (count >= 2) { + pingSound = view_as(StringToInt(parts[1])); + } + if (count >= 3) { + markerPos = StringToInt(parts[2]); + } + } + else { + // Default values + shortcut = true; + pingSound = true; + markerPos = MK_TYPE_CROSSHAIR; + } +} + public void ReadClientCookies(int client) { - char buffer[32]; - GetClientCookie(client, g_hShortcut, buffer, 32); - g_bShorcut[client] = view_as(StringToInt(buffer)) || buffer[0] == '\0'; + bool shortcut, pingSound; + int markerPos; - GetClientCookie(client, g_hPingSound, buffer, 32); - g_bPingSound[client] = view_as(StringToInt(buffer)) || buffer[0] == '\0'; + ParseClientCookie(client, shortcut, pingSound, markerPos); - GetClientCookie(client, g_hMarkerPos, buffer, 32); - g_iMarkerPos[client] = (buffer[0] != '\0') ? StringToInt(buffer) : MK_TYPE_CROSSHAIR; + g_bShorcut[client] = shortcut; + g_bPingSound[client] = pingSound; + g_iMarkerPos[client] = markerPos; } public void SetClientCookies(int client) { if (!client || !IsClientInGame(client) || IsFakeClient(client)) return; - char sValue[8]; + // Read current cookie values + bool currentShortcut, currentPingSound; + int currentMarkerPos; + ParseClientCookie(client, currentShortcut, currentPingSound, currentMarkerPos); - FormatEx(sValue, sizeof(sValue), "%i", g_bShorcut[client]); - SetClientCookie(client, g_hShortcut, sValue); + // Check if values have changed + bool shortcutChanged = g_bShorcut[client] != currentShortcut; + bool pingSoundChanged = g_bPingSound[client] != currentPingSound; + bool markerPosChanged = g_iMarkerPos[client] != currentMarkerPos; - FormatEx(sValue, sizeof(sValue), "%i", g_iMarkerPos[client]); - SetClientCookie(client, g_hMarkerPos, sValue); + // If no values have changed, no need to save + if (!shortcutChanged && !pingSoundChanged && !markerPosChanged) { + return; + } + + // Otherwise, save the chain format + char sValue[64]; + FormatEx(sValue, sizeof(sValue), "%d|%d|%d", + g_bShorcut[client] ? 1 : 0, + g_bPingSound[client] ? 1 : 0, + g_iMarkerPos[client]); + + SetClientCookie(client, g_hZLeaderSettings, sValue); } public void ZLeaderCookieHandler(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) { @@ -594,9 +630,11 @@ public int ZLeaderSettingHandler(Menu menu, MenuAction action, int param1, int p g_bShorcut[param1] = !g_bShorcut[param1]; FormatEx(status, 64, "%T", g_bShorcut[param1] ? "Enabled Chat" : "Disabled Chat", param1); CPrintToChat(param1, "%T %T", "Prefix", param1, "You set shortcut", param1, status); + SetClientCookies(param1); } else if (strcmp(info, "markerpos", false) == 0) { g_iMarkerPos[param1] = (g_iMarkerPos[param1] == MK_TYPE_CLIENT) ? MK_TYPE_CROSSHAIR : MK_TYPE_CLIENT; CPrintToChat(param1, "%T %T", "Prefix", param1, (g_iMarkerPos[param1] == MK_TYPE_CLIENT) ? "Marker Pos Player Position" : "Marker Pos Crosshair", param1); + SetClientCookies(param1); } ZLeaderSetting(param1); @@ -622,8 +660,6 @@ public void OnClientDisconnect(int client) { FormatEx(g_sSteamIDs2[client], sizeof(g_sSteamIDs2[]), ""); FormatEx(g_sSteamIDs64[client], sizeof(g_sSteamIDs64[]), ""); - - SetClientCookies(client); } public void OnPlayerTeam(Event event, const char[] name, bool dontBroadcast) { diff --git a/addons/sourcemod/scripting/include/zleader.inc b/addons/sourcemod/scripting/include/zleader.inc index 21b515e..96549bd 100644 --- a/addons/sourcemod/scripting/include/zleader.inc +++ b/addons/sourcemod/scripting/include/zleader.inc @@ -4,8 +4,8 @@ #define _zleader_included #define ZLeader_V_MAJOR "3" -#define ZLeader_V_MINOR "6" -#define ZLeader_V_PATCH "6" +#define ZLeader_V_MINOR "7" +#define ZLeader_V_PATCH "0" #define ZLeader_VERSION ZLeader_V_MAJOR..."."...ZLeader_V_MINOR..."."...ZLeader_V_PATCH From 0051014ad697e112cf0e1a505fd0ea660da13e5e Mon Sep 17 00:00:00 2001 From: Rushaway Date: Sat, 12 Jul 2025 14:25:52 +0200 Subject: [PATCH 2/2] Improve late load --- addons/sourcemod/scripting/ZLeader.sp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/sourcemod/scripting/ZLeader.sp b/addons/sourcemod/scripting/ZLeader.sp index 45cee96..fb511d1 100644 --- a/addons/sourcemod/scripting/ZLeader.sp +++ b/addons/sourcemod/scripting/ZLeader.sp @@ -56,7 +56,8 @@ int g_Serial_Beacon = 0, g_iCooldownBeamPing[MAXPLAYERS + 1] = {0, ... }, g_iCurrentLeader[MAXLEADER] = {-1, -1, -1}; -bool g_bPlugin_ccc, +bool g_bLate, + g_bPlugin_ccc, g_bPlugin_vipcore, g_Plugin_BaseComm, g_bPlugin_SourceCommsPP, @@ -138,6 +139,7 @@ public Plugin myinfo = { }; public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) { + g_bLate = late; CreateNative("ZL_SetLeader", Native_SetLeader); CreateNative("ZL_IsClientLeader", Native_IsClientLeader); CreateNative("ZL_RemoveLeader", Native_RemoveLeader); @@ -236,12 +238,17 @@ public void OnPluginStart() { AddMultiTargetFilter("@leader", Filter_Leader, "Current Leader", false); AddMultiTargetFilter("@!leader", Filter_NotLeader, "Every one but the Current Leader", false); + if (!g_bLate) + return; + /* Late load */ char sSteam32ID[32]; for (int i = 1; i < MaxClients; i++) { if (IsClientInGame(i) && !IsFakeClient(i) && IsClientAuthorized(i) && GetClientAuthId(i, AuthId_Steam2, sSteam32ID, sizeof(sSteam32ID))) OnClientAuthorized(i, sSteam32ID); } + + g_bLate = false; } public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue) { @@ -3036,4 +3043,4 @@ void Reset_ClientFromLeaderSlots(int client) { g_iCurrentLeader[i] = -1; } } -} \ No newline at end of file +}