Skip to content

Commit d5a3fc9

Browse files
HualY_Less
authored and
Y_Less
committed
Add warning if a plugin tries to register an AMX native whose name is registered
1 parent 7dc02de commit d5a3fc9

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

Server/Components/Pawn/Plugin/Plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static const void
5757
reinterpret_cast<void*>(&amx_PushArray),
5858
reinterpret_cast<void*>(&amx_PushString),
5959
reinterpret_cast<void*>(&amx_RaiseError),
60-
reinterpret_cast<void*>(&amx_Register),
60+
reinterpret_cast<void*>(&amx_RegisterChecked),
6161
reinterpret_cast<void*>(&amx_Release),
6262
reinterpret_cast<void*>(&amx_SetCallback),
6363
reinterpret_cast<void*>(&amx_SetDebugHook),

Server/Components/Pawn/Script/Script.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdarg.h>
1818

1919
#include "Script.hpp"
20+
#include "../Manager/Manager.hpp"
2021

2122
extern "C"
2223
{
@@ -307,3 +308,51 @@ int AMXAPI amx_StrSize(const cell* cstr, int* length)
307308
} /* if */
308309
return AMX_ERR_NONE;
309310
}
311+
312+
static AMX_NATIVE findfunction(const char* name, const AMX_NATIVE_INFO* list, int number)
313+
{
314+
int i;
315+
316+
assert(list != NULL);
317+
for (i = 0; list[i].name != NULL && (i < number || number == -1); i++)
318+
if (strcmp(name, list[i].name) == 0)
319+
return list[i].func;
320+
return NULL;
321+
}
322+
323+
int AMXAPI amx_RegisterChecked(AMX* amx, const AMX_NATIVE_INFO* list, int number)
324+
{
325+
AMX_FUNCPART* func;
326+
AMX_HEADER* hdr;
327+
int i, numnatives, err;
328+
AMX_NATIVE funcptr;
329+
330+
hdr = (AMX_HEADER*)amx->base;
331+
assert(hdr != NULL);
332+
assert(hdr->magic == AMX_MAGIC);
333+
assert(hdr->natives <= hdr->libraries);
334+
numnatives = NUMENTRIES(hdr, natives, libraries);
335+
336+
err = AMX_ERR_NONE;
337+
func = GETENTRY(hdr, natives, 0);
338+
for (i = 0; i < numnatives; i++)
339+
{
340+
funcptr = (list != NULL) ? findfunction(GETENTRYNAME(hdr, func), list, number) : NULL;
341+
if (func->address == 0)
342+
{
343+
/* this function is not yet located */
344+
if (funcptr != NULL)
345+
((AMX_FUNCWIDE*)func)->address = funcptr;
346+
else
347+
err = AMX_ERR_NOTFOUND;
348+
}
349+
else if (funcptr != NULL && PawnManager::Get()->core)
350+
{
351+
PawnManager::Get()->core->logLn(LogLevel::Warning, "Tried to register native which is already registered: %s", GETENTRYNAME(hdr, func));
352+
} /* if */
353+
func = (AMX_FUNCPART*)((unsigned char*)func + hdr->defsize);
354+
} /* for */
355+
if (err == AMX_ERR_NONE)
356+
amx->flags |= AMX_FLAG_NTVREG;
357+
return err;
358+
}

Server/Components/Pawn/Script/Script.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using namespace Impl;
2727
int AMXAPI amx_GetNativeByIndex(AMX const* amx, int index, AMX_NATIVE_INFO* ret);
2828
int AMXAPI amx_MakeAddr(AMX* amx, cell* phys_addr, cell* amx_addr);
2929
int AMXAPI amx_StrSize(const cell* cstr, int* length);
30+
int AMXAPI amx_RegisterChecked(AMX* amx, const AMX_NATIVE_INFO* list, int number);
3031

3132
enum DefaultReturnValue
3233
{
@@ -81,12 +82,12 @@ class PawnScript
8182
inline int PushArray(cell* amx_addr, cell** phys_addr, const cell array[], int numcells) { return amx_PushArray(&amx_, amx_addr, phys_addr, array, numcells); }
8283
inline int PushString(cell* amx_addr, cell** phys_addr, StringView string, bool pack, bool use_wchar) { return amx_PushStringLen(&amx_, amx_addr, phys_addr, string.data(), string.length(), pack, use_wchar); }
8384
inline int RaiseError(int error) { return amx_RaiseError(&amx_, error); }
84-
inline int Register(const AMX_NATIVE_INFO* nativelist, int number) { return amx_Register(&amx_, nativelist, number); }
85+
inline int Register(const AMX_NATIVE_INFO* nativelist, int number) { return amx_RegisterChecked(&amx_, nativelist, number); }
8586
inline int Register(char const _FAR* name, AMX_NATIVE func)
8687
{
8788
AMX_NATIVE_INFO
8889
nativelist = { name, func };
89-
return amx_Register(&amx_, &nativelist, 1);
90+
return amx_RegisterChecked(&amx_, &nativelist, 1);
9091
}
9192
inline int Release(cell amx_addr) { return amx_Release(&amx_, amx_addr); }
9293
inline int SetCallback(AMX_CALLBACK callback) { return amx_SetCallback(&amx_, callback); }

Server/Components/Pawn/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static StaticArray<void*, NUM_AMX_FUNCS> AMX_FUNCTIONS = {
5151
reinterpret_cast<void*>(&amx_PushArray),
5252
reinterpret_cast<void*>(&amx_PushString),
5353
reinterpret_cast<void*>(&amx_RaiseError),
54-
reinterpret_cast<void*>(&amx_Register),
54+
reinterpret_cast<void*>(&amx_RegisterChecked),
5555
reinterpret_cast<void*>(&amx_Release),
5656
reinterpret_cast<void*>(&amx_SetCallback),
5757
reinterpret_cast<void*>(&amx_SetDebugHook),

0 commit comments

Comments
 (0)