From bbe7765de70ee900be8a764ca5780fc6ee2cc8b1 Mon Sep 17 00:00:00 2001 From: praydog Date: Wed, 26 Feb 2025 12:34:25 -0800 Subject: [PATCH] SDK: Fix get_underlying_type on new RE Engine versions --- shared/sdk/RETypeDefinition.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/shared/sdk/RETypeDefinition.cpp b/shared/sdk/RETypeDefinition.cpp index fb99be2b..3ee2f016 100644 --- a/shared/sdk/RETypeDefinition.cpp +++ b/shared/sdk/RETypeDefinition.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include @@ -350,9 +352,6 @@ sdk::RETypeDefinition* RETypeDefinition::get_underlying_type() const { } #if TDB_VER > 49 - // get the underlying type of the enum - // and then hash the name of the type instead - static auto get_underlying_type_method = this->get_method("GetUnderlyingType"); const auto runtime_type = this->get_runtime_type(); // dont forget to do this, passing nullptr into GetUnderlyingType causes System.ArgumentNullException @@ -362,11 +361,24 @@ sdk::RETypeDefinition* RETypeDefinition::get_underlying_type() const { return nullptr; } + // get the underlying type of the enum + // and then hash the name of the type instead + static const auto system_runtime_type_type = sdk::find_type_definition("System.RuntimeType"); + static const auto old_get_underlying_type_method = system_runtime_type_type != nullptr ? system_runtime_type_type->get_method("GetUnderlyingType") : nullptr; + static const auto new_underlying_type_method = system_runtime_type_type != nullptr ? system_runtime_type_type->get_method("GetEnumUnderlyingType") : nullptr; + const auto get_underlying_type_method = old_get_underlying_type_method != nullptr ? old_get_underlying_type_method : new_underlying_type_method; + + if (get_underlying_type_method == nullptr) { + std::unique_lock _{ g_underlying_mtx }; + g_underlying_types[this] = nullptr; + SPDLOG_WARN("[RETypeDefinition] Failed to find GetUnderlyingType method for {}", this->get_full_name()); + return nullptr; + } + const auto underlying_type = get_underlying_type_method->call<::REManagedObject*>(sdk::get_thread_context(), runtime_type); if (underlying_type != nullptr) { - static auto system_runtime_type_type = sdk::find_type_definition("System.RuntimeType"); - static auto get_name_method = system_runtime_type_type->get_method("get_FullName"); + static const auto get_name_method = system_runtime_type_type->get_method("get_FullName"); const auto full_name = get_name_method->call<::REManagedObject*>(sdk::get_thread_context(), underlying_type);