Skip to content

Commit

Permalink
SDK/Lua: Add RETypeDefinition::get_types_inheriting_from_this()
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 2, 2025
1 parent 8225c1e commit 26aa7c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
22 changes: 21 additions & 1 deletion shared/sdk/RETypeDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ bool RETypeDefinition::has_fieldptr_offset() const {
#endif
}

bool RETypeDefinition::is_a(sdk::RETypeDefinition* other) const {
bool RETypeDefinition::is_a(const sdk::RETypeDefinition* other) const {
if (other == nullptr) {
return false;
}
Expand Down Expand Up @@ -1133,4 +1133,24 @@ uint32_t RETypeDefinition::get_flags() const {
bool RETypeDefinition::should_pass_by_pointer() const {
return !is_value_type() || (get_valuetype_size() > sizeof(void*) || (!is_primitive() && !is_enum()));
}

std::vector<RETypeDefinition*> RETypeDefinition::get_types_inherting_from_this() const {
std::vector<RETypeDefinition*> out{};
auto tdb = RETypeDB::get();

// Maybe optimize by making a dependency graph?
for (auto i = 0; i < tdb->numTypes; ++i) {
auto type = tdb->get_type(i);

if (type == nullptr) {
continue;
}

if (type->is_a(this)) {
out.push_back(type);
}
}

return out;
}
} // namespace sdk
4 changes: 3 additions & 1 deletion shared/sdk/RETypeDefinition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ struct RETypeDefinition : public sdk::RETypeDefinition_ {
uint32_t get_index() const;
int32_t get_fieldptr_offset() const;
bool has_fieldptr_offset() const;
bool is_a(sdk::RETypeDefinition* other) const;
bool is_a(const sdk::RETypeDefinition* other) const;
bool is_a(std::string_view other) const;

::via::clr::VMObjType get_vm_obj_type() const;
Expand Down Expand Up @@ -396,6 +396,8 @@ struct RETypeDefinition : public sdk::RETypeDefinition_ {
::REObjectInfo* get_managed_vt() const;
uint32_t get_flags() const;

std::vector<RETypeDefinition*> get_types_inherting_from_this() const;

private:
void set_vm_obj_type(::via::clr::VMObjType type); // for REFramework shenanigans only!
};
Expand Down
4 changes: 3 additions & 1 deletion src/mods/bindings/Sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,9 @@ void bindings::open_sdk(ScriptState* s) {

return false;
},
"create_instance", &::sdk::RETypeDefinition::create_instance_full);
"create_instance", &::sdk::RETypeDefinition::create_instance_full,
"get_types_inheriting_from_this", &::sdk::RETypeDefinition::get_types_inherting_from_this
);

auto method_call = [](sdk::REMethodDefinition* def, sol::object obj, sol::variadic_args va) {
auto l = va.lua_state();
Expand Down

0 comments on commit 26aa7c6

Please sign in to comment.