1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core-legacy/sourcehook/test/testvphooks.cpp Sun Jan 10 02:46:18 2010 -0500
1.3 @@ -0,0 +1,260 @@
1.4 +#include <string>
1.5 +#include "sourcehook.h"
1.6 +#include "sourcehook_test.h"
1.7 +#include "testevents.h"
1.8 +
1.9 +// TEST VP HOOKS
1.10 +// Test vfnptr-wide hooks
1.11 +
1.12 +namespace
1.13 +{
1.14 + StateList g_States;
1.15 + SourceHook::ISourceHook *g_SHPtr;
1.16 + SourceHook::Plugin g_PLID;
1.17 +
1.18 + class IBase;
1.19 +
1.20 + MAKE_STATE_1(State_D1_Func1, IBase *);
1.21 + MAKE_STATE_1(State_D2_Func1, IBase *);
1.22 + MAKE_STATE_1(State_Func1_Pre, IBase *);
1.23 + MAKE_STATE_1(State_Func1_Post, IBase *);
1.24 +
1.25 + MAKE_STATE_1(State_D1_Func2, IBase *);
1.26 + MAKE_STATE_1(State_D2_Func2, IBase *);
1.27 + MAKE_STATE_1(State_Func2_Pre, IBase *);
1.28 + MAKE_STATE_1(State_Func2_Post, IBase *);
1.29 +
1.30 + MAKE_STATE_2(State_D1_Func3, IBase *, int);
1.31 + MAKE_STATE_2(State_D2_Func3, IBase *, int);
1.32 + MAKE_STATE_2(State_Func3_Pre, IBase *, int);
1.33 + MAKE_STATE_2(State_Func3_Post, IBase *, int);
1.34 +
1.35 + class IBase
1.36 + {
1.37 + public:
1.38 + virtual void Func1() = 0;
1.39 + virtual void Func2() = 0;
1.40 + virtual void Func3(int x) = 0;
1.41 + };
1.42 +
1.43 + class CDerived1 : public IBase
1.44 + {
1.45 + public:
1.46 + virtual void Func1()
1.47 + {
1.48 + ADD_STATE(State_D1_Func1(this));
1.49 + }
1.50 + virtual void Func2()
1.51 + {
1.52 + ADD_STATE(State_D1_Func2(this));
1.53 + }
1.54 + virtual void Func3(int x)
1.55 + {
1.56 + ADD_STATE(State_D1_Func3(this, x));
1.57 + }
1.58 + };
1.59 +
1.60 + class CDerived2 : public IBase
1.61 + {
1.62 + public:
1.63 + virtual void Func1()
1.64 + {
1.65 + ADD_STATE(State_D2_Func1(this));
1.66 + }
1.67 + virtual void Func2()
1.68 + {
1.69 + ADD_STATE(State_D2_Func2(this));
1.70 + }
1.71 + virtual void Func3(int x)
1.72 + {
1.73 + ADD_STATE(State_D2_Func3(this, x));
1.74 + }
1.75 + };
1.76 +
1.77 + void Handler_Func1_Pre()
1.78 + {
1.79 + ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
1.80 + }
1.81 + void Handler_Func1_Post()
1.82 + {
1.83 + ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
1.84 + }
1.85 + int g_F2_Pre_HookToRemove = 0;
1.86 + void Handler_Func2_Pre()
1.87 + {
1.88 + ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
1.89 + SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
1.90 + }
1.91 + void Handler_Func2_Post()
1.92 + {
1.93 + ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
1.94 + }
1.95 +
1.96 +
1.97 + void Handler_Func3_Pre(int x)
1.98 + {
1.99 + ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
1.100 +
1.101 + RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
1.102 + }
1.103 + void Handler_Func3_Post(int x)
1.104 + {
1.105 + ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
1.106 + }
1.107 +
1.108 + SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
1.109 + SH_DECL_HOOK0_void(IBase, Func2, SH_NOATTRIB, 0);
1.110 + SH_DECL_HOOK1_void(IBase, Func3, SH_NOATTRIB, 0, int);
1.111 +
1.112 + SH_DECL_MANUALHOOK1_void(IBase_Func3_Manual, 2, 0, 0, int);
1.113 +}
1.114 +
1.115 +bool TestVPHooks(std::string &error)
1.116 +{
1.117 + GET_SHPTR(g_SHPtr);
1.118 + g_PLID = 1337;
1.119 +
1.120 + CDerived1 d1i1;
1.121 + CDerived1 d1i2;
1.122 + CDerived2 d2i1;
1.123 +
1.124 + IBase *p_d1i1 = &d1i1;
1.125 + IBase *p_d1i2 = &d1i2;
1.126 + IBase *p_d2i1 = &d2i1;
1.127 +
1.128 + int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.129 +
1.130 + p_d1i1->Func1();
1.131 + p_d1i2->Func1();
1.132 + p_d2i1->Func1();
1.133 +
1.134 + CHECK_STATES((&g_States,
1.135 + new State_Func1_Pre(p_d1i1),
1.136 + new State_D1_Func1(p_d1i1),
1.137 +
1.138 + new State_Func1_Pre(p_d1i2),
1.139 + new State_D1_Func1(p_d1i2),
1.140 +
1.141 + new State_D2_Func1(p_d2i1),
1.142 + NULL), "Part 1");
1.143 +
1.144 + SH_REMOVE_HOOK_ID(hook1);
1.145 +
1.146 + p_d1i1->Func1();
1.147 + p_d1i2->Func1();
1.148 + p_d2i1->Func1();
1.149 +
1.150 + CHECK_STATES((&g_States,
1.151 + new State_D1_Func1(p_d1i1),
1.152 +
1.153 + new State_D1_Func1(p_d1i2),
1.154 +
1.155 + new State_D2_Func1(p_d2i1),
1.156 + NULL), "Part 2");
1.157 +
1.158 +
1.159 + // Normal hook, then vp hook
1.160 +
1.161 + int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.162 + hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.163 +
1.164 + p_d1i1->Func1();
1.165 + p_d1i2->Func1();
1.166 + p_d2i1->Func1();
1.167 +
1.168 + CHECK_STATES((&g_States,
1.169 + new State_Func1_Pre(p_d1i1),
1.170 + new State_Func1_Pre(p_d1i1),
1.171 + new State_D1_Func1(p_d1i1),
1.172 +
1.173 + new State_Func1_Pre(p_d1i2),
1.174 + new State_D1_Func1(p_d1i2),
1.175 +
1.176 + new State_D2_Func1(p_d2i1),
1.177 + NULL), "Part 3");
1.178 +
1.179 + SH_REMOVE_HOOK_ID(hook1);
1.180 +
1.181 + p_d1i1->Func1();
1.182 + p_d1i2->Func1();
1.183 + p_d2i1->Func1();
1.184 +
1.185 + CHECK_STATES((&g_States,
1.186 + new State_Func1_Pre(p_d1i1),
1.187 + new State_D1_Func1(p_d1i1),
1.188 +
1.189 + new State_D1_Func1(p_d1i2),
1.190 +
1.191 + new State_D2_Func1(p_d2i1),
1.192 + NULL), "Part 4");
1.193 +
1.194 + SH_REMOVE_HOOK_ID(hook2);
1.195 +
1.196 + p_d1i1->Func1();
1.197 + p_d1i2->Func1();
1.198 + p_d2i1->Func1();
1.199 +
1.200 + CHECK_STATES((&g_States,
1.201 + new State_D1_Func1(p_d1i1),
1.202 +
1.203 + new State_D1_Func1(p_d1i2),
1.204 +
1.205 + new State_D2_Func1(p_d2i1),
1.206 + NULL), "Part 5");
1.207 +
1.208 + // Test this:
1.209 + // Normal hook AND vp hook on Func2
1.210 + // Func2's pre handler removes the VP hook.
1.211 +
1.212 + hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.213 + hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.214 +
1.215 + g_F2_Pre_HookToRemove = hook1;
1.216 + p_d1i1->Func2();
1.217 + p_d1i1->Func2();
1.218 +
1.219 + CHECK_STATES((&g_States,
1.220 + new State_Func2_Pre(p_d1i1),
1.221 + new State_D1_Func2(p_d1i1),
1.222 +
1.223 + new State_Func2_Pre(p_d1i1),
1.224 + new State_D1_Func2(p_d1i1),
1.225 +
1.226 + NULL), "Part 6");
1.227 +
1.228 + SH_REMOVE_HOOK_ID(hook1);
1.229 +
1.230 + // Hook function 3:
1.231 + // Using manualhook, VP
1.232 + hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.233 +
1.234 + // Normally, VP
1.235 + hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.236 +
1.237 + // Normally, no VP
1.238 + int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.239 +
1.240 + p_d1i1->Func3(1);
1.241 +
1.242 + CHECK_STATES((&g_States,
1.243 + new State_Func3_Pre(p_d1i1, 1), // manual vp hook
1.244 + new State_Func3_Pre(p_d1i1, 2), // normal vp hook
1.245 + new State_Func3_Pre(p_d1i1, 3), // normal non-vp hook
1.246 +
1.247 + new State_D1_Func3(p_d1i1, 4), // function
1.248 +
1.249 + NULL), "Part 7.1");
1.250 +
1.251 + p_d1i2->Func3(1);
1.252 +
1.253 + CHECK_STATES((&g_States,
1.254 + new State_Func3_Pre(p_d1i2, 1), // manual vp hook
1.255 + new State_Func3_Pre(p_d1i2, 2), // normal vp hook
1.256 +
1.257 + new State_D1_Func3(p_d1i2, 3), // function
1.258 +
1.259 + NULL), "Part 7.2");
1.260 +
1.261 + return true;
1.262 +}
1.263 +