From e50f0ac85de69cfb6235733d7973376543076639 Mon Sep 17 00:00:00 2001 From: pv Date: Thu, 28 Sep 2023 20:21:42 +0300 Subject: [PATCH 1/2] Fix script ref handling --- src/scripting/FunctionOverride.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/scripting/FunctionOverride.cpp b/src/scripting/FunctionOverride.cpp index 837910d5..42840379 100644 --- a/src/scripting/FunctionOverride.cpp +++ b/src/scripting/FunctionOverride.cpp @@ -277,8 +277,13 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext, const bool isScriptRef = pArg->type->GetType() == RED4ext::ERTTIType::ScriptReference; - // Exception here we need to allocate the inner object as well - if (isScriptRef) + // Determines if script ref was created from rvalue and other side expects us to copy it. + // If true, we must allocate the memory for the value and initialize our own script ref. + // If false, we don't have to initialize anything on our side and pInstance will be filled + // with a pointer to the original value. + const bool isTemporary = (apFrame->paramFlags >> apFrame->currentParam) & 1; + + if (isScriptRef && isTemporary) { auto* pInnerType = static_cast(pType)->innerType; auto* pScriptRef = static_cast*>(pInstance); @@ -310,7 +315,7 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext, } // Release inner values - if (isScriptRef) + if (isScriptRef && isTemporary) { auto* pScriptRef = static_cast*>(pInstance); pScriptRef->innerType->Destruct(pScriptRef->ref); From c14240eec9012ccd75816abbdf56d10c3d857f44 Mon Sep 17 00:00:00 2001 From: pv Date: Thu, 28 Sep 2023 20:33:21 +0300 Subject: [PATCH 2/2] Disable TweakDB changes tracking --- src/scripting/LuaVM_Hooks.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/scripting/LuaVM_Hooks.cpp b/src/scripting/LuaVM_Hooks.cpp index fdaa176a..e6af8945 100644 --- a/src/scripting/LuaVM_Hooks.cpp +++ b/src/scripting/LuaVM_Hooks.cpp @@ -329,6 +329,9 @@ uint64_t LuaVM::HookTweakDBLoad(uintptr_t aThis, uintptr_t aParam) { const auto ret = s_vm->m_realTweakDBLoad(aThis, aParam); + // Disable changes tracking added to TweakDB in patch 2.0 and causing instability. + RED4ext::TweakDB::Get()->unk160 = 0; + s_vm->PostInitializeTweakDB(); return ret;