Improve CSquirrelVM methods

* Made part of class, hooks are static.
* Added proper return and parameter types to script compiler methods.
This commit is contained in:
Kawe Mazidjatari 2023-05-04 00:24:10 +02:00
parent 199c4caede
commit f1da8d315d
2 changed files with 35 additions and 33 deletions

View File

@ -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()) 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 RSON::Node_t* rson = mod.LoadScriptCompileList(); // allocs parsed rson buffer
if (!rson) 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]; const char* scriptPathArray[1024];
int scriptCount = 0; int scriptCount = 0;
Script_SetCompilingVM(vm, rson); Script_SetCompilingVM(this, rson);
if (Script_ParseCompileListRSON( if (Script_ParseCompileListRSON(
vm->GetContext(), GetContext(),
mod.GetScriptCompileListPath().string().c_str(), mod.GetScriptCompileListPath().string().c_str(),
rson, rson,
(char**)scriptPathArray, &scriptCount, (char**)scriptPathArray, &scriptCount,
@ -361,12 +361,12 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm)
scriptPathArray[i] = pszScriptPath; scriptPathArray[i] = pszScriptPath;
} }
switch (vm->GetVM()->GetContext()) switch (GetVM()->GetContext())
{ {
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
case SQCONTEXT::SERVER: case SQCONTEXT::SERVER:
{ {
v_CSquirrelVM_CompileScriptsFromArray_SV(vm, vm->GetContext(), (char**)scriptPathArray, scriptCount); v_CSquirrelVM_CompileScriptsFromArray_SV(this, GetContext(), (char**)scriptPathArray, scriptCount);
break; break;
} }
#endif #endif
@ -374,7 +374,7 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm)
case SQCONTEXT::CLIENT: case SQCONTEXT::CLIENT:
case SQCONTEXT::UI: case SQCONTEXT::UI:
{ {
v_CSquirrelVM_CompileScriptsFromArray_UICL(vm, vm->GetContext(), (char**)scriptPathArray, scriptCount); v_CSquirrelVM_CompileScriptsFromArray_UICL(this, GetContext(), (char**)scriptPathArray, scriptCount);
break; break;
} }
#endif #endif
@ -393,27 +393,27 @@ void CSquirrelVM_CompileModScripts(CSquirrelVM* vm)
#ifndef DEDICATED #ifndef DEDICATED
__int64 CSquirrelVM_CompileUICLScripts(CSquirrelVM* vm) bool CSquirrelVM::CompileClientScripts(CSquirrelVM* vm)
{ {
HSQUIRRELVM v = vm->GetVM(); HSQUIRRELVM v = vm->GetVM();
DevMsg(v->GetNativePrintContext(), (char*)"Loading and compiling script lists\n"); 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 #endif
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
__int64 CSquirrelVM_CompileSVScripts(__int64 a1) bool CSquirrelVM::CompileServerScripts(int numPrecompiled)
{ {
HSQUIRRELVM v = g_pServerScript->GetVM(); HSQUIRRELVM v = g_pServerScript->GetVM();
DevMsg(v->GetNativePrintContext(), (char*)"Loading and compiling script lists\n"); 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 #endif
@ -427,10 +427,10 @@ void VSquirrelVM::Attach() const
DetourAttach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript); DetourAttach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript);
#ifndef DEDICATED #ifndef DEDICATED
DetourAttach((LPVOID*)&v_CSquirrelVM_CompileUICLScripts, &CSquirrelVM_CompileUICLScripts); DetourAttach((LPVOID*)&v_CSquirrelVM_CompileClientScripts, &CSquirrelVM::CompileClientScripts);
#endif #endif
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
DetourAttach((LPVOID*)&v_CSquirrelVM_CompileSVScripts, &CSquirrelVM_CompileSVScripts); DetourAttach((LPVOID*)&v_CSquirrelVM_CompileServerScripts, &CSquirrelVM::CompileServerScripts);
#endif #endif
} }
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
@ -444,9 +444,9 @@ void VSquirrelVM::Detach() const
DetourDetach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript); DetourDetach((LPVOID*)&v_Script_LoadScript, &Script_LoadScript);
#ifndef DEDICATED #ifndef DEDICATED
DetourDetach((LPVOID*)&v_CSquirrelVM_CompileUICLScripts, &CSquirrelVM_CompileUICLScripts); DetourDetach((LPVOID*)&v_CSquirrelVM_CompileClientScripts, &CSquirrelVM::CompileClientScripts);
#endif #endif
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
DetourDetach((LPVOID*)&v_CSquirrelVM_CompileSVScripts, &CSquirrelVM_CompileSVScripts); DetourDetach((LPVOID*)&v_CSquirrelVM_CompileServerScripts, &CSquirrelVM::CompileServerScripts);
#endif #endif
} }

View File

@ -49,15 +49,17 @@ static_assert(sizeof(ScriptFunctionBinding_t) == 0x68);
class CSquirrelVM class CSquirrelVM
{ {
public: public:
HSQUIRRELVM GetVM() const FORCEINLINE HSQUIRRELVM GetVM() const { return m_sqVM; }
{ FORCEINLINE SQCONTEXT GetContext() const { return m_iContext; }
return m_sqVM;
}
SQCONTEXT GetContext() const void CompileModScripts();
{
return m_iContext; #ifndef DEDICATED
} static bool CompileClientScripts(CSquirrelVM* vm);
#endif
#ifndef CLIENT_DLL
static bool CompileServerScripts(int numPrecompiled);
#endif
private: private:
SQChar pad0[0x8]; SQChar pad0[0x8];
@ -97,8 +99,8 @@ inline CMemory p_Script_LoadScript;
inline auto v_Script_LoadScript = p_Script_LoadScript.RCast<SQBool(*)(HSQUIRRELVM v, const SQChar* path, const SQChar* name, SQInteger flags)>(); inline auto v_Script_LoadScript = p_Script_LoadScript.RCast<SQBool(*)(HSQUIRRELVM v, const SQChar* path, const SQChar* name, SQInteger flags)>();
#ifndef DEDICATED #ifndef DEDICATED
inline CMemory p_CSquirrelVM_CompileUICLScripts; inline CMemory p_CSquirrelVM_CompileClientScripts;
inline auto v_CSquirrelVM_CompileUICLScripts = p_CSquirrelVM_CompileUICLScripts.RCast<__int64(__fastcall*)(CSquirrelVM* vm)>(); inline auto v_CSquirrelVM_CompileClientScripts = p_CSquirrelVM_CompileClientScripts.RCast<bool(__fastcall*)(CSquirrelVM* vm)>();
inline CMemory p_CSquirrelVM_CompileScriptsFromArray_UICL; inline CMemory p_CSquirrelVM_CompileScriptsFromArray_UICL;
inline auto v_CSquirrelVM_CompileScriptsFromArray_UICL = p_CSquirrelVM_CompileScriptsFromArray_UICL.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>(); inline auto v_CSquirrelVM_CompileScriptsFromArray_UICL = p_CSquirrelVM_CompileScriptsFromArray_UICL.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>();
@ -108,8 +110,8 @@ inline auto v_Script_SetCompilingVM_UICL = p_Script_SetCompilingVM_UICL.RCast<vo
#endif #endif
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
inline CMemory p_CSquirrelVM_CompileSVScripts; inline CMemory p_CSquirrelVM_CompileServerScripts;
inline auto v_CSquirrelVM_CompileSVScripts = p_CSquirrelVM_CompileSVScripts.RCast<__int64(__fastcall*)(__int64 a1)>(); inline auto v_CSquirrelVM_CompileServerScripts = p_CSquirrelVM_CompileServerScripts.RCast<bool(__fastcall*)(int numPrecompiled)>();
inline CMemory p_CSquirrelVM_CompileScriptsFromArray_SV; inline CMemory p_CSquirrelVM_CompileScriptsFromArray_SV;
inline auto v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>(); inline auto v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>();
@ -182,8 +184,8 @@ class VSquirrelVM : public IDetour
#ifndef DEDICATED #ifndef DEDICATED
// cl/ui scripts.rson compiling // cl/ui scripts.rson compiling
p_CSquirrelVM_CompileUICLScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 88 05 ? ? ? ? 33 C0").FollowNearCallSelf(); p_CSquirrelVM_CompileClientScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 88 05 ? ? ? ? 33 C0").FollowNearCallSelf();
v_CSquirrelVM_CompileUICLScripts = p_CSquirrelVM_CompileUICLScripts.RCast<__int64(__fastcall*)(CSquirrelVM * vm)>(); v_CSquirrelVM_CompileClientScripts = p_CSquirrelVM_CompileClientScripts.RCast<bool(__fastcall*)(CSquirrelVM*)>();
p_CSquirrelVM_CompileScriptsFromArray_UICL = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 44 0F B6 F0 48 85 DB").FollowNearCallSelf(); 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<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>(); v_CSquirrelVM_CompileScriptsFromArray_UICL = p_CSquirrelVM_CompileScriptsFromArray_UICL.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>();
@ -194,8 +196,8 @@ class VSquirrelVM : public IDetour
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
// sv scripts.rson compiling // sv scripts.rson compiling
p_CSquirrelVM_CompileSVScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 33 DB 88 05 ? ? ? ? ").FollowNearCallSelf(); p_CSquirrelVM_CompileServerScripts = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 33 DB 88 05 ? ? ? ? ").FollowNearCallSelf();
v_CSquirrelVM_CompileSVScripts = p_CSquirrelVM_CompileSVScripts.RCast<__int64(__fastcall*)(__int64 a1)>(); v_CSquirrelVM_CompileServerScripts = p_CSquirrelVM_CompileServerScripts.RCast<bool(__fastcall*)(int)>();
p_CSquirrelVM_CompileScriptsFromArray_SV = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 0F B6 F0 48 85 DB").FollowNearCallSelf(); p_CSquirrelVM_CompileScriptsFromArray_SV = g_GameDll.FindPatternSIMD("E8 ? ? ? ? 0F B6 F0 48 85 DB").FollowNearCallSelf();
v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>(); v_CSquirrelVM_CompileScriptsFromArray_SV = p_CSquirrelVM_CompileScriptsFromArray_SV.RCast<bool(__fastcall*)(CSquirrelVM* vm, SQCONTEXT context, char** scriptArray, int scriptCount)>();