From a88cab390ad0c5c075400b53b3239bcdda1b21d3 Mon Sep 17 00:00:00 2001 From: praydog Date: Sun, 16 Jun 2024 13:47:10 -0700 Subject: [PATCH] Lua: Add support for setting properties --- lua-api/lib/src/ScriptContext.cpp | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lua-api/lib/src/ScriptContext.cpp b/lua-api/lib/src/ScriptContext.cpp index 7553b794..93e6463c 100644 --- a/lua-api/lib/src/ScriptContext.cpp +++ b/lua-api/lib/src/ScriptContext.cpp @@ -237,6 +237,57 @@ sol::object prop_to_object(sol::this_state s, uevr::API::UObject& self, const st return sol::make_object(s, sol::lua_nil); } +void set_property(sol::this_state s, uevr::API::UObject& self, const std::wstring& name, sol::object value) { + const auto c = self.get_class(); + + if (c == nullptr) { + return; + } + + const auto desc = c->find_property(name.c_str()); + + if (desc == nullptr) { + return; + } + + const auto propc = desc->get_class(); + + if (propc == nullptr) { + return; + } + + const auto name_hash = utility::hash(propc->get_fname()->to_string()); + + switch (name_hash) { + case L"BoolProperty"_fnv: + self.set_bool_property(name, value.as()); + return; + case L"FloatProperty"_fnv: + self.get_property(name) = value.as(); + return; + case L"DoubleProperty"_fnv: + self.get_property(name) = value.as(); + return; + case L"IntProperty"_fnv: + self.get_property(name) = value.as(); + return; + case L"UIntProperty"_fnv: + case L"UInt32Property"_fnv: + self.get_property(name) = value.as(); + return; + case L"NameProperty"_fnv: + //return sol::make_object(s, self.get_property(name)); + throw sol::error("Setting FName properties is not supported (yet)"); + case L"ObjectProperty"_fnv: + self.get_property(name) = value.as(); + return; + case L"ArrayProperty"_fnv: + throw sol::error("Setting array properties is not supported (yet)"); + }; + + // NONE +} + int ScriptContext::setup_bindings() { m_lua.registry()["uevr_context"] = this; @@ -456,6 +507,7 @@ int ScriptContext::setup_bindings() { "get_property", [](sol::this_state s, uevr::API::UObject& self, const std::wstring& name) -> sol::object { return prop_to_object(s, self, name); }, + "set_property", &set_property, sol::meta_function::index, [](sol::this_state s, uevr::API::UObject& self, sol::object index_obj) -> sol::object { if (!index_obj.is()) { return sol::make_object(s, sol::lua_nil); @@ -463,6 +515,14 @@ int ScriptContext::setup_bindings() { const auto name = utility::widen(index_obj.as()); return prop_to_object(s, self, name); + }, + sol::meta_function::new_index, [](sol::this_state s, uevr::API::UObject& self, sol::object index_obj, sol::object value) { + if (!index_obj.is()) { + return; + } + + const auto name = utility::widen(index_obj.as()); + set_property(s, self, name, value); } );