From 5baca6e6c78e0aa2bee5e79a5c0b138045e16112 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Fri, 1 Nov 2024 01:17:43 +0100 Subject: [PATCH 1/7] feat: add new NSGetModsInformation method --- primedev/scripts/client/scriptmodmenu.cpp | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 5ffe0fdf7..7e7b5d845 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -1,6 +1,62 @@ #include "mods/modmanager.h" #include "squirrel/squirrel.h" +ADD_SQFUNC("array", NSGetModsInformation, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +{ + g_pSquirrel->newarray(sqvm, 0); + + for (Mod& mod : g_pModManager->m_LoadedMods) + { + g_pSquirrel->pushnewstructinstance(sqvm, 9); + + // name + g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 0); + + // description + g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 1); + + // version + g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 2); + + // download link + g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 3); + + // load priority + g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); + g_pSquirrel->sealstructslot(sqvm, 4); + + // enabled + g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); + g_pSquirrel->sealstructslot(sqvm, 5); + + // required on client + g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); + g_pSquirrel->sealstructslot(sqvm, 6); + + // is remote + g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); + g_pSquirrel->sealstructslot(sqvm, 7); + + // convars + g_pSquirrel->newarray(sqvm); + for (ModConVar* cvar : mod.ConVars) + { + g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); + } + g_pSquirrel->sealstructslot(sqvm, 8); + + // add current object to squirrel array + g_pSquirrel->arrayappend(sqvm, -2); + } + + return SQRESULT_NOTNULL; +} + ADD_SQFUNC("array", NSGetModNames, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { g_pSquirrel->newarray(sqvm, 0); From 88ed5de17794a85234d6037341f4d9059691a4ba Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Fri, 1 Nov 2024 01:18:54 +0100 Subject: [PATCH 2/7] refactor: remove unused methods --- primedev/scripts/client/scriptmodmenu.cpp | 109 ---------------------- 1 file changed, 109 deletions(-) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 7e7b5d845..405f37a86 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -105,91 +105,6 @@ ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptCo return SQRESULT_NULL; } -ADD_SQFUNC("bool", NSIsModRemote, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - -ADD_SQFUNC("string", NSGetModDescriptionByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushstring(sqvm, mod.Description.c_str()); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - -ADD_SQFUNC("string", NSGetModVersionByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushstring(sqvm, mod.Version.c_str()); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - -ADD_SQFUNC("string", NSGetModDownloadLinkByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str()); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - -ADD_SQFUNC("int", NSGetModLoadPriority, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); @@ -207,30 +122,6 @@ ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext: return SQRESULT_NULL; } -ADD_SQFUNC( - "array", NSGetModConvarsByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - g_pSquirrel->newarray(sqvm, 0); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - for (ModConVar* cvar : mod.ConVars) - { - g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); - g_pSquirrel->arrayappend(sqvm, -2); - } - - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NOTNULL; // return empty array -} - ADD_SQFUNC("void", NSReloadMods, "", "", ScriptContext::UI) { NOTE_UNUSED(sqvm); From d389b7c7fa6517d18cabd0f349eac0b9ff203a5a Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Fri, 1 Nov 2024 01:58:24 +0100 Subject: [PATCH 3/7] feat: add NSGetModVersions method --- primedev/scripts/client/scriptmodmenu.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 405f37a86..d6bdb3ff4 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -122,6 +122,24 @@ ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext: return SQRESULT_NULL; } +ADD_SQFUNC("array", NSGetModVersions, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +{ + const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); + g_pSquirrel->newarray(sqvm, 0); + + // manual lookup, not super performant but eh not a big deal + for (Mod& mod : g_pModManager->m_LoadedMods) + { + if (!mod.Name.compare(modName)) + { + g_pSquirrel->pushstring(sqvm, mod.Version.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); + } + } + + return SQRESULT_NOTNULL; +} + ADD_SQFUNC("void", NSReloadMods, "", "", ScriptContext::UI) { NOTE_UNUSED(sqvm); From 348f8687d766a2f23fc8b5ef55c14423afae3ea1 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 20 Nov 2024 19:15:17 +0100 Subject: [PATCH 4/7] feat: add NSGetModInformation method --- primedev/scripts/client/scriptmodmenu.cpp | 79 ++++++++++++++++++----- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index d6bdb3ff4..8577e24e4 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -57,6 +57,68 @@ ADD_SQFUNC("array", NSGetModsInformation, "", "", ScriptContext::SERVER return SQRESULT_NOTNULL; } +ADD_SQFUNC("array", NSGetModInformation, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +{ + const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); + g_pSquirrel->newarray(sqvm, 0); + + for (Mod& mod : g_pModManager->m_LoadedMods) + { + if (mod.Name.compare(modName) != 0) + { + continue; + } + + g_pSquirrel->pushnewstructinstance(sqvm, 9); + + // name + g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 0); + + // description + g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 1); + + // version + g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 2); + + // download link + g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 3); + + // load priority + g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); + g_pSquirrel->sealstructslot(sqvm, 4); + + // enabled + g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); + g_pSquirrel->sealstructslot(sqvm, 5); + + // required on client + g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); + g_pSquirrel->sealstructslot(sqvm, 6); + + // is remote + g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); + g_pSquirrel->sealstructslot(sqvm, 7); + + // convars + g_pSquirrel->newarray(sqvm); + for (ModConVar* cvar : mod.ConVars) + { + g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); + } + g_pSquirrel->sealstructslot(sqvm, 8); + + // add current object to squirrel array + g_pSquirrel->arrayappend(sqvm, -2); + } + + return SQRESULT_NOTNULL; +} + ADD_SQFUNC("array", NSGetModNames, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { g_pSquirrel->newarray(sqvm, 0); @@ -105,23 +167,6 @@ ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptCo return SQRESULT_NULL; } -ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - ADD_SQFUNC("array", NSGetModVersions, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); From 15a43ecc752721b610dd8691c19459834ee96fbf Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 20 Nov 2024 19:15:45 +0100 Subject: [PATCH 5/7] refactor: remove NSGetModVersions method --- primedev/scripts/client/scriptmodmenu.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 8577e24e4..330a5fd17 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -167,24 +167,6 @@ ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptCo return SQRESULT_NULL; } -ADD_SQFUNC("array", NSGetModVersions, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - g_pSquirrel->newarray(sqvm, 0); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushstring(sqvm, mod.Version.c_str()); - g_pSquirrel->arrayappend(sqvm, -2); - } - } - - return SQRESULT_NOTNULL; -} - ADD_SQFUNC("void", NSReloadMods, "", "", ScriptContext::UI) { NOTE_UNUSED(sqvm); From 867e8f078d788fff05583aeb11bae9907102b8c7 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 20 Nov 2024 21:07:55 +0100 Subject: [PATCH 6/7] refactor: remove NSIsModEnabled method --- primedev/scripts/client/scriptmodmenu.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 330a5fd17..6d270cbfe 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -132,23 +132,6 @@ ADD_SQFUNC("array", NSGetModNames, "", "", ScriptContext::SERVER | Scrip return SQRESULT_NOTNULL; } -ADD_SQFUNC("bool", NSIsModEnabled, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); - return SQRESULT_NOTNULL; - } - } - - return SQRESULT_NULL; -} - ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); From 9bda30cff38564a38a09023f841bba25819e268d Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 20 Nov 2024 22:36:05 +0100 Subject: [PATCH 7/7] refactor: remove code duplication in ModToSquirrel conversion --- primedev/scripts/client/scriptmodmenu.cpp | 130 ++++++++-------------- 1 file changed, 45 insertions(+), 85 deletions(-) diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 6d270cbfe..7a2627c84 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -1,57 +1,62 @@ #include "mods/modmanager.h" #include "squirrel/squirrel.h" -ADD_SQFUNC("array", NSGetModsInformation, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +template void ModToSquirrel(HSQUIRRELVM sqvm, Mod& mod) { - g_pSquirrel->newarray(sqvm, 0); + g_pSquirrel->pushnewstructinstance(sqvm, 9); - for (Mod& mod : g_pModManager->m_LoadedMods) - { - g_pSquirrel->pushnewstructinstance(sqvm, 9); + // name + g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 0); - // name - g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 0); + // description + g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 1); - // description - g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 1); + // version + g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 2); - // version - g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 2); + // download link + g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 3); - // download link - g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 3); + // load priority + g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); + g_pSquirrel->sealstructslot(sqvm, 4); - // load priority - g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); - g_pSquirrel->sealstructslot(sqvm, 4); + // enabled + g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); + g_pSquirrel->sealstructslot(sqvm, 5); - // enabled - g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); - g_pSquirrel->sealstructslot(sqvm, 5); + // required on client + g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); + g_pSquirrel->sealstructslot(sqvm, 6); - // required on client - g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); - g_pSquirrel->sealstructslot(sqvm, 6); + // is remote + g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); + g_pSquirrel->sealstructslot(sqvm, 7); - // is remote - g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); - g_pSquirrel->sealstructslot(sqvm, 7); + // convars + g_pSquirrel->newarray(sqvm); + for (ModConVar* cvar : mod.ConVars) + { + g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); + } + g_pSquirrel->sealstructslot(sqvm, 8); - // convars - g_pSquirrel->newarray(sqvm); - for (ModConVar* cvar : mod.ConVars) - { - g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); - g_pSquirrel->arrayappend(sqvm, -2); - } - g_pSquirrel->sealstructslot(sqvm, 8); + // add current object to squirrel array + g_pSquirrel->arrayappend(sqvm, -2); +} - // add current object to squirrel array - g_pSquirrel->arrayappend(sqvm, -2); +ADD_SQFUNC("array", NSGetModsInformation, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +{ + g_pSquirrel->newarray(sqvm, 0); + + for (Mod& mod : g_pModManager->m_LoadedMods) + { + ModToSquirrel(sqvm, mod); } return SQRESULT_NOTNULL; @@ -68,52 +73,7 @@ ADD_SQFUNC("array", NSGetModInformation, "string modName", "", ScriptCo { continue; } - - g_pSquirrel->pushnewstructinstance(sqvm, 9); - - // name - g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 0); - - // description - g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 1); - - // version - g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 2); - - // download link - g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); - g_pSquirrel->sealstructslot(sqvm, 3); - - // load priority - g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); - g_pSquirrel->sealstructslot(sqvm, 4); - - // enabled - g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); - g_pSquirrel->sealstructslot(sqvm, 5); - - // required on client - g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); - g_pSquirrel->sealstructslot(sqvm, 6); - - // is remote - g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); - g_pSquirrel->sealstructslot(sqvm, 7); - - // convars - g_pSquirrel->newarray(sqvm); - for (ModConVar* cvar : mod.ConVars) - { - g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); - g_pSquirrel->arrayappend(sqvm, -2); - } - g_pSquirrel->sealstructslot(sqvm, 8); - - // add current object to squirrel array - g_pSquirrel->arrayappend(sqvm, -2); + ModToSquirrel(sqvm, mod); } return SQRESULT_NOTNULL;