core/sourcehook/test/testvphooks.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 // 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 	// Normal hook, then vp hook
   157 
   158 	int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   159 	hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   160 
   161 	p_d1i1->Func1();
   162 	p_d1i2->Func1();
   163 	p_d2i1->Func1();
   164 
   165 	CHECK_STATES((&g_States,
   166 		new State_Func1_Pre(p_d1i1),
   167 		new State_Func1_Pre(p_d1i1),
   168 		new State_D1_Func1(p_d1i1),
   169 
   170 		new State_Func1_Pre(p_d1i2),
   171 		new State_D1_Func1(p_d1i2),
   172 
   173 		new State_D2_Func1(p_d2i1),
   174 		NULL), "Part 3");
   175 
   176 	SH_REMOVE_HOOK_ID(hook1);
   177 
   178 	p_d1i1->Func1();
   179 	p_d1i2->Func1();
   180 	p_d2i1->Func1();
   181 
   182 	CHECK_STATES((&g_States,
   183 		new State_Func1_Pre(p_d1i1),
   184 		new State_D1_Func1(p_d1i1),
   185 
   186 		new State_D1_Func1(p_d1i2),
   187 
   188 		new State_D2_Func1(p_d2i1),
   189 		NULL), "Part 4");
   190 
   191 	SH_REMOVE_HOOK_ID(hook2);
   192 
   193 	p_d1i1->Func1();
   194 	p_d1i2->Func1();
   195 	p_d2i1->Func1();
   196 
   197 	CHECK_STATES((&g_States,
   198 		new State_D1_Func1(p_d1i1),
   199 
   200 		new State_D1_Func1(p_d1i2),
   201 
   202 		new State_D2_Func1(p_d2i1),
   203 		NULL), "Part 5");
   204 
   205 	// Test this:
   206 	// Normal hook AND vp hook on Func2
   207 	// Func2's pre handler removes the VP hook.
   208 
   209 	hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   210 	hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   211 
   212 	g_F2_Pre_HookToRemove = hook1;
   213 	p_d1i1->Func2();
   214 	p_d1i1->Func2();
   215 
   216 	CHECK_STATES((&g_States,
   217 		new State_Func2_Pre(p_d1i1),
   218 		new State_D1_Func2(p_d1i1),
   219 
   220 		new State_Func2_Pre(p_d1i1),
   221 		new State_D1_Func2(p_d1i1),
   222 
   223 		NULL), "Part 6");
   224 
   225 	SH_REMOVE_HOOK_ID(hook1);
   226 
   227 	// Hook function 3:
   228 	//  Using manualhook, VP
   229 	hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   230 
   231 	//  Normally, VP
   232 	hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   233 
   234 	// Normally, no VP
   235 	int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   236 
   237 	p_d1i1->Func3(1);
   238 
   239 	CHECK_STATES((&g_States,
   240 		new State_Func3_Pre(p_d1i1, 1),		// manual vp hook
   241 		new State_Func3_Pre(p_d1i1, 2),		// normal vp hook
   242 		new State_Func3_Pre(p_d1i1, 3),		// normal non-vp hook
   243 
   244 		new State_D1_Func3(p_d1i1, 4),		// function
   245 
   246 		NULL), "Part 7.1");
   247 
   248 	p_d1i2->Func3(1);
   249 
   250 	CHECK_STATES((&g_States,
   251 		new State_Func3_Pre(p_d1i2, 1),		// manual vp hook
   252 		new State_Func3_Pre(p_d1i2, 2),		// normal vp hook
   253 
   254 		new State_D1_Func3(p_d1i2, 3),		// function
   255 
   256 		NULL), "Part 7.2");
   257 
   258 	return true;
   259 }
   260