Adds const version of rev_bar method

Adds a const overload for the `rev_bar` virtual method.
This allows calling the method on const instances of the reverse engineered class.
This commit is contained in:
2025-11-01 13:28:34 +03:00
parent 9bb94ee33b
commit ade281cdd2
2 changed files with 21 additions and 4 deletions

View File

@@ -23,15 +23,27 @@ namespace omath::rev_eng
return *reinterpret_cast<Type*>(reinterpret_cast<std::uintptr_t>(this) + offset); return *reinterpret_cast<Type*>(reinterpret_cast<std::uintptr_t>(this) + offset);
} }
template<std::size_t id, class ReturnType> template<std::size_t id, class ReturnType, class... Args>
ReturnType call_virtual_method(auto... arg_list) ReturnType call_virtual_method(Args&&... arg_list)
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
using VirtualMethodType = ReturnType(__thiscall*)(void*, decltype(arg_list)...); using VirtualMethodType = ReturnType(__thiscall*)(void*, decltype(arg_list)...);
#else #else
using VirtualMethodType = ReturnType(*)(void*, decltype(arg_list)...); using VirtualMethodType = ReturnType (*)(void*, decltype(arg_list)...);
#endif #endif
return (*reinterpret_cast<VirtualMethodType**>(this))[id](this, arg_list...); return (*reinterpret_cast<VirtualMethodType**>(this))[id](this, std::forward<Args>(arg_list)...);
}
template<std::size_t id, class ReturnType,class... Args>
ReturnType call_virtual_method(Args&&... arg_list) const
{
using This = std::remove_cv_t<std::remove_pointer_t<decltype(this)>>;
#ifdef _MSC_VER
using VirtualMethodType = ReturnType(__thiscall*)(const void*, decltype(arg_list)...);
#else
using VirtualMethodType = ReturnType (*)(void*, decltype(arg_list)...);
#endif
return (*reinterpret_cast<VirtualMethodType**>(const_cast<This*>(this)))[id](
this, std::forward<Args>(arg_list)...);
} }
}; };
} // namespace omath::rev_eng } // namespace omath::rev_eng

View File

@@ -34,6 +34,10 @@ public:
{ {
return call_virtual_method<1, int>(); return call_virtual_method<1, int>();
} }
[[nodiscard]] int rev_bar_const() const
{
return call_virtual_method<1, int>();
}
}; };
@@ -47,4 +51,5 @@ TEST(unit_test_reverse_enineering, read_test)
EXPECT_EQ(player_original.bar(), player_reversed->rev_bar()); EXPECT_EQ(player_original.bar(), player_reversed->rev_bar());
EXPECT_EQ(player_original.foo(), player_reversed->rev_foo()); EXPECT_EQ(player_original.foo(), player_reversed->rev_foo());
EXPECT_EQ(player_original.bar(), player_reversed->rev_bar_const());
} }