From 9434c543c7333cbd97bfdb4ecff7fe2b399646ae Mon Sep 17 00:00:00 2001 From: pv Date: Mon, 13 Dec 2021 02:58:32 +0300 Subject: [PATCH] Fix Lua wrapper for structs --- src/reverse/ClassReference.cpp | 26 +++++++++++++++++++++----- src/reverse/ClassReference.h | 4 +++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/reverse/ClassReference.cpp b/src/reverse/ClassReference.cpp index ba80815f..3de7a402 100644 --- a/src/reverse/ClassReference.cpp +++ b/src/reverse/ClassReference.cpp @@ -1,20 +1,36 @@ #include #include "ClassReference.h" +#include "CET.h" ClassReference::ClassReference(const TiltedPhoques::Locked& aView, RED4ext::CBaseRTTIType* apClass, RED4ext::ScriptInstance apInstance) : ClassType(aView, apClass) { - // Hack for now until we use their allocators, classes can actually be pointers to structs - // GI just happens to be a 8-byte struct with only a pointer in it - m_pInstance = std::make_unique(apClass->GetSize()); - memcpy(m_pInstance.get(), apInstance, apClass->GetSize()); + m_pInstance = apClass->GetAllocator()->AllocAligned(apClass->GetSize(), apClass->GetAlignment()).memory; + apClass->Construct(m_pInstance); + apClass->Assign(m_pInstance, apInstance); +} + +ClassReference::ClassReference(ClassReference&& aOther) noexcept + : ClassType(std::move(aOther)) + , m_pInstance(aOther.m_pInstance) +{ + aOther.m_pInstance = nullptr; +} + +ClassReference::~ClassReference() +{ + if (m_pInstance && CET::IsRunning()) + { + m_pType->Destruct(m_pInstance); + m_pType->GetAllocator()->Free(m_pInstance); + } } RED4ext::ScriptInstance ClassReference::GetHandle() const { - return m_pInstance.get(); + return m_pInstance; } RED4ext::ScriptInstance ClassReference::GetValuePtr() const diff --git a/src/reverse/ClassReference.h b/src/reverse/ClassReference.h index bb8e7598..edd36236 100644 --- a/src/reverse/ClassReference.h +++ b/src/reverse/ClassReference.h @@ -7,10 +7,12 @@ struct ClassReference : ClassType ClassReference(const TiltedPhoques::Locked& aView, RED4ext::CBaseRTTIType* apClass, RED4ext::ScriptInstance apInstance); + ClassReference(ClassReference&& aOther) noexcept; + virtual ~ClassReference(); virtual RED4ext::ScriptInstance GetHandle() const override; virtual RED4ext::ScriptInstance GetValuePtr() const override; private: - std::unique_ptr m_pInstance; + RED4ext::ScriptInstance m_pInstance; };