Skip to content

Commit bc2a177

Browse files
authored
Merge pull request #1136 from Novaenia/struct_property_lua
feat: support for registered properties in UScriptStruct ⇔ Lua conversion
2 parents 838e009 + 1007ba4 commit bc2a177

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

UE4SS/include/LuaType/LuaCustomProperty.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <memory>
44
#include <string>
55
#include <vector>
6+
#include <functional>
67

78
#include <String/StringType.hpp>
89

@@ -34,6 +35,10 @@ namespace RC::LuaType
3435
public:
3536
auto add(StringType property_name, std::unique_ptr<Unreal::CustomProperty>) -> void;
3637
auto clear() -> void;
38+
39+
using ForEachCallable = std::function<bool(LuaCustomProperty const&)>;
40+
auto for_each(Unreal::UObject* base, const ForEachCallable& callable) -> bool;
41+
3742
auto find_or_nullptr(Unreal::UObject* base, StringType property_name) -> Unreal::FProperty*;
3843
};
3944

UE4SS/src/LuaType/LuaCustomProperty.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace RC::LuaType
2626
properties.clear();
2727
}
2828

29-
auto LuaCustomProperty::PropertyList::find_or_nullptr(Unreal::UObject* base, StringType property_name) -> Unreal::FProperty*
29+
auto LuaCustomProperty::PropertyList::for_each(Unreal::UObject* base, const ForEachCallable& callable) -> bool
3030
{
3131
Unreal::FProperty* custom_property_found{};
3232

@@ -66,14 +66,28 @@ namespace RC::LuaType
6666
}
6767
}
6868

69-
if (class_matches && property_name == property_item.m_name)
69+
if (class_matches && !callable(property_item))
7070
{
71-
// Compare name here
72-
custom_property_found = property_item.m_property.get();
73-
break;
71+
return false;
7472
}
7573
}
7674

75+
return true;
76+
}
77+
78+
auto LuaCustomProperty::PropertyList::find_or_nullptr(Unreal::UObject* base, StringType property_name) -> Unreal::FProperty*
79+
{
80+
Unreal::FProperty* custom_property_found{};
81+
82+
for_each(base, [&](LuaCustomProperty const& property) {
83+
bool match = property.m_name == property_name;
84+
if (match)
85+
{
86+
custom_property_found = property.m_property.get();
87+
}
88+
return !match;
89+
});
90+
7791
return custom_property_found;
7892
}
7993
} // namespace RC::LuaType

UE4SS/src/LuaType/LuaUObject.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <Unreal/UInterface.hpp>
3333
#include <Unreal/World.hpp>
3434
#include <Unreal/Engine/UDataTable.hpp>
35+
#include <UnrealCustom/CustomProperty.hpp>
3536

3637
#pragma warning(default : 4005)
3738
#include <DynamicOutput/DynamicOutput.hpp>
@@ -538,10 +539,8 @@ namespace RC::LuaType
538539
return;
539540
}
540541

541-
for (Unreal::FProperty* field : Unreal::TFieldRange<Unreal::FProperty>(script_struct, Unreal::EFieldIterationFlags::IncludeSuper | Unreal::EFieldIterationFlags::IncludeDeprecated))
542-
{
542+
auto handle_property = [&](Unreal::FProperty* field, const std::string& field_name) {
543543
Unreal::FName field_type_fname = field->GetClass().GetFName();
544-
const std::string field_name = to_utf8_string(field->GetName());
545544

546545
// Push the field name (key for the table)
547546
lua_pushstring(lua.get_lua_state(), field_name.c_str());
@@ -550,7 +549,7 @@ namespace RC::LuaType
550549
// For lua_rawget, we need the actual stack position of the table
551550
// If table_index is positive, it stays the same even after pushing the key
552551
// If it's negative, we need to adjust for the key we just pushed
553-
552+
554553
// Adjust index for negative values after pushing key
555554
int adjusted_index = table_index;
556555
if (table_index < 0)
@@ -564,7 +563,7 @@ namespace RC::LuaType
564563
if (table_value_type == LUA_TNIL || table_value_type == LUA_TNONE)
565564
{
566565
lua.discard_value(-1);
567-
continue;
566+
return;
568567
}
569568

570569
int32_t name_comparison_index = field_type_fname.GetComparisonIndex();
@@ -595,6 +594,16 @@ namespace RC::LuaType
595594
);
596595

597596
}
597+
};
598+
599+
LuaCustomProperty::StaticStorage::property_list.for_each(script_struct, [&](LuaCustomProperty const& lua_property) {
600+
handle_property(lua_property.m_property.get(), to_utf8_string(lua_property.m_name));
601+
return true;
602+
});
603+
604+
for (Unreal::FProperty* field : Unreal::TFieldRange<Unreal::FProperty>(script_struct, Unreal::EFieldIterationFlags::IncludeSuper | Unreal::EFieldIterationFlags::IncludeDeprecated))
605+
{
606+
handle_property(field, to_utf8_string(field->GetName()));
598607
}
599608
}
600609

@@ -620,13 +629,9 @@ namespace RC::LuaType
620629
return;
621630
}
622631

623-
LuaMadeSimple::Lua::Table lua_table = create_new_table
624-
? lua.prepare_new_table()
625-
: lua.get_table();
632+
LuaMadeSimple::Lua::Table lua_table = create_new_table ? lua.prepare_new_table() : lua.get_table();
626633

627-
for (Unreal::FProperty* field : Unreal::TFieldRange<Unreal::FProperty>(script_struct, Unreal::EFieldIterationFlags::IncludeSuper | Unreal::EFieldIterationFlags::IncludeDeprecated))
628-
{
629-
std::string field_name = to_utf8_string(field->GetName());
634+
auto handle_property = [&](Unreal::FProperty* field, const std::string& field_name) {
630635
Unreal::FName field_type_fname = field->GetClass().GetFName();
631636
int32_t name_comparison_index = field_type_fname.GetComparisonIndex();
632637

@@ -684,6 +689,16 @@ namespace RC::LuaType
684689
field_type_fname.ToString()
685690
);
686691
}
692+
};
693+
694+
LuaCustomProperty::StaticStorage::property_list.for_each(script_struct, [&](LuaCustomProperty const& lua_property) {
695+
handle_property(lua_property.m_property.get(), to_utf8_string(lua_property.m_name));
696+
return true;
697+
});
698+
699+
for (Unreal::FProperty* field : Unreal::TFieldRange<Unreal::FProperty>(script_struct, Unreal::EFieldIterationFlags::IncludeSuper | Unreal::EFieldIterationFlags::IncludeDeprecated))
700+
{
701+
handle_property(field, to_utf8_string(field->GetName()));
687702
}
688703

689704
lua_table.make_local();

0 commit comments

Comments
 (0)