core/sourcehook/test/testrefret.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
child 733 2365240861a2
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 #include "testevents.h"
     2 #include "sourcehook_test.h"
     3 
     4 // Tests support for functions which return references
     5 
     6 namespace
     7 {
     8 	StateList g_States;
     9 	SourceHook::ISourceHook *g_SHPtr;
    10 	SourceHook::Plugin g_PLID;
    11 
    12 	MAKE_STATE_1(State_Func1_Pre1, int*);			// p1: the ref Func1_Pre1 is going to return
    13 	MAKE_STATE_3(State_Func1_Pre2, META_RES, int*, int*);	// p1: current status
    14 															// p2: override ret
    15 															// p3: what this handler is going to supercede with
    16 	MAKE_STATE_1(State_Func1, int*);				// p1: the ref Func1 is going to return
    17 	MAKE_STATE_2(State_Func1_Post1, int*, int*);	// p1: orig_ret; p2: override_ret
    18 	MAKE_STATE_1(State_Func1_Post2, int*);			// p1: what it's going to return
    19 	MAKE_STATE_1(State_Func1_Ret, int*);			// p1: the ref it returned
    20 
    21 
    22 	MAKE_STATE_2(State_Func2_Pre1, int, const int*);				// p1: func's p1; p2: what it's going to ret
    23 	MAKE_STATE_2(State_Func2, int, const int*);						// p1: func's p1; p2: what it's going to ret
    24 	MAKE_STATE_3(State_Func2_Post1, int, const int*, const int*);	// p1: func's p1; p2: orig ret; p3: override ret
    25 	MAKE_STATE_1(State_Func2_Ret, const int*);						// p1: ret
    26 
    27 	int g_Var;
    28 
    29 	class Test
    30 	{
    31 	public:
    32 		int m_Var1;
    33 		int m_Var2;
    34 
    35 		Test() : m_Var1(87)
    36 		{
    37 		}
    38 
    39 		virtual int& Func1()
    40 		{
    41 			ADD_STATE(State_Func1(&m_Var1));
    42 			return m_Var1;
    43 		}
    44 		virtual const int& Func2(int p1)
    45 		{
    46 			ADD_STATE(State_Func2(p1, &m_Var2));
    47 			m_Var2 = p1;
    48 			return m_Var2;
    49 		}
    50 	};
    51 
    52 	class CHook
    53 	{
    54 	public:
    55 		int m_Var;
    56 		virtual int& Func1_Pre1()
    57 		{
    58 			ADD_STATE(State_Func1_Pre1(&m_Var));
    59 			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    60 		}
    61 		virtual int &Func1_Pre2()
    62 		{
    63 			int &overrideret = META_RESULT_OVERRIDE_RET(int&);
    64 			overrideret = 1337;
    65 			ADD_STATE(State_Func1_Pre2(META_RESULT_STATUS, &overrideret, &g_Var));
    66 			RETURN_META_VALUE(MRES_SUPERCEDE, g_Var);
    67 		}
    68 		virtual int& Func1_Post1()
    69 		{
    70 			ADD_STATE(State_Func1_Post1(&META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    71 			RETURN_META_NOREF(MRES_IGNORED, int&);
    72 		}
    73 		virtual int& Func1_Post2()
    74 		{
    75 			ADD_STATE(State_Func1_Post2(&m_Var));
    76 			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    77 		}
    78 
    79 		virtual const int& Func2_Pre1(int p1)
    80 		{
    81 			ADD_STATE(State_Func2_Pre1(p1, &m_Var));
    82 			RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
    83 		}
    84 
    85 		virtual const int& Func2_Post1(int p1)
    86 		{
    87 			ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    88 			RETURN_META_NOREF(MRES_IGNORED, const int&);
    89 		}
    90 	};
    91 
    92 	SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
    93 	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
    94 
    95 	Test *MyTestFactory()
    96 	{
    97 		return new Test;
    98 	}
    99 }
   100 
   101 bool TestRefRet(std::string &error)
   102 {
   103 	GET_SHPTR(g_SHPtr);
   104 	g_PLID = 1;
   105 
   106 	Test *pTest = MyTestFactory();
   107 	CAutoPtrDestruction<Test> apd(pTest);
   108 	CHook hook;
   109 
   110 	int &ret1 = pTest->Func1();
   111 	ADD_STATE(State_Func1_Ret(&ret1));
   112 
   113 	CHECK_STATES((&g_States,
   114 		new State_Func1(&pTest->m_Var1),
   115 		new State_Func1_Ret(&pTest->m_Var1),
   116 		NULL), "Part 1");
   117 
   118 	// Now add Func1_Pre1, which supercedes and returns hook.m_Var
   119 	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
   120 
   121 	int &ret2 = pTest->Func1();
   122 	ADD_STATE(State_Func1_Ret(&ret2));
   123 	
   124 	CHECK_STATES((&g_States,
   125 		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   126 		new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   127 		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   128 		NULL), "Part 2");
   129 
   130 	// Now add Func1_Post1, which only reports orig ret and override ret
   131 	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
   132 
   133 	int &ret3 = pTest->Func1();
   134 	ADD_STATE(State_Func1_Ret(&ret3));
   135 	
   136 	CHECK_STATES((&g_States,
   137 		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   138 		new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   139 		new State_Func1_Post1(&pTest->m_Var1, &hook.m_Var),	// origret(=p1) is what it wanted to
   140 															// return, overrideret(=p2) is pre1's var
   141 		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   142 		NULL), "Part 3");
   143 
   144 	// Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
   145 	// and add Func1_Post2, which overrides and returns hook.m_Var again.
   146 
   147 	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
   148 	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
   149 
   150 	int &ret4 = pTest->Func1();
   151 	ADD_STATE(State_Func1_Ret(&ret4));
   152 
   153 	CHECK_STATES((&g_States,
   154 		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   155 		new State_Func1_Pre2(MRES_OVERRIDE,			// current status
   156 							&hook.m_Var,			// override ret (which it set to 1337)
   157 							&g_Var),				// what it's going to ret, AND supercede with
   158 
   159 		new State_Func1_Post1(&g_Var, &g_Var),	// origret(=p1) is what pre2 superceded with,
   160 												//  so  overrideret(=p2) has to be the same
   161 		new State_Func1_Post2(&hook.m_Var),			// post2 is going to override with hook.m_Var again
   162 		new State_Func1_Ret(&hook.m_Var),		// ==>>> hook.m_Var is returned
   163 		NULL), "Part 4");
   164 
   165 	CHECK_COND(hook.m_Var == 1337, "Part 4.1");
   166 	
   167 	// Through a callclass
   168 	SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(pTest);
   169 	int &ret5 = SH_CALL(cc1, &Test::Func1)();
   170 	ADD_STATE(State_Func1_Ret(&ret5));
   171 
   172 	CHECK_STATES((&g_States,
   173 		new State_Func1(&pTest->m_Var1),
   174 		new State_Func1_Ret(&pTest->m_Var1),
   175 		NULL), "Part 5");
   176 
   177 	SH_RELEASE_CALLCLASS(cc1);
   178 
   179 	////////////////////////////////////////////////////////////////////////////////////////
   180 	// Func2 tests
   181 	const int &ret21 = pTest->Func2(500);
   182 	ADD_STATE(State_Func2_Ret(&ret21));
   183 
   184 	CHECK_STATES((&g_States,
   185 		new State_Func2(500, &pTest->m_Var2),
   186 		new State_Func2_Ret(&pTest->m_Var2),
   187 		NULL), "Part 6");
   188 
   189 	SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Pre1), false);
   190 	SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
   191 
   192 	const int &ret22 = pTest->Func2(500);
   193 	ADD_STATE(State_Func2_Ret(&ret22));
   194 
   195 	CHECK_STATES((&g_States,
   196 		new State_Func2_Pre1(500, &hook.m_Var),		// p1 was 500; it's going to override with hook.m_Var; and also change p1 to 1337
   197 		new State_Func2(1337, &pTest->m_Var2),		// p1 was 1337; it's going to ret pTest->m_Var2
   198 		new State_Func2_Post1(1337,					// p1 was 1337
   199 			&pTest->m_Var2,							// orig ret was pTest->m_Var2
   200 			&hook.m_Var),							// override ret was hook.m_Var
   201 		new State_Func2_Ret(&hook.m_Var),			// really returned hook.m_Var
   202 		NULL), "Part 7");
   203 	
   204    	return true;
   205 }