Skip to content

Commit

Permalink
Fix script references in overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed Dec 12, 2021
1 parent 4ed5744 commit 0c1eeab
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/scripting/FunctionOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,20 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
auto* pType = pArg->type;
auto* pAllocator = pType->GetAllocator();

auto* pInstance = pAllocator->Alloc(pType->GetSize()).memory;
auto* pInstance = pAllocator->AllocAligned(pType->GetSize(), pType->GetAlignment()).memory;
pType->Init(pInstance);

bool isScriptRef = pArg->type->GetType() == RED4ext::ERTTIType::ScriptReference;

// Exception here we need to allocate the inner object as well
if (isScriptRef)
{
RED4ext::ScriptRef<void>* pScriptRef = (RED4ext::ScriptRef<void>*)pInstance;
auto pInnerType = pScriptRef->innerType;

auto pInnerInstance = pInnerType->GetAllocator()->Alloc(pInnerType->GetSize()).memory;
pInnerType->Init(pInnerInstance);

pScriptRef->ref = pInnerInstance;
auto* pInnerType = ((RED4ext::CRTTIScriptReferenceType*)pType)->innerType;
auto* pScriptRef = (RED4ext::ScriptRef<void>*)pInstance;
pScriptRef->innerType = pInnerType;
pScriptRef->hash = pInnerType->GetName();
pScriptRef->ref = pInnerType->GetAllocator()->AllocAligned(pInnerType->GetSize(), pInnerType->GetAlignment()).memory;
pInnerType->Init(pScriptRef->ref);
}

RED4ext::CStackType arg;
Expand All @@ -260,9 +259,10 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
// Release inner values
if (isScriptRef)
{
RED4ext::ScriptRef<void>* pScriptRef = (RED4ext::ScriptRef<void>*)pInstance;
auto* pScriptRef = (RED4ext::ScriptRef<void>*)pInstance;
pScriptRef->innerType->Destroy(pScriptRef->ref);
pScriptRef->innerType->GetAllocator()->Free(pScriptRef->ref);
pScriptRef->ref = nullptr;
}

if (!pArg->flags.isOut || apFrame->unk30)
Expand Down

0 comments on commit 0c1eeab

Please sign in to comment.