-
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathpawn.hpp
More file actions
301 lines (266 loc) · 9.83 KB
/
pawn.hpp
File metadata and controls
301 lines (266 loc) · 9.83 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* The original code is copyright (c) 2022, open.mp team and contributors.
*/
#pragma once
#include <core.hpp>
#include <amx/amx.h>
#include <array>
#include <string>
#include <vector>
#include <Server/Components/Actors/actors.hpp>
#include <Server/Components/Checkpoints/checkpoints.hpp>
#include <Server/Components/Classes/classes.hpp>
#include <Server/Components/Console/console.hpp>
#include <Server/Components/Databases/databases.hpp>
#include <Server/Components/Dialogs/dialogs.hpp>
#include <Server/Components/Fixes/fixes.hpp>
#include <Server/Components/GangZones/gangzones.hpp>
#include <Server/Components/Menus/menus.hpp>
#include <Server/Components/Objects/objects.hpp>
#include <Server/Components/Pickups/pickups.hpp>
#include <Server/Components/Recordings/recordings.hpp>
#include <Server/Components/TextDraws/textdraws.hpp>
#include <Server/Components/TextLabels/textlabels.hpp>
#include <Server/Components/Timers/timers.hpp>
#include <Server/Components/Variables/variables.hpp>
#include <Server/Components/Vehicles/vehicles.hpp>
#include <Server/Components/CustomModels/custommodels.hpp>
#define SCRIPT_API(name, prototype) PAWN_NATIVE(openmp_scripting, name, prototype)
#define SCRIPT_API_FAILRET(name, failret, prototype) PAWN_NATIVE_FAILRET(openmp_scripting, failret, name, prototype)
#define EXTERN_API(name, prototype) PAWN_NATIVE_DECL(openmp_scripting, name, prototype)
constexpr int NUM_AMX_FUNCS = 52;
int AMXAPI amx_GetNativeByIndex(AMX const* amx, int index, AMX_NATIVE_INFO* ret);
int AMXAPI amx_MakeAddr(AMX* amx, cell* phys_addr, cell* amx_addr);
int AMXAPI amx_StrSize(const cell* cstr, int* length);
enum DefaultReturnValue
{
DefaultReturnValue_False,
DefaultReturnValue_True
};
struct PawnLookup
{
ICore* core = nullptr;
IConfig* config = nullptr;
IPlayerPool* players = nullptr;
IActorsComponent* actors = nullptr;
ICheckpointsComponent* checkpoints = nullptr;
IClassesComponent* classes = nullptr;
IConsoleComponent* console = nullptr;
IDatabasesComponent* databases = nullptr;
IDialogsComponent* dialogs = nullptr;
IGangZonesComponent* gangzones = nullptr;
IFixesComponent* fixes = nullptr;
IMenusComponent* menus = nullptr;
IObjectsComponent* objects = nullptr;
IPickupsComponent* pickups = nullptr;
IRecordingsComponent* recordings = nullptr;
ITextDrawsComponent* textdraws = nullptr;
ITextLabelsComponent* textlabels = nullptr;
ITimersComponent* timers = nullptr;
IVariablesComponent* vars = nullptr;
IVehiclesComponent* vehicles = nullptr;
ICustomModelsComponent* models = nullptr;
};
PawnLookup* getAmxLookups();
struct IPawnScript
{
// Wrap the AMX API.
virtual int Allot(int cells, cell* amx_addr, cell** phys_addr) = 0;
virtual int Callback(cell index, cell* result, const cell* params) = 0;
virtual int Cleanup() = 0;
virtual int Clone(AMX* amxClone, void* data) const = 0;
virtual int Exec(cell* retval, int index) = 0;
virtual int FindNative(char const* name, int* index) const = 0;
virtual int FindPublic(char const* funcname, int* index) const = 0;
virtual int FindPubVar(char const* varname, cell* amx_addr) const = 0;
virtual int FindTagId(cell tag_id, char* tagname) const = 0;
virtual int Flags(uint16_t* flags) const = 0;
virtual int GetAddr(cell amx_addr, cell** phys_addr) const = 0;
virtual int GetNative(int index, char* funcname) const = 0;
virtual int GetNativeByIndex(int index, AMX_NATIVE_INFO* ret) const = 0;
virtual int GetPublic(int index, char* funcname) const = 0;
virtual int GetPubVar(int index, char* varname, cell* amx_addr) const = 0;
virtual int GetString(char const* dest, const cell* source, bool use_wchar, size_t size) const = 0;
virtual int GetString(char* dest, const cell* source, bool use_wchar, size_t size) = 0;
virtual int GetTag(int index, char* tagname, cell* tag_id) const = 0;
virtual int GetUserData(long tag, void** ptr) const = 0;
virtual int Init(void* program) = 0;
virtual int InitJIT(void* reloc_table, void* native_code) = 0;
virtual int MakeAddr(cell* phys_addr, cell* amx_addr) const = 0;
virtual int MemInfo(long* codesize, long* datasize, long* stackheap) const = 0;
virtual int NameLength(int* length) const = 0;
virtual AMX_NATIVE_INFO* NativeInfo(char const* name, AMX_NATIVE func) const = 0;
virtual int NumNatives(int* number) const = 0;
virtual int NumPublics(int* number) const = 0;
virtual int NumPubVars(int* number) const = 0;
virtual int NumTags(int* number) const = 0;
virtual int Push(cell value) = 0;
virtual int PushArray(cell* amx_addr, cell** phys_addr, const cell array[], int numcells) = 0;
virtual int PushString(cell* amx_addr, cell** phys_addr, StringView string, bool pack, bool use_wchar) = 0;
virtual int RaiseError(int error) = 0;
virtual int Register(const AMX_NATIVE_INFO* nativelist, int number) = 0;
// Don't forget:
//
// using IPawnScript::Register;
//
// In inheriting classes.
inline int Register(char const* name, AMX_NATIVE func)
{
AMX_NATIVE_INFO nativelist = { name, func };
return Register(&nativelist, 1);
}
virtual int Release(cell amx_addr) = 0;
virtual int SetCallback(AMX_CALLBACK callback) = 0;
virtual int SetDebugHook(AMX_DEBUG debug) = 0;
virtual int SetString(cell* dest, StringView source, bool pack, bool use_wchar, size_t size) const = 0;
virtual int SetUserData(long tag, void* ptr) = 0;
virtual int StrLen(const cell* cstring, int* length) const = 0;
virtual int StrSize(const cell* cstr, int* length) const = 0;
virtual int UTF8Check(char const* string, int* length) const = 0;
virtual int UTF8Get(char const* string, char const** endptr, cell* value) const = 0;
virtual int UTF8Len(const cell* cstr, int* length) const = 0;
virtual int UTF8Put(char* string, char** endptr, int maxchars, cell value) const = 0;
virtual cell GetCIP() const = 0;
virtual cell GetHEA() const = 0;
virtual cell GetSTP() const = 0;
virtual cell GetSTK() const = 0;
virtual cell GetHLW() const = 0;
virtual cell GetFRM() const = 0;
virtual void SetCIP(cell v) = 0;
virtual void SetHEA(cell v) = 0;
virtual void SetSTP(cell v) = 0;
virtual void SetSTK(cell v) = 0;
virtual void SetHLW(cell v) = 0;
virtual void SetFRM(cell v) = 0;
virtual AMX* GetAMX() = 0;
virtual void PrintError(int err) = 0;
virtual int GetID() const = 0;
virtual bool IsLoaded() const = 0;
template <typename... T>
void Call(cell& ret, int idx, T... args)
{
// Check if the public exists.
if (idx == INT_MAX)
{
return;
}
int err = CallChecked(idx, ret, args...);
// Step 1: Try call a crashdetect-like callback, but don't get caught in a loop.
// Step 2: Print it.
if (err != AMX_ERR_NONE)
{
PrintError(err);
}
}
template <typename... T>
cell Call(char const* name, DefaultReturnValue defaultRetValue, T... args)
{
int idx;
cell ret = defaultRetValue;
if (!FindPublic(name, &idx))
{
Call(ret, idx, args...);
}
return ret;
}
template <typename... T>
inline cell Call(std::string const& name, DefaultReturnValue defaultRetValue, T... args)
{
return Call(name.c_str(), defaultRetValue, args...);
}
// Call a function using an idx we know is correct.
template <typename... T>
int CallChecked(int idx, cell& ret, T... args)
{
cell amx_addr = GetHEA();
// Push all the arguments, using templates to resolve the correct function to use.
int err = PushOne(args...);
if (err == AMX_ERR_NONE)
err = Exec(&ret, idx);
// Release everything at once. Technically all that `Release` does is reset the heap back
// to where it was before the call to any memory-allocating functions. You could do that
// for every allocating parameter in reverse order, or just do it once for all of them together.
Release(amx_addr);
return err;
}
inline int PushOne()
{
return AMX_ERR_NONE;
}
template <typename O, typename... T>
inline int PushOne(O arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
return Push((cell)arg);
return ret;
}
template <typename... T>
inline int PushOne(float arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
return Push(amx_ftoc(arg));
return ret;
}
template <typename... T>
inline int PushOne(double arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
{
float a = (float)arg;
return Push(amx_ftoc(a));
}
return ret;
}
// TL;DR: BAD
template <typename... T>
inline int PushOne(char* arg, T... args) = delete;
template <typename... T>
inline int PushOne(StringView arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
return PushString(nullptr, nullptr, arg, false, false);
return ret;
}
template <typename U, size_t N, typename... T>
inline int PushOne(StaticArray<U, N> const& arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
return PushArray(nullptr, nullptr, arg.data(), arg.size());
return ret;
}
template <typename U, typename... T>
inline int PushOne(std::vector<U> const& arg, T... args)
{
int ret = PushOne(args...);
if (ret == AMX_ERR_NONE)
return PushArray(nullptr, nullptr, arg.data(), arg.size());
return ret;
}
};
struct PawnEventHandler
{
virtual void onAmxLoad(IPawnScript& script) = 0;
virtual void onAmxUnload(IPawnScript& script) = 0;
};
static const UID PawnComponent_UID = UID(0x78906cd9f19c36a6);
struct IPawnComponent : public IComponent
{
PROVIDE_UID(PawnComponent_UID);
/// Get the PawnEventHandler event dispatcher
virtual IEventDispatcher<PawnEventHandler>& getEventDispatcher() = 0;
virtual const StaticArray<void*, NUM_AMX_FUNCS>& getAmxFunctions() const = 0;
virtual IPawnScript const* getScript(AMX* amx) const = 0;
virtual IPawnScript* getScript(AMX* amx) = 0;
/// Get a set of all the available scripts.
virtual IPawnScript* mainScript() = 0;
virtual const Span<IPawnScript*> sideScripts() = 0;
};