sourcehook/test/test3.cpp
author Pavol Marko <pavolmarko@gmail.com>
Mon May 07 18:35:59 2007 +0000 (2007-05-07)
changeset 324 257f0837a885
parent 294 0d87bc8606f8
child 328 3a04ef79edf1
permissions -rw-r--r--
Added hook ids
Added VP hooks and VP Hook test
New macro syntax (SH_ADD_HOOK + SH_STATIC/SH_MEMBER)
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST3
     6 // Tests with inheritance / thisptroffsets
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_Func1_Called, void*);			// param1: This pointer
    15 	MAKE_STATE_1(State_Func2_Called, void*);			// param1: This pointer
    16 	MAKE_STATE_1(State_Func3_Called, void*);			// param1: This pointer
    17 
    18 	MAKE_STATE_1(State_Func1H_Called, void*);
    19 	MAKE_STATE_1(State_Func2H_Called, void*);
    20 	MAKE_STATE_1(State_Func3H_Called, void*);
    21 
    22 	class Base1
    23 	{
    24 		int a;
    25 	public:
    26 		virtual void Func1()
    27 		{
    28 			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    29 		}
    30 	};
    31 	class Base2
    32 	{
    33 		int b;
    34 	public:
    35 		virtual void Func2()
    36 		{
    37 			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
    38 		}
    39 	};
    40 	class Derived : public Base1, public Base2
    41 	{
    42 		int c;
    43 	public:
    44 		virtual void Func3()
    45 		{
    46 			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    47 		}
    48 	};
    49 
    50 	class DerivedDerived : public Derived { };
    51 
    52 	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    53 	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    54 	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    55 	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    56 	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    57 
    58 	void Handler_Func1()
    59 	{
    60 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    61 	}
    62 	void Handler_Func2()
    63 	{
    64 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    65 	}
    66 	void Handler_Func3()
    67 	{
    68 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    69 	}
    70 }
    71 
    72 bool TestThisPtrOffs(std::string &error)
    73 {
    74 	GET_SHPTR(g_SHPtr);
    75 	g_PLID = 1337;
    76 
    77 	DerivedDerived inst;
    78 	Derived *pD = &inst;
    79 	Base1 *pB1 = pD;
    80 	Base2 *pB2 = pD;
    81 
    82 	// It should be:
    83 	// pB1 = pD
    84 	// pB2 > pB1
    85 
    86 	// 1)
    87 	//  Get a callclass for pD
    88 	//  Verify whether the this pointers are correct
    89 	//  Also call them normally to make sure that we aren't messing it up ;)
    90 	SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
    91 
    92 	SH_CALL(pD_CC, &Derived::Func1)();
    93 	SH_CALL(pD_CC, &Derived::Func2)();
    94 	SH_CALL(pD_CC, &Derived::Func3)();
    95 	pD->Func1();
    96 	pD->Func2();
    97 	pD->Func3();
    98 
    99 	CHECK_STATES((&g_States,
   100 		new State_Func1_Called(pB1),
   101 		new State_Func2_Called(pB2),
   102 		new State_Func3_Called(pD),
   103 		new State_Func1_Called(pB1),
   104 		new State_Func2_Called(pB2),
   105 		new State_Func3_Called(pD),
   106 		NULL), "Part 1");
   107 
   108 	SH_CALL(pD_CC, &Base1::Func1)();
   109 	SH_CALL(pD_CC, &Base2::Func2)();
   110 
   111 	CHECK_STATES((&g_States,
   112 		new State_Func1_Called(pB1),
   113 		new State_Func2_Called(pB2),
   114 		NULL), "Part 1.1");
   115 
   116 	// 2)
   117 	//   Get callclasses for the other ones and verify it as well
   118 
   119 	SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
   120 	SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
   121 
   122 	SH_CALL(pB1_CC, &Base1::Func1)();
   123 	SH_CALL(pB2_CC, &Base2::Func2)();
   124 
   125 	CHECK_STATES((&g_States,
   126 		new State_Func1_Called(pB1),
   127 		new State_Func2_Called(pB2),
   128 		NULL), "Part 2");
   129 
   130 
   131 	// 3) Add hooks on them (referring to them through pD1 / Derived)
   132 	//   Check whether the hooks are called with the correct this pointer
   133 
   134 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   135 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   136 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   137 
   138 	pD->Func1();
   139 	pD->Func2();
   140 	pD->Func3();
   141 	pB1->Func1();
   142 	pB2->Func2();
   143 
   144 	// The handlers should always be called with the pointer to Derived
   145 	CHECK_STATES((&g_States,
   146 		new State_Func1H_Called(pD),
   147 		new State_Func1_Called(pB1),
   148 		new State_Func2H_Called(pD),
   149 		new State_Func2_Called(pB2),
   150 		new State_Func3H_Called(pD),
   151 		new State_Func3_Called(pD),
   152 		new State_Func1H_Called(pD),
   153 		new State_Func1_Called(pB1),
   154 		new State_Func2H_Called(pD),
   155 		new State_Func2_Called(pB2),
   156 		NULL), "Part 3");
   157 
   158 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   159 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   160 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   161 
   162 	// 4)
   163 	//   Now add the hooks on Base1 and Base2 and check again
   164 
   165 	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   166 	SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   167 	SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   168 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   169 
   170 	pD->Func1();
   171 	pD->Func2();
   172 	pD->Func3();
   173 	pB1->Func1();
   174 	pB2->Func2();
   175 
   176 	// This time, the handlers for Func1 should be called with pB1 and the handlers
   177 	// for Func2 should be called with pB2
   178 	CHECK_STATES((&g_States,
   179 		new State_Func1H_Called(pB1),
   180 		new State_Func1_Called(pB1),
   181 		new State_Func2H_Called(pB2),
   182 		new State_Func2_Called(pB2),
   183 		new State_Func3H_Called(pD),
   184 		new State_Func3_Called(pD),
   185 		new State_Func1H_Called(pB1),
   186 		new State_Func1_Called(pB1),
   187 		new State_Func2H_Called(pB2),
   188 		new State_Func2_Called(pB2),
   189 		NULL), "Part 4");
   190 
   191 	SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   192 	SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   193 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   194 
   195 
   196 	// 5)
   197 	//   Add some hooks, and use callclasses
   198 
   199 	// 5.1) First off, add all of them on pD
   200 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   201 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   202 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   203 
   204 	pD->Func1();
   205 	pD->Func2();
   206 	pD->Func3();
   207 
   208 	CHECK_STATES((&g_States,
   209 		new State_Func1H_Called(pD),
   210 		new State_Func1_Called(pB1),
   211 		new State_Func2H_Called(pD),
   212 		new State_Func2_Called(pB2),
   213 		new State_Func3H_Called(pD),
   214 		new State_Func3_Called(pD),
   215 		NULL), "Part 5.1");
   216 
   217 	SH_CALL(pD_CC, &Derived::Func1)();
   218 	SH_CALL(pD_CC, &Derived::Func2)();
   219 	SH_CALL(pD_CC, &Derived::Func3)();
   220 	SH_CALL(pB1_CC, &Base1::Func1)();
   221 	SH_CALL(pB2_CC, &Base2::Func2)();
   222 
   223 	CHECK_STATES((&g_States,
   224 		new State_Func1_Called(pB1),
   225 		new State_Func2_Called(pB2),
   226 		new State_Func3_Called(pD),
   227 		new State_Func1_Called(pB1),
   228 		new State_Func2_Called(pB2),
   229 		NULL), "Part 5.2");
   230 
   231 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   232 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   233 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   234 
   235 	SH_RELEASE_CALLCLASS(pB1_CC);
   236 	SH_RELEASE_CALLCLASS(pB2_CC);
   237 	SH_RELEASE_CALLCLASS(pD_CC);
   238 
   239 	return true;
   240 }