Skip to content

Commit

Permalink
feat: add write_thunk_call variant, simplify code (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
powerof3 authored Sep 21, 2023
1 parent 89da8fb commit 5ec0449
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions CommonLibSF/include/SFSE/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,36 @@ namespace SFSE
namespace stl
{
template <class T, std::size_t Size = 5>
constexpr void write_thunk_call() noexcept
constexpr void write_thunk_call(const std::uintptr_t a_address) noexcept
{
SFSE::AllocTrampoline(14);
auto& trampoline = SFSE::GetTrampoline();
T::func = trampoline.write_call<Size>(T::address, T::Thunk);
T::func = trampoline.write_call<Size>(a_address, T::Thunk);
}

template <class T, std::size_t Size = 5>
constexpr void write_thunk_call() noexcept
{
write_thunk_call<T, Size>(T::address);
}

template <class To, class From>
constexpr void write_vfunc() noexcept
template <class T>
constexpr void write_vfunc(const REL::Offset a_offset) noexcept
{
REL::Relocation<std::uintptr_t> vtbl{ To::VTABLE[0] };
From::func = vtbl.write_vfunc(From::idx, From::Thunk);
REL::Relocation<std::uintptr_t> vtbl{ a_offset };
T::func = vtbl.write_vfunc(T::idx, T::Thunk);
}

template <class To, class From>
constexpr void write_vfunc(const std::size_t a_vtable_idx) noexcept
{
REL::Relocation<std::uintptr_t> vtbl{ To::VTABLE[a_vtable_idx] };
From::func = vtbl.write_vfunc(From::idx, From::Thunk);
write_vfunc<From>(To::VTABLE[a_vtable_idx]);
}

template <class T>
constexpr void write_vfunc(const REL::Offset a_offset) noexcept
template <class To, class From>
constexpr void write_vfunc() noexcept
{
REL::Relocation<std::uintptr_t> vtbl{ a_offset };
T::func = vtbl.write_vfunc(T::idx, T::Thunk);
write_vfunc<From>(To::VTABLE[0]);
}
} // namespace stl
} // namespace SFSE

0 comments on commit 5ec0449

Please sign in to comment.