core/sourcehook/test/testref.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 "testevents.h"
     2 #include "sourcehook_test.h"
     3 
     4 // References and private copy constructors and stuff like that!
     5 
     6 /*
     7 BAILOPAN, 29.04.2006 21:51 :
     8 onoto-san, interesting error from VS
     9 we have this function:
    10 void GabHaben(ISomething &sth);
    11 
    12 SomethingDerived sth;
    13 and SH_CALL(cc, &WHAT::GabHaben)(sth);
    14 "No copy constructor available for class 'SomethingDerived'"
    15 */
    16 
    17 
    18 namespace
    19 {
    20 	StateList g_States;
    21 	SourceHook::ISourceHook *g_SHPtr;
    22 	SourceHook::Plugin g_PLID;
    23 
    24 	MAKE_STATE_1(State_Result, int);
    25 	MAKE_STATE_1(State_Result_InHook, int);
    26 
    27 	MAKE_STATE_2(State_F2_Called, int, int);
    28 	MAKE_STATE_2(State_F2_Handler_Called, int, int);
    29 
    30 	struct POD
    31 	{
    32 		int a;
    33 		int b;
    34 		int c;
    35 		int d;
    36 		int e;
    37 		int f;
    38 	};
    39 	
    40 	class CBase
    41 	{
    42 		// Private copy constructor!
    43 		CBase(CBase &other)
    44 		{
    45 		}
    46 	public:
    47 		CBase()
    48 		{
    49 		}
    50 
    51 		virtual int Func()
    52 		{
    53 			return 0;
    54 		}
    55 	};
    56 
    57 	class CDerived : public CBase
    58 	{
    59 		virtual int Func()
    60 		{
    61 			return 10;
    62 		}
    63 	};
    64 
    65 	// Complicated derived shit !
    66 	class COtherWeirdBase
    67 	{
    68 		int member;
    69 	};
    70 	class CDerived2 : private COtherWeirdBase, public CBase
    71 	{
    72 		int m_Return;
    73 	public:
    74 		CDerived2(int a) : m_Return(a)
    75 		{
    76 		}
    77 
    78 		virtual int Func()
    79 		{
    80 			return m_Return;
    81 		}
    82 	};
    83 
    84 	class CHello
    85 	{
    86 	public:
    87 		virtual int Func(CBase &sth)
    88 		{
    89 			return sth.Func();
    90 		}
    91 
    92 		virtual void F2(POD a)
    93 		{
    94 		}
    95 	};
    96 
    97 
    98 	class Test2
    99 	{
   100 	public:
   101 		virtual void F1(){}
   102 		virtual void F2(POD &a)
   103 		{
   104 			ADD_STATE(State_F2_Called(a.a, a.b));
   105 		}
   106 	};
   107 
   108 	class CHook
   109 	{
   110 	public:
   111 		virtual int Func(CBase &sth)
   112 		{
   113 			ADD_STATE(State_Result_InHook(sth.Func()));
   114 			RETURN_META_VALUE(MRES_SUPERCEDE, 20);
   115 		}
   116 
   117 		virtual void F2(POD &a)
   118 		{
   119 			ADD_STATE(State_F2_Handler_Called(a.a, a.b));
   120 		}
   121 
   122 		virtual void F21(POD a)
   123 		{
   124 		}
   125 	};
   126 
   127 	SH_DECL_HOOK1(CHello, Func, SH_NOATTRIB, 0, int, CBase&);
   128 	SH_DECL_HOOK1_void(CHello, F2, SH_NOATTRIB, 0, POD);
   129 	SH_DECL_HOOK1_void(Test2, F2, SH_NOATTRIB, 0, POD&);
   130 
   131 	CHello *MyInstanceFactory()
   132 	{
   133 		return new CHello;
   134 	}
   135 }
   136 
   137 
   138 bool TestRef(std::string &error)
   139 {
   140 	GET_SHPTR(g_SHPtr);
   141 	g_PLID = 1;
   142 
   143 	CBase base;
   144 	CDerived der;
   145 	CDerived2 der2(11);
   146 	CDerived2 der3(12);
   147 	CHello *pHello = MyInstanceFactory();
   148 	Test2 *pTest2 = new Test2;
   149 	CAutoPtrDestruction<CHello> apd(pHello);
   150 	CAutoPtrDestruction<Test2> apd2(pTest2);
   151 	CHook hook;
   152 
   153 	SourceHook::CallClass<CHello> *cc = SH_GET_CALLCLASS(pHello);
   154 
   155 	ADD_STATE(State_Result(pHello->Func(base)));
   156 	ADD_STATE(State_Result(pHello->Func(der)));
   157 	ADD_STATE(State_Result(pHello->Func(der2)));
   158 	ADD_STATE(State_Result(pHello->Func(der3)));
   159 
   160 	CHECK_STATES((&g_States,
   161 		new State_Result(0),
   162 		new State_Result(10),
   163 		new State_Result(11),
   164 		new State_Result(12),
   165 		NULL), "Part 1");
   166 
   167 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   168 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   169 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   170 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   171 
   172 	CHECK_STATES((&g_States,
   173 		new State_Result(0),
   174 		new State_Result(10),
   175 		new State_Result(11),
   176 		new State_Result(12),
   177 		NULL), "Part 2");
   178 
   179 	SH_ADD_HOOK(CHello, Func, pHello, SH_MEMBER(&hook, &CHook::Func), false);
   180 
   181 	ADD_STATE(State_Result(pHello->Func(base)));
   182 	ADD_STATE(State_Result(pHello->Func(der)));
   183 	ADD_STATE(State_Result(pHello->Func(der2)));
   184 	ADD_STATE(State_Result(pHello->Func(der3)));
   185 
   186 	CHECK_STATES((&g_States,
   187 		new State_Result_InHook(0),
   188 		new State_Result(20),
   189 		new State_Result_InHook(10),
   190 		new State_Result(20),
   191 		new State_Result_InHook(11),
   192 		new State_Result(20),
   193 		new State_Result_InHook(12),
   194 		new State_Result(20),
   195 		NULL), "Part 3");
   196 
   197 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   198 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   199 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   200 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   201 
   202 	CHECK_STATES((&g_States,
   203 		new State_Result(0),
   204 		new State_Result(10),
   205 		new State_Result(11),
   206 		new State_Result(12),
   207 		NULL), "Part 4");
   208 
   209 	// Check for correct ref proto handling
   210 
   211 	SH_ADD_HOOK(CHello, F2, pHello, SH_MEMBER(&hook, &CHook::F21), false);
   212 	SH_ADD_HOOK(Test2, F2, pTest2, SH_MEMBER(&hook, &CHook::F2), false);
   213 
   214 	POD pod = { 1, 2 };
   215 	pTest2->F2(pod);
   216 
   217 	int a = 5;
   218 
   219 	pHello->F2(pod);
   220 
   221 	CHECK_STATES((&g_States,
   222 		new State_F2_Handler_Called(1, 2),
   223 		new State_F2_Called(1, 2),
   224 		NULL), "Part 5");
   225 
   226    	return true;
   227 }