Skip to content

Commit

Permalink
Lua: Add read/write functions to StructObject
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Dec 12, 2024
1 parent bc82512 commit d75a7a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
26 changes: 17 additions & 9 deletions lua-api/lib/include/ScriptUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ 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 <typename T>
void write_t(uevr::API::UObject* self, size_t offset, T value) {
template<typename T>
inline T read_t_struct(void* self, uevr::API::UStruct* c, 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<uevr::API::UScriptStruct*>(c);

Expand All @@ -34,14 +34,12 @@ namespace lua::utility {
throw sol::error("Offset out of bounds");
}

*(T*)((uintptr_t)self + offset) = value;
return *(T*)((uintptr_t)self + offset);
}

template<typename T>
T read_t(uevr::API::UObject* self, size_t offset) {
template <typename T>
inline void write_t_struct(void* self, uevr::API::UStruct* c, 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<uevr::API::UScriptStruct*>(c);

Expand All @@ -54,6 +52,16 @@ namespace lua::utility {
throw sol::error("Offset out of bounds");
}

return *(T*)((uintptr_t)self + offset);
*(T*)((uintptr_t)self + offset) = value;
}

template <typename T>
void write_t(uevr::API::UObject* self, size_t offset, T value) {
write_t_struct<T>(self, self->get_class(), offset, value);
}

template<typename T>
T read_t(uevr::API::UObject* self, size_t offset) {
return read_t_struct<T>(self, self->get_class(), offset);
}
}
38 changes: 22 additions & 16 deletions lua-api/lib/src/datatypes/StructObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
#include <datatypes/StructObject.hpp>

namespace lua::datatypes {
template<typename T>
T read_t_structobject(StructObject& self, size_t offset) {
return utility::read_t_struct<T>(self.object, self.desc, offset);
}

template<typename T>
void write_t_structobject(StructObject& self, size_t offset, T value) {
utility::write_t_struct<T>(self.object, self.desc, offset, value);
}

void StructObject::construct(uevr::API::UStruct* def) {
// TODO: Call constructor? Not important for now
if (def->is_a(uevr::API::UScriptStruct::static_class())) {
Expand Down Expand Up @@ -53,22 +63,18 @@ namespace lua::datatypes {
"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);
},
"write_qword", [](StructObject& self, size_t offset, uint64_t value) {
size_t size = 0;
if (self.desc->is_a(uevr::API::UScriptStruct::static_class())) {
auto script_struct = static_cast<uevr::API::UScriptStruct*>(self.desc);

size = script_struct->get_struct_size();
} else {
size = self.desc->get_properties_size();
}

if (offset + sizeof(uint64_t) > size) {
throw sol::error("Offset out of bounds");
}

*(uint64_t*)((uintptr_t)self.object + offset) = value;
},
"write_qword", &write_t_structobject<uint64_t>,
"write_dword", &write_t_structobject<uint32_t>,
"write_word", &write_t_structobject<uint16_t>,
"write_byte", &write_t_structobject<uint8_t>,
"write_float", &write_t_structobject<float>,
"write_double", &write_t_structobject<double>,
"read_qword", &read_t_structobject<uint64_t>,
"read_dword", &read_t_structobject<uint32_t>,
"read_word", &read_t_structobject<uint16_t>,
"read_byte", &read_t_structobject<uint8_t>,
"read_float", &read_t_structobject<float>,
"read_double", &read_t_structobject<double>,
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);
Expand Down

0 comments on commit d75a7a3

Please sign in to comment.