2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
7 // Test modifying parameters from hook handlers
12 SourceHook::ISourceHook *g_SHPtr;
13 SourceHook::Plugin g_PLID;
15 MAKE_STATE_1(State_Func1, int);
16 MAKE_STATE_1(State_H1_Func1, int);
17 MAKE_STATE_1(State_H2_Func1, int);
18 MAKE_STATE_2(State_HP_Func1, int, void*);
20 MAKE_STATE_1(State_Func2, int);
21 MAKE_STATE_1(State_H1_Func2, int);
22 MAKE_STATE_1(State_H2_Func2, int);
23 MAKE_STATE_2(State_HP_Func2, int, int);
25 MAKE_STATE_2(State_Func22, int, int);
26 MAKE_STATE_2(State_H1_Func22, int, int);
27 MAKE_STATE_2(State_HP1_Func22, int, int);
28 MAKE_STATE_2(State_HP2_Func22, int, int);
32 virtual void Func1(int a)
34 ADD_STATE(State_Func1(a));
37 virtual int Func2(int a)
39 ADD_STATE(State_Func2(a));
44 virtual int Func2(int a, int b)
46 ADD_STATE(State_Func22(a, b));
51 // GCC's optimizer is too good. I had to add this in order to make it execute a virtual table lookup!
52 struct Whatever : Test
56 void Handler1_Func1(int a)
58 ADD_STATE(State_H1_Func1(a));
59 RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (5));
61 void Handler2_Func1(int a)
63 ADD_STATE(State_H2_Func1(a));
64 RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (a - 5));
66 void HandlerPost_Func1(int a)
68 ADD_STATE(State_HP_Func1(a, META_IFACEPTR(void)));
72 int Handler1_Func2(int a)
74 ADD_STATE(State_H1_Func2(a));
75 // Pfeeehhh, ugly, I know, but I had to test it :)
76 RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 500,
77 static_cast<int (Test::*)(int)>(&Test::Func2), (a - 10));
80 int Handler2_Func2(int a)
82 ADD_STATE(State_H2_Func2(a));
83 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, 0,
84 static_cast<int (Test::*)(int)>(&Test::Func2), (a - 10));
87 int HandlerPost_Func2(int a)
89 ADD_STATE(State_HP_Func2(a, META_RESULT_ORIG_RET(int)));
90 RETURN_META_VALUE(MRES_IGNORED, 0);
93 int Handler1_Func22(int a, int b)
95 ADD_STATE(State_H1_Func22(a, b));
96 RETURN_META_VALUE(MRES_IGNORED, 0);
99 int HandlerPost1_Func22(int a, int b)
101 ADD_STATE(State_HP1_Func22(a, b));
102 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, 0, static_cast<int (Test::*)(int, int)>(&Test::Func2), (1, 2));
105 int HandlerPost1A_Func22(int a, int b)
107 ADD_STATE(State_HP1_Func22(a, b));
108 RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 0, static_cast<int (Test::*)(int, int)>(&Test::Func2), (1, 2));
111 int HandlerPost2_Func22(int a, int b)
113 ADD_STATE(State_HP2_Func22(a, b));
114 RETURN_META_VALUE(MRES_IGNORED, 0);
117 SH_DECL_HOOK1_void(Test, Func1, SH_NOATTRIB, 0, int);
118 SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, int, int);
119 SH_DECL_HOOK2(Test, Func2, SH_NOATTRIB, 1, int, int, int);
122 bool TestRecall(std::string &error)
130 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler1_Func1), false);
131 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
132 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(HandlerPost_Func1), true);
136 CHECK_STATES((&g_States,
137 new State_H1_Func1(77),
138 new State_H2_Func1(5),
140 new State_HP_Func1(0, ptr),
143 SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(Handler1_Func1), false);
144 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
145 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
146 SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
150 CHECK_STATES((&g_States,
151 new State_H2_Func1(77),
152 new State_H2_Func1(72),
153 new State_H2_Func1(67),
154 new State_H2_Func1(62),
156 new State_HP_Func1(57, ptr),
159 SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
160 SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(HandlerPost_Func1), true);
164 CHECK_STATES((&g_States,
170 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func2), false);
171 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost_Func2), true);
173 int a = ptr->Func2(77);
174 CHECK_STATES((&g_States,
175 new State_H1_Func2(77),
177 new State_HP_Func2(67, 1000), // 1000 because it's the ORIG_RET
180 CHECK_COND(a == 500, "Part 4.1");
182 // Func2, with other handler
183 SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func2), false);
184 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler2_Func2), false);
187 CHECK_STATES((&g_States,
188 new State_H2_Func2(77),
190 new State_HP_Func2(67, 1000), // 1000 because it's the ORIG_RET
193 CHECK_COND(a == 1000, "Part 4.2.1"); // Should return 1000 as well.
195 // Func22 -> post recalls
199 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func22), false);
200 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1A_Func22), true);
201 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
203 a = ptr->Func2(10, 11);
204 CHECK_STATES((&g_States,
205 new State_H1_Func22(10, 11),
206 new State_Func22(10, 11),
207 new State_HP1_Func22(10, 11),
208 new State_HP2_Func22(1, 2),
211 CHECK_COND(a == 0, "Part 5.1");
214 SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1A_Func22), true);
215 SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
217 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1_Func22), true);
218 SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
220 a = ptr->Func2(10, 11);
221 CHECK_STATES((&g_States,
222 new State_H1_Func22(10, 11),
223 new State_Func22(10, 11),
224 new State_HP1_Func22(10, 11),
225 new State_HP2_Func22(1, 2),
228 CHECK_COND(a == 0xDEADFC, "Part 5.1");