From 1a810f69fe4cab82b352c574763959d4390b84ce Mon Sep 17 00:00:00 2001 From: praydog Date: Wed, 30 Oct 2024 18:40:04 -0700 Subject: [PATCH] Lua: Add various UObject read/write functions --- lua-api/lib/include/ScriptUtility.hpp | 39 +++++++++++++++++++++++++++ lua-api/lib/src/ScriptContext.cpp | 32 ++++++++++------------ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/lua-api/lib/include/ScriptUtility.hpp b/lua-api/lib/include/ScriptUtility.hpp index c9cbd492..8dcaf8ba 100644 --- a/lua-api/lib/include/ScriptUtility.hpp +++ b/lua-api/lib/include/ScriptUtility.hpp @@ -17,4 +17,43 @@ namespace lua::utility { sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API::UFunction* fn, sol::variadic_args args); sol::object call_function(sol::this_state s, uevr::API::UObject* self, const std::wstring& name, sol::variadic_args args); + + template + void write_t(uevr::API::UObject* self, size_t offset, T value) { + size_t size = 0; + const auto c = self->get_class(); + if (c->is_a(uevr::API::UScriptStruct::static_class())) { + auto script_struct = reinterpret_cast(c); + + size = script_struct->get_struct_size(); + } else { + size = c->get_properties_size(); + } + + if (offset + sizeof(T) > size) { + throw sol::error("Offset out of bounds"); + } + + *(T*)((uintptr_t)self + offset) = value; + } + + template + T read_t(uevr::API::UObject* self, size_t offset) { + size_t size = 0; + const auto c = self->get_class(); + + if (c->is_a(uevr::API::UScriptStruct::static_class())) { + auto script_struct = reinterpret_cast(c); + + size = script_struct->get_struct_size(); + } else { + size = c->get_properties_size(); + } + + if (offset + sizeof(T) > size) { + throw sol::error("Offset out of bounds"); + } + + return *(T*)((uintptr_t)self + offset); + } } \ No newline at end of file diff --git a/lua-api/lib/src/ScriptContext.cpp b/lua-api/lib/src/ScriptContext.cpp index 68d4d4e1..2df9496e 100644 --- a/lua-api/lib/src/ScriptContext.cpp +++ b/lua-api/lib/src/ScriptContext.cpp @@ -585,23 +585,18 @@ int ScriptContext::setup_bindings() { return sol::make_object(s, result); // TODO: convert? }, - "write_qword", [](API::UObject* self, size_t offset, uint64_t value) { - size_t size = 0; - const auto c = self->get_class(); - if (c->is_a(uevr::API::UScriptStruct::static_class())) { - auto script_struct = reinterpret_cast(c); - - size = script_struct->get_struct_size(); - } else { - size = c->get_properties_size(); - } - - if (offset + sizeof(uint64_t) > size) { - throw sol::error("Offset out of bounds"); - } - - *(uint64_t*)((uintptr_t)self + offset) = value; - }, + "write_qword", &lua::utility::write_t, + "write_dword", &lua::utility::write_t, + "write_word", &lua::utility::write_t, + "write_byte", &lua::utility::write_t, + "write_float", &lua::utility::write_t, + "write_double", &lua::utility::write_t, + "read_qword", &lua::utility::read_t, + "read_dword", &lua::utility::read_t, + "read_word", &lua::utility::read_t, + "read_byte", &lua::utility::read_t, + "read_float", &lua::utility::read_t, + "read_double", &lua::utility::read_t, 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); @@ -964,7 +959,8 @@ int ScriptContext::setup_bindings() { return sol::make_object(s, state); }, - "remove_motion_controller_state", &uevr::API::UObjectHook::remove_motion_controller_state + "remove_motion_controller_state", &uevr::API::UObjectHook::remove_motion_controller_state, + "remove_all_motion_controller_states", &uevr::API::UObjectHook::remove_all_motion_controller_states ); m_lua.new_usertype("UEVR_API",