This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
executable file
·81 lines (64 loc) · 2.08 KB
/
common.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include <cstring>
typedef void* (*InstantiateInterfaceFn) ();
struct InterfaceReg
{
InstantiateInterfaceFn m_CreateFn;
const char *m_pName;
InterfaceReg *m_pNext;
};
inline void**& getvtable(void* inst, size_t offset = 0)
{
return *reinterpret_cast<void***>((size_t)inst + offset);
}
inline const void** getvtable(const void* inst, size_t offset = 0)
{
return *reinterpret_cast<const void***>((size_t)inst + offset);
}
template<typename Fn>
inline Fn getvfunc(void* inst, size_t index, size_t offset = 0)
{
return reinterpret_cast<Fn>(getvtable(inst, offset)[index]);
}
template <typename interface>
interface* GetInterface(const char* filename, const char* version, bool exact = false)
{
void* library = dlopen(filename, RTLD_NOLOAD | RTLD_NOW);
if (!library)
return nullptr;
void* interfaces_sym = dlsym(library, "s_pInterfaceRegs");
if (!interfaces_sym)
{
dlclose(library);
return nullptr;
}
dlclose(library);
InterfaceReg* interfaces = *reinterpret_cast<InterfaceReg**>(interfaces_sym);
InterfaceReg* cur_interface;
for (cur_interface = interfaces; cur_interface; cur_interface = cur_interface->m_pNext)
{
if (exact)
{
if (strcmp(cur_interface->m_pName, version) == 0)
return reinterpret_cast<interface*>(cur_interface->m_CreateFn());
}
else
{
if (strstr(cur_interface->m_pName, version) && strlen(cur_interface->m_pName) - 3 == strlen(version))
return reinterpret_cast<interface*>(cur_interface->m_CreateFn());
}
}
return nullptr;
}
inline uintptr_t GetAbsoluteAddress(uintptr_t instruction_ptr, int offset, int size)
{
return instruction_ptr + *reinterpret_cast<uint32_t*>(instruction_ptr + offset) + size;
};
template <typename T>
T GetSymbolAddress(const char* filename, const char* symbol)
{
void* handle = dlopen(filename, RTLD_NOW);
T result = reinterpret_cast<T>(dlsym(handle, symbol));
dlclose(handle);
return result;
};