From 2a0d1f94e1597f9c2f0e22606270bd5d7890fe0d Mon Sep 17 00:00:00 2001 From: pv Date: Sat, 2 Oct 2021 14:22:36 +0300 Subject: [PATCH 1/2] Mark gamedataTDBIDHelper as static helper --- src/reverse/RTTIHelper.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/reverse/RTTIHelper.cpp b/src/reverse/RTTIHelper.cpp index 97f703de..dfa3758c 100644 --- a/src/reverse/RTTIHelper.cpp +++ b/src/reverse/RTTIHelper.cpp @@ -118,6 +118,7 @@ void RTTIHelper::AddFunctionAlias(const std::string& acAliasClassName, const std bool RTTIHelper::IsFunctionAlias(RED4ext::CBaseFunction* apFunc) { static const auto s_cTweakDBInterfaceHash = RED4ext::FNV1a("gamedataTweakDBInterface"); + static const auto s_cTDBIDHelperHash = RED4ext::FNV1a("gamedataTDBIDHelper"); if (m_extendedFunctions.contains(kGlobalHash)) { @@ -131,9 +132,9 @@ bool RTTIHelper::IsFunctionAlias(RED4ext::CBaseFunction* apFunc) { const auto cClassHash = apFunc->GetParent()->name.hash; - // TweakDBInterface is special. - // All of its methods are non-static, but they can only be used as static ones. - if (cClassHash == s_cTweakDBInterfaceHash) + // TweakDBInterface and TDBID classes are special. + // All of their methods are non-static, but they can only be used as static ones. + if (cClassHash == s_cTweakDBInterfaceHash || cClassHash == s_cTDBIDHelperHash) return true; if (m_extendedFunctions.contains(cClassHash)) From 543fa18aa1bb0785b1d9a310d7d5035ae1ab0998 Mon Sep 17 00:00:00 2001 From: pv Date: Sat, 2 Oct 2021 14:40:36 +0300 Subject: [PATCH 2/2] Drop second implementation of ToRED --- src/scripting/Scripting.cpp | 106 ++++-------------------------------- 1 file changed, 10 insertions(+), 96 deletions(-) diff --git a/src/scripting/Scripting.cpp b/src/scripting/Scripting.cpp index bdcbb6ac..d1181f32 100644 --- a/src/scripting/Scripting.cpp +++ b/src/scripting/Scripting.cpp @@ -946,102 +946,16 @@ RED4ext::CStackType Scripting::ToRED(sol::object aObject, RED4ext::CBaseRTTIType return result; } -void Scripting::ToRED(sol::object aObject, RED4ext::CStackType* apType) +void Scripting::ToRED(sol::object aObject, RED4ext::CStackType* apRet) { - const bool hasData = aObject != sol::nil; + static thread_local TiltedPhoques::ScratchAllocator s_scratchMemory(1 << 13); - if (apType->type) - { - if (apType->type == s_stringType) - { - std::string str; - if (hasData) - { - sol::state_view v(aObject.lua_state()); - str = v["tostring"](aObject); - } - RED4ext::CString value(str.c_str()); - *static_cast(apType->value) = value; - } - else if (apType->type->GetType() == RED4ext::ERTTIType::Handle) - { - if (aObject.is()) - { - auto* pSubType = static_cast(apType->type)->parent; - auto* pType = static_cast(aObject.as()->m_pType); - if (pType && pType->IsA(pSubType)) - { - if (hasData) - *static_cast*>(apType->value) = - RED4ext::Handle(aObject.as().m_strongHandle); - else - *static_cast*>(apType->value) = - RED4ext::Handle(); - } - } - else if (aObject.is()) - { - auto* pSubType = static_cast(apType->type)->parent; - auto* pType = static_cast(aObject.as()->m_pType); - if (pType && pType->IsA(pSubType)) - { - if (hasData) - *static_cast*>(apType->value) = - RED4ext::Handle(aObject.as().m_weakHandle); - else - *static_cast*>(apType->value) = - RED4ext::Handle(); - } - } - } - else if (apType->type->GetType() == RED4ext::ERTTIType::WeakHandle) - { - if (aObject.is()) - { - auto* pSubType = static_cast(apType->type)->parent; - auto* pType = static_cast(aObject.as()->m_pType); - if (pType && pType->IsA(pSubType)) - { - if (hasData) - *static_cast*>(apType->value) = - RED4ext::WeakHandle(aObject.as().m_weakHandle); - else - *static_cast*>(apType->value) = - RED4ext::WeakHandle(); - } - } - else if (aObject.is()) // Handle Implicit Cast - { - auto* pSubType = static_cast(apType->type)->parent; - auto* pType = static_cast(aObject.as()->m_pType); - if (pType && pType->IsA(pSubType)) - { - if (hasData) - *static_cast*>(apType->value) = - RED4ext::WeakHandle(aObject.as().m_strongHandle); - else - *static_cast*>(apType->value) = - RED4ext::WeakHandle(); - } - } - } - else if (apType->type->GetType() == RED4ext::ERTTIType::Array) - { - //TODO: Support arrays - } - else if (apType->type->GetType() == RED4ext::ERTTIType::ScriptReference) - { - if (aObject.is()) - { - auto* pClassRef = aObject.as(); - auto* pScriptRef = static_cast*>(apType->value); - pScriptRef->innerType = pClassRef->m_pType; - pScriptRef->ref = pClassRef->GetHandle(); - } - } - else - { - Converter::ToRED(aObject, apType); - } - } + auto result = ToRED(aObject, apRet->type, &s_scratchMemory); + + apRet->type->Assign(apRet->value, result.value); + + if (apRet->type->GetType() != RED4ext::ERTTIType::Class) + apRet->type->Destroy(result.value); + + s_scratchMemory.Reset(); }