plugins/testsuite/fwdtest1.sp
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
     1 #include <sourcemod>
     2 
     3 public Plugin:myinfo = 
     4 {
     5 	name = "Forward Testing Lab #1",
     6 	author = "AlliedModders LLC",
     7 	description = "Tests suite #1 for forwards created by plugins",
     8 	version = "1.0.0.0",
     9 	url = "http://www.sourcemod.net/"
    10 };
    11 
    12 new Handle:g_GlobalFwd = INVALID_HANDLE;
    13 new Handle:g_PrivateFwd = INVALID_HANDLE;
    14 
    15 public OnPluginStart()
    16 {	
    17 	RegServerCmd("test_create_gforward", Command_CreateGlobalForward);
    18 	RegServerCmd("test_create_pforward", Command_CreatePrivateForward);
    19 	RegServerCmd("test_exec_gforward", Command_ExecGlobalForward);
    20 	RegServerCmd("test_exec_pforward", Command_ExecPrivateForward);
    21 }
    22 
    23 public OnPluginEnd()
    24 {
    25 	CloseHandle(g_GlobalFwd);
    26 	CloseHandle(g_PrivateFwd);
    27 }
    28 
    29 public Action:Command_CreateGlobalForward(args)
    30 {
    31 	if (g_GlobalFwd != INVALID_HANDLE)
    32 	{
    33 		CloseHandle(g_GlobalFwd);
    34 	}
    35 	
    36 	g_GlobalFwd = CreateGlobalForward("OnGlobalForward", ET_Ignore, Param_Any, Param_Cell, Param_Float, Param_String, Param_Array, Param_CellByRef, Param_FloatByRef);
    37 	
    38 	if (g_GlobalFwd == INVALID_HANDLE)
    39 	{
    40 		PrintToServer("Failed to create global forward!");
    41 	}
    42 	
    43 	return Plugin_Handled;
    44 }
    45 
    46 public Action:Command_CreatePrivateForward(args)
    47 {
    48 	new Handle:pl;
    49 	new Function:func;
    50 	
    51 	if (g_PrivateFwd != INVALID_HANDLE)
    52 	{
    53 		CloseHandle(g_PrivateFwd);
    54 	}
    55 	
    56 	g_PrivateFwd = CreateForward(ET_Hook, Param_Cell, Param_String, Param_VarArgs);
    57 	
    58 	if (g_PrivateFwd == INVALID_HANDLE)
    59 	{
    60 		PrintToServer("Failed to create private forward!")
    61 	}
    62 	
    63 	pl = FindPluginByFile("fwdtest2.smx");
    64 	
    65 	if (!pl)
    66 	{
    67 		PrintToServer("Could not find fwdtest2.smx!");
    68 		return Plugin_Handled;
    69 	}
    70 	
    71 	func = GetFunctionByName(pl, "OnPrivateForward");
    72 	
    73 	/* This shouldn't happen, but oh well */
    74 	if (func == INVALID_FUNCTION)
    75 	{
    76 		PrintToServer("Could not find \"OnPrivateForward\" in fwdtest2.smx!");
    77 		return Plugin_Handled;
    78 	}
    79 	
    80 	if (!AddToForward(g_PrivateFwd, pl, func) || !AddToForward(g_PrivateFwd, GetMyHandle(), ZohMyGod))
    81 	{
    82 		PrintToServer("Failed to add functions to private forward!");
    83 		return Plugin_Handled;
    84 	}
    85 	
    86 	return Plugin_Handled;
    87 }
    88 
    89 public Action:Command_ExecGlobalForward(args)
    90 {
    91 	new a = 99;
    92 	new Float:b = 4.215;
    93 	new err, ret;
    94 	
    95 	if (g_GlobalFwd == INVALID_HANDLE)
    96 	{
    97 		PrintToServer("Failed to execute global forward. Create it first.");
    98 		return Plugin_Handled;
    99 	}
   100 	
   101 	PrintToServer("Beginning call to %d functions in global forward \"OnGlobalForward\"", GetForwardFunctionCount(g_GlobalFwd));
   102 	
   103 	Call_StartForward(g_GlobalFwd);
   104 	Call_PushCell(OnPluginStart);
   105 	Call_PushCell(7);
   106 	Call_PushFloat(-8.5);
   107 	Call_PushString("Anata wa doko desu ka?");
   108 	Call_PushArray({0.0, 1.1, 2.2}, 3);
   109 	Call_PushCellRef(a);
   110 	Call_PushFloatRef(b);
   111 	err = Call_Finish(ret);
   112 	
   113 	PrintToServer("Call to global forward \"OnGlobalForward\" completed");
   114 	PrintToServer("Error code = %d (expected: %d)", err, 0);
   115 	PrintToServer("Return value = %d (expected: %d)", ret, Plugin_Continue);
   116 	
   117 	PrintToServer("a = %d (expected: %d)", a, 777);
   118 	PrintToServer("b = %f (expected: %f)", b, -0.782);
   119 	
   120 	return Plugin_Handled;
   121 }
   122 
   123 public Action:Command_ExecPrivateForward(args)
   124 {
   125 	new err, ret;
   126 	
   127 	if (g_PrivateFwd == INVALID_HANDLE)
   128 	{
   129 		PrintToServer("Failed to execute private forward. Create it first.");
   130 		return Plugin_Handled;
   131 	}
   132 	
   133 	PrintToServer("Beginning call to %d functions in private forward", GetForwardFunctionCount(g_PrivateFwd));
   134 	
   135 	Call_StartForward(g_PrivateFwd);
   136 	Call_PushCell(24);
   137 	Call_PushString("I am a format string: %d %d %d %d %d %d");
   138 	Call_PushCell(0);
   139 	Call_PushCell(1);
   140 	Call_PushCell(2);
   141 	Call_PushCell(3);
   142 	Call_PushCell(4);
   143 	Call_PushCell(5);
   144 	err = Call_Finish(ret);
   145 	
   146 	PrintToServer("Call to private forward completed");
   147 	PrintToServer("Error code = %d (expected: %d)", err, 0);
   148 	PrintToServer("Return value = %d (expected: %d)", ret, Plugin_Handled);
   149 
   150 	return Plugin_Handled;
   151 }
   152 
   153 public Action:ZohMyGod(num, const String:format[], ...)
   154 {
   155 	decl String:buffer[128];
   156 	
   157 	PrintToServer("Inside private forward #1");
   158 	
   159 	PrintToServer("num = %d (expected: %d)", num, 24);
   160 	
   161 	VFormat(buffer, sizeof(buffer), format, 3);
   162 	PrintToServer("buffer = \"%s\" (expected: \"%s\")", buffer, "I am a format string: 0 1 2 3 4 5");
   163 	
   164 	PrintToServer("End private forward #1");
   165 	
   166 	return Plugin_Continue;
   167 }