Skip to content

Commit ebd2887

Browse files
committedDec 11, 2013
Macros to make everything pretty
1 parent f9bb659 commit ebd2887

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed
 

‎CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cmake_minimum_required(VERSION 2.8)
12
project (StarboundAPI)
23

34
if(WIN32)
@@ -6,6 +7,10 @@ if(WIN32)
67
)
78
endif()
89

10+
if(MSVC)
11+
MESSAGE("TURN OFF INCREMENTAL LINKING OR BAD SHIT WILL HAPPEN")
12+
endif()
13+
914
add_library(SBAPIDll SHARED
1015
${OS_FILES}
1116
main.cpp

‎Hooking.h

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifdef _WIN32
1111
#define ASM_START _asm {
1212
#define ASM_END }
13+
#define ASM_MACRO_SS _asm
1314
#define EXPORT _declspec(dllexport)
1415
#define FASTCALL __fastcall
1516
#define NAKED _declspec(naked)
@@ -25,6 +26,29 @@
2526
//addr is temporary, will be replaced with symbol/signature later
2627
#define HOOKMEMBERFUNCTION(funcName, addr) void hook_funcName();
2728

29+
#define HOOKDEST_DECL(classname) friend class classname##_init; \
30+
public: \
31+
static std::unordered_map<std::string, void*> m_selfFuncAddrs; \
32+
static std::unordered_map<std::string, void*> m_funcAddrs;
33+
#define HOOKDEST_DEF(classname) std::unordered_map<std::string, void*> PluginTitle::m_selfFuncAddrs; \
34+
std::unordered_map<std::string, void*> PluginTitle::m_funcAddrs;
35+
36+
#define HOOKTABLE_START(classname) template<typename T> \
37+
struct classname##_hookinit { \
38+
classname##_hookinit() {
39+
40+
#define SETUPHOOKEDFUNC(tgtClassname, funcName) T::m_selfFuncAddrs[ #funcName ] = &(void*&)T::##funcName; \
41+
g_hookedClasses[ #tgtClassname ]->hookMemberFunc( #funcName , &(void*&)T::##funcName);
42+
43+
#define HOOKTABLE_END(classname) }}; \
44+
static classname##_hookinit<##classname##> classname##_init;
45+
46+
#define CALL_FUNC(funcName, addrVarName) addrVarName = m_funcAddrs[ #funcName ]; \
47+
ASM_START \
48+
ASM_MACRO_SS mov ecx, this \
49+
ASM_MACRO_SS call addrVarName \
50+
ASM_END
51+
2852
class ClassHooker;
2953
extern std::unordered_map<std::string, ClassHooker*> g_hookedClasses;
3054

‎Title.cpp

+16-20
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,29 @@ struct TitleStruct
2222
//plugin
2323

2424
//figuring out how to do the plugin side
25-
class PluginTitle : public TitleStruct
25+
struct PluginTitle : public TitleStruct
2626
{
27-
friend class PluginTitle_hookinit;
2827
void update(ClassHooker *ch)
2928
{
3029
ch->unhookMemberFunc("update");
31-
void* dest = (void*)0x00419500;
32-
ASM_START
33-
mov ecx, this
34-
call dest
35-
ASM_END
30+
void* funcAddr;
31+
CALL_FUNC(update, funcAddr);
3632
ch->hookMemberFunc("update", m_selfFuncAddrs["update"]);
3733
return;
3834
}
39-
public:
40-
static std::unordered_map<std::string, void*> m_selfFuncAddrs;
35+
HOOKDEST_DECL(PluginTitle)
4136
};
42-
std::unordered_map<std::string, void*> PluginTitle::m_selfFuncAddrs;
37+
HOOKDEST_DEF(PluginTitle)
4338

44-
class PluginTitle_hookinit
45-
{
46-
public:
47-
PluginTitle_hookinit()
48-
{
49-
PluginTitle::m_selfFuncAddrs["update"] = &(void*&)PluginTitle::update;
50-
g_hookedClasses["Title"]->hookMemberFunc("update", &(void*&)PluginTitle::update);
51-
}
52-
};
39+
HOOKTABLE_START(PluginTitle)
40+
SETUPHOOKEDFUNC(Title, update)
41+
HOOKTABLE_END(PluginTitle)
5342

54-
static PluginTitle_hookinit init;
43+
/*template<typename T> struct PluginTitle_hookinit {
44+
PluginTitle_hookinit()
45+
{
46+
T::m_selfFuncAddrs[ "update" ] = &(void*&)T::update;
47+
g_hookedClasses[ "Title" ]->hookMemberFunc( "update" , &(void*&)T::update);
48+
}
49+
};
50+
static PluginTitle_hookinit<PluginTitle> PluginTitle_init;*/

0 commit comments

Comments
 (0)
Please sign in to comment.