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