From 8bf6c196cd23337e5b4fad43a5b56b206a7eb9d8 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 21 May 2022 18:15:48 +0200 Subject: [PATCH] Improved SQVM structure * Added more fields to the existing structure. * Removed redundant C string casts. Note: The global context objects appear to be a different class containing a pointer (0x8 in) to the VM objects. --- r5dev/squirrel/sqvm.cpp | 37 ++++++++++++++++++------------------ r5dev/squirrel/sqvm.h | 42 +++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/r5dev/squirrel/sqvm.cpp b/r5dev/squirrel/sqvm.cpp index af4dcce8..95dc0475 100644 --- a/r5dev/squirrel/sqvm.cpp +++ b/r5dev/squirrel/sqvm.cpp @@ -73,7 +73,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...) va_end(args); }///////////////////////////// - std::string vmStr = SQVM_LOG_T[static_cast(context)].c_str(); + std::string vmStr = SQVM_LOG_T[static_cast(context)]; vmStr.append(buf); if (sq_showvmoutput->GetInt() > 0) { @@ -87,7 +87,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...) { wconsole->debug(vmStr); #ifdef DEDICATED - g_pRConServer->Send(vmStr.c_str()); + g_pRConServer->Send(vmStr); #endif // DEDICATED } else @@ -99,10 +99,10 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...) if (strstr(buf, "SCRIPT ERROR:") || strstr(buf, " -> ")) { bError = true; - vmStrAnsi = SQVM_ERROR_ANSI_LOG_T[static_cast(context)].c_str(); + vmStrAnsi = SQVM_ERROR_ANSI_LOG_T[static_cast(context)]; } else { - vmStrAnsi = SQVM_WARNING_ANSI_LOG_T[static_cast(context)].c_str(); + vmStrAnsi = SQVM_WARNING_ANSI_LOG_T[static_cast(context)]; } } else if (g_bSQAuxBadLogic) @@ -112,19 +112,19 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...) bError = true; bColorOverride = true; g_bSQAuxBadLogic = false; - vmStrAnsi = SQVM_ERROR_ANSI_LOG_T[static_cast(context)].c_str(); + vmStrAnsi = SQVM_ERROR_ANSI_LOG_T[static_cast(context)]; } else { - vmStrAnsi = SQVM_ANSI_LOG_T[static_cast(context)].c_str(); + vmStrAnsi = SQVM_ANSI_LOG_T[static_cast(context)]; } } else { - vmStrAnsi = SQVM_ANSI_LOG_T[static_cast(context)].c_str(); + vmStrAnsi = SQVM_ANSI_LOG_T[static_cast(context)]; } vmStrAnsi.append(buf); wconsole->debug(vmStrAnsi); #ifdef DEDICATED - g_pRConServer->Send(vmStrAnsi.c_str()); + g_pRConServer->Send(vmStrAnsi); #endif // DEDICATED } @@ -206,7 +206,7 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger* static std::shared_ptr wconsole = spdlog::get("win_console"); static std::shared_ptr sqlogger = spdlog::get("sqvm_warn"); - std::string vmStr = SQVM_LOG_T[static_cast(context)].c_str(); + std::string vmStr = SQVM_LOG_T[static_cast(context)]; std::string svConstructor(*ppString, *nStringSize); // Get string from memory via std::string constructor. vmStr.append(svConstructor); @@ -222,7 +222,7 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger* } else { - std::string vmStrAnsi = SQVM_WARNING_ANSI_LOG_T[static_cast(context)].c_str(); + std::string vmStrAnsi = SQVM_WARNING_ANSI_LOG_T[static_cast(context)]; vmStrAnsi.append(svConstructor); wconsole->debug(vmStrAnsi); #ifdef DEDICATED @@ -498,19 +498,19 @@ const SQCONTEXT SQVM_GetContextIndex(HSQUIRRELVM v) // Input : context - // Output : SQVM* //--------------------------------------------------------------------------------- -HSQUIRRELVM SQVM_GetVM(SQCONTEXT context) +CSquirrelVM* SQVM_GetContextObject(SQCONTEXT context) { switch (context) { #ifndef CLIENT_DLL case SQCONTEXT::SERVER: - return g_pServerVM.GetValue(); + return g_pServerVM.GetValue(); #endif // !CLIENT_DLL #ifndef DEDICATED case SQCONTEXT::CLIENT: - return g_pClientVM.GetValue(); + return g_pClientVM.GetValue(); case SQCONTEXT::UI: - return g_pUIVM.GetValue(); + return g_pUIVM.GetValue(); #endif // !DEDICATED default: return nullptr; @@ -524,22 +524,21 @@ HSQUIRRELVM SQVM_GetVM(SQCONTEXT context) //--------------------------------------------------------------------------------- void SQVM_Execute(const SQChar* code, SQCONTEXT context) { - HSQUIRRELVM v = SQVM_GetVM(context); + HSQUIRRELVM v = SQVM_GetContextObject(context)->GetVM(); if (!v) { Error(eDLL_T::ENGINE, "Attempted to run %s script while VM isn't initialized\n", SQVM_GetContextName(context)); return; } - SQVM* vTable = v->GetVTable(); SQRESULT compileResult{}; SQBufState bufState = SQBufState(code); - compileResult = sq_compilebuffer(vTable, &bufState, "console", -1); + compileResult = sq_compilebuffer(v, &bufState, "console", -1); if (compileResult >= 0) { - sq_pushroottable(vTable); - SQRESULT callResult = sq_call(vTable, 1, false, false); + sq_pushroottable(v); + SQRESULT callResult = sq_call(v, 1, false, false); } } diff --git a/r5dev/squirrel/sqvm.h b/r5dev/squirrel/sqvm.h index 80489aae..29bd139f 100644 --- a/r5dev/squirrel/sqvm.h +++ b/r5dev/squirrel/sqvm.h @@ -18,25 +18,39 @@ struct SQVM } #endif - char pad0[0x8]; SQVM* _vftable; - _BYTE gap0[12]; + _BYTE gap000[16]; #if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2) - _BYTE gap1[4]; SQCONTEXT _contextidx; - _BYTE gap2[8]; + _BYTE gap001[8]; #endif - _BYTE gap3[22]; + _BYTE gap002[4]; + void* _ncvftable; + void* _table; + _BYTE gap003[14]; void* _callstack; - void* unk0; + __int64 _unk; SQInteger _stackbase; SQInteger unk5c; SQSharedState* _sharedstate; - char gap4[16]; + char gap004[16]; int _top; }; typedef SQVM* HSQUIRRELVM; +class CSquirrelVM +{ +public: + HSQUIRRELVM GetVM() const + { + return _vm; + } + +private: + char pad0[0x8]; + HSQUIRRELVM _vm; +}; + struct ScriptFunctionBinding_t { const SQChar* m_szScriptName; // 00 @@ -153,7 +167,7 @@ SQBool SQVM_CreateUIVM(); const SQChar* SQVM_GetContextName(SQCONTEXT context); const SQCONTEXT SQVM_GetContextIndex(HSQUIRRELVM v); -HSQUIRRELVM SQVM_GetVM(SQCONTEXT context); +CSquirrelVM* SQVM_GetContextObject(SQCONTEXT context); void SQVM_Execute(const SQChar* code, SQCONTEXT context); void SQVM_Attach(); @@ -234,20 +248,20 @@ class HSQVM : public IDetour v_SQVM_LoadScript = p_SQVM_LoadScript.RCast(); /*48 8B C4 48 89 48 08 55 41 56 48 8D 68*/ v_SQVM_LoadRson = p_SQVM_LoadRson.RCast(); /*4C 8B DC 49 89 5B 08 57 48 81 EC A0 00 00 00 33*/ v_SQVM_WarningCmd = p_SQVM_WarningCmd.RCast(); /*40 53 48 83 EC 30 33 DB 48 8D 44 24 ?? 4C 8D 4C 24 ??*/ - v_SQVM_RegisterFunc = p_SQVM_RegisterFunc.RCast(); /*48 83 EC 38 45 0F B6 C8*/ + v_SQVM_RegisterFunc = p_SQVM_RegisterFunc.RCast(); /*48 83 EC 38 45 0F B6 C8*/ v_SQVM_CompileError = p_SQVM_CompileError.RCast(); /*48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 48 89 7C 24 ? 41 56 48 81 EC ? ? ? ? 48 8B D9 4C 8B F2*/ #if !defined (CLIENT_DLL) - v_SQVM_InitializeSVGlobalScriptStructs = p_SQVM_InitializeSVGlobalScriptStructs.RCast(); /*48 89 74 24 ?? 57 48 83 EC 30 48 8B 3D ?? ?? ?? ?? 48 8B F1*/ + v_SQVM_InitializeSVGlobalScriptStructs = p_SQVM_InitializeSVGlobalScriptStructs.RCast(); /*48 89 74 24 ?? 57 48 83 EC 30 48 8B 3D ?? ?? ?? ?? 48 8B F1*/ #endif // !CLIENT_DLL #if !defined (DEDICATED) - v_SQVM_InitializeCLGlobalScriptStructs = p_SQVM_InitializeCLGlobalScriptStructs.RCast(); /*48 89 74 24 ?? 48 89 7C 24 ?? 41 56 48 83 EC 30 48 63 C2 48 8D 3D ?? ?? ?? ??*/ + v_SQVM_InitializeCLGlobalScriptStructs = p_SQVM_InitializeCLGlobalScriptStructs.RCast(); /*48 89 74 24 ?? 48 89 7C 24 ?? 41 56 48 83 EC 30 48 63 C2 48 8D 3D ?? ?? ?? ??*/ #endif // !DEDICATED #if !defined (CLIENT_DLL) - v_SQVM_CreateServerVM = p_SQVM_CreateServerVM.RCast(); /*40 53 56 48 83 EC 48 48 8D 0D ?? ?? ?? ??*/ + v_SQVM_CreateServerVM = p_SQVM_CreateServerVM.RCast(); /*40 53 56 48 83 EC 48 48 8D 0D ?? ?? ?? ??*/ #endif // !CLIENT_DLL #if !defined (DEDICATED) - v_SQVM_CreateClientVM = p_SQVM_CreateClientVM.RCast(); /*40 53 41 57 48 83 EC 68 48 83 3D ?? ?? ?? ?? ??*/ - v_SQVM_CreateUIVM = p_SQVM_CreateUIVM.RCast(); /*40 53 48 83 EC 20 48 8B 1D ?? ?? ?? ?? C6 05 ?? ?? ?? ?? ??*/ + v_SQVM_CreateClientVM = p_SQVM_CreateClientVM.RCast(); /*40 53 41 57 48 83 EC 68 48 83 3D ?? ?? ?? ?? ??*/ + v_SQVM_CreateUIVM = p_SQVM_CreateUIVM.RCast(); /*40 53 48 83 EC 20 48 8B 1D ?? ?? ?? ?? C6 05 ?? ?? ?? ?? ??*/ #endif // !DEDICATED } virtual void GetVar(void) const