Skip to content

Commit

Permalink
Lua: Add StructObject.new(UStruct*)
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 26, 2024
1 parent 525b785 commit e20ba57
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lua-api/lib/include/datatypes/StructObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
namespace lua::datatypes {
struct StructObject {
StructObject(void* obj, uevr::API::UStruct* def) : object{ obj }, desc{ def } {}
StructObject(uevr::API::UStruct* def); // Allocates a new structure given a definition
StructObject(uevr::API::UObject* obj);
~StructObject();

void construct(uevr::API::UStruct* def);

void* object{ nullptr };
uevr::API::UStruct* desc{ nullptr };

std::vector<uint8_t> created_object{}; // Only used when the object is created by second constructor
};

void bind_struct_object(sol::state_view& lua);
Expand Down
42 changes: 41 additions & 1 deletion lua-api/lib/src/datatypes/StructObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@
#include <datatypes/StructObject.hpp>

namespace lua::datatypes {
void StructObject::construct(uevr::API::UStruct* def) {
// TODO: Call constructor? Not important for now
if (def->is_a(uevr::API::UScriptStruct::static_class())) {
auto script_struct = static_cast<uevr::API::UScriptStruct*>(def);

created_object.resize(script_struct->get_struct_size());
memset(created_object.data(), 0, created_object.size());
} else {
created_object.resize(def->get_properties_size());
}

object = created_object.data();
desc = def;
}

StructObject::StructObject(uevr::API::UStruct* def) {
if (def == nullptr) {
throw sol::error("Cannot create a StructObject from a nullptr UStruct");
}

construct(def);
}

StructObject::StructObject(uevr::API::UObject* obj) {
if (obj == nullptr) {
throw sol::error("Cannot create a StructObject from a nullptr UObject");
}

if (!obj->is_a(uevr::API::UStruct::static_class())) {
throw sol::error("Cannot create a StructObject from a UObject that is not a UStruct");
}

construct(static_cast<uevr::API::UStruct*>(obj));
}

StructObject::~StructObject() {

}

void bind_struct_object(sol::state_view& lua) {
lua.new_usertype<StructObject>("StructObject",
"get_address", [](StructObject& self) { return (uintptr_t)self.object; },
Expand All @@ -30,7 +69,8 @@ namespace lua::datatypes {

const auto name = ::utility::widen(index_obj.as<std::string>());
lua::utility::set_property(s, self->object, self->desc, name, value);
}
},
sol::meta_function::construct, sol::constructors<StructObject(uevr::API::UStruct*), StructObject(uevr::API::UObject*)>()
);
}
}

0 comments on commit e20ba57

Please sign in to comment.