sourcehook/test/testrecall.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 05 06:11:53 2006 +0000 (2006-10-05)
changeset 262 5c2892f5b935
parent 179 3ade923f508b
child 324 257f0837a885
permissions -rw-r--r--
string fixups
     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 
   117 bool TestRecall(std::string &error)
   118 {
   119 	GET_SHPTR(g_SHPtr);
   120 	g_PLID = 1337;
   121 
   122 	Test inst;
   123 	Test *ptr = &inst;
   124 
   125 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler1_Func1, false);
   126 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   127 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, HandlerPost_Func1, true);
   128 
   129 	ptr->Func1(77);
   130 
   131 	CHECK_STATES((&g_States,
   132 		new State_H1_Func1(77),
   133 		new State_H2_Func1(5),
   134 		new State_Func1(0),
   135 		new State_HP_Func1(0, ptr),
   136 		NULL), "Part 1");
   137 
   138 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, Handler1_Func1, false);
   139 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   140 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   141 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   142 
   143 	ptr->Func1(77);
   144 
   145 	CHECK_STATES((&g_States,
   146 		new State_H2_Func1(77),
   147 		new State_H2_Func1(72),
   148 		new State_H2_Func1(67),
   149 		new State_H2_Func1(62),
   150 		new State_Func1(57),
   151 		new State_HP_Func1(57, ptr),
   152 		NULL), "Part 2");
   153 
   154 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   155 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, HandlerPost_Func1, true);
   156 
   157 	ptr->Func1(77);
   158 
   159 	CHECK_STATES((&g_States,
   160 		new State_Func1(77),
   161 		NULL), "Part 3");
   162 
   163 	// Func2
   164 
   165 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, Handler1_Func2, false);
   166 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost_Func2, true);
   167 
   168 	int a = ptr->Func2(77);
   169 	CHECK_STATES((&g_States,
   170 		new State_H1_Func2(77),
   171 		new State_Func2(67),
   172 		new State_HP_Func2(67, 1000),			// 1000 because it's the ORIG_RET
   173 		NULL), "Part 4");
   174 
   175 	CHECK_COND(a == 500, "Part 4.1");
   176 
   177 	// Func2, with other handler
   178 	SH_REMOVE_HOOK_STATICFUNC(Test, Func2, ptr, Handler1_Func2, false);
   179 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, Handler2_Func2, false);
   180 
   181 	a = ptr->Func2(77);
   182 	CHECK_STATES((&g_States,
   183 		new State_H2_Func2(77),
   184 		new State_Func2(67),
   185 		new State_HP_Func2(67, 1000),			// 1000 because it's the ORIG_RET
   186 		NULL), "Part 4.2");
   187 
   188 	CHECK_COND(a == 1000, "Part 4.2.1");		// Should return 1000 as well.
   189 
   190 	// Func22 -> post recalls
   191 
   192 	// 1) WITH OVERRIDE
   193 
   194 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, Handler1_Func22, false);
   195 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost1A_Func22, true);
   196 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost2_Func22, true);
   197 	
   198 	a = ptr->Func2(10, 11);
   199 	CHECK_STATES((&g_States,
   200 		new State_H1_Func22(10, 11),
   201 		new State_Func22(10, 11),
   202 		new State_HP1_Func22(10, 11),
   203 		new State_HP2_Func22(1, 2),
   204 		NULL), "Part 5");
   205 
   206 	CHECK_COND(a == 0, "Part 5.1");
   207 
   208 	// 2) WITH IGNORE
   209 	SH_REMOVE_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost1A_Func22, true);
   210 	SH_REMOVE_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost2_Func22, true);
   211 
   212 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost1_Func22, true);
   213 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost2_Func22, true);
   214 
   215 	a = ptr->Func2(10, 11);
   216 	CHECK_STATES((&g_States,
   217 		new State_H1_Func22(10, 11),
   218 		new State_Func22(10, 11),
   219 		new State_HP1_Func22(10, 11),
   220 		new State_HP2_Func22(1, 2),
   221 		NULL), "Part 5");
   222 
   223 	CHECK_COND(a == 0xDEADFC, "Part 5.1");
   224 
   225 	return true;
   226 }