Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Mod information #826

Merged
merged 11 commits into from
Nov 21, 2024
171 changes: 53 additions & 118 deletions primedev/scripts/client/scriptmodmenu.cpp
Original file line number Diff line number Diff line change
@@ -1,180 +1,115 @@
#include "mods/modmanager.h"
#include "squirrel/squirrel.h"

ADD_SQFUNC("array<string>", NSGetModNames, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
template <ScriptContext context> void ModToSquirrel(HSQUIRRELVM sqvm, Mod& mod)
{
g_pSquirrel<context>->newarray(sqvm, 0);
g_pSquirrel<context>->pushnewstructinstance(sqvm, 9);

for (Mod& mod : g_pModManager->m_LoadedMods)
{
g_pSquirrel<context>->pushstring(sqvm, mod.Name.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}
// name
g_pSquirrel<context>->pushstring(sqvm, mod.Name.c_str(), -1);
g_pSquirrel<context>->sealstructslot(sqvm, 0);

return SQRESULT_NOTNULL;
}
// description
g_pSquirrel<context>->pushstring(sqvm, mod.Description.c_str(), -1);
g_pSquirrel<context>->sealstructslot(sqvm, 1);

ADD_SQFUNC("bool", NSIsModEnabled, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
// version
g_pSquirrel<context>->pushstring(sqvm, mod.Version.c_str(), -1);
g_pSquirrel<context>->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<context>->pushbool(sqvm, mod.m_bEnabled);
return SQRESULT_NOTNULL;
}
}
// download link
g_pSquirrel<context>->pushstring(sqvm, mod.DownloadLink.c_str(), -1);
g_pSquirrel<context>->sealstructslot(sqvm, 3);

return SQRESULT_NULL;
}
// load priority
g_pSquirrel<context>->pushinteger(sqvm, mod.LoadPriority);
g_pSquirrel<context>->sealstructslot(sqvm, 4);

ADD_SQFUNC("void", NSSetModEnabled, "string modName, bool enabled", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
const SQBool enabled = g_pSquirrel<context>->getbool(sqvm, 2);
// enabled
g_pSquirrel<context>->pushbool(sqvm, mod.m_bEnabled);
g_pSquirrel<context>->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<context>->getstring(sqvm, 1);
// required on client
g_pSquirrel<context>->pushbool(sqvm, mod.RequiredOnClient);
g_pSquirrel<context>->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<context>->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<context>->getstring(sqvm, 1);
// is remote
g_pSquirrel<context>->pushbool(sqvm, mod.m_bIsRemote);
g_pSquirrel<context>->sealstructslot(sqvm, 7);

// manual lookup, not super performant but eh not a big deal
for (Mod& mod : g_pModManager->m_LoadedMods)
// convars
g_pSquirrel<context>->newarray(sqvm);
for (ModConVar* cvar : mod.ConVars)
{
if (!mod.Name.compare(modName))
{
g_pSquirrel<context>->pushstring(sqvm, mod.Description.c_str());
return SQRESULT_NOTNULL;
}
g_pSquirrel<context>->pushstring(sqvm, cvar->Name.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}
g_pSquirrel<context>->sealstructslot(sqvm, 8);

return SQRESULT_NULL;
// add current object to squirrel array
g_pSquirrel<context>->arrayappend(sqvm, -2);
}

ADD_SQFUNC("string", NSGetModVersionByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("array<ModInfo>", NSGetModsInformation, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
g_pSquirrel<context>->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<context>->pushstring(sqvm, mod.Version.c_str());
return SQRESULT_NOTNULL;
}
ModToSquirrel<context>(sqvm, mod);
}

return SQRESULT_NULL;
return SQRESULT_NOTNULL;
}

ADD_SQFUNC("string", NSGetModDownloadLinkByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("array<ModInfo>", NSGetModInformation, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
g_pSquirrel<context>->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<context>->pushstring(sqvm, mod.DownloadLink.c_str());
return SQRESULT_NOTNULL;
continue;
}
ModToSquirrel<context>(sqvm, mod);
}

return SQRESULT_NULL;
return SQRESULT_NOTNULL;
}

ADD_SQFUNC("int", NSGetModLoadPriority, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("array<string>", NSGetModNames, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
g_pSquirrel<context>->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<context>->pushinteger(sqvm, mod.LoadPriority);
return SQRESULT_NOTNULL;
}
g_pSquirrel<context>->pushstring(sqvm, mod.Name.c_str());
g_pSquirrel<context>->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<context>->getstring(sqvm, 1);
const SQBool enabled = g_pSquirrel<context>->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<context>->pushbool(sqvm, mod.RequiredOnClient);
return SQRESULT_NOTNULL;
mod.m_bEnabled = enabled;
return SQRESULT_NULL;
}
}

return SQRESULT_NULL;
}

ADD_SQFUNC(
"array<string>", NSGetModConvarsByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
g_pSquirrel<context>->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<context>->pushstring(sqvm, cvar->Name.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}

return SQRESULT_NOTNULL;
}
}

return SQRESULT_NOTNULL; // return empty array
}

ADD_SQFUNC("void", NSReloadMods, "", "", ScriptContext::UI)
{
NOTE_UNUSED(sqvm);
Expand Down