forked from danielkrupinski/Osiris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVmtSwap.h
43 lines (35 loc) · 1.09 KB
/
VmtSwap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <Platform/CallingConventions.h>
#include <Platform/PlatformSpecific.h>
class VmtSwap {
public:
void init(void* base) noexcept;
void restore() noexcept
{
*reinterpret_cast<std::uintptr_t**>(base) = oldVmt;
}
template<typename T>
void hookAt(std::size_t index, T fun) const noexcept
{
newVmt[index + dynamicCastInfoLength] = reinterpret_cast<std::uintptr_t>(fun);
}
template<typename T, std::size_t Idx, typename ...Args>
constexpr auto getOriginal(Args...) const noexcept
{
return reinterpret_cast<T(THISCALL_CONV*)(void*, Args...)>(oldVmt[Idx]);
}
template<typename T, std::size_t Idx, typename ...Args>
constexpr auto callOriginal(Args... args) const noexcept
{
return getOriginal<T, Idx>(args...)(base, args...);
}
private:
static constexpr auto dynamicCastInfoLength = WIN32_LINUX(1, 2);
void* base = nullptr;
std::uintptr_t* oldVmt = nullptr;
std::unique_ptr<std::uintptr_t[]> newVmt;
std::size_t length = 0;
};