core/sourcehook/test/test2.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 #include <stdarg.h>
     5 
     6 // TEST2
     7 // Vafmt and Overloaded functions
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_EatYams_Called, int);
    15 	MAKE_STATE_1(State_EatYams_Handler_Called, int);
    16 	MAKE_STATE_2(State_Vafmt_Called, int, std::string);
    17 	MAKE_STATE_2(State_Vafmt_PreHandler_Called, int, std::string);
    18 	MAKE_STATE_2(State_Vafmt_PostHandler_Called, int, std::string);
    19 
    20 	class IGaben
    21 	{
    22 	public:
    23 		virtual void EatYams()
    24 		{
    25 			ADD_STATE(State_EatYams_Called(0));
    26 		}
    27 		virtual bool EatYams(const char *location) const
    28 		{
    29 			ADD_STATE(State_EatYams_Called(1));
    30 			return true;
    31 		}
    32 		virtual void Vafmt1(bool param1, int x, const char *fmt, ...)
    33 		{
    34 			va_list ap;
    35 			va_start(ap, fmt);
    36 			char buffer[9999];
    37 			vsnprintf(buffer, 9998, fmt, ap);
    38 			buffer[9998] = 0;
    39 			va_end(ap);
    40 			ADD_STATE(State_Vafmt_Called(1, std::string(buffer)));
    41 		}
    42 		virtual float Vafmt2(const char *fmt, ...)
    43 		{
    44 			va_list ap;
    45 			va_start(ap, fmt);
    46 			char buffer[9999];
    47 			vsnprintf(buffer, 9998, fmt, ap);
    48 			buffer[9998] = 0;
    49 			va_end(ap);
    50 			ADD_STATE(State_Vafmt_Called(2, std::string(buffer)));
    51 			return 0.0f;
    52 		}
    53 	};
    54 
    55 	SH_DECL_HOOK0_void(IGaben, EatYams, SH_NOATTRIB, 0);
    56 	SH_DECL_HOOK1(IGaben, EatYams, const, 1, bool, const char *);
    57 	SH_DECL_HOOK2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
    58 	SH_DECL_HOOK0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
    59 
    60 	SH_DECL_EXTERN2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
    61 	SH_DECL_EXTERN0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
    62 
    63 	// Hook also using manual hooks!
    64 	SH_DECL_MANUALHOOK0_vafmt(IGaben_Vafmt2, 3, 0, 0, float);
    65 	SH_DECL_MANUALEXTERN0_vafmt(IGaben_Vafmt2, float);
    66 
    67 	SH_DECL_MANUALHOOK2_void_vafmt(IGaben_Vafmt1, 2, 0, 0, bool, int);
    68 	SH_DECL_MANUALEXTERN2_void_vafmt(IGaben_Vafmt1, bool, int);
    69 
    70 	void EatYams0_Handler()
    71 	{
    72 		ADD_STATE(State_EatYams_Handler_Called(0));
    73 	}
    74 
    75 	bool EatYams1_Handler(const char *loc)
    76 	{
    77 		ADD_STATE(State_EatYams_Handler_Called(1));
    78 		return true;
    79 	}
    80 
    81 	void Vafmt1_PreHandler(bool param1, int x, const char *in)
    82 	{
    83 		ADD_STATE(State_Vafmt_PreHandler_Called(1, std::string(in)));
    84 	}
    85 	void Vafmt1_PostHandler(bool param1, int x, const char *in)
    86 	{
    87 		ADD_STATE(State_Vafmt_PostHandler_Called(1, std::string(in)));
    88 	}
    89 	float Vafmt2_PreHandler(const char *in)
    90 	{
    91 		ADD_STATE(State_Vafmt_PreHandler_Called(2, std::string(in)));
    92 		return 0.0f;
    93 	}
    94 	float Vafmt2_PostHandler(const char *in)
    95 	{
    96 		ADD_STATE(State_Vafmt_PostHandler_Called(2, std::string(in)));
    97 		return 0.0f;
    98 	}
    99 
   100 	IGaben *MyInstanceFactory()
   101 	{
   102 		return new IGaben;
   103 	}
   104 }
   105 
   106 
   107 bool TestVafmtAndOverload(std::string &error)
   108 {
   109 	int h1, h2, h3, h4;
   110 	GET_SHPTR(g_SHPtr);
   111 	g_PLID = 1337;	
   112 
   113 	IGaben *pGab = MyInstanceFactory();
   114 	CAutoPtrDestruction<IGaben> apd(pGab);
   115 
   116 	// Part 1
   117 	SH_CALL(pGab, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
   118 	SH_CALL(pGab, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
   119 
   120 	h1 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
   121 	h2 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
   122 
   123 	pGab->EatYams();
   124 	pGab->EatYams("Here!");
   125 
   126 	SH_REMOVE_HOOK_ID(h1);
   127 	SH_REMOVE_HOOK_ID(h2);
   128 
   129 	CHECK_STATES((&g_States, 
   130 		new State_EatYams_Called(0),
   131 		new State_EatYams_Called(1),
   132 		new State_EatYams_Handler_Called(0),
   133 		new State_EatYams_Called(0),
   134 		new State_EatYams_Handler_Called(1),
   135 		new State_EatYams_Called(1),
   136 		NULL),"Part 1");
   137 
   138 	// Part 2
   139 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   140 	SH_CALL(pGab, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
   141 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   142 	SH_CALL(pGab, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
   143 
   144 	CHECK_STATES((&g_States,
   145 		new State_Vafmt_Called(1, "Hello BA1L"),
   146 		new State_Vafmt_Called(1, "Hello BA1L"),
   147 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   148 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   149 		NULL), "Part 2");
   150 
   151 	// Part 3
   152 	h1 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
   153 	h2 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
   154 	h3 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
   155 	h4 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
   156 
   157 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   158 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   159 
   160 	CHECK_STATES((&g_States,
   161 		new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
   162 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   163 		new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
   164 
   165 		new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
   166 		new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
   167 		new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
   168 		NULL), "Part 3");
   169 
   170 	
   171 	// Check LARGE stuff
   172 	char tmpbuf[501];
   173 	for (int i = 0; i < 500; ++i)
   174 		tmpbuf[i] = (i % 10) + '0';
   175 	tmpbuf[500] = 0;
   176 
   177 	pGab->Vafmt2("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
   178 		tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf,
   179 		tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf, tmpbuf);
   180 
   181 	char refbuf[SourceHook::STRBUF_LEN]; 
   182 	for (int i = 0; i < (SourceHook::STRBUF_LEN - 1); ++i)
   183 		refbuf[i] = (i % 10) + '0';
   184 	refbuf[SourceHook::STRBUF_LEN - 1] = 0;
   185 
   186 	CHECK_STATES((&g_States,
   187 		new State_Vafmt_PreHandler_Called(2, std::string(refbuf)),
   188 		new State_Vafmt_Called(2, std::string(refbuf)),
   189 		new State_Vafmt_PostHandler_Called(2, std::string(refbuf)),
   190 		NULL
   191 		), "Part 3.1");
   192 
   193 	// Part 4
   194 	SH_REMOVE_HOOK_ID(h1);
   195 	SH_REMOVE_HOOK_ID(h2);
   196 	SH_REMOVE_HOOK_ID(h3);
   197 	SH_REMOVE_HOOK_ID(h4);
   198 
   199 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   200 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   201 
   202 	CHECK_STATES((&g_States,
   203 		new State_Vafmt_Called(1, "Hello BA1L"),
   204 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   205 		NULL), "Part 4");
   206 
   207 	// Part5: Part3 but with manualhooks
   208 	h1 = SH_ADD_MANUALHOOK(IGaben_Vafmt1, pGab, SH_STATIC(Vafmt1_PreHandler), false);
   209 	h2 = SH_ADD_MANUALHOOK(IGaben_Vafmt1, pGab, SH_STATIC(Vafmt1_PostHandler), true);
   210 	h3 = SH_ADD_MANUALHOOK(IGaben_Vafmt2, pGab, SH_STATIC(Vafmt2_PreHandler), false);
   211 	h4 = SH_ADD_MANUALHOOK(IGaben_Vafmt2, pGab, SH_STATIC(Vafmt2_PostHandler), true);
   212 
   213 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   214 	SH_CALL(pGab, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
   215 	SH_MCALL(pGab, IGaben_Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
   216 
   217 	CHECK_STATES((&g_States,
   218 		new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
   219 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   220 		new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
   221 
   222 		// SH_CALL and SH_MCALL
   223 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   224 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   225 		NULL), "Part 5.1");
   226 
   227 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   228 	SH_CALL(pGab, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
   229 	SH_MCALL(pGab, IGaben_Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
   230 
   231 	CHECK_STATES((&g_States,
   232 		new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
   233 		new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
   234 		new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
   235 
   236 		// SH_CALL and SH_MCALL
   237 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   238 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   239 		NULL), "Part 5.2");
   240 
   241 	return true;
   242 }