WopsS / RED4ext.SDK

A library to create mods for REDengine 4 (Cyberpunk 2077), independently of RED4ext.
MIT License
93 stars 31 forks source link

Improve Variant #36

Closed WSSDude closed 2 years ago

WSSDude commented 3 years ago

Modified structure according to what was discussed before. Added some extra checks in regards to placement of things.

These should be working ToVariant/FromVariant functions we have in CET, needs testing... I could include them if you want, just tell me where to put them and I'll remake them for this x)

    aLuaGlobal["ToVariant"] = [this](Type& aInstance) -> sol::object {
        auto* pType = aInstance.GetType();
        auto* pHandle = aInstance.GetHandle();

        if (!pType || !pHandle)
            return sol::nil;

        auto lockedState = m_lua.Lock();
        auto& luaState = lockedState.Get();

        return sol::object(luaState, sol::in_place, Variant(pType, pHandle));
    };

    aLuaGlobal["FromVariant"] = [this](Variant& aVariant) -> sol::object {
        if (aVariant.type == 0)
            return sol::nil;

        RED4ext::CStackType result;
        if ((aVariant.type & 1) == 0)
        {
            result.type = reinterpret_cast<RED4ext::IRTTIType*>(aVariant.type); 
            result.value = aVariant.instance;
        }
        else
        {
            result.type = reinterpret_cast<RED4ext::IRTTIType*>(aVariant.type & (~1ull));
            result.value = &aVariant.inlinedBuf;
        }

        auto lockedState = m_lua.Lock();

        return Scripting::ToLua(lockedState, result);
    };
WSSDude commented 2 years ago

If someone wants to pick up where i left off, feel free to do so... But prolly is dead and will be rewritten from scratch...