core/sourcehook/test/test4.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_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST4
     6 // Tests of plugin management system
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE(State_Func1_Called);
    15 	MAKE_STATE(State_Func2_Called);
    16 	MAKE_STATE(State_Func3_Called);
    17 
    18 	MAKE_STATE(State_Func1H_Called);
    19 	MAKE_STATE(State_Func2H_Called);
    20 	MAKE_STATE(State_Func3H_Called);
    21 
    22 	class Test
    23 	{
    24 	public:
    25 		virtual void Func1()
    26 		{
    27 			ADD_STATE(State_Func1_Called);
    28 		}
    29 
    30 		virtual void Func2()
    31 		{
    32 			ADD_STATE(State_Func2_Called);
    33 		}
    34 
    35 		virtual void Func3()
    36 		{
    37 			ADD_STATE(State_Func3_Called);
    38 		}
    39 	};
    40 
    41 	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 1);
    42 	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 1);
    43 	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 1);
    44 
    45 	void Handler_Func1()
    46 	{
    47 		ADD_STATE(State_Func1H_Called);
    48 	}
    49 	void Handler_Func2()
    50 	{
    51 		ADD_STATE(State_Func2H_Called);
    52 	}
    53 	void Handler_Func3()
    54 	{
    55 		ADD_STATE(State_Func3H_Called);
    56 	}
    57 
    58 	Test *MyTestFactory()
    59 	{
    60 		return new Test;
    61 	}
    62 }
    63 
    64 bool TestPlugSys(std::string &error)
    65 {
    66 	GET_SHPTR(g_SHPtr);
    67 	g_PLID = 1;
    68 
    69 	Test *pInst = MyTestFactory();
    70 	CAutoPtrDestruction<Test> apd(pInst);
    71 
    72 	// 1)
    73 	//   Add hooks, then issue a complete shutdown
    74 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
    75 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
    76 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
    77 
    78 	pInst->Func1();
    79 	pInst->Func2();
    80 	pInst->Func3();
    81 
    82 	CHECK_STATES((&g_States,
    83 		new State_Func1H_Called,
    84 		new State_Func1_Called,
    85 		new State_Func2_Called,
    86 		new State_Func2H_Called,
    87 		new State_Func3H_Called,
    88 		new State_Func3_Called,
    89 		NULL), "Part 1.1");
    90 
    91 	Test_CompleteShutdown(g_SHPtr);
    92 
    93 	pInst->Func1();
    94 	pInst->Func2();
    95 	pInst->Func3();
    96 
    97 	CHECK_STATES((&g_States,
    98 		new State_Func1_Called,
    99 		new State_Func2_Called,
   100 		new State_Func3_Called,
   101 		NULL), "Part 1.2");
   102 
   103 	// 2)
   104 	//   Add hooks from "different plugins", then shutdown the plugins
   105 
   106 	g_PLID = 1;
   107 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   108 	g_PLID = 2;
   109 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   110 	g_PLID = 3;
   111 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   112 
   113 	g_PLID = 1;
   114 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   115 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   116 
   117 	g_PLID = 2;
   118 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   119 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   120 
   121 	g_PLID = 3;
   122 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   123 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   124 
   125 	pInst->Func1();
   126 	pInst->Func2();
   127 	pInst->Func3();
   128 
   129 	CHECK_STATES((&g_States,
   130 		new State_Func1H_Called,
   131 		new State_Func1H_Called,
   132 		new State_Func1H_Called,
   133 		new State_Func1_Called,
   134 		new State_Func2_Called,
   135 		new State_Func2H_Called,
   136 		new State_Func2H_Called,
   137 		new State_Func2H_Called,
   138 		new State_Func3H_Called,
   139 		new State_Func3H_Called,
   140 		new State_Func3H_Called,
   141 		new State_Func3_Called,
   142 		NULL), "Part 2.1");
   143 
   144 	// Unload plugins one by one
   145 	Test_UnloadPlugin(g_SHPtr, 3);
   146 
   147 	pInst->Func1();
   148 	pInst->Func2();
   149 	pInst->Func3();
   150 
   151 	CHECK_STATES((&g_States,
   152 		new State_Func1H_Called,
   153 		new State_Func1H_Called,
   154 		new State_Func1_Called,
   155 		new State_Func2_Called,
   156 		new State_Func2H_Called,
   157 		new State_Func2H_Called,
   158 		new State_Func3H_Called,
   159 		new State_Func3H_Called,
   160 		new State_Func3_Called,
   161 		NULL), "Part 2.3.1");
   162 
   163 	Test_UnloadPlugin(g_SHPtr, 2);
   164 
   165 	pInst->Func1();
   166 	pInst->Func2();
   167 	pInst->Func3();
   168 
   169 	CHECK_STATES((&g_States,
   170 		new State_Func1H_Called,
   171 		new State_Func1_Called,
   172 		new State_Func2_Called,
   173 		new State_Func2H_Called,
   174 		new State_Func3H_Called,
   175 		new State_Func3_Called,
   176 		NULL), "Part 2.4.1");
   177 
   178 	Test_UnloadPlugin(g_SHPtr, 1);
   179 
   180 	pInst->Func1();
   181 	pInst->Func2();
   182 	pInst->Func3();
   183 
   184 	CHECK_STATES((&g_States,
   185 		new State_Func1_Called,
   186 		new State_Func2_Called,
   187 		new State_Func3_Called,
   188 		NULL), "Part 2.5.1");
   189 
   190 
   191 	// 3)
   192 	//   Add hooks from "different plugins", then pause the plugins
   193 
   194 	g_PLID = 1;
   195 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   196 	g_PLID = 2;
   197 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   198 	g_PLID = 3;
   199 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   200 
   201 	g_PLID = 1;
   202 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   203 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   204 
   205 	g_PLID = 2;
   206 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   207 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   208 
   209 	g_PLID = 3;
   210 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   211 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   212 
   213 	pInst->Func1();
   214 	pInst->Func2();
   215 	pInst->Func3();
   216 
   217 	CHECK_STATES((&g_States,
   218 		new State_Func1H_Called,
   219 		new State_Func1H_Called,
   220 		new State_Func1H_Called,
   221 		new State_Func1_Called,
   222 		new State_Func2_Called,
   223 		new State_Func2H_Called,
   224 		new State_Func2H_Called,
   225 		new State_Func2H_Called,
   226 		new State_Func3H_Called,
   227 		new State_Func3H_Called,
   228 		new State_Func3H_Called,
   229 		new State_Func3_Called,
   230 		NULL), "Part 3.1");
   231 
   232 	// Unload plugins one by one
   233 	Test_PausePlugin(g_SHPtr, 3);
   234 
   235 	pInst->Func1();
   236 	pInst->Func2();
   237 	pInst->Func3();
   238 
   239 	CHECK_STATES((&g_States,
   240 		new State_Func1H_Called,
   241 		new State_Func1H_Called,
   242 		new State_Func1_Called,
   243 		new State_Func2_Called,
   244 		new State_Func2H_Called,
   245 		new State_Func2H_Called,
   246 		new State_Func3H_Called,
   247 		new State_Func3H_Called,
   248 		new State_Func3_Called,
   249 		NULL), "Part 3.3.1");
   250 
   251 	Test_PausePlugin(g_SHPtr, 2);
   252 
   253 	pInst->Func1();
   254 	pInst->Func2();
   255 	pInst->Func3();
   256 
   257 	CHECK_STATES((&g_States,
   258 		new State_Func1H_Called,
   259 		new State_Func1_Called,
   260 		new State_Func2_Called,
   261 		new State_Func2H_Called,
   262 		new State_Func3H_Called,
   263 		new State_Func3_Called,
   264 		NULL), "Part 3.4.1");
   265 
   266 	Test_PausePlugin(g_SHPtr, 1);
   267 
   268 	pInst->Func1();
   269 	pInst->Func2();
   270 	pInst->Func3();
   271 
   272 	CHECK_STATES((&g_States,
   273 		new State_Func1_Called,
   274 		new State_Func2_Called,
   275 		new State_Func3_Called,
   276 		NULL), "Part 3.5.1");
   277 
   278 	Test_UnpausePlugin(g_SHPtr, 1);
   279 	Test_UnpausePlugin(g_SHPtr, 2);
   280 	Test_UnpausePlugin(g_SHPtr, 3);
   281 
   282 	pInst->Func1();
   283 	pInst->Func2();
   284 	pInst->Func3();
   285 
   286 	CHECK_STATES((&g_States,
   287 		new State_Func1H_Called,
   288 		new State_Func1H_Called,
   289 		new State_Func1H_Called,
   290 		new State_Func1_Called,
   291 		new State_Func2_Called,
   292 		new State_Func2H_Called,
   293 		new State_Func2H_Called,
   294 		new State_Func2H_Called,
   295 		new State_Func3H_Called,
   296 		new State_Func3H_Called,
   297 		new State_Func3H_Called,
   298 		new State_Func3_Called,
   299 		NULL), "Part 3.7");
   300 
   301 	// 4) Shutdown :)
   302 	Test_CompleteShutdown(g_SHPtr);
   303 
   304 	pInst->Func1();
   305 	pInst->Func2();
   306 	pInst->Func3();
   307 
   308 	CHECK_STATES((&g_States,
   309 		new State_Func1_Called,
   310 		new State_Func2_Called,
   311 		new State_Func3_Called,
   312 		NULL), "Part 4");
   313 
   314 	return true;
   315 }