Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member indexing for value types #487

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/mods/bindings/Sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ namespace api::sdk {
std::vector<void*>& build_args(sol::variadic_args va);
sol::object parse_data(lua_State* l, void* data, ::sdk::RETypeDefinition* data_type, bool from_method);
sol::object get_native_field(sol::object obj, ::sdk::RETypeDefinition* ty, const char* name);
sol::object get_native_field_from_field(sol::object obj, ::sdk::RETypeDefinition* ty, ::sdk::REField* field);
sol::object get_field_or_method(sol::object obj, const char* name);
void set_native_field(lua_State* l, sol::object obj, ::sdk::RETypeDefinition* ty, const char* name, sol::object value);

Expand Down Expand Up @@ -357,6 +358,31 @@ struct ValueType {

::api::sdk::set_native_field(l, sol::make_object(l, (void*)address()), type, name, value);
}

sol::object index(sol::this_state s, const char* name) {
if (type == nullptr) {
return sol::make_object(s, sol::nil);
}


auto field = type->get_field(name);
if (field != nullptr) {
return api::sdk::get_native_field_from_field(sol::make_object(s, (void*)address()), type, field);
}
auto method = type->get_method(name);
if (method != nullptr) {
return sol::make_object(s, method);
}

return sol::make_object(s, sol::nil);
}

void new_index(sol::this_state s, const char* name, sol::object assign) {
if (type == nullptr) {
return;
}
return api::sdk::set_native_field(s, sol::make_object(s, (void*)address()), type, name, assign);
};
};

struct MemoryView {
Expand Down Expand Up @@ -1565,6 +1591,8 @@ void bindings::open_sdk(ScriptState* s) {

lua.new_usertype<api::sdk::ValueType>("ValueType",
sol::meta_function::construct, sol::constructors<api::sdk::ValueType(sdk::RETypeDefinition*)>(),
sol::meta_function::index, &api::sdk::ValueType::index,
sol::meta_function::new_index, &api::sdk::ValueType::new_index,
"data", &api::sdk::ValueType::data,
"type", &api::sdk::ValueType::type,
"write_byte", &api::sdk::ValueType::write_memory<uint8_t>,
Expand Down