diff --git a/primedev/scripts/client/scriptmodmenu.cpp b/primedev/scripts/client/scriptmodmenu.cpp index 5ffe0fdf7..7a2627c84 100644 --- a/primedev/scripts/client/scriptmodmenu.cpp +++ b/primedev/scripts/client/scriptmodmenu.cpp @@ -1,180 +1,115 @@ #include "mods/modmanager.h" #include "squirrel/squirrel.h" -ADD_SQFUNC("array", NSGetModNames, "", "", 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->pushstring(sqvm, mod.Name.c_str()); - g_pSquirrel->arrayappend(sqvm, -2); - } + // name + g_pSquirrel->pushstring(sqvm, mod.Name.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 0); - return SQRESULT_NOTNULL; -} + // description + g_pSquirrel->pushstring(sqvm, mod.Description.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 1); -ADD_SQFUNC("bool", NSIsModEnabled, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); + // version + g_pSquirrel->pushstring(sqvm, mod.Version.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 2); - // 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; - } - } + // download link + g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str(), -1); + g_pSquirrel->sealstructslot(sqvm, 3); - return SQRESULT_NULL; -} + // load priority + g_pSquirrel->pushinteger(sqvm, mod.LoadPriority); + g_pSquirrel->sealstructslot(sqvm, 4); -ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); - const SQBool enabled = g_pSquirrel->getbool(sqvm, 2); + // enabled + g_pSquirrel->pushbool(sqvm, mod.m_bEnabled); + g_pSquirrel->sealstructslot(sqvm, 5); - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) - { - if (!mod.Name.compare(modName)) - { - mod.m_bEnabled = enabled; - return SQRESULT_NULL; - } - } - - return SQRESULT_NULL; -} - -ADD_SQFUNC("bool", NSIsModRemote, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) -{ - const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); + // required on client + g_pSquirrel->pushbool(sqvm, mod.RequiredOnClient); + g_pSquirrel->sealstructslot(sqvm, 6); - // 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); + // is remote + g_pSquirrel->pushbool(sqvm, mod.m_bIsRemote); + g_pSquirrel->sealstructslot(sqvm, 7); - // manual lookup, not super performant but eh not a big deal - for (Mod& mod : g_pModManager->m_LoadedMods) + // convars + g_pSquirrel->newarray(sqvm); + for (ModConVar* cvar : mod.ConVars) { - if (!mod.Name.compare(modName)) - { - g_pSquirrel->pushstring(sqvm, mod.Description.c_str()); - return SQRESULT_NOTNULL; - } + g_pSquirrel->pushstring(sqvm, cvar->Name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); } + g_pSquirrel->sealstructslot(sqvm, 8); - return SQRESULT_NULL; + // add current object to squirrel array + g_pSquirrel->arrayappend(sqvm, -2); } -ADD_SQFUNC("string", NSGetModVersionByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +ADD_SQFUNC("array", NSGetModsInformation, "", "", 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()); - return SQRESULT_NOTNULL; - } + ModToSquirrel(sqvm, mod); } - return SQRESULT_NULL; + return SQRESULT_NOTNULL; } -ADD_SQFUNC("string", NSGetModDownloadLinkByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +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); - // manual lookup, not super performant but eh not a big deal for (Mod& mod : g_pModManager->m_LoadedMods) { - if (!mod.Name.compare(modName)) + if (mod.Name.compare(modName) != 0) { - g_pSquirrel->pushstring(sqvm, mod.DownloadLink.c_str()); - return SQRESULT_NOTNULL; + continue; } + ModToSquirrel(sqvm, mod); } - return SQRESULT_NULL; + return SQRESULT_NOTNULL; } -ADD_SQFUNC("int", NSGetModLoadPriority, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +ADD_SQFUNC("array", NSGetModNames, "", "", 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->pushinteger(sqvm, mod.LoadPriority); - return SQRESULT_NOTNULL; - } + g_pSquirrel->pushstring(sqvm, mod.Name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); } - return SQRESULT_NULL; + return SQRESULT_NOTNULL; } -ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) { const SQChar* modName = g_pSquirrel->getstring(sqvm, 1); + const SQBool enabled = g_pSquirrel->getbool(sqvm, 2); // 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; + mod.m_bEnabled = enabled; + return SQRESULT_NULL; } } 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);