Skip to content

Commit

Permalink
Lua: Add support for reading/modifying StructProperty properties
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 26, 2024
1 parent 329001f commit eb7f498
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lua-api/lib/include/ScriptUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace lua::utility {
bool is_ue5();

sol::object prop_to_object(sol::this_state s, void* self, uevr::API::FProperty* desc, bool is_self_temporary = false);
sol::object prop_to_object(sol::this_state s, void* self, uevr::API::UStruct* desc, const std::wstring& name);
sol::object prop_to_object(sol::this_state s, uevr::API::UObject* self, const std::wstring& name);

void set_property(sol::this_state s, void* self, uevr::API::UStruct* desc, const std::wstring& name, sol::object value);
void set_property(sol::this_state s, uevr::API::UObject* self, const std::wstring& name, sol::object value);

sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API::UFunction* fn, sol::variadic_args args);
Expand Down
6 changes: 5 additions & 1 deletion lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <windows.h>

#include "datatypes/Vector.hpp"
#include "datatypes/StructObject.hpp"

#include "ScriptUtility.hpp"
#include "ScriptContext.hpp"
Expand Down Expand Up @@ -160,6 +161,7 @@ int ScriptContext::setup_bindings() {
m_lua.registry()["uevr_context"] = this;

lua::datatypes::bind_vectors(m_lua);
lua::datatypes::bind_struct_object(m_lua);

m_lua.new_usertype<UEVR_PluginInitializeParam>("UEVR_PluginInitializeParam",
"uevr_module", &UEVR_PluginInitializeParam::uevr_module,
Expand Down Expand Up @@ -396,7 +398,9 @@ int ScriptContext::setup_bindings() {
"get_property", [](sol::this_state s, uevr::API::UObject* self, const std::wstring& name) -> sol::object {
return lua::utility::prop_to_object(s, self, name);
},
"set_property", &lua::utility::set_property,
"set_property", [](sol::this_state s, uevr::API::UObject* self, const std::wstring& name, sol::object value) {
lua::utility::set_property(s, self, name, value);
},
"call", [](sol::this_state s, uevr::API::UObject* self, const std::wstring& name, sol::variadic_args args) -> sol::object {
return lua::utility::call_function(s, self, name, args);
},
Expand Down
32 changes: 21 additions & 11 deletions lua-api/lib/src/ScriptUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <utility/String.hpp>

#include <datatypes/Vector.hpp>
#include <datatypes/StructObject.hpp>
#include <ScriptUtility.hpp>

namespace lua::utility {
Expand Down Expand Up @@ -110,8 +111,9 @@ sol::object prop_to_object(sol::this_state s, void* self, uevr::API::FProperty*
}
}

// TODO: Return a reflected struct
return sol::make_object(s, sol::lua_nil);
auto struct_object = lua::datatypes::StructObject{struct_data, struct_desc};

return sol::make_object(s, struct_object);
}
case L"ArrayProperty"_fnv:
{
Expand Down Expand Up @@ -156,13 +158,7 @@ sol::object prop_to_object(sol::this_state s, void* self, uevr::API::FProperty*
return sol::make_object(s, sol::lua_nil);
}

sol::object prop_to_object(sol::this_state s, uevr::API::UObject* self, const std::wstring& name) {
const auto c = self->get_class();

if (c == nullptr) {
return sol::make_object(s, sol::lua_nil);
}

sol::object prop_to_object(sol::this_state s, void* self, uevr::API::UStruct* c, const std::wstring& name) {
const auto desc = c->find_property(name.c_str());

if (desc == nullptr) {
Expand All @@ -179,13 +175,17 @@ sol::object prop_to_object(sol::this_state s, uevr::API::UObject* self, const st
return prop_to_object(s, self, desc);
}

void set_property(sol::this_state s, uevr::API::UObject* self, const std::wstring& name, sol::object value) {
sol::object prop_to_object(sol::this_state s, uevr::API::UObject* self, const std::wstring& name) {
const auto c = self->get_class();

if (c == nullptr) {
throw sol::error("[set_property] Object has no class");
return sol::make_object(s, sol::lua_nil);
}

return prop_to_object(s, self, c, name);
}

void set_property(sol::this_state s, void* self, uevr::API::UStruct* c, const std::wstring& name, sol::object value) {
const auto desc = c->find_property(name.c_str());

if (desc == nullptr) {
Expand Down Expand Up @@ -238,6 +238,16 @@ void set_property(sol::this_state s, uevr::API::UObject* self, const std::wstrin
// NONE
}

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) {
throw sol::error("[set_property] Object has no class");
}

set_property(s, self, c, name, value);
}

sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API::UFunction* fn, sol::variadic_args args) {
const auto fn_args = fn->get_child_properties();

Expand Down
27 changes: 26 additions & 1 deletion lua-api/lib/src/datatypes/StructObject.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
#include <utility/String.hpp>

#include <ScriptUtility.hpp>
#include <datatypes/StructObject.hpp>

namespace lua::datatypes {
void bind_struct_object(sol::state_view& lua) {
lua.new_usertype<StructObject>("StructObject",
"get_address", [](StructObject& self) { return (uintptr_t)self.object; },
"get_struct", [](StructObject& self) { return self.desc; }
"get_struct", [](StructObject& self) { return self.desc; },
"get_property", [](sol::this_state s, StructObject* self, const std::wstring& name) -> sol::object {
return lua::utility::prop_to_object(s, self->object, self->desc, name);
},
"set_property", [](sol::this_state s, StructObject* self, const std::wstring& name, sol::object value) {
lua::utility::set_property(s, self->object, self->desc, name, value);
},
sol::meta_function::index, [](sol::this_state s, StructObject* self, sol::object index_obj) -> sol::object {
if (!index_obj.is<std::string>()) {
return sol::make_object(s, sol::lua_nil);
}

const auto name = ::utility::widen(index_obj.as<std::string>());

return lua::utility::prop_to_object(s, self->object, self->desc, name);
},
sol::meta_function::new_index, [](sol::this_state s, StructObject* self, sol::object index_obj, sol::object value) {
if (!index_obj.is<std::string>()) {
return;
}

const auto name = ::utility::widen(index_obj.as<std::string>());
lua::utility::set_property(s, self->object, self->desc, name, value);
}
);
}
}

0 comments on commit eb7f498

Please sign in to comment.