From f1da8d315d7bfc04e5b60533b83b4e72caf1d9ce Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 4 May 2023 00:24:10 +0200 Subject: [PATCH] Improve CSquirrelVM methods * Made part of class, hooks are static. * Added proper return and parameter types to script compiler methods. --- r5dev/squirrel/sqscript.cpp | 34 +++++++++++++++++----------------- r5dev/squirrel/sqscript.h | 34 ++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/r5dev/squirrel/sqscript.cpp b/r5dev/squirrel/sqscript.cpp index 505071a3..d3f607a8 100644 --- a/r5dev/squirrel/sqscript.cpp +++ b/r5dev/squirrel/sqscript.cpp @@ -318,7 +318,7 @@ void Script_SetCompilingVM(CSquirrelVM* vm, RSON::Node_t* rson) } } -void CSquirrelVM_CompileModScripts(CSquirrelVM* vm) +void CSquirrelVM::CompileModScripts() { for (auto& mod : g_pModSystem->GetModList()) { @@ -331,15 +331,15 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm) RSON::Node_t* rson = mod.LoadScriptCompileList(); // allocs parsed rson buffer if (!rson) - Error(vm->GetVM()->GetNativePrintContext(), EXIT_FAILURE, "%s: Failed to load RSON file %s\n", __FUNCTION__, mod.GetScriptCompileListPath().string().c_str()); + Error(GetVM()->GetNativePrintContext(), EXIT_FAILURE, "%s: Failed to load RSON file %s\n", __FUNCTION__, mod.GetScriptCompileListPath().string().c_str()); const char* scriptPathArray[1024]; int scriptCount = 0; - Script_SetCompilingVM(vm, rson); + Script_SetCompilingVM(this, rson); if (Script_ParseCompileListRSON( - vm->GetContext(), + GetContext(), mod.GetScriptCompileListPath().string().c_str(), rson, (char**)scriptPathArray, &scriptCount, @@ -361,12 +361,12 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm) scriptPathArray[i] = pszScriptPath; } - switch (vm->GetVM()->GetContext()) + switch (GetVM()->GetContext()) { #ifndef CLIENT_DLL case SQCONTEXT::SERVER: { - v_CSquirrelVM_CompileScriptsFromArray_SV(vm, vm->GetContext(), (char**)scriptPathArray, scriptCount); + v_CSquirrelVM_CompileScriptsFromArray_SV(this, GetContext(), (char**)scriptPathArray, scriptCount); break; } #endif @@ -374,7 +374,7 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm) case SQCONTEXT::CLIENT: case SQCONTEXT::UI: { - v_CSquirrelVM_CompileScriptsFromArray_UICL(vm, vm->GetContext(), (char**)scriptPathArray, scriptCount); + v_CSquirrelVM_CompileScriptsFromArray_UICL(this, GetContext(), (char**)scriptPathArray, scriptCount); break; } #endif @@ -393,27 +393,27 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm) #ifndef DEDICATED -__int64 CSquirrelVM_CompileUICLScripts(CSquirrelVM* vm) +bool CSquirrelVM::CompileClientScripts(CSquirrelVM* vm) { HSQUIRRELVM v = vm->GetVM(); DevMsg(v->GetNativePrintContext(), (char*)"Loading and compiling script lists\n"); - CSquirrelVM_CompileModScripts(vm); + vm->CompileModScripts(); - return v_CSquirrelVM_CompileUICLScripts(vm); + return v_CSquirrelVM_CompileClientScripts(vm); } #endif #ifndef CLIENT_DLL -__int64 CSquirrelVM_CompileSVScripts(__int64 a1) +bool CSquirrelVM::CompileServerScripts(int numPrecompiled) { HSQUIRRELVM v = g_pServerScript->GetVM(); DevMsg(v->GetNativePrintContext(), (char*)"Loading and compiling script lists\n"); - CSquirrelVM_CompileModScripts(g_pServerScript); + g_pServerScript->CompileModScripts(); - return v_CSquirrelVM_CompileSVScripts(a1); + return v_CSquirrelVM_CompileServerScripts(numPrecompiled); } #endif @@ -427,10 +427,10 @@ void VSquirrelVM::Attach() const DetourAttach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript); #ifndef DEDICATED - DetourAttach((LPVOID*)&v_CSquirrelVM_CompileUICLScripts, &CSquirrelVM_CompileUICLScripts); + DetourAttach((LPVOID*)&v_CSquirrelVM_CompileClientScripts, &CSquirrelVM::CompileClientScripts); #endif #ifndef CLIENT_DLL - DetourAttach((LPVOID*)&v_CSquirrelVM_CompileSVScripts, &CSquirrelVM_CompileSVScripts); + DetourAttach((LPVOID*)&v_CSquirrelVM_CompileServerScripts, &CSquirrelVM::CompileServerScripts); #endif } //--------------------------------------------------------------------------------- @@ -444,9 +444,9 @@ void VSquirrelVM::Detach() const DetourDetach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript); #ifndef DEDICATED - DetourDetach((LPVOID*)&v_CSquirrelVM_CompileUICLScripts, &CSquirrelVM_CompileUICLScripts); + DetourDetach((LPVOID*)&v_CSquirrelVM_CompileClientScripts, &CSquirrelVM::CompileClientScripts); #endif #ifndef CLIENT_DLL - DetourDetach((LPVOID*)&v_CSquirrelVM_CompileSVScripts, &CSquirrelVM_CompileSVScripts); + DetourDetach((LPVOID*)&v_CSquirrelVM_CompileServerScripts, &CSquirrelVM::CompileServerScripts); #endif } diff --git a/r5dev/squirrel/sqscript.h b/r5dev/squirrel/sqscript.h index 08ccec6c..45ccc4bf 100644 --- a/r5dev/squirrel/sqscript.h +++ b/r5dev/squirrel/sqscript.h @@ -49,15 +49,17 @@ static_assert(sizeof(ScriptFunctionBinding_t) == 0x68); class CSquirrelVM { public: - HSQUIRRELVM GetVM() const - { - return m_sqVM; - } + FORCEINLINE HSQUIRRELVM GetVM() const { return m_sqVM; } + FORCEINLINE SQCONTEXT GetContext() const { return m_iContext; } - SQCONTEXT GetContext() const - { - return m_iContext; - } + void CompileModScripts(); + +#ifndef DEDICATED + static bool CompileClientScripts(CSquirrelVM* vm); +#endif +#ifndef CLIENT_DLL + static bool CompileServerScripts(int numPrecompiled); +#endif private: SQChar pad0[0x8]; @@ -97,8 +99,8 @@ inline CMemory p_Script_LoadScript; inline auto v_Script_LoadScript = p_Script_LoadScript.RCast(); #ifndef DEDICATED -inline CMemory p_CSquirrelVM_CompileUICLScripts; -inline auto v_CSquirrelVM_CompileUICLScripts = p_CSquirrelVM_CompileUICLScripts.RCast<__int64(__fastcall*)(CSquirrelVM* vm)>(); +inline CMemory p_CSquirrelVM_CompileClientScripts; +inline auto v_CSquirrelVM_CompileClientScripts = p_CSquirrelVM_CompileClientScripts.RCast(); inline CMemory p_CSquirrelVM_CompileScriptsFromArray_UICL; inline auto v_CSquirrelVM_CompileScriptsFromArray_UICL = p_CSquirrelVM_CompileScriptsFromArray_UICL.RCast(); @@ -108,8 +110,8 @@ inline auto v_Script_SetCompilingVM_UICL = p_Script_SetCompilingVM_UICL.RCast(); +inline CMemory p_CSquirrelVM_CompileServerScripts; +inline auto v_CSquirrelVM_CompileServerScripts = p_CSquirrelVM_CompileServerScripts.RCast(); inline CMemory p_CSquirrelVM_CompileScriptsFromArray_SV; inline auto v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast(); @@ -182,8 +184,8 @@ class VSquirrelVM : public IDetour #ifndef DEDICATED // cl/ui scripts.rson compiling - p_CSquirrelVM_CompileUICLScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 88 05 ? ? ? ? 33 C0").FollowNearCallSelf(); - v_CSquirrelVM_CompileUICLScripts = p_CSquirrelVM_CompileUICLScripts.RCast<__int64(__fastcall*)(CSquirrelVM * vm)>(); + p_CSquirrelVM_CompileClientScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 88 05 ? ? ? ? 33 C0").FollowNearCallSelf(); + v_CSquirrelVM_CompileClientScripts = p_CSquirrelVM_CompileClientScripts.RCast(); p_CSquirrelVM_CompileScriptsFromArray_UICL = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 44 0F B6 F0 48 85 DB").FollowNearCallSelf(); v_CSquirrelVM_CompileScriptsFromArray_UICL = p_CSquirrelVM_CompileScriptsFromArray_UICL.RCast(); @@ -194,8 +196,8 @@ class VSquirrelVM : public IDetour #ifndef CLIENT_DLL // sv scripts.rson compiling - p_CSquirrelVM_CompileSVScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 33 DB 88 05 ? ? ? ? ").FollowNearCallSelf(); - v_CSquirrelVM_CompileSVScripts = p_CSquirrelVM_CompileSVScripts.RCast<__int64(__fastcall*)(__int64 a1)>(); + p_CSquirrelVM_CompileServerScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 33 DB 88 05 ? ? ? ? ").FollowNearCallSelf(); + v_CSquirrelVM_CompileServerScripts = p_CSquirrelVM_CompileServerScripts.RCast(); p_CSquirrelVM_CompileScriptsFromArray_SV = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 0F B6 F0 48 85 DB").FollowNearCallSelf(); v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast();