Skip to content

Commit

Permalink
vscript: Move squirrel types to their respective files (R2Northstar#788)
Browse files Browse the repository at this point in the history
Refactor logic to move Squirrel types to their own respective files and extend existing layouts in the process where applicable.
Contains additional smaller fixes.
  • Loading branch information
F1F7Y authored and wolf109909 committed Sep 8, 2024
1 parent 16f9559 commit c3371d7
Show file tree
Hide file tree
Showing 28 changed files with 728 additions and 736 deletions.
18 changes: 17 additions & 1 deletion primedev/Northstar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ add_library(
"squirrel/squirrelautobind.cpp"
"squirrel/squirrelautobind.h"
"squirrel/squirrelclasstypes.h"
"squirrel/squirreldatatypes.h"
"util/concurrentqueue.h"
"util/printcommands.cpp"
"util/printcommands.h"
Expand All @@ -201,6 +200,23 @@ add_library(
"util/version.h"
"util/wininfo.cpp"
"util/wininfo.h"
"vscript/languages/squirrel_re/include/squirrel.h"
"vscript/languages/squirrel_re/squirrel/sqarray.h"
"vscript/languages/squirrel_re/squirrel/sqclosure.h"
"vscript/languages/squirrel_re/squirrel/sqcompiler.h"
"vscript/languages/squirrel_re/squirrel/sqfunctionproto.h"
"vscript/languages/squirrel_re/squirrel/sqlexer.h"
"vscript/languages/squirrel_re/squirrel/sqobject.h"
"vscript/languages/squirrel_re/squirrel/sqopcodes.h"
"vscript/languages/squirrel_re/squirrel/sqstate.h"
"vscript/languages/squirrel_re/squirrel/sqstring.h"
"vscript/languages/squirrel_re/squirrel/sqstruct.h"
"vscript/languages/squirrel_re/squirrel/sqtable.h"
"vscript/languages/squirrel_re/squirrel/squserdata.h"
"vscript/languages/squirrel_re/squirrel/sqvector.h"
"vscript/languages/squirrel_re/squirrel/sqvm.h"
"vscript/languages/squirrel_re/vsquirrel.h"
"vscript/vscript.h"
"windows/libsys.cpp"
"windows/libsys.h"
"util/base64.cpp"
Expand Down
2 changes: 1 addition & 1 deletion primedev/mods/modsavefiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ ADD_SQFUNC("int", NSGetTotalSpaceRemaining, "", "", ScriptContext::CLIENT | Scri
// ok, I'm just gonna explain what the fuck is going on here because this
// is the pinnacle of my stupidity and I do not want to touch this ever
// again, yet someone will eventually have to maintain this.
template <ScriptContext context> std::string EncodeJSON(HSquirrelVM* sqvm)
template <ScriptContext context> std::string EncodeJSON(HSQUIRRELVM sqvm)
{
// new rapidjson
rapidjson_document doc;
Expand Down
6 changes: 3 additions & 3 deletions primedev/scripts/scriptdatatables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Datatable

ConVar* Cvar_ns_prefer_datatable_from_disk;

template <ScriptContext context> Datatable* (*SQ_GetDatatableInternal)(HSquirrelVM* sqvm);
template <ScriptContext context> Datatable* (*SQ_GetDatatableInternal)(HSQUIRRELVM sqvm);

struct CSVData
{
Expand Down Expand Up @@ -853,12 +853,12 @@ void ConCommand_dump_datatables(const CCommand& args)

ON_DLL_LOAD_RELIESON("server.dll", ServerScriptDatatables, ServerSquirrel, (CModule module))
{
SQ_GetDatatableInternal<ScriptContext::SERVER> = module.Offset(0x1250f0).RCast<Datatable* (*)(HSquirrelVM*)>();
SQ_GetDatatableInternal<ScriptContext::SERVER> = module.Offset(0x1250f0).RCast<Datatable* (*)(HSQUIRRELVM)>();
}

ON_DLL_LOAD_RELIESON("client.dll", ClientScriptDatatables, ClientSquirrel, (CModule module))
{
SQ_GetDatatableInternal<ScriptContext::CLIENT> = module.Offset(0x1C9070).RCast<Datatable* (*)(HSquirrelVM*)>();
SQ_GetDatatableInternal<ScriptContext::CLIENT> = module.Offset(0x1C9070).RCast<Datatable* (*)(HSQUIRRELVM)>();
SQ_GetDatatableInternal<ScriptContext::UI> = SQ_GetDatatableInternal<ScriptContext::CLIENT>;
}

Expand Down
10 changes: 5 additions & 5 deletions primedev/scripts/scripthttprequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ template <ScriptContext context> int HttpRequestHandler::MakeHttpRequest(const H

// int NS_InternalMakeHttpRequest(int method, string baseUrl, table<string, string> headers, table<string, string> queryParams,
// string contentType, string body, int timeout, string userAgent)
template <ScriptContext context> SQRESULT SQ_InternalMakeHttpRequest(HSquirrelVM* sqvm)
template <ScriptContext context> SQRESULT SQ_InternalMakeHttpRequest(HSQUIRRELVM sqvm)
{
if (!g_httpRequestHandler || !g_httpRequestHandler->IsRunning())
{
Expand All @@ -477,7 +477,7 @@ template <ScriptContext context> SQRESULT SQ_InternalMakeHttpRequest(HSquirrelVM
SQTable* headerTable = sqvm->_stackOfCurrentFunction[3]._VAL.asTable;
for (int idx = 0; idx < headerTable->_numOfNodes; ++idx)
{
tableNode* node = &headerTable->_nodes[idx];
SQTable::_HashNode* node = &headerTable->_nodes[idx];

if (node->key._Type == OT_STRING && node->val._Type == OT_ARRAY)
{
Expand All @@ -499,7 +499,7 @@ template <ScriptContext context> SQRESULT SQ_InternalMakeHttpRequest(HSquirrelVM
SQTable* queryTable = sqvm->_stackOfCurrentFunction[4]._VAL.asTable;
for (int idx = 0; idx < queryTable->_numOfNodes; ++idx)
{
tableNode* node = &queryTable->_nodes[idx];
SQTable::_HashNode* node = &queryTable->_nodes[idx];

if (node->key._Type == OT_STRING && node->val._Type == OT_ARRAY)
{
Expand Down Expand Up @@ -529,14 +529,14 @@ template <ScriptContext context> SQRESULT SQ_InternalMakeHttpRequest(HSquirrelVM
}

// bool NSIsHttpEnabled()
template <ScriptContext context> SQRESULT SQ_IsHttpEnabled(HSquirrelVM* sqvm)
template <ScriptContext context> SQRESULT SQ_IsHttpEnabled(HSQUIRRELVM sqvm)
{
g_pSquirrel<context>->pushbool(sqvm, !IsHttpDisabled());
return SQRESULT_NOTNULL;
}

// bool NSIsLocalHttpAllowed()
template <ScriptContext context> SQRESULT SQ_IsLocalHttpAllowed(HSquirrelVM* sqvm)
template <ScriptContext context> SQRESULT SQ_IsLocalHttpAllowed(HSQUIRRELVM sqvm)
{
g_pSquirrel<context>->pushbool(sqvm, IsLocalHttpAllowed());
return SQRESULT_NOTNULL;
Expand Down
12 changes: 6 additions & 6 deletions primedev/scripts/scriptjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#undef GetObject // fuck microsoft developers
#endif

template <ScriptContext context> void
DecodeJsonArray(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* arr)
template <ScriptContext context>
void DecodeJsonArray(HSQUIRRELVM sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* arr)
{
g_pSquirrel<context>->newarray(sqvm, 0);

Expand Down Expand Up @@ -46,8 +46,8 @@ DecodeJsonArray(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>
}
}

template <ScriptContext context> void
DecodeJsonTable(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj)
template <ScriptContext context>
void DecodeJsonTable(HSQUIRRELVM sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj)
{
g_pSquirrel<context>->newtable(sqvm);

Expand Down Expand Up @@ -103,7 +103,7 @@ template <ScriptContext context> void EncodeJSONTable(
{
for (int i = 0; i < table->_numOfNodes; i++)
{
tableNode* node = &table->_nodes[i];
SQTable::_HashNode* node = &table->_nodes[i];
if (node->key._Type == OT_STRING)
{
rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>> newObj(rapidjson::kObjectType);
Expand Down Expand Up @@ -236,7 +236,7 @@ ADD_SQFUNC(
doc.SetObject();

// temp until this is just the func parameter type
HSquirrelVM* vm = (HSquirrelVM*)sqvm;
HSQUIRRELVM vm = (HSQUIRRELVM)sqvm;
SQTable* table = vm->_stackOfCurrentFunction[1]._VAL.asTable;
EncodeJSONTable<context>(table, &doc, doc.GetAllocator());

Expand Down
2 changes: 1 addition & 1 deletion primedev/scripts/scriptjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ template <ScriptContext context> void EncodeJSONTable(
rapidjson::MemoryPoolAllocator<SourceAllocator>& allocator);

template <ScriptContext context> void
DecodeJsonTable(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj);
DecodeJsonTable(HSQUIRRELVM sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj);
22 changes: 12 additions & 10 deletions primedev/squirrel/squirrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "ns_version.h"
#include "core/vanilla.h"

#include "vscript/vscript.h"

#include <any>

AUTOHOOK_INIT()
Expand Down Expand Up @@ -245,8 +247,8 @@ template <ScriptContext context> void SquirrelManager<context>::ExecuteCode(cons

spdlog::info("Executing {} script code {} ", GetContextName(context), pCode);

std::string strCode(pCode);
CompileBufferState bufferState = CompileBufferState(strCode);
// NOTE: SQBufferState doesn't strdup pCode!
SQBufferState bufferState = SQBufferState(pCode);

SQRESULT compileResult = compilebuffer(&bufferState, "console");
spdlog::info("sq_compilebuffer returned {}", PrintSQRESULT.at(compileResult));
Expand Down Expand Up @@ -301,14 +303,14 @@ template <ScriptContext context> void SquirrelManager<context>::AddFuncOverride(
}

// hooks
bool IsUIVM(ScriptContext context, HSquirrelVM* pSqvm)
bool IsUIVM(ScriptContext context, HSQUIRRELVM pSqvm)
{
NOTE_UNUSED(context);
return ScriptContext(pSqvm->sharedState->cSquirrelVM->vmContext) == ScriptContext::UI;
}

template <ScriptContext context> void* (*sq_compiler_create)(HSquirrelVM* sqvm, void* a2, void* a3, SQBool bShouldThrowError);
template <ScriptContext context> void* __fastcall sq_compiler_createHook(HSquirrelVM* sqvm, void* a2, void* a3, SQBool bShouldThrowError)
template <ScriptContext context> void* (*sq_compiler_create)(HSQUIRRELVM sqvm, void* a2, void* a3, SQBool bShouldThrowError);
template <ScriptContext context> void* __fastcall sq_compiler_createHook(HSQUIRRELVM sqvm, void* a2, void* a3, SQBool bShouldThrowError)
{
// store whether errors generated from this compile should be fatal
if (IsUIVM(context, sqvm))
Expand All @@ -319,8 +321,8 @@ template <ScriptContext context> void* __fastcall sq_compiler_createHook(HSquirr
return sq_compiler_create<context>(sqvm, a2, a3, bShouldThrowError);
}

template <ScriptContext context> SQInteger (*SQPrint)(HSquirrelVM* sqvm, const char* fmt);
template <ScriptContext context> SQInteger SQPrintHook(HSquirrelVM* sqvm, const char* fmt, ...)
template <ScriptContext context> SQInteger (*SQPrint)(HSQUIRRELVM sqvm, const char* fmt);
template <ScriptContext context> SQInteger SQPrintHook(HSQUIRRELVM sqvm, const char* fmt, ...)
{
NOTE_UNUSED(sqvm);

Expand Down Expand Up @@ -390,9 +392,9 @@ template <ScriptContext context> void __fastcall DestroyVMHook(void* a1, CSquirr
spdlog::info("DestroyVM {} {}", GetContextName(realContext), (void*)sqvm);
}

template <ScriptContext context> void (*SQCompileError)(HSquirrelVM* sqvm, const char* error, const char* file, int line, int column);
template <ScriptContext context> void (*SQCompileError)(HSQUIRRELVM sqvm, const char* error, const char* file, int line, int column);
template <ScriptContext context>
void __fastcall ScriptCompileErrorHook(HSquirrelVM* sqvm, const char* error, const char* file, int line, int column)
void __fastcall ScriptCompileErrorHook(HSQUIRRELVM sqvm, const char* error, const char* file, int line, int column)
{
bool bIsFatalError = g_pSquirrel<context>->m_bFatalCompilationErrors;
ScriptContext realContext = context; // ui and client use the same function so we use this for prints
Expand Down Expand Up @@ -536,7 +538,7 @@ template <ScriptContext context> void ConCommand_script(const CCommand& args)
g_pSquirrel<context>->ExecuteCode(args.ArgS());
}

template <ScriptContext context> SQRESULT SQ_StubbedFunc(HSquirrelVM* sqvm)
template <ScriptContext context> SQRESULT SQ_StubbedFunc(HSQUIRRELVM sqvm)
{
SQStackInfos si;
g_pSquirrel<context>->sq_stackinfos(sqvm, 0, si);
Expand Down
Loading

0 comments on commit c3371d7

Please sign in to comment.