Skip to content

Commit

Permalink
Merge pull request #620 from psiberx/master
Browse files Browse the repository at this point in the history
Fix ToRED for overrides, fix TDBID helper
  • Loading branch information
Yamashi authored Oct 2, 2021
2 parents 8ab3d43 + 543fa18 commit bd9736f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 99 deletions.
7 changes: 4 additions & 3 deletions src/reverse/RTTIHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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))
Expand Down
106 changes: 10 additions & 96 deletions src/scripting/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RED4ext::CString*>(apType->value) = value;
}
else if (apType->type->GetType() == RED4ext::ERTTIType::Handle)
{
if (aObject.is<StrongReference>())
{
auto* pSubType = static_cast<RED4ext::CClass*>(apType->type)->parent;
auto* pType = static_cast<RED4ext::CClass*>(aObject.as<StrongReference*>()->m_pType);
if (pType && pType->IsA(pSubType))
{
if (hasData)
*static_cast<RED4ext::Handle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::Handle<RED4ext::IScriptable>(aObject.as<StrongReference>().m_strongHandle);
else
*static_cast<RED4ext::Handle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::Handle<RED4ext::IScriptable>();
}
}
else if (aObject.is<WeakReference>())
{
auto* pSubType = static_cast<RED4ext::CClass*>(apType->type)->parent;
auto* pType = static_cast<RED4ext::CClass*>(aObject.as<WeakReference*>()->m_pType);
if (pType && pType->IsA(pSubType))
{
if (hasData)
*static_cast<RED4ext::Handle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::Handle<RED4ext::IScriptable>(aObject.as<WeakReference>().m_weakHandle);
else
*static_cast<RED4ext::Handle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::Handle<RED4ext::IScriptable>();
}
}
}
else if (apType->type->GetType() == RED4ext::ERTTIType::WeakHandle)
{
if (aObject.is<WeakReference>())
{
auto* pSubType = static_cast<RED4ext::CClass*>(apType->type)->parent;
auto* pType = static_cast<RED4ext::CClass*>(aObject.as<WeakReference*>()->m_pType);
if (pType && pType->IsA(pSubType))
{
if (hasData)
*static_cast<RED4ext::WeakHandle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::WeakHandle<RED4ext::IScriptable>(aObject.as<WeakReference>().m_weakHandle);
else
*static_cast<RED4ext::WeakHandle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::WeakHandle<RED4ext::IScriptable>();
}
}
else if (aObject.is<StrongReference>()) // Handle Implicit Cast
{
auto* pSubType = static_cast<RED4ext::CClass*>(apType->type)->parent;
auto* pType = static_cast<RED4ext::CClass*>(aObject.as<StrongReference*>()->m_pType);
if (pType && pType->IsA(pSubType))
{
if (hasData)
*static_cast<RED4ext::WeakHandle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::WeakHandle<RED4ext::IScriptable>(aObject.as<StrongReference>().m_strongHandle);
else
*static_cast<RED4ext::WeakHandle<RED4ext::IScriptable>*>(apType->value) =
RED4ext::WeakHandle<RED4ext::IScriptable>();
}
}
}
else if (apType->type->GetType() == RED4ext::ERTTIType::Array)
{
//TODO: Support arrays
}
else if (apType->type->GetType() == RED4ext::ERTTIType::ScriptReference)
{
if (aObject.is<ClassReference>())
{
auto* pClassRef = aObject.as<ClassReference*>();
auto* pScriptRef = static_cast<RED4ext::ScriptRef<void>*>(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();
}

0 comments on commit bd9736f

Please sign in to comment.