Skip to content

Commit

Permalink
Fix Lua wrapper for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed Dec 12, 2021
1 parent 0c1eeab commit 9434c54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/reverse/ClassReference.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
#include <stdafx.h>

#include "ClassReference.h"
#include "CET.h"

ClassReference::ClassReference(const TiltedPhoques::Locked<sol::state, std::recursive_mutex>& 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<uint8_t[]>(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
Expand Down
4 changes: 3 additions & 1 deletion src/reverse/ClassReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ struct ClassReference : ClassType
ClassReference(const TiltedPhoques::Locked<sol::state, std::recursive_mutex>& 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<uint8_t[]> m_pInstance;
RED4ext::ScriptInstance m_pInstance;
};

0 comments on commit 9434c54

Please sign in to comment.