core/sourcehook/test/testrecall.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
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 <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 
     6 // TESTRECALL
     7 // Test modifying parameters from hook handlers
     8 
     9 namespace
    10 {
    11 	StateList g_States;
    12 	SourceHook::ISourceHook *g_SHPtr;
    13 	SourceHook::Plugin g_PLID;
    14 
    15 	MAKE_STATE_1(State_Func1, int);
    16 	MAKE_STATE_1(State_H1_Func1, int);
    17 	MAKE_STATE_1(State_H2_Func1, int);
    18 	MAKE_STATE_2(State_HP_Func1, int, void*);
    19 
    20 	MAKE_STATE_1(State_Func2, int);
    21 	MAKE_STATE_1(State_H1_Func2, int);
    22 	MAKE_STATE_1(State_H2_Func2, int);
    23 	MAKE_STATE_2(State_HP_Func2, int, int);
    24 
    25 	MAKE_STATE_2(State_Func22, int, int);
    26 	MAKE_STATE_2(State_H1_Func22, int, int);
    27 	MAKE_STATE_2(State_HP1_Func22, int, int);
    28 	MAKE_STATE_2(State_HP2_Func22, int, int);
    29 
    30 	struct Test
    31 	{
    32 		virtual void Func1(int a)
    33 		{
    34 			ADD_STATE(State_Func1(a));
    35 		}
    36 
    37 		virtual int Func2(int a)
    38 		{
    39 			ADD_STATE(State_Func2(a));
    40 			return 1000;
    41 		}
    42 
    43 		// Overloaded version
    44 		virtual int Func2(int a, int b)
    45 		{
    46 			ADD_STATE(State_Func22(a, b));
    47 			return 0xDEADFC;
    48 		}
    49 	};
    50 
    51 	void Handler1_Func1(int a)
    52 	{
    53 		ADD_STATE(State_H1_Func1(a));
    54 		RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (5));
    55 	}
    56 	void Handler2_Func1(int a)
    57 	{
    58 		ADD_STATE(State_H2_Func1(a));
    59 		RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (a - 5));
    60 	}
    61 	void HandlerPost_Func1(int a)
    62 	{
    63 		ADD_STATE(State_HP_Func1(a, META_IFACEPTR(void)));
    64 	}
    65 
    66 
    67 	int Handler1_Func2(int a)
    68 	{
    69 		ADD_STATE(State_H1_Func2(a));
    70 		// Pfeeehhh, ugly, I know, but I had to test it :)
    71 		RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 500, 
    72 			static_cast<int (Test::*)(int)>(&Test::Func2), (a - 10));
    73 	}
    74 
    75 	int Handler2_Func2(int a)
    76 	{
    77 		ADD_STATE(State_H2_Func2(a));
    78 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, 0, 
    79 			static_cast<int (Test::*)(int)>(&Test::Func2), (a - 10));
    80 	}
    81 
    82 	int HandlerPost_Func2(int a)
    83 	{
    84 		ADD_STATE(State_HP_Func2(a, META_RESULT_ORIG_RET(int)));
    85 		RETURN_META_VALUE(MRES_IGNORED, 0);
    86 	}
    87 
    88 	int Handler1_Func22(int a, int b)
    89 	{
    90 		ADD_STATE(State_H1_Func22(a, b));
    91 		RETURN_META_VALUE(MRES_IGNORED, 0);
    92 	}
    93 
    94 	int HandlerPost1_Func22(int a, int b)
    95 	{
    96 		ADD_STATE(State_HP1_Func22(a, b));
    97 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, 0, static_cast<int (Test::*)(int, int)>(&Test::Func2), (1, 2));
    98 	}
    99 
   100 	int HandlerPost1A_Func22(int a, int b)
   101 	{
   102 		ADD_STATE(State_HP1_Func22(a, b));
   103 		RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 0, static_cast<int (Test::*)(int, int)>(&Test::Func2), (1, 2));
   104 	}
   105 
   106 	int HandlerPost2_Func22(int a, int b)
   107 	{
   108 		ADD_STATE(State_HP2_Func22(a, b));
   109 		RETURN_META_VALUE(MRES_IGNORED, 0);
   110 	}
   111 
   112 	SH_DECL_HOOK1_void(Test, Func1, SH_NOATTRIB, 0, int);
   113 	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, int, int);
   114 	SH_DECL_HOOK2(Test, Func2, SH_NOATTRIB, 1, int, int, int);
   115 
   116 	Test *MyTestFactory()
   117 	{
   118 		return new Test;
   119 	}
   120 }
   121 
   122 bool TestRecall(std::string &error)
   123 {
   124 	GET_SHPTR(g_SHPtr);
   125 	g_PLID = 1337;
   126 
   127 	Test *ptr = MyTestFactory();
   128 	CAutoPtrDestruction<Test> apd(ptr);
   129 
   130 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler1_Func1), false);
   131 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
   132 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(HandlerPost_Func1), true);
   133 
   134 	ptr->Func1(77);
   135 
   136 	CHECK_STATES((&g_States,
   137 		new State_H1_Func1(77),
   138 		new State_H2_Func1(5),
   139 		new State_Func1(0),
   140 		new State_HP_Func1(0, ptr),
   141 		NULL), "Part 1");
   142 
   143 	SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(Handler1_Func1), false);
   144 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
   145 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
   146 	SH_ADD_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
   147 
   148 	ptr->Func1(77);
   149 
   150 	CHECK_STATES((&g_States,
   151 		new State_H2_Func1(77),
   152 		new State_H2_Func1(72),
   153 		new State_H2_Func1(67),
   154 		new State_H2_Func1(62),
   155 		new State_Func1(57),
   156 		new State_HP_Func1(57, ptr),
   157 		NULL), "Part 2");
   158 
   159 	SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(Handler2_Func1), false);
   160 	SH_REMOVE_HOOK(Test, Func1, ptr, SH_STATIC(HandlerPost_Func1), true);
   161 
   162 	ptr->Func1(77);
   163 
   164 	CHECK_STATES((&g_States,
   165 		new State_Func1(77),
   166 		NULL), "Part 3");
   167 
   168 	// Func2
   169 
   170 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func2), false);
   171 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost_Func2), true);
   172 
   173 	int a = ptr->Func2(77);
   174 	CHECK_STATES((&g_States,
   175 		new State_H1_Func2(77),
   176 		new State_Func2(67),
   177 		new State_HP_Func2(67, 1000),			// 1000 because it's the ORIG_RET
   178 		NULL), "Part 4");
   179 
   180 	CHECK_COND(a == 500, "Part 4.1");
   181 
   182 	// Func2, with other handler
   183 	SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func2), false);
   184 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler2_Func2), false);
   185 
   186 	a = ptr->Func2(77);
   187 	CHECK_STATES((&g_States,
   188 		new State_H2_Func2(77),
   189 		new State_Func2(67),
   190 		new State_HP_Func2(67, 1000),			// 1000 because it's the ORIG_RET
   191 		NULL), "Part 4.2");
   192 
   193 	CHECK_COND(a == 1000, "Part 4.2.1");		// Should return 1000 as well.
   194 
   195 	// Func22 -> post recalls
   196 
   197 	// 1) WITH OVERRIDE
   198 
   199 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(Handler1_Func22), false);
   200 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1A_Func22), true);
   201 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
   202 	
   203 	a = ptr->Func2(10, 11);
   204 	CHECK_STATES((&g_States,
   205 		new State_H1_Func22(10, 11),
   206 		new State_Func22(10, 11),
   207 		new State_HP1_Func22(10, 11),
   208 		new State_HP2_Func22(1, 2),
   209 		NULL), "Part 5");
   210 
   211 	CHECK_COND(a == 0, "Part 5.1");
   212 
   213 	// 2) WITH IGNORE
   214 	SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1A_Func22), true);
   215 	SH_REMOVE_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
   216 
   217 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost1_Func22), true);
   218 	SH_ADD_HOOK(Test, Func2, ptr, SH_STATIC(HandlerPost2_Func22), true);
   219 
   220 	a = ptr->Func2(10, 11);
   221 	CHECK_STATES((&g_States,
   222 		new State_H1_Func22(10, 11),
   223 		new State_Func22(10, 11),
   224 		new State_HP1_Func22(10, 11),
   225 		new State_HP2_Func22(1, 2),
   226 		NULL), "Part 5");
   227 
   228 	CHECK_COND(a == 0xDEADFC, "Part 5.1");
   229 
   230 	return true;
   231 }