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); 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;