|
| 1 | +/******************************************************************* |
| 2 | +* @file platform.hpp |
| 3 | +* @brief Platform based macros. |
| 4 | +* @author SAR Team |
| 5 | +*********************************************************************/ |
| 6 | + |
| 7 | +#pragma once |
| 8 | +#ifdef _WIN32 |
| 9 | +# include "minhook/include/MinHook.h" |
| 10 | +#endif |
| 11 | + |
| 12 | +#ifdef _WIN32 |
| 13 | +# define __rescalll __thiscall |
| 14 | +#else |
| 15 | +# define __rescalll __attribute__((__cdecl__)) |
| 16 | +# define __stdcall |
| 17 | +#endif |
| 18 | + |
| 19 | +#ifdef _WIN32 |
| 20 | +# define MODULE_EXTENSION ".dll" |
| 21 | +# define __rescall __thiscall |
| 22 | +# define DLL_EXPORT extern "C" __declspec(dllexport) |
| 23 | +# define SEEK_DIR_CUR std::ios_base::_Seekdir::_Seekcur |
| 24 | +# define STDCALL_NAME(base, param_bytes) "_" base "@" #param_bytes |
| 25 | + |
| 26 | +# define DECL_DETOUR(name, ...) \ |
| 27 | + using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 28 | + static _##name name; \ |
| 29 | + static int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 30 | +# define DECL_DETOUR_T(type, name, ...) \ |
| 31 | + using _##name = type(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 32 | + static _##name name; \ |
| 33 | + static type __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 34 | +# define DECL_DETOUR_B(name, ...) \ |
| 35 | + using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 36 | + static _##name name; \ |
| 37 | + static _##name name##Base; \ |
| 38 | + static int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 39 | + |
| 40 | +# define DETOUR(name, ...) \ |
| 41 | + int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 42 | +# define DETOUR_T(type, name, ...) \ |
| 43 | + type __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 44 | +# define DETOUR_B(name, ...) \ |
| 45 | + int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__) |
| 46 | + |
| 47 | +namespace { |
| 48 | + bool mhInitialized = false; |
| 49 | +} |
| 50 | +# define MH_HOOK(name, target) \ |
| 51 | + if (!mhInitialized) { \ |
| 52 | + MH_Initialize(); \ |
| 53 | + mhInitialized = true; \ |
| 54 | + } \ |
| 55 | + name = reinterpret_cast<decltype(name)>(target); \ |
| 56 | + MH_CreateHook(reinterpret_cast<LPVOID>(target), name##_Hook, reinterpret_cast<LPVOID *>(&name##_Trampoline)); \ |
| 57 | + MH_EnableHook(reinterpret_cast<LPVOID>(target)); |
| 58 | +# define MH_HOOK_MID(name, target) \ |
| 59 | + if (!mhInitialized) { \ |
| 60 | + MH_Initialize(); \ |
| 61 | + mhInitialized = true; \ |
| 62 | + } \ |
| 63 | + name = target; \ |
| 64 | + MH_CreateHook(reinterpret_cast<LPVOID>(name), name##_Hook, reinterpret_cast<LPVOID *>(&name##_Trampoline)); \ |
| 65 | + MH_EnableHook(reinterpret_cast<LPVOID>(name)); |
| 66 | +# define MH_UNHOOK(name) \ |
| 67 | + if (name) { \ |
| 68 | + MH_DisableHook(reinterpret_cast<LPVOID>(name)); \ |
| 69 | + MH_RemoveHook(reinterpret_cast<LPVOID>(name)); \ |
| 70 | + } |
| 71 | +# define DECL_DETOUR_MID_MH(name) \ |
| 72 | + static uintptr_t name; \ |
| 73 | + static uintptr_t name##_Trampoline; \ |
| 74 | + static void name##_Hook() |
| 75 | +# define DECL_DETOUR_MH(name, ...) \ |
| 76 | + using _##name = int(__thiscall *)(void *thisptr, __VA_ARGS__); \ |
| 77 | + static _##name name; \ |
| 78 | + static _##name name##_Trampoline; \ |
| 79 | + static int __fastcall name##_Hook(void *thisptr, int edx, __VA_ARGS__) |
| 80 | + |
| 81 | +# define DETOUR_MID_MH(name) \ |
| 82 | + __declspec(naked) void name##_Hook() |
| 83 | +# define DETOUR_MH(name, ...) \ |
| 84 | + int __fastcall name##_Hook(void *thisptr, int edx, __VA_ARGS__) |
| 85 | +#else |
| 86 | +# define MODULE_EXTENSION ".so" |
| 87 | +# define __rescall __attribute__((__cdecl__)) |
| 88 | +# define __cdecl __attribute__((__cdecl__)) |
| 89 | +# define __fastcall __attribute__((__fastcall__)) |
| 90 | +# define DLL_EXPORT extern "C" __attribute__((visibility("default"))) |
| 91 | +# define SEEK_DIR_CUR std::ios_base::cur |
| 92 | +# define STDCALL_NAME(base, param_bytes) base |
| 93 | + |
| 94 | +# define DECL_DETOUR(name, ...) \ |
| 95 | + using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 96 | + static _##name name; \ |
| 97 | + static int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 98 | +# define DECL_DETOUR_T(type, name, ...) \ |
| 99 | + using _##name = type(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 100 | + static _##name name; \ |
| 101 | + static type __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 102 | +# define DECL_DETOUR_B(name, ...) \ |
| 103 | + using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \ |
| 104 | + static _##name name; \ |
| 105 | + static _##name name##Base; \ |
| 106 | + static int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 107 | + |
| 108 | +# define DETOUR(name, ...) \ |
| 109 | + int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 110 | +# define DETOUR_T(type, name, ...) \ |
| 111 | + type __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 112 | +# define DETOUR_B(name, ...) \ |
| 113 | + int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__) |
| 114 | +#endif |
0 commit comments