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
135 changes: 50 additions & 85 deletions primedev/scripts/client/scriptmodmenu.cpp
Original file line number Diff line number Diff line change
@@ -1,106 +1,76 @@
#include "mods/modmanager.h"
#include "squirrel/squirrel.h"

ADD_SQFUNC("array<string>", NSGetModNames, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("array<ModInfo>", NSGetModsInformation, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
g_pSquirrel<context>->newarray(sqvm, 0);

for (Mod& mod : g_pModManager->m_LoadedMods)
{
g_pSquirrel<context>->pushstring(sqvm, mod.Name.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}
g_pSquirrel<context>->pushnewstructinstance(sqvm, 9);

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

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

return SQRESULT_NULL;
}
// download link
g_pSquirrel<context>->pushstring(sqvm, mod.DownloadLink.c_str(), -1);
g_pSquirrel<context>->sealstructslot(sqvm, 3);

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);
// load priority
g_pSquirrel<context>->pushinteger(sqvm, mod.LoadPriority);
g_pSquirrel<context>->sealstructslot(sqvm, 4);

// 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;
}
}
// enabled
g_pSquirrel<context>->pushbool(sqvm, mod.m_bEnabled);
g_pSquirrel<context>->sealstructslot(sqvm, 5);

return SQRESULT_NULL;
}
// required on client
g_pSquirrel<context>->pushbool(sqvm, mod.RequiredOnClient);
g_pSquirrel<context>->sealstructslot(sqvm, 6);

ADD_SQFUNC("bool", NSIsModRemote, "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)
{
if (!mod.Name.compare(modName))
// convars
g_pSquirrel<context>->newarray(sqvm);
for (ModConVar* cvar : mod.ConVars)
{
g_pSquirrel<context>->pushbool(sqvm, mod.m_bIsRemote);
return SQRESULT_NOTNULL;
g_pSquirrel<context>->pushstring(sqvm, cvar->Name.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}
}

return SQRESULT_NULL;
}
g_pSquirrel<context>->sealstructslot(sqvm, 8);
Alystrasz marked this conversation as resolved.
Show resolved Hide resolved

ADD_SQFUNC("string", NSGetModDescriptionByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
const SQChar* modName = g_pSquirrel<context>->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<context>->pushstring(sqvm, mod.Description.c_str());
return SQRESULT_NOTNULL;
}
// add current object to squirrel array
g_pSquirrel<context>->arrayappend(sqvm, -2);
}

return SQRESULT_NULL;
return SQRESULT_NOTNULL;
}

ADD_SQFUNC("string", NSGetModVersionByModName, "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>->pushstring(sqvm, mod.Version.c_str());
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("string", NSGetModDownloadLinkByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("bool", NSIsModEnabled, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
Alystrasz marked this conversation as resolved.
Show resolved Hide resolved
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);

Expand All @@ -109,25 +79,26 @@ ADD_SQFUNC("string", NSGetModDownloadLinkByModName, "string modName", "", Script
{
if (!mod.Name.compare(modName))
{
g_pSquirrel<context>->pushstring(sqvm, mod.DownloadLink.c_str());
g_pSquirrel<context>->pushbool(sqvm, mod.m_bEnabled);
return SQRESULT_NOTNULL;
}
}

return SQRESULT_NULL;
}

ADD_SQFUNC("int", NSGetModLoadPriority, "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>->pushinteger(sqvm, mod.LoadPriority);
return SQRESULT_NOTNULL;
mod.m_bEnabled = enabled;
return SQRESULT_NULL;
}
}

Expand All @@ -151,8 +122,7 @@ ADD_SQFUNC("bool", NSIsModRequiredOnClient, "string modName", "", ScriptContext:
return SQRESULT_NULL;
}

ADD_SQFUNC(
"array<string>", NSGetModConvarsByModName, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
ADD_SQFUNC("array<string>", NSGetModVersions, "string modName", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
Alystrasz marked this conversation as resolved.
Show resolved Hide resolved
{
const SQChar* modName = g_pSquirrel<context>->getstring(sqvm, 1);
g_pSquirrel<context>->newarray(sqvm, 0);
Expand All @@ -162,17 +132,12 @@ ADD_SQFUNC(
{
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;
g_pSquirrel<context>->pushstring(sqvm, mod.Version.c_str());
g_pSquirrel<context>->arrayappend(sqvm, -2);
}
}

return SQRESULT_NOTNULL; // return empty array
return SQRESULT_NOTNULL;
}

ADD_SQFUNC("void", NSReloadMods, "", "", ScriptContext::UI)
Expand Down