sourcehook/test/testvphooks.cpp
author Christian Hammacher <bmasterc@gmail.com>
Wed Oct 10 21:48:44 2007 +0000 (2007-10-10)
changeset 411 7c1526f514b3
parent 380 f9e21a2ac558
child 420 9c0613cb6f50
permissions -rw-r--r--
updating installer for OrangeBox compatibility
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 
     6 // TEST VP HOOKS
     7 // Test vfnptr-wide hooks
     8 
     9 namespace
    10 {
    11 	StateList g_States;
    12 	SourceHook::ISourceHook *g_SHPtr;
    13 	SourceHook::Plugin g_PLID;
    14 
    15 	class IBase;
    16 
    17 	MAKE_STATE_1(State_D1_Func1, IBase *);
    18 	MAKE_STATE_1(State_D2_Func1, IBase *);
    19 	MAKE_STATE_1(State_Func1_Pre, IBase *);
    20 	MAKE_STATE_1(State_Func1_Post, IBase *);
    21 
    22 	MAKE_STATE_1(State_D1_Func2, IBase *);
    23 	MAKE_STATE_1(State_D2_Func2, IBase *);
    24 	MAKE_STATE_1(State_Func2_Pre, IBase *);
    25 	MAKE_STATE_1(State_Func2_Post, IBase *);
    26 
    27 	MAKE_STATE_2(State_D1_Func3, IBase *, int);
    28 	MAKE_STATE_2(State_D2_Func3, IBase *, int);
    29 	MAKE_STATE_2(State_Func3_Pre, IBase *, int);
    30 	MAKE_STATE_2(State_Func3_Post, IBase *, int);
    31 
    32 	class IBase
    33 	{
    34 	public:
    35 		virtual void Func1() = 0;
    36 		virtual void Func2() = 0;
    37 		virtual void Func3(int x) = 0;
    38 	};
    39 
    40 	class CDerived1 : public IBase
    41 	{
    42 	public:
    43 		virtual void Func1()
    44 		{
    45 			ADD_STATE(State_D1_Func1(this));
    46 		}
    47 		virtual void Func2()
    48 		{
    49 			ADD_STATE(State_D1_Func2(this));
    50 		}
    51 		virtual void Func3(int x)
    52 		{
    53 			ADD_STATE(State_D1_Func3(this, x));
    54 		}
    55 	};
    56 
    57 	class CDerived2 : public IBase
    58 	{
    59 	public:
    60 		virtual void Func1()
    61 		{
    62 			ADD_STATE(State_D2_Func1(this));
    63 		}
    64 		virtual void Func2()
    65 		{
    66 			ADD_STATE(State_D2_Func2(this));
    67 		}
    68 		virtual void Func3(int x)
    69 		{
    70 			ADD_STATE(State_D2_Func3(this, x));
    71 		}
    72 	};
    73 
    74 	void Handler_Func1_Pre()
    75 	{
    76 		ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
    77 	}
    78 	void Handler_Func1_Post()
    79 	{
    80 		ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
    81 	}
    82 	int g_F2_Pre_HookToRemove = 0;
    83 	void Handler_Func2_Pre()
    84 	{
    85 		ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
    86 		SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
    87 	}
    88 	void Handler_Func2_Post()
    89 	{
    90 		ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
    91 	}
    92 
    93 
    94 	void Handler_Func3_Pre(int x)
    95 	{
    96 		ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
    97 
    98 		RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
    99 	}
   100 	void Handler_Func3_Post(int x)
   101 	{
   102 		ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
   103 	}
   104 
   105 	SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
   106 	SH_DECL_HOOK0_void(IBase, Func2, SH_NOATTRIB, 0);
   107 	SH_DECL_HOOK1_void(IBase, Func3, SH_NOATTRIB, 0, int);
   108 
   109 	SH_DECL_MANUALHOOK1_void(IBase_Func3_Manual, 2, 0, 0, int);
   110 }
   111 
   112 bool TestVPHooks(std::string &error)
   113 {
   114 	GET_SHPTR(g_SHPtr);
   115 	g_PLID = 1337;
   116 
   117 	CDerived1 d1i1;
   118 	CDerived1 d1i2;
   119 	CDerived2 d2i1;
   120 
   121 	IBase *p_d1i1 = &d1i1;
   122 	IBase *p_d1i2 = &d1i2;
   123 	IBase *p_d2i1 = &d2i1;
   124 	
   125 	int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   126 
   127 	p_d1i1->Func1();
   128 	p_d1i2->Func1();
   129 	p_d2i1->Func1();
   130 
   131 	CHECK_STATES((&g_States,
   132 		new State_Func1_Pre(p_d1i1),
   133 		new State_D1_Func1(p_d1i1),
   134 		
   135 		new State_Func1_Pre(p_d1i2),
   136 		new State_D1_Func1(p_d1i2),
   137 
   138 		new State_D2_Func1(p_d2i1),
   139 		NULL), "Part 1");
   140 
   141 	SH_REMOVE_HOOK_ID(hook1);
   142 
   143 	p_d1i1->Func1();
   144 	p_d1i2->Func1();
   145 	p_d2i1->Func1();
   146 
   147 	CHECK_STATES((&g_States,
   148 		new State_D1_Func1(p_d1i1),
   149 
   150 		new State_D1_Func1(p_d1i2),
   151 
   152 		new State_D2_Func1(p_d2i1),
   153 		NULL), "Part 2");
   154 
   155 
   156 
   157 	// Normal hook, then vp hook
   158 
   159 	int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   160 	hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   161 
   162 	p_d1i1->Func1();
   163 	p_d1i2->Func1();
   164 	p_d2i1->Func1();
   165 
   166 	CHECK_STATES((&g_States,
   167 		new State_Func1_Pre(p_d1i1),
   168 		new State_Func1_Pre(p_d1i1),
   169 		new State_D1_Func1(p_d1i1),
   170 
   171 		new State_Func1_Pre(p_d1i2),
   172 		new State_D1_Func1(p_d1i2),
   173 
   174 		new State_D2_Func1(p_d2i1),
   175 		NULL), "Part 3");
   176 
   177 	SH_REMOVE_HOOK_ID(hook1);
   178 
   179 	p_d1i1->Func1();
   180 	p_d1i2->Func1();
   181 	p_d2i1->Func1();
   182 
   183 	CHECK_STATES((&g_States,
   184 		new State_Func1_Pre(p_d1i1),
   185 		new State_D1_Func1(p_d1i1),
   186 
   187 		new State_D1_Func1(p_d1i2),
   188 
   189 		new State_D2_Func1(p_d2i1),
   190 		NULL), "Part 4");
   191 
   192 	SH_REMOVE_HOOK_ID(hook2);
   193 
   194 	p_d1i1->Func1();
   195 	p_d1i2->Func1();
   196 	p_d2i1->Func1();
   197 
   198 	CHECK_STATES((&g_States,
   199 		new State_D1_Func1(p_d1i1),
   200 
   201 		new State_D1_Func1(p_d1i2),
   202 
   203 		new State_D2_Func1(p_d2i1),
   204 		NULL), "Part 5");
   205 
   206 	// Test this:
   207 	// Normal hook AND vp hook on Func2
   208 	// Func2's pre handler removes the VP hook.
   209 
   210 	hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   211 	hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   212 
   213 	g_F2_Pre_HookToRemove = hook1;
   214 	p_d1i1->Func2();
   215 	p_d1i1->Func2();
   216 
   217 	CHECK_STATES((&g_States,
   218 		new State_Func2_Pre(p_d1i1),
   219 		new State_D1_Func2(p_d1i1),
   220 
   221 		new State_Func2_Pre(p_d1i1),
   222 		new State_D1_Func2(p_d1i1),
   223 
   224 		NULL), "Part 6");
   225 
   226 	SH_REMOVE_HOOK_ID(hook1);
   227 
   228 	// Hook function 3:
   229 	//  Using manualhook, VP
   230 	hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   231 
   232 	//  Normally, VP
   233 	hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   234 
   235 	// Normally, no VP
   236 	int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   237 
   238 	p_d1i1->Func3(1);
   239 
   240 	CHECK_STATES((&g_States,
   241 		new State_Func3_Pre(p_d1i1, 1),		// manual vp hook
   242 		new State_Func3_Pre(p_d1i1, 2),		// normal vp hook
   243 		new State_Func3_Pre(p_d1i1, 3),		// normal non-vp hook
   244 
   245 		new State_D1_Func3(p_d1i1, 4),		// function
   246 
   247 		NULL), "Part 7.1");
   248 
   249 	p_d1i2->Func3(1);
   250 
   251 	CHECK_STATES((&g_States,
   252 		new State_Func3_Pre(p_d1i2, 1),		// manual vp hook
   253 		new State_Func3_Pre(p_d1i2, 2),		// normal vp hook
   254 
   255 		new State_D1_Func3(p_d1i2, 3),		// function
   256 
   257 		NULL), "Part 7.2");
   258 
   259 	return true;
   260 }