-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
143 lines (117 loc) · 3.43 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
TODO: Put your copyright notice and license text here.
*/
#include <cstddef>
#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"
#include "pluginconfig.h"
#include "pluginutils.h"
#define CheckArgs() pluginutils::CheckNumberOfArguments(amx, params, num_args_expected)
extern void *pAMXFunctions;
void *(*logprintf)(const char *fmt, ...);
static cell AMX_NATIVE_CALL n_HelloWorld(AMX *amx, cell *params)
{
logprintf("%s: This line was printed from a plugin", pluginutils::GetCurrentNativeFunctionName(amx));
return 1;
}
static cell AMX_NATIVE_CALL n_HelloWorld_PrintNumber(AMX *amx, cell *params)
{
enum
{
args_size,
arg_number,
__dummy_elem_, num_args_expected = __dummy_elem_ - 1
};
if (!CheckArgs())
return 0;
logprintf("%s: %d", pluginutils::GetCurrentNativeFunctionName(amx), params[arg_number]);
return 1;
}
static cell AMX_NATIVE_CALL n_HelloWorld_PrintString(AMX *amx, cell *params)
{
enum
{
args_size,
arg_str,
__dummy_elem_, num_args_expected = __dummy_elem_ - 1
};
if (!CheckArgs())
return 0;
char *str;
int error;
str = pluginutils::GetCString(amx, params[arg_str], error);
if (error != AMX_ERR_NONE)
return amx_RaiseError(amx, error), 0;
logprintf("%s: %s", pluginutils::GetCurrentNativeFunctionName(amx), str);
free(str); // The string returned by GetCString() needs to be freed manually.
return 1;
}
static cell AMX_NATIVE_CALL n_HelloWorld_CheckArgsTest(AMX *amx, cell *params)
{
enum
{
args_size,
arg_first,
__dummy_elem_, num_args_expected = __dummy_elem_ - 1
};
if (!CheckArgs())
return 0;
// The number of arguments for this function defined in the include file
// is invalid, so this code should never occur.
logprintf("This line shouldn't be printed");
return 1;
}
static AMX_NATIVE orig_IsPlayerConnected;
static cell AMX_NATIVE_CALL hook_IsPlayerConnected(AMX *amx, cell *params)
{
logprintf("Hello from hook_IsPlayerConnected");
return orig_IsPlayerConnected(amx, params);
}
static AMX_NATIVE_INFO plugin_natives[] =
{
{ "HelloWorld", n_HelloWorld },
{ "HelloWorld_PrintNumber", n_HelloWorld_PrintNumber },
{ "HelloWorld_PrintString", n_HelloWorld_PrintString },
{ "HelloWorld_CheckArgsTest", n_HelloWorld_CheckArgsTest }
};
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
return PLUGIN_SUPPORTS_FLAGS;
}
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
{
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
logprintf = (void *(*)(const char *fmt, ...))ppData[PLUGIN_DATA_LOGPRINTF];
if (NULL == pAMXFunctions || NULL == logprintf)
return false;
int plug_ver_major, plug_ver_minor, plug_ver_build;
pluginutils::SplitVersion(PLUGIN_VERSION, plug_ver_major, plug_ver_minor, plug_ver_build);
logprintf(" %s plugin v%d.%d.%d is OK", PLUGIN_NAME, plug_ver_major, plug_ver_minor, plug_ver_build);
return true;
}
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
logprintf(" %s plugin was unloaded", PLUGIN_NAME);
}
PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
{
if (!pluginutils::CheckIncludeVersion(amx))
return 0;
amx_Register(amx, plugin_natives, (int)arraysize(plugin_natives));
// Native function hooking example.
bool native_found = pluginutils::ReplaceNative(
amx, "IsPlayerConnected", hook_IsPlayerConnected, &orig_IsPlayerConnected);
if (native_found)
logprintf("IsPlayerConnected hooked successfully");
return 1;
}
PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
{
return AMX_ERR_NONE;
}
/*
PLUGIN_EXPORT int PLUGIN_CALL ProcessTick()
{
return AMX_ERR_NONE;
}
*/