Removed "trust the prototype" model which could lead to problems. Instead, every CVfnPtr now has a list of hook managers.
-> Removed CHookManagerContainer
Also removed listcat (unnecessary since "V2" interface)
2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
8 // :TODO: test more extensively
13 SourceHook::ISourceHook *g_SHPtr;
14 SourceHook::Plugin g_PLID;
16 MAKE_STATE_1(State_Func1_Called, void*); // param1: This pointer
17 MAKE_STATE_2(State_Func2_Called, void*, int); // param1: This pointer; param2: parameter
18 MAKE_STATE_1(State_Func3_Called, void*); // param1: This pointer
19 MAKE_STATE_2(State_Func4_Called, void*, int); // param1: This pointer; param2: parameter
20 MAKE_STATE_1(State_Func5_Called, void*); // param1: This pointer
22 MAKE_STATE_1(State_Func1H_Called, void*);
23 MAKE_STATE_2(State_Func2H_Called, void*, int);
24 MAKE_STATE_1(State_Func3H_Called, void*);
25 MAKE_STATE_2(State_Func4H_Called, void*, int);
27 MAKE_STATE_1(State_Return, short);
34 ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
36 virtual void Func2(int x)
38 ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this), x));
43 ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
46 virtual short Func4(int x)
48 ADD_STATE(State_Func4_Called(reinterpret_cast<void*>(this), x));
53 ADD_STATE(State_Func5_Called(reinterpret_cast<void*>(this)));
57 SH_DECL_HOOK0_void(TheWall, Func1, SH_NOATTRIB, 0);
58 SH_DECL_HOOK1_void(TheWall, Func2, SH_NOATTRIB, 0, int);
59 SH_DECL_HOOK0(TheWall, Func3, SH_NOATTRIB, 0, short);
60 SH_DECL_HOOK1(TheWall, Func4, SH_NOATTRIB, 0, short, int);
62 SH_DECL_MANUALHOOK0_void(TheWall_Func1, 0, 0, 0);
63 SH_DECL_MANUALHOOK1_void(TheWall_Func2, 1, 0, 0, int);
64 SH_DECL_MANUALHOOK0(TheWall_Func3, 2, 0, 0, short);
65 SH_DECL_MANUALHOOK1(TheWall_Func4, 3, 0, 0, short, int);
67 typedef void (SourceHook::EmptyClass::*MFP_Func1)();
68 typedef void (SourceHook::EmptyClass::*MFP_Func2)(int);
69 typedef short (SourceHook::EmptyClass::*MFP_Func3)();
70 typedef short (SourceHook::EmptyClass::*MFP_Func4)(int);
74 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
76 void Handler_Func2(int x)
78 ADD_STATE(State_Func2H_Called(META_IFACEPTR(void), x));
79 RETURN_META_MNEWPARAMS(MRES_IGNORED, TheWall_Func2, (0x87654321));
83 ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
86 short Handler_Func4(int x)
88 ADD_STATE(State_Func4H_Called(META_IFACEPTR(void), x));
90 // newparams (recall) using the manual hook declaration!
91 RETURN_META_VALUE_MNEWPARAMS(MRES_IGNORED, 0, TheWall_Func4, (0x12345678));
98 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
99 RETURN_META(MRES_SUPERCEDE);
103 TheWall *MyInstanceFactory()
109 bool TestManual(std::string &error)
114 TheWall *p = MyInstanceFactory();
115 CAutoPtrDestruction<TheWall> apd(p);
117 SourceHook::ManualCallClass *cc = SH_GET_MCALLCLASS(p, sizeof(void*));
120 // Call each function
123 ADD_STATE(State_Return(p->Func3()));
124 ADD_STATE(State_Return(p->Func4(400)));
126 CHECK_STATES((&g_States,
127 new State_Func1_Called(p),
128 new State_Func2_Called(p, 200),
129 new State_Func3_Called(p),
131 new State_Func4_Called(p, 400),
136 // Now call each function through the manual call class, using the hook decl and manually
137 SH_MCALL(cc, TheWall_Func1)();
138 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
139 SH_MCALL(cc, TheWall_Func2)(200);
140 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
141 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
142 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
143 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
144 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
146 CHECK_STATES((&g_States,
147 new State_Func1_Called(p),
148 new State_Func1_Called(p),
149 new State_Func2_Called(p, 200),
150 new State_Func2_Called(p, 200),
151 new State_Func3_Called(p),
153 new State_Func3_Called(p),
155 new State_Func4_Called(p, 400),
157 new State_Func4_Called(p, 400),
162 // Hook each function normally, call them
163 SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
164 SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
165 SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
166 SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
170 ADD_STATE(State_Return(p->Func3()));
171 ADD_STATE(State_Return(p->Func4(400)));
173 CHECK_STATES((&g_States,
174 new State_Func1H_Called(p),
175 new State_Func1_Called(p),
176 new State_Func2H_Called(p, 200),
177 new State_Func2_Called(p, 0x87654321),
178 new State_Func3H_Called(p),
179 new State_Func3_Called(p),
181 new State_Func4H_Called(p, 400),
182 new State_Func4_Called(p, 0x12345678),
186 // Call them through the mcallclass
188 // Now call each function through the manual call class, using the hook decl and manually
189 SH_MCALL(cc, TheWall_Func1)();
190 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
191 SH_MCALL(cc, TheWall_Func2)(200);
192 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
193 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
194 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
195 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
196 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
198 CHECK_STATES((&g_States,
199 new State_Func1_Called(p),
200 new State_Func1_Called(p),
201 new State_Func2_Called(p, 200),
202 new State_Func2_Called(p, 200),
203 new State_Func3_Called(p),
205 new State_Func3_Called(p),
207 new State_Func4_Called(p, 400),
209 new State_Func4_Called(p, 400),
214 SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
215 SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
216 SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
217 SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
220 // Hook each function manually, call them
222 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
223 SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
224 SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
225 SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
229 ADD_STATE(State_Return(p->Func3()));
230 ADD_STATE(State_Return(p->Func4(400)));
232 CHECK_STATES((&g_States,
233 new State_Func1H_Called(p),
234 new State_Func1_Called(p),
235 new State_Func2H_Called(p, 200),
236 new State_Func2_Called(p, 0x87654321),
237 new State_Func3H_Called(p),
238 new State_Func3_Called(p),
240 new State_Func4H_Called(p, 400),
241 new State_Func4_Called(p, 0x12345678),
245 // Now through the mcallclass
247 // Call them through the mcallclass
249 // Now call each function through the manual call class, using the hook decl and manually
250 SH_MCALL(cc, TheWall_Func1)();
251 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
252 SH_MCALL(cc, TheWall_Func2)(200);
253 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
254 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
255 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
256 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
257 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
259 CHECK_STATES((&g_States,
260 new State_Func1_Called(p),
261 new State_Func1_Called(p),
262 new State_Func2_Called(p, 200),
263 new State_Func2_Called(p, 200),
264 new State_Func3_Called(p),
266 new State_Func3_Called(p),
268 new State_Func4_Called(p, 400),
270 new State_Func4_Called(p, 400),
275 SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
276 SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
277 SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
278 SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
281 // Hook each function manually, then normally, call, unhook
283 AnotherBrick handler_inst;
286 // 1) tests sh_add_manualhook
287 // 2) in my tests, the proto of the manual hook was not equal to the proto of the auto hook
288 // (because there are no attribs for manual hooks).
289 // sourcehook.cpp did a !strcmp(..), so it assigned a new hook manager even though there
290 // already was one for this vfnptr. This hook manager stored the pointer of the original
291 // hook manager's hookfunc as the orig pointer - everything seemingly worked.
292 // The problem with this is that returning MRES_SUPERCEDE (as AnotherBrick::Handler_Func1
293 // does) will supercede the second hook func from being called - thus bypassing the call
294 // of the auto hook here.
295 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
296 SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
297 SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
298 SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
300 SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
301 SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
302 SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
303 SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
307 ADD_STATE(State_Return(p->Func3()));
308 ADD_STATE(State_Return(p->Func4(400)));
310 CHECK_STATES((&g_States,
311 new State_Func1H_Called(p),
312 new State_Func1H_Called(p),
313 //new State_Func1_Called(p),
314 new State_Func2H_Called(p, 200),
315 new State_Func2H_Called(p, 0x87654321),
316 new State_Func2_Called(p, 0x87654321),
317 new State_Func3H_Called(p),
318 new State_Func3H_Called(p),
319 new State_Func3_Called(p),
321 new State_Func4H_Called(p, 400),
322 new State_Func4H_Called(p, 0x12345678),
323 new State_Func4_Called(p, 0x12345678),
327 SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
328 SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
329 SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
330 SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
332 SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
333 SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
334 SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
335 SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
337 // 5) Reconfigure TheWall_Func1 to actually hook Func5:
338 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
339 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
340 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
344 CHECK_STATES((&g_States,
345 new State_Func1H_Called(p),
346 new State_Func5_Called(p),
349 // 6) Test auto-remove on reconfig
350 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 0, 0, 0);
355 CHECK_STATES((&g_States,
356 new State_Func1_Called(p),
357 new State_Func5_Called(p),