sourcehook/test/testrecall.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Dec 23 23:32:21 2005 +0000 (2005-12-23)
changeset 165 76da01197c4c
parent 163 377bdd31c28d
child 179 3ade923f508b
permissions -rw-r--r--
Uh.
     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_2(State_HP_Func2, int, int);
    23 
    24 	struct Test
    25 	{
    26 		virtual void Func1(int a)
    27 		{
    28 			ADD_STATE(State_Func1(a));
    29 		}
    30 
    31 		virtual int Func2(int a)
    32 		{
    33 			ADD_STATE(State_Func2(a));
    34 			return 1000;
    35 		}
    36 
    37 		// Overloaded version
    38 		virtual int Func2(int a, int b)
    39 		{
    40 			return 0xDEADFC;
    41 		}
    42 	};
    43 
    44 	void Handler1_Func1(int a)
    45 	{
    46 		ADD_STATE(State_H1_Func1(a));
    47 		RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (5));
    48 	}
    49 	void Handler2_Func1(int a)
    50 	{
    51 		ADD_STATE(State_H2_Func1(a));
    52 		RETURN_META_NEWPARAMS(MRES_IGNORED, &Test::Func1, (a - 5));
    53 	}
    54 	void HandlerPost_Func1(int a)
    55 	{
    56 		ADD_STATE(State_HP_Func1(a, META_IFACEPTR(void)));
    57 	}
    58 
    59 
    60 	int Handler1_Func2(int a)
    61 	{
    62 		ADD_STATE(State_H1_Func2(a));
    63 		// Pfeeehhh, ugly, I know, but I had to test it :)
    64 		RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 500, 
    65 			static_cast<int (Test::*)(int)>(&Test::Func2), (a - 10));
    66 	}
    67 
    68 	int HandlerPost_Func2(int a)
    69 	{
    70 		ADD_STATE(State_HP_Func2(a, META_RESULT_ORIG_RET(int)));
    71 		RETURN_META_VALUE(MRES_IGNORED, 0);
    72 	}
    73 
    74 	SH_DECL_HOOK1_void(Test, Func1, SH_NOATTRIB, 0, int);
    75 	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, int, int);
    76 }
    77 
    78 bool TestRecall(std::string &error)
    79 {
    80 	GET_SHPTR(g_SHPtr);
    81 	g_PLID = 1337;
    82 
    83 	Test inst;
    84 	Test *ptr = &inst;
    85 
    86 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler1_Func1, false);
    87 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
    88 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, HandlerPost_Func1, true);
    89 
    90 	ptr->Func1(77);
    91 
    92 	CHECK_STATES((&g_States,
    93 		new State_H1_Func1(77),
    94 		new State_H2_Func1(5),
    95 		new State_Func1(0),
    96 		new State_HP_Func1(0, ptr),
    97 		NULL), "Part 1");
    98 
    99 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, Handler1_Func1, false);
   100 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   101 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   102 	SH_ADD_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   103 
   104 	ptr->Func1(77);
   105 
   106 	CHECK_STATES((&g_States,
   107 		new State_H2_Func1(77),
   108 		new State_H2_Func1(72),
   109 		new State_H2_Func1(67),
   110 		new State_H2_Func1(62),
   111 		new State_Func1(57),
   112 		new State_HP_Func1(57, ptr),
   113 		NULL), "Part 2");
   114 
   115 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, Handler2_Func1, false);
   116 	SH_REMOVE_HOOK_STATICFUNC(Test, Func1, ptr, HandlerPost_Func1, true);
   117 
   118 	ptr->Func1(77);
   119 
   120 	CHECK_STATES((&g_States,
   121 		new State_Func1(77),
   122 		NULL), "Part 3");
   123 
   124 	// Func2
   125 
   126 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, Handler1_Func2, false);
   127 	SH_ADD_HOOK_STATICFUNC(Test, Func2, ptr, HandlerPost_Func2, true);
   128 
   129 	int a = ptr->Func2(77);
   130 	CHECK_STATES((&g_States,
   131 		new State_H1_Func2(77),
   132 		new State_Func2(67),
   133 		new State_HP_Func2(67, 1000),			// 1000 because it's the ORIG_RET
   134 		NULL), "Part 4");
   135 
   136 	CHECK_COND(a == 500, "Part 4.1");
   137 
   138 	return true;
   139 }