diff --git a/SDK/amx/amx.h b/SDK/amx/amx.h new file mode 100644 index 0000000..14b76c4 --- /dev/null +++ b/SDK/amx/amx.h @@ -0,0 +1,439 @@ +/* Pawn Abstract Machine (for the Pawn language) + * + * Copyright (c) ITB CompuPhase, 1997-2005 + * + * This software is provided "as-is", without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from + * the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in + * a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Version: $Id: amx.h,v 1.5 2006/03/26 16:56:15 spookie Exp $ + */ + +#if defined FREEBSD && !defined __FreeBSD__ + #define __FreeBSD__ +#endif +#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ + #include +#endif + +#ifndef AMX_H_INCLUDED +#define AMX_H_INCLUDED + +#if defined HAVE_STDINT_H + #include +#else + #if defined __LCC__ || defined __DMC__ || defined LINUX + #if defined HAVE_INTTYPES_H + #include + #else + #include + #endif + #elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L + /* The ISO C99 defines the int16_t and int_32t types. If the compiler got + * here, these types are probably undefined. + */ + #if defined __MACH__ + #include + typedef unsigned short int uint16_t; + typedef unsigned long int uint32_t; + #elif defined __FreeBSD__ + #include + #else + typedef short int int16_t; + typedef unsigned short int uint16_t; + #if defined SN_TARGET_PS2 + typedef int int32_t; + typedef unsigned int uint32_t; + #else + typedef long int int32_t; + typedef unsigned long int uint32_t; + #endif + #if defined __WIN32__ || defined _WIN32 || defined WIN32 + typedef __int64 int64_t; + typedef unsigned __int64 uint64_t; + #define HAVE_I64 + #elif defined __GNUC__ + typedef long long int64_t; + typedef unsigned long long uint64_t; + #define HAVE_I64 + #endif + #endif + #endif + #define HAVE_STDINT_H +#endif +#if defined _LP64 || defined WIN64 || defined _WIN64 + #if !defined __64BIT__ + #define __64BIT__ + #endif +#endif + +#if HAVE_ALLOCA_H + #include +#endif +#if defined __WIN32__ || defined _WIN32 || defined WIN32 /* || defined __MSDOS__ */ + #if !defined alloca + #define alloca(n) _alloca(n) + #endif +#endif + +#if !defined arraysize + #define arraysize(array) (sizeof(array) / sizeof((array)[0])) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined PAWN_DLL + #if !defined AMX_NATIVE_CALL + #define AMX_NATIVE_CALL __stdcall + #endif + #if !defined AMXAPI + #define AMXAPI __stdcall + #endif +#endif + +/* calling convention for native functions */ +#if !defined AMX_NATIVE_CALL + #define AMX_NATIVE_CALL +#endif +/* calling convention for all interface functions and callback functions */ +#if !defined AMXAPI + #if defined STDECL + #define AMXAPI __stdcall + #elif defined CDECL + #define AMXAPI __cdecl + #elif defined GCC_HASCLASSVISIBILITY + #define AMXAPI __attribute__ ((visibility("default"))) + #else + #define AMXAPI + #endif +#endif +#if !defined AMXEXPORT + #define AMXEXPORT +#endif + +/* File format version Required AMX version + * 0 (original version) 0 + * 1 (opcodes JUMP.pri, SWITCH and CASETBL) 1 + * 2 (compressed files) 2 + * 3 (public variables) 2 + * 4 (opcodes SWAP.pri/alt and PUSHADDR) 4 + * 5 (tagnames table) 4 + * 6 (reformatted header) 6 + * 7 (name table, opcodes SYMTAG & SYSREQ.D) 7 + * 8 (opcode STMT, renewed debug interface) 8 + */ +#define CUR_FILE_VERSION 8 /* current file version; also the current AMX version */ +#define MIN_FILE_VERSION 6 /* lowest supported file format version for the current AMX version */ +#define MIN_AMX_VERSION 8 /* minimum AMX version needed to support the current file format */ + +#if !defined PAWN_CELL_SIZE + #define PAWN_CELL_SIZE 32 /* by default, use 32-bit cells */ +#endif +#if PAWN_CELL_SIZE==16 + typedef uint16_t ucell; + typedef int16_t cell; +#elif PAWN_CELL_SIZE==32 + typedef uint32_t ucell; + typedef int32_t cell; +#elif PAWN_CELL_SIZE==64 + typedef uint64_t ucell; + typedef int64_t cell; +#else + #error Unsupported cell size (PAWN_CELL_SIZE) +#endif + +#define UNPACKEDMAX ((1L << (sizeof(cell)-1)*8) - 1) +#define UNLIMITED (~1u >> 1) + +struct tagAMX; +typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct tagAMX *amx, cell *params); +typedef int (AMXAPI *AMX_CALLBACK)(struct tagAMX *amx, cell index, + cell *result, cell *params); +typedef int (AMXAPI *AMX_DEBUG)(struct tagAMX *amx); +#if !defined _FAR + #define _FAR +#endif + +#if defined _MSC_VER + #pragma warning(disable:4103) /* disable warning message 4103 that complains + * about pragma pack in a header file */ + #pragma warning(disable:4100) /* "'%$S' : unreferenced formal parameter" */ +#endif + +/* Some compilers do not support the #pragma align, which should be fine. Some + * compilers give a warning on unknown #pragmas, which is not so fine... + */ +#if (defined SN_TARGET_PS2 || defined __GNUC__) && !defined AMX_NO_ALIGN + #define AMX_NO_ALIGN +#endif + +#if defined __GNUC__ + #define PACKED __attribute__((packed)) +#else + #define PACKED +#endif + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=mac68k + #else + #pragma pack(push) + #pragma pack(1) /* structures must be packed (byte-aligned) */ + #if defined __TURBOC__ + #pragma option -a- /* "pack" pragma for older Borland compilers */ + #endif + #endif +#endif + +typedef struct tagAMX_NATIVE_INFO { + const char _FAR *name PACKED; + AMX_NATIVE func PACKED; +} PACKED AMX_NATIVE_INFO; + +#define AMX_USERNUM 4 +#define sEXPMAX 19 /* maximum name length for file version <= 6 */ +#define sNAMEMAX 31 /* maximum name length of symbol name */ + +typedef struct tagAMX_FUNCSTUB { + ucell address PACKED; + char name[sEXPMAX+1] PACKED; +} PACKED AMX_FUNCSTUB; + +typedef struct tagFUNCSTUBNT { + ucell address PACKED; + uint32_t nameofs PACKED; +} PACKED AMX_FUNCSTUBNT; + +/* The AMX structure is the internal structure for many functions. Not all + * fields are valid at all times; many fields are cached in local variables. + */ +typedef struct tagAMX { + unsigned char _FAR *base PACKED; /* points to the AMX header plus the code, optionally also the data */ + unsigned char _FAR *data PACKED; /* points to separate data+stack+heap, may be NULL */ + AMX_CALLBACK callback PACKED; + AMX_DEBUG debug PACKED; /* debug callback */ + /* for external functions a few registers must be accessible from the outside */ + cell cip PACKED; /* instruction pointer: relative to base + amxhdr->cod */ + cell frm PACKED; /* stack frame base: relative to base + amxhdr->dat */ + cell hea PACKED; /* top of the heap: relative to base + amxhdr->dat */ + cell hlw PACKED; /* bottom of the heap: relative to base + amxhdr->dat */ + cell stk PACKED; /* stack pointer: relative to base + amxhdr->dat */ + cell stp PACKED; /* top of the stack: relative to base + amxhdr->dat */ + int flags PACKED; /* current status, see amx_Flags() */ + /* user data */ + long usertags[AMX_USERNUM] PACKED; + void _FAR *userdata[AMX_USERNUM] PACKED; + /* native functions can raise an error */ + int error PACKED; + /* passing parameters requires a "count" field */ + int paramcount; + /* the sleep opcode needs to store the full AMX status */ + cell pri PACKED; + cell alt PACKED; + cell reset_stk PACKED; + cell reset_hea PACKED; + cell sysreq_d PACKED; /* relocated address/value for the SYSREQ.D opcode */ + #if defined JIT + /* support variables for the JIT */ + int reloc_size PACKED; /* required temporary buffer for relocations */ + long code_size PACKED; /* estimated memory footprint of the native code */ + #endif +} PACKED AMX; + +/* The AMX_HEADER structure is both the memory format as the file format. The + * structure is used internaly. + */ +typedef struct tagAMX_HEADER { + int32_t size PACKED; /* size of the "file" */ + uint16_t magic PACKED; /* signature */ + char file_version PACKED; /* file format version */ + char amx_version PACKED; /* required version of the AMX */ + int16_t flags PACKED; + int16_t defsize PACKED; /* size of a definition record */ + int32_t cod PACKED; /* initial value of COD - code block */ + int32_t dat PACKED; /* initial value of DAT - data block */ + int32_t hea PACKED; /* initial value of HEA - start of the heap */ + int32_t stp PACKED; /* initial value of STP - stack top */ + int32_t cip PACKED; /* initial value of CIP - the instruction pointer */ + int32_t publics PACKED; /* offset to the "public functions" table */ + int32_t natives PACKED; /* offset to the "native functions" table */ + int32_t libraries PACKED; /* offset to the table of libraries */ + int32_t pubvars PACKED; /* the "public variables" table */ + int32_t tags PACKED; /* the "public tagnames" table */ + int32_t nametable PACKED; /* name table */ +} PACKED AMX_HEADER; + +#if PAWN_CELL_SIZE==16 + #define AMX_MAGIC 0xf1e2 +#elif PAWN_CELL_SIZE==32 + #define AMX_MAGIC 0xf1e0 +#elif PAWN_CELL_SIZE==64 + #define AMX_MAGIC 0xf1e1 +#endif + +enum { + AMX_ERR_NONE, + /* reserve the first 15 error codes for exit codes of the abstract machine */ + AMX_ERR_EXIT, /* forced exit */ + AMX_ERR_ASSERT, /* assertion failed */ + AMX_ERR_STACKERR, /* stack/heap collision */ + AMX_ERR_BOUNDS, /* index out of bounds */ + AMX_ERR_MEMACCESS, /* invalid memory access */ + AMX_ERR_INVINSTR, /* invalid instruction */ + AMX_ERR_STACKLOW, /* stack underflow */ + AMX_ERR_HEAPLOW, /* heap underflow */ + AMX_ERR_CALLBACK, /* no callback, or invalid callback */ + AMX_ERR_NATIVE, /* native function failed */ + AMX_ERR_DIVIDE, /* divide by zero */ + AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */ + AMX_ERR_INVSTATE, /* invalid state for this access */ + + AMX_ERR_MEMORY = 16, /* out of memory */ + AMX_ERR_FORMAT, /* invalid file format */ + AMX_ERR_VERSION, /* file is for a newer version of the AMX */ + AMX_ERR_NOTFOUND, /* function not found */ + AMX_ERR_INDEX, /* invalid index parameter (bad entry point) */ + AMX_ERR_DEBUG, /* debugger cannot run */ + AMX_ERR_INIT, /* AMX not initialized (or doubly initialized) */ + AMX_ERR_USERDATA, /* unable to set user data field (table full) */ + AMX_ERR_INIT_JIT, /* cannot initialize the JIT */ + AMX_ERR_PARAMS, /* parameter error */ + AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */ + AMX_ERR_GENERAL, /* general error (unknown or unspecific error) */ +}; + +/* AMX_FLAG_CHAR16 0x01 no longer used */ +#define AMX_FLAG_DEBUG 0x02 /* symbolic info. available */ +#define AMX_FLAG_COMPACT 0x04 /* compact encoding */ +#define AMX_FLAG_BYTEOPC 0x08 /* opcode is a byte (not a cell) */ +#define AMX_FLAG_NOCHECKS 0x10 /* no array bounds checking; no STMT opcode */ +#define AMX_FLAG_NTVREG 0x1000 /* all native functions are registered */ +#define AMX_FLAG_JITC 0x2000 /* abstract machine is JIT compiled */ +#define AMX_FLAG_BROWSE 0x4000 /* busy browsing */ +#define AMX_FLAG_RELOC 0x8000 /* jump/call addresses relocated */ + +#define AMX_EXEC_MAIN -1 /* start at program entry point */ +#define AMX_EXEC_CONT -2 /* continue from last address */ + +#define AMX_USERTAG(a,b,c,d) ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24)) + +#if !defined AMX_COMPACTMARGIN + #define AMX_COMPACTMARGIN 64 +#endif + +/* for native functions that use floating point parameters, the following + * two macros are convenient for casting a "cell" into a "float" type _without_ + * changing the bit pattern + */ +#if PAWN_CELL_SIZE==32 + #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */ + #define amx_ctof(c) ( * ((float*)&c) ) /* cell to float */ +#elif PAWN_CELL_SIZE==64 + #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */ + #define amx_ctof(c) ( * ((double*)&c) ) /* cell to float */ +#else + #error Unsupported cell size +#endif + +#define amx_StrParam(amx,param,result) \ + do { \ + cell *amx_cstr_; int amx_length_; \ + amx_GetAddr((amx), (param), &amx_cstr_); \ + amx_StrLen(amx_cstr_, &amx_length_); \ + if (amx_length_ > 0 && \ + ((result) = (char*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) \ + amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_ + 1); \ + else (result) = NULL; \ + } while (0) + +uint16_t * AMXAPI amx_Align16(uint16_t *v); +uint32_t * AMXAPI amx_Align32(uint32_t *v); +#if defined _I64_MAX || defined HAVE_I64 + uint64_t * AMXAPI amx_Align64(uint64_t *v); +#endif +int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr); +int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params); +int AMXAPI amx_Cleanup(AMX *amx); +int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data); +int AMXAPI amx_Exec(AMX *amx, cell *retval, int index); +int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index); +int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index); +int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr); +int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname); +int AMXAPI amx_Flags(AMX *amx,uint16_t *flags); +int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr); +int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr); +int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar, size_t size); +int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id); +int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr); +int AMXAPI amx_Init(AMX *amx, void *program); +int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code); +int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap); +int AMXAPI amx_NameLength(AMX *amx, int *length); +AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name, AMX_NATIVE func); +int AMXAPI amx_NumNatives(AMX *amx, int *number); +int AMXAPI amx_NumPublics(AMX *amx, int *number); +int AMXAPI amx_NumPubVars(AMX *amx, int *number); +int AMXAPI amx_NumTags(AMX *amx, int *number); +int AMXAPI amx_Push(AMX *amx, cell value); +int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells); +int AMXAPI amx_PushString(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar); +int AMXAPI amx_RaiseError(AMX *amx, int error); +int AMXAPI amx_Register(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number); +int AMXAPI amx_Release(AMX *amx, cell amx_addr); +int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback); +int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug); +int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar, size_t size); +int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr); +int AMXAPI amx_StrLen(const cell *cstring, int *length); +int AMXAPI amx_UTF8Check(const char *string, int *length); +int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value); +int AMXAPI amx_UTF8Len(const cell *cstr, int *length); +int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value); + +#if PAWN_CELL_SIZE==16 + #define amx_AlignCell(v) amx_Align16(v) +#elif PAWN_CELL_SIZE==32 + #define amx_AlignCell(v) amx_Align32(v) +#elif PAWN_CELL_SIZE==64 && (defined _I64_MAX || defined HAVE_I64) + #define amx_AlignCell(v) amx_Align64(v) +#else + #error Unsupported cell size +#endif + +#define amx_RegisterFunc(amx, name, func) \ + amx_Register((amx), amx_NativeInfo((name),(func)), 1); + +#if !defined AMX_NO_ALIGN + #if defined LINUX || defined __FreeBSD__ + #pragma pack() /* reset default packing */ + #elif defined MACOS && defined __MWERKS__ + #pragma options align=reset + #else + #pragma pack(pop) /* reset previous packing */ + #endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* AMX_H_INCLUDED */ diff --git a/SDK/amx/getch.c b/SDK/amx/getch.c new file mode 100644 index 0000000..3221ae8 --- /dev/null +++ b/SDK/amx/getch.c @@ -0,0 +1,80 @@ +/* Extremely inefficient but portable POSIX getch() */ +#ifndef WIN32 + +#include +#include +#include /* for tcgetattr() and tcsetattr() */ +#include /* for read() */ +#include +#include +#include +#include "getch.h" + +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif + +int +getch (void) +{ + struct termios save_termios; + struct termios ios; + int c = 0; + + if (!isatty (STDIN_FILENO)) + return EOF; + + if (tcgetattr (STDIN_FILENO, &save_termios) < 0) + return EOF; + + ios = save_termios; + ios.c_lflag &= ~(ICANON | ECHO | ISIG); + ios.c_cc[VMIN] = 1; /* read() will return with one char */ + ios.c_cc[VTIME] = 0; /* read() blocks forever */ + + if (tcsetattr (STDIN_FILENO, TCSANOW, &ios) < 0) + return EOF; + + if (read (STDIN_FILENO, &c, 1) != 1) + c = EOF; + + tcsetattr (STDIN_FILENO, TCSANOW, &save_termios); + + return c; +} + +int +kbhit (void) +{ + struct termios save_termios; + struct termios ios; + fd_set inp; + struct timeval timeout = {0, 0}; + int result; + + if (!isatty (STDIN_FILENO)) + return 0; + + if (tcgetattr (STDIN_FILENO, &save_termios) < 0) + return 0; + + ios = save_termios; + ios.c_lflag &= ~(ICANON | ECHO | ISIG); + ios.c_cc[VMIN] = 1; /* read() will return with one char */ + ios.c_cc[VTIME] = 0; /* read() blocks forever */ + + if (tcsetattr (STDIN_FILENO, TCSANOW, &ios) < 0) + return 0; + + /* set up select() args */ + FD_ZERO(&inp); + FD_SET(STDIN_FILENO, &inp); + + result = select (STDIN_FILENO+1, &inp, NULL, NULL, &timeout) == 1; + + tcsetattr (STDIN_FILENO, TCSANOW, &save_termios); + + return result; +} + +#endif diff --git a/SDK/amx/getch.h b/SDK/amx/getch.h new file mode 100644 index 0000000..71cdd83 --- /dev/null +++ b/SDK/amx/getch.h @@ -0,0 +1,15 @@ +/* Extremely inefficient but portable POSIX getch(), see getch.c */ +#ifndef GETCH_H +#define GETCH_H + +#if defined __cplusplus + extern "C" { +#endif +int getch(void); +int kbhit(void); + +#if defined __cplusplus +} +#endif + +#endif /* GETCH_H */ diff --git a/SDK/amx/sclinux.h b/SDK/amx/sclinux.h new file mode 100644 index 0000000..f22497a --- /dev/null +++ b/SDK/amx/sclinux.h @@ -0,0 +1,47 @@ +/* + * Things needed to compile under linux. + * + * Should be reworked totally to use GNU's 'configure' + */ +#ifndef SCLINUX_H +#define SCLINUX_H + +/* getchar() is not a 'cool' replacement for MSDOS getch: Linux/unix depends on the features activated or not about the + * controlling terminal's tty. This means that ioctl(2) calls must be performed, for instance to have the controlling + * terminal tty's in 'raw' mode, if we want to be able to fetch a single character. This also means that everything must + * be put back correctly when the function ends. See GETCH.C for an implementation. + * + * For interactive use of SRUN/SDBG if would be much better to use GNU's readline package: the user would be able to + * have a complete emacs/vi like line editing system. + */ +#include "getch.h" + +#define stricmp(a,b) strcasecmp(a,b) +#define strnicmp(a,b,c) strncasecmp(a,b,c) + +/* + * WinWorld wants '\'. Unices do not. + */ +#define DIRECTORY_SEP_CHAR '/' +#define DIRECTORY_SEP_STR "/" + +/* + * SC assumes that a computer is Little Endian unless told otherwise. It uses + * (and defines) the macros BYTE_ORDER and BIG_ENDIAN. + * For Linux, we must overrule these settings with those defined in glibc. + */ +#if !defined __BYTE_ORDER +# include +#endif + +#if defined __OpenBSD__ || defined __FreeBSD__ +# define __BYTE_ORDER BYTE_ORDER +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __BIG_ENDIAN BIG_ENDIAN +#endif + +#if !defined __BYTE_ORDER +# error "Can't figure computer byte order (__BYTE_ORDER macro not found)" +#endif + +#endif /* SCLINUX_H */ diff --git a/SDK/amxplugin.cpp b/SDK/amxplugin.cpp new file mode 100644 index 0000000..930c8c3 --- /dev/null +++ b/SDK/amxplugin.cpp @@ -0,0 +1,630 @@ +//---------------------------------------------------------- +// +// SA:MP Multiplayer Modification For GTA:SA +// Copyright 2004-2007 SA:MP Team +// +//---------------------------------------------------------- + +// +// This provides an interface to call amx library functions +// within samp-server. +// +// To use: +// Define the extern in your main source file: +// extern void *pAMXFunctions; +// And, in your Initialize function, assign: +// pAMXFunctions = data[PLUGIN_DATA_AMX]; +// +// +// WIN32: To regenerate thunks for calls from prototypes in amx.h +// Run a regex with: +// S: ^(.*)(AMXAPI amx_)([^(]*)([^\;]*);$ +// R: NUDE \1\2\3\4\n{\n\t_asm mov eax, pAMXFunctions;\n\t_asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_\3*4];\n}\n +// +// LINUX/BSD: To regenerate thunks for calls from prototypes in amx.h +// Run a regex with: +// S: ^(.*)(AMXAPI amx_)([^(]*)([^\;]*);$ +// R: typedef \1 AMXAPI (*amx_\3_t)\4;\n\1\2\3\4\n{\n\tamx_\3_t fn = ((amx_\3_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_\3];\n\treturn fn\4;\n}\n +// Modify internal function calls as nessesary +// + +//---------------------------------------------------------- + +#include "plugin.h" + +//---------------------------------------------------------- + +void *pAMXFunctions; + +//---------------------------------------------------------- + +#if (defined(WIN32) || defined(_WIN32)) && defined(_MSC_VER) + +// Optimized Inline Assembly Thunks for MS VC++ + +#define NUDE _declspec(naked) + +NUDE uint16_t * AMXAPI amx_Align16(uint16_t *v) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Align16*4]; +} + +NUDE uint32_t * AMXAPI amx_Align32(uint32_t *v) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Align32*4]; +} + +#if defined _I64_MAX || defined HAVE_I64 +NUDE uint64_t * AMXAPI amx_Align64(uint64_t *v) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Align64*4]; +} + +#endif +NUDE int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Allot*4]; +} + +NUDE int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Callback*4]; +} + +NUDE int AMXAPI amx_Cleanup(AMX *amx) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Cleanup*4]; +} + +NUDE int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Clone*4]; +} + +NUDE int AMXAPI amx_Exec(AMX *amx, cell *retval, int index) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Exec*4]; +} + +NUDE int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_FindNative*4]; +} + +NUDE int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_FindPublic*4]; +} + +NUDE int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_FindPubVar*4]; +} + +NUDE int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_FindTagId*4]; +} + +NUDE int AMXAPI amx_Flags(AMX *amx,uint16_t *flags) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Flags*4]; +} + +NUDE int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetAddr*4]; +} + +NUDE int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetNative*4]; +} + +NUDE int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetPublic*4]; +} + +NUDE int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetPubVar*4]; +} + +NUDE int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar, size_t size) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetString*4]; +} + +NUDE int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetTag*4]; +} + +NUDE int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_GetUserData*4]; +} + +NUDE int AMXAPI amx_Init(AMX *amx, void *program) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Init*4]; +} + +NUDE int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_InitJIT*4]; +} + +NUDE int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_MemInfo*4]; +} + +NUDE int AMXAPI amx_NameLength(AMX *amx, int *length) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NameLength*4]; +} + +NUDE AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name, AMX_NATIVE func) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NativeInfo*4]; +} + +NUDE int AMXAPI amx_NumNatives(AMX *amx, int *number) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NumNatives*4]; +} + +NUDE int AMXAPI amx_NumPublics(AMX *amx, int *number) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NumPublics*4]; +} + +NUDE int AMXAPI amx_NumPubVars(AMX *amx, int *number) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NumPubVars*4]; +} + +NUDE int AMXAPI amx_NumTags(AMX *amx, int *number) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_NumTags*4]; +} + +NUDE int AMXAPI amx_Push(AMX *amx, cell value) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Push*4]; +} + +NUDE int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_PushArray*4]; +} + +NUDE int AMXAPI amx_PushString(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_PushString*4]; +} + +NUDE int AMXAPI amx_RaiseError(AMX *amx, int error) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_RaiseError*4]; +} + +NUDE int AMXAPI amx_Register(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Register*4]; +} + +NUDE int AMXAPI amx_Release(AMX *amx, cell amx_addr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_Release*4]; +} + +NUDE int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_SetCallback*4]; +} + +NUDE int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_SetDebugHook*4]; +} + +NUDE int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar, size_t size) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_SetString*4]; +} + +NUDE int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_SetUserData*4]; +} + +NUDE int AMXAPI amx_StrLen(const cell *cstring, int *length) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_StrLen*4]; +} + +NUDE int AMXAPI amx_UTF8Check(const char *string, int *length) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_UTF8Check*4]; +} + +NUDE int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_UTF8Get*4]; +} + +NUDE int AMXAPI amx_UTF8Len(const cell *cstr, int *length) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_UTF8Len*4]; +} + +NUDE int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value) +{ + _asm mov eax, pAMXFunctions; + _asm jmp dword ptr [eax+PLUGIN_AMX_EXPORT_UTF8Put*4]; +} + +#else + +// Unoptimized Thunks (Linux/BSD/non MSVC++) + +typedef uint16_t * AMXAPI (*amx_Align16_t)(uint16_t *v); +uint16_t * AMXAPI amx_Align16(uint16_t *v) +{ + amx_Align16_t fn = ((amx_Align16_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Align16]; + return fn(v); +} + +typedef uint32_t * AMXAPI (*amx_Align32_t)(uint32_t *v); +uint32_t * AMXAPI amx_Align32(uint32_t *v) +{ + amx_Align32_t fn = ((amx_Align32_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Align32]; + return fn(v); +} + +#if defined _I64_MAX || defined HAVE_I64 +typedef uint64_t * AMXAPI (*amx_Align64_t)(uint64_t *v); + uint64_t * AMXAPI amx_Align64(uint64_t *v) +{ + amx_Align64_t fn = ((amx_Align64_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Align64]; + return fn(v); +} + +#endif +typedef int AMXAPI (*amx_Allot_t)(AMX *amx, int cells, cell *amx_addr, cell **phys_addr); +int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr) +{ + amx_Allot_t fn = ((amx_Allot_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Allot]; + return fn(amx, cells, amx_addr, phys_addr); +} + +typedef int AMXAPI (*amx_Callback_t)(AMX *amx, cell index, cell *result, cell *params); +int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params) +{ + amx_Callback_t fn = ((amx_Callback_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Callback]; + return fn(amx, index, result, params); +} + +typedef int AMXAPI (*amx_Cleanup_t)(AMX *amx); +int AMXAPI amx_Cleanup(AMX *amx) +{ + amx_Cleanup_t fn = ((amx_Cleanup_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Cleanup]; + return fn(amx); +} + +typedef int AMXAPI (*amx_Clone_t)(AMX *amxClone, AMX *amxSource, void *data); +int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data) +{ + amx_Clone_t fn = ((amx_Clone_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Clone]; + return fn(amxClone, amxSource, data); +} + +typedef int AMXAPI (*amx_Exec_t)(AMX *amx, cell *retval, int index); +int AMXAPI amx_Exec(AMX *amx, cell *retval, int index) +{ + amx_Exec_t fn = ((amx_Exec_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Exec]; + return fn(amx, retval, index); +} + +typedef int AMXAPI (*amx_FindNative_t)(AMX *amx, const char *name, int *index); +int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index) +{ + amx_FindNative_t fn = ((amx_FindNative_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_FindNative]; + return fn(amx, name, index); +} + +typedef int AMXAPI (*amx_FindPublic_t)(AMX *amx, const char *funcname, int *index); +int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index) +{ + amx_FindPublic_t fn = ((amx_FindPublic_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_FindPublic]; + return fn(amx, funcname, index); +} + +typedef int AMXAPI (*amx_FindPubVar_t)(AMX *amx, const char *varname, cell *amx_addr); +int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr) +{ + amx_FindPubVar_t fn = ((amx_FindPubVar_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_FindPubVar]; + return fn(amx, varname, amx_addr); +} + +typedef int AMXAPI (*amx_FindTagId_t)(AMX *amx, cell tag_id, char *tagname); +int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname) +{ + amx_FindTagId_t fn = ((amx_FindTagId_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_FindTagId]; + return fn(amx, tag_id, tagname); +} + +typedef int AMXAPI (*amx_Flags_t)(AMX *amx,uint16_t *flags); +int AMXAPI amx_Flags(AMX *amx,uint16_t *flags) +{ + amx_Flags_t fn = ((amx_Flags_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Flags]; + return fn(amx,flags); +} + +typedef int AMXAPI (*amx_GetAddr_t)(AMX *amx,cell amx_addr,cell **phys_addr); +int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr) +{ + amx_GetAddr_t fn = ((amx_GetAddr_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetAddr]; + return fn(amx,amx_addr,phys_addr); +} + +typedef int AMXAPI (*amx_GetNative_t)(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname) +{ + amx_GetNative_t fn = ((amx_GetNative_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetNative]; + return fn(amx, index, funcname); +} + +typedef int AMXAPI (*amx_GetPublic_t)(AMX *amx, int index, char *funcname); +int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname) +{ + amx_GetPublic_t fn = ((amx_GetPublic_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetPublic]; + return fn(amx, index, funcname); +} + +typedef int AMXAPI (*amx_GetPubVar_t)(AMX *amx, int index, char *varname, cell *amx_addr); +int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr) +{ + amx_GetPubVar_t fn = ((amx_GetPubVar_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetPubVar]; + return fn(amx, index, varname, amx_addr); +} + +typedef int AMXAPI (*amx_GetString_t)(char *dest,const cell *source, int use_wchar, size_t size); +int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar, size_t size) +{ + amx_GetString_t fn = ((amx_GetString_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetString]; + return fn(dest,source, use_wchar, size); +} + +typedef int AMXAPI (*amx_GetTag_t)(AMX *amx, int index, char *tagname, cell *tag_id); +int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id) +{ + amx_GetTag_t fn = ((amx_GetTag_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetTag]; + return fn(amx, index, tagname, tag_id); +} + +typedef int AMXAPI (*amx_GetUserData_t)(AMX *amx, long tag, void **ptr); +int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr) +{ + amx_GetUserData_t fn = ((amx_GetUserData_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_GetUserData]; + return fn(amx, tag, ptr); +} + +typedef int AMXAPI (*amx_Init_t)(AMX *amx, void *program); +int AMXAPI amx_Init(AMX *amx, void *program) +{ + amx_Init_t fn = ((amx_Init_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Init]; + return fn(amx, program); +} + +typedef int AMXAPI (*amx_InitJIT_t)(AMX *amx, void *reloc_table, void *native_code); +int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code) +{ + amx_InitJIT_t fn = ((amx_InitJIT_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_InitJIT]; + return fn(amx, reloc_table, native_code); +} + +typedef int AMXAPI (*amx_MemInfo_t)(AMX *amx, long *codesize, long *datasize, long *stackheap); +int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap) +{ + amx_MemInfo_t fn = ((amx_MemInfo_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_MemInfo]; + return fn(amx, codesize, datasize, stackheap); +} + +typedef int AMXAPI (*amx_NameLength_t)(AMX *amx, int *length); +int AMXAPI amx_NameLength(AMX *amx, int *length) +{ + amx_NameLength_t fn = ((amx_NameLength_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NameLength]; + return fn(amx, length); +} + +typedef AMX_NATIVE_INFO * AMXAPI (*amx_NativeInfo_t)(const char *name, AMX_NATIVE func); +AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name, AMX_NATIVE func) +{ + amx_NativeInfo_t fn = ((amx_NativeInfo_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NativeInfo]; + return fn(name, func); +} + +typedef int AMXAPI (*amx_NumNatives_t)(AMX *amx, int *number); +int AMXAPI amx_NumNatives(AMX *amx, int *number) +{ + amx_NumNatives_t fn = ((amx_NumNatives_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NumNatives]; + return fn(amx, number); +} + +typedef int AMXAPI (*amx_NumPublics_t)(AMX *amx, int *number); +int AMXAPI amx_NumPublics(AMX *amx, int *number) +{ + amx_NumPublics_t fn = ((amx_NumPublics_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NumPublics]; + return fn(amx, number); +} + +typedef int AMXAPI (*amx_NumPubVars_t)(AMX *amx, int *number); +int AMXAPI amx_NumPubVars(AMX *amx, int *number) +{ + amx_NumPubVars_t fn = ((amx_NumPubVars_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NumPubVars]; + return fn(amx, number); +} + +typedef int AMXAPI (*amx_NumTags_t)(AMX *amx, int *number); +int AMXAPI amx_NumTags(AMX *amx, int *number) +{ + amx_NumTags_t fn = ((amx_NumTags_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_NumTags]; + return fn(amx, number); +} + +typedef int AMXAPI (*amx_Push_t)(AMX *amx, cell value); +int AMXAPI amx_Push(AMX *amx, cell value) +{ + amx_Push_t fn = ((amx_Push_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Push]; + return fn(amx, value); +} + +typedef int AMXAPI (*amx_PushArray_t)(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells); +int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells) +{ + amx_PushArray_t fn = ((amx_PushArray_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_PushArray]; + return fn(amx, amx_addr, phys_addr, array, numcells); +} + +typedef int AMXAPI (*amx_PushString_t)(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar); +int AMXAPI amx_PushString(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar) +{ + amx_PushString_t fn = ((amx_PushString_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_PushString]; + return fn(amx, amx_addr, phys_addr, string, pack, use_wchar); +} + +typedef int AMXAPI (*amx_RaiseError_t)(AMX *amx, int error); +int AMXAPI amx_RaiseError(AMX *amx, int error) +{ + amx_RaiseError_t fn = ((amx_RaiseError_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_RaiseError]; + return fn(amx, error); +} + +typedef int AMXAPI (*amx_Register_t)(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number); +int AMXAPI amx_Register(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number) +{ + amx_Register_t fn = ((amx_Register_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Register]; + return fn(amx, nativelist, number); +} + +typedef int AMXAPI (*amx_Release_t)(AMX *amx, cell amx_addr); +int AMXAPI amx_Release(AMX *amx, cell amx_addr) +{ + amx_Release_t fn = ((amx_Release_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_Release]; + return fn(amx, amx_addr); +} + +typedef int AMXAPI (*amx_SetCallback_t)(AMX *amx, AMX_CALLBACK callback); +int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback) +{ + amx_SetCallback_t fn = ((amx_SetCallback_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_SetCallback]; + return fn(amx, callback); +} + +typedef int AMXAPI (*amx_SetDebugHook_t)(AMX *amx, AMX_DEBUG debug); +int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug) +{ + amx_SetDebugHook_t fn = ((amx_SetDebugHook_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_SetDebugHook]; + return fn(amx, debug); +} + +typedef int AMXAPI (*amx_SetString_t)(cell *dest, const char *source, int pack, int use_wchar, size_t size); +int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar, size_t size) +{ + amx_SetString_t fn = ((amx_SetString_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_SetString]; + return fn(dest, source, pack, use_wchar, size); +} + +typedef int AMXAPI (*amx_SetUserData_t)(AMX *amx, long tag, void *ptr); +int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr) +{ + amx_SetUserData_t fn = ((amx_SetUserData_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_SetUserData]; + return fn(amx, tag, ptr); +} + +typedef int AMXAPI (*amx_StrLen_t)(const cell *cstring, int *length); +int AMXAPI amx_StrLen(const cell *cstring, int *length) +{ + amx_StrLen_t fn = ((amx_StrLen_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_StrLen]; + return fn(cstring, length); +} + +typedef int AMXAPI (*amx_UTF8Check_t)(const char *string, int *length); +int AMXAPI amx_UTF8Check(const char *string, int *length) +{ + amx_UTF8Check_t fn = ((amx_UTF8Check_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_UTF8Check]; + return fn(string, length); +} + +typedef int AMXAPI (*amx_UTF8Get_t)(const char *string, const char **endptr, cell *value); +int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value) +{ + amx_UTF8Get_t fn = ((amx_UTF8Get_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_UTF8Get]; + return fn(string, endptr, value); +} + +typedef int AMXAPI (*amx_UTF8Len_t)(const cell *cstr, int *length); +int AMXAPI amx_UTF8Len(const cell *cstr, int *length) +{ + amx_UTF8Len_t fn = ((amx_UTF8Len_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_UTF8Len]; + return fn(cstr, length); +} + +typedef int AMXAPI (*amx_UTF8Put_t)(char *string, char **endptr, int maxchars, cell value); +int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value) +{ + amx_UTF8Put_t fn = ((amx_UTF8Put_t*)pAMXFunctions)[PLUGIN_AMX_EXPORT_UTF8Put]; + return fn(string, endptr, maxchars, value); +} + +#endif + +//---------------------------------------------------------- +// EOF diff --git a/SDK/plugin.h b/SDK/plugin.h new file mode 100644 index 0000000..7bb7133 --- /dev/null +++ b/SDK/plugin.h @@ -0,0 +1,12 @@ +//---------------------------------------------------------- +// +// SA:MP Multiplayer Modification For GTA:SA +// Copyright 2004-2006 SA:MP Team +// +//---------------------------------------------------------- + +#include "plugincommon.h" +#include "amx/amx.h" + +//---------------------------------------------------------- +// EOF diff --git a/SDK/plugincommon.h b/SDK/plugincommon.h new file mode 100644 index 0000000..47023fe --- /dev/null +++ b/SDK/plugincommon.h @@ -0,0 +1,123 @@ +//---------------------------------------------------------- +// +// SA:MP Multiplayer Modification For GTA:SA +// Copyright 2004-2006 SA:MP Team +// +//---------------------------------------------------------- + +#ifndef _PLUGINCOMMON_H_INCLUDED +#define _PLUGINCOMMON_H_INCLUDED + +//---------------------------------------------------------- + +#define SAMP_PLUGIN_VERSION 0x0200 + +//---------------------------------------------------------- + +#ifdef __cplusplus + #define PLUGIN_EXTERN_C extern "C" +#else + #define PLUGIN_EXTERN_C +#endif + +#if defined(LINUX) || defined(FREEBSD) || defined(__FreeBSD__) || defined(__OpenBSD__) + #ifndef __GNUC__ + #pragma message "Warning: Not using a GNU compiler." + #endif + #define PLUGIN_CALL + #ifndef SAMPSVR + // Compile code with -fvisibility=hidden to hide non-exported functions. + #define PLUGIN_EXPORT PLUGIN_EXTERN_C __attribute__((visibility("default"))) + #else + #define PLUGIN_EXPORT PLUGIN_EXTERN_C + #endif +#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) + #ifndef _MSC_VER + #pragma message "Warning: Not using a VC++ compiler." + #endif + #define PLUGIN_CALL __stdcall + #define PLUGIN_EXPORT PLUGIN_EXTERN_C +#else + #error "You must define one of WIN32, LINUX or FREEBSD" +#endif + +//---------------------------------------------------------- + +enum SUPPORTS_FLAGS +{ + SUPPORTS_VERSION = SAMP_PLUGIN_VERSION, + SUPPORTS_VERSION_MASK = 0xffff, + SUPPORTS_AMX_NATIVES = 0x10000, + SUPPORTS_PROCESS_TICK = 0x20000, +}; + +//---------------------------------------------------------- + +enum PLUGIN_DATA_TYPE +{ + // For some debugging + PLUGIN_DATA_LOGPRINTF = 0x00, // void (*logprintf)(char* format, ...) + + // AMX + PLUGIN_DATA_AMX_EXPORTS = 0x10, // void* AmxFunctionTable[] (see PLUGIN_AMX_EXPORT) + PLUGIN_DATA_CALLPUBLIC_FS = 0x11, // int (*AmxCallPublicFilterScript)(char *szFunctionName) + PLUGIN_DATA_CALLPUBLIC_GM = 0x12, // int (*AmxCallPublicGameMode)(char *szFunctionName) + +}; + +//---------------------------------------------------------- + +enum PLUGIN_AMX_EXPORT +{ + PLUGIN_AMX_EXPORT_Align16 = 0, + PLUGIN_AMX_EXPORT_Align32 = 1, + PLUGIN_AMX_EXPORT_Align64 = 2, + PLUGIN_AMX_EXPORT_Allot = 3, + PLUGIN_AMX_EXPORT_Callback = 4, + PLUGIN_AMX_EXPORT_Cleanup = 5, + PLUGIN_AMX_EXPORT_Clone = 6, + PLUGIN_AMX_EXPORT_Exec = 7, + PLUGIN_AMX_EXPORT_FindNative = 8, + PLUGIN_AMX_EXPORT_FindPublic = 9, + PLUGIN_AMX_EXPORT_FindPubVar = 10, + PLUGIN_AMX_EXPORT_FindTagId = 11, + PLUGIN_AMX_EXPORT_Flags = 12, + PLUGIN_AMX_EXPORT_GetAddr = 13, + PLUGIN_AMX_EXPORT_GetNative = 14, + PLUGIN_AMX_EXPORT_GetPublic = 15, + PLUGIN_AMX_EXPORT_GetPubVar = 16, + PLUGIN_AMX_EXPORT_GetString = 17, + PLUGIN_AMX_EXPORT_GetTag = 18, + PLUGIN_AMX_EXPORT_GetUserData = 19, + PLUGIN_AMX_EXPORT_Init = 20, + PLUGIN_AMX_EXPORT_InitJIT = 21, + PLUGIN_AMX_EXPORT_MemInfo = 22, + PLUGIN_AMX_EXPORT_NameLength = 23, + PLUGIN_AMX_EXPORT_NativeInfo = 24, + PLUGIN_AMX_EXPORT_NumNatives = 25, + PLUGIN_AMX_EXPORT_NumPublics = 26, + PLUGIN_AMX_EXPORT_NumPubVars = 27, + PLUGIN_AMX_EXPORT_NumTags = 28, + PLUGIN_AMX_EXPORT_Push = 29, + PLUGIN_AMX_EXPORT_PushArray = 30, + PLUGIN_AMX_EXPORT_PushString = 31, + PLUGIN_AMX_EXPORT_RaiseError = 32, + PLUGIN_AMX_EXPORT_Register = 33, + PLUGIN_AMX_EXPORT_Release = 34, + PLUGIN_AMX_EXPORT_SetCallback = 35, + PLUGIN_AMX_EXPORT_SetDebugHook = 36, + PLUGIN_AMX_EXPORT_SetString = 37, + PLUGIN_AMX_EXPORT_SetUserData = 38, + PLUGIN_AMX_EXPORT_StrLen = 39, + PLUGIN_AMX_EXPORT_UTF8Check = 40, + PLUGIN_AMX_EXPORT_UTF8Get = 41, + PLUGIN_AMX_EXPORT_UTF8Len = 42, + PLUGIN_AMX_EXPORT_UTF8Put = 43, +}; + +//---------------------------------------------------------- + +#endif // _PLUGINCOMMON_H_INCLUDED + +//---------------------------------------------------------- +// EOF diff --git a/socket.sln b/socket.sln new file mode 100644 index 0000000..c0b4ca1 --- /dev/null +++ b/socket.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "socket", "socket\socket.vcproj", "{100FDA1A-8D16-4E0E-BD4B-D97EA8540A3E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {100FDA1A-8D16-4E0E-BD4B-D97EA8540A3E}.Debug|Win32.ActiveCfg = Debug|Win32 + {100FDA1A-8D16-4E0E-BD4B-D97EA8540A3E}.Debug|Win32.Build.0 = Debug|Win32 + {100FDA1A-8D16-4E0E-BD4B-D97EA8540A3E}.Release|Win32.ActiveCfg = Release|Win32 + {100FDA1A-8D16-4E0E-BD4B-D97EA8540A3E}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/socket/CNatives.cpp b/socket/CNatives.cpp new file mode 100644 index 0000000..f185538 --- /dev/null +++ b/socket/CNatives.cpp @@ -0,0 +1,91 @@ +#pragma once + +#include "CNatives.h" + +CString* g_pString; +extern logprintf_t logprintf; +extern CSocket* g_pSocket; + +// native Socket:create_socket(); + +cell AMX_NATIVE_CALL n_create_socket(AMX* amx, cell* params) +{ + return (cell)g_pSocket->create_socket(params[1]); +} + +cell AMX_NATIVE_CALL n_bind_socket(AMX* amx, cell* params) +{ + char* szIP; + g_pString->Get(amx, params[2], szIP); + cell ret_val = g_pSocket->bind_socket(params[1], szIP); + free(szIP); + return ret_val; +} + +cell AMX_NATIVE_CALL n_set_max_connections(AMX* amx, cell* params) +{ + return (cell)g_pSocket->set_max_connections(params[1], params[2]); +} + +cell AMX_NATIVE_CALL n_connect_socket(AMX* amx, cell* params) +{ + char* szHost; + g_pString->Get(amx, params[2], szHost); + cell ret_val = g_pSocket->connect_socket(params[1], szHost, params[3]); + free(szHost); + return ret_val; +} + +cell AMX_NATIVE_CALL n_listen_socket(AMX* amx, cell* params) +{ + return (cell)g_pSocket->listen_socket(params[1], params[2]); +} + +cell AMX_NATIVE_CALL n_stop_listen_socket(AMX* amx, cell* params) +{ + return (cell)g_pSocket->stop_listen_socket(params[1]); +} + +cell AMX_NATIVE_CALL n_destroy_socket(AMX* amx, cell* params) +{ + return g_pSocket->destroy_socket(params[1]); +} + +cell AMX_NATIVE_CALL n_close_remote_connection(AMX* amx, cell* params) +{ + return g_pSocket->close_remote_connection(params[1], params[2]); +} + +cell AMX_NATIVE_CALL n_send_socket(AMX* amx, cell* params) +{ + char* szData; + g_pString->Get(amx, params[2], szData); + cell ret_val = g_pSocket->send_socket(params[1], szData, params[3]); + free(szData); + return ret_val; +} + +cell AMX_NATIVE_CALL n_sendto_remote_client(AMX* amx, cell* params) +{ + char* szData; + g_pString->Get(amx, params[3], szData); + cell ret_val = g_pSocket->sendto_remote_client(params[1], params[2], szData); + free(szData); + return ret_val; +} + +cell AMX_NATIVE_CALL n_is_remote_client_connected(AMX* amx, cell* params) +{ + return g_pSocket->is_remote_client_connected(params[1], params[2]); +} + +cell AMX_NATIVE_CALL n_is_socket_valid(AMX* amx, cell* params) +{ + return g_pSocket->is_socket_valid(params[1]); +} + +cell AMX_NATIVE_CALL n_get_remote_client_ip(AMX* amx, cell* params) +{ + g_pString->Set(amx, params[3], g_pSocket->get_remote_client_ip(params[1], params[2])); + return 1; +} \ No newline at end of file diff --git a/socket/CNatives.h b/socket/CNatives.h new file mode 100644 index 0000000..075bfc8 --- /dev/null +++ b/socket/CNatives.h @@ -0,0 +1,17 @@ +#pragma once + +#include "main.h" + +cell AMX_NATIVE_CALL n_create_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_bind_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_set_max_connections(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_connect_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_listen_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_stop_listen_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_destroy_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_close_remote_connection(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_send_socket(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_sendto_remote_client(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_is_remote_client_connected(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_is_socket_valid(AMX* amx, cell* params); +cell AMX_NATIVE_CALL n_get_remote_client_ip(AMX* amx, cell* params); \ No newline at end of file diff --git a/socket/CSocket.cpp b/socket/CSocket.cpp new file mode 100644 index 0000000..73d5bba --- /dev/null +++ b/socket/CSocket.cpp @@ -0,0 +1,427 @@ +#pragma once + +#include "CSocket.h" + +CThread* g_pThread; +extern CSocket* g_pSocket; +extern logprintf_t logprintf; + +CSocket::CSocket() +{ + // c'tor + g_pThread = new CThread(); + std::fill(m_pSocket, m_pSocket+(sizeof(m_pSocket)/4), -1); +#ifdef WIN32 + WORD wVersionRequested; + WSADATA wsaData; + wVersionRequested = MAKEWORD(2, 2); + int err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0 || LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { + logprintf("Winsock failed to initialize: %d\n", WSAGetLastError()); + this->~CSocket(); + } +#endif +} + +CSocket::~CSocket() +{ + // d'tor +#ifdef WIN32 + WSACleanup(); +#endif + for(int i = 0;i < MAX_SOCK;i++) { + if(m_pSocket[i] != (-1)) { + destroy_socket(i); + } + } + //g_pThread->Kill(m_ThreadHandle[]); +} + +int CSocket::create_socket(int type) +{ + int iSlot = find_free_slot(m_pSocket, (sizeof(m_pSocket)/4)); + if(type == SOCK_STREAM) + m_pSocketInfo[iSlot].tcp = true; + else + m_pSocketInfo[iSlot].tcp = false; + m_pSocket[iSlot] = socket(AF_INET, type, 0); + if(m_pSocket[iSlot] == -1) { + logprintf("socket_create(): Failed."); + return (-1); + } + m_pSocketInfo[iSlot].success = true; // initiates that the socket has been successfully created + return iSlot; +} + +int CSocket::find_free_slot(int* arr, int size) +{ + for(int i = 0;i < size;i++) { + if(arr[i] == (-1)) { + return i; + } + } + return 0xFFFF; +} + +int CSocket::set_max_connections(int socketid, int maxcon) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("set_max_connections(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + //m_pSocketInfo[socketid].max_clients = new int[maxcon]; + m_pSocketInfo[socketid].connected_clients = (int*)malloc(sizeof(int)*maxcon); + std::fill(m_pSocketInfo[socketid].connected_clients, m_pSocketInfo[socketid].connected_clients+maxcon, -1); + m_pSocketInfo[socketid].max_clients = maxcon; + return 1; +} + +int CSocket::connect_socket(int socketid, char* address, int port) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("socket_connect(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + struct sockaddr_in addr; + struct hostent *host; + host = gethostbyname(address); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr = *((struct in_addr *) host->h_addr); + if(strlen(m_pSocketInfo[socketid].bind_ip) > 0) { + sockaddr_in bind_addr; + bind_addr.sin_addr.s_addr = inet_addr(m_pSocketInfo[socketid].bind_ip); + bind_addr.sin_family = AF_INET; + if(bind(m_pSocket[socketid], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == -1) { + logprintf("socket_connect(): Socket ID %d has failed to bind IP %s.", socketid, m_pSocketInfo[socketid].bind_ip); + return 0; + } + } + if(connect(m_pSocket[socketid], (struct sockaddr *)&addr, sizeof(sockaddr)) == SOCKET_ERROR) { + logprintf("socket_connect(): Socket ID %d has failed to connect.", socketid); + return 0; + } + set_nonblocking_socket(m_pSocket[socketid]); + m_pSocketInfo[socketid].is_client = true; // that way the thread knows the socket will act as a client + m_pSocketInfo[socketid].active_thread = true; + g_pThread->Start(socket_receive_thread, (void*)socketid); + return 1; +} + +int CSocket::listen_socket(int socketid, int port) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("socket_listen(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + memset(&(addr.sin_zero), 0, 8); + if(!strlen(m_pSocketInfo[socketid].bind_ip)) { + addr.sin_addr.s_addr = htonl(INADDR_ANY); + } else { + addr.sin_addr.s_addr = inet_addr(m_pSocketInfo[socketid].bind_ip); + } + if(bind(m_pSocket[socketid], (struct sockaddr *)&addr, sizeof(addr)) == -1) { + logprintf("socket_listen(): Socket has failed to bind on port %d.", port); + return 0; + } + if(m_pSocketInfo[socketid].tcp) { + if(listen(m_pSocket[socketid], 10) == -1) { + logprintf("socket_listen(): Socket has failed to listen on port %d.", port); + return 0; + } + } + m_pSocketInfo[socketid].is_client = false; // that way the thread knows the socket will act as a server + m_pSocketInfo[socketid].listen = true; + m_pSocketInfo[socketid].active_thread = true; + if(m_pSocketInfo[socketid].tcp) + g_pThread->Start(socket_connection_thread, (void*)socketid); + g_pThread->Start(socket_receive_thread, (void*)socketid); + return 1; +} + +int CSocket::stop_listen_socket(int socketid) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("stop_listen_socket(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + close_socket(m_pSocket[socketid]); + m_pSocketInfo[socketid].listen = false; + m_pSocketInfo[socketid].active_thread = false; + return 1; +} + +int CSocket::bind_socket(int socketid, char* ip) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("socket_bind(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + strcpy(m_pSocketInfo[socketid].bind_ip, ip); + return 1; +} + +int CSocket::destroy_socket(int socketid) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("socket_destroy(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + if(m_pSocket[socketid] != (-1)) { + m_pSocketInfo[socketid].active_thread = false; + if(!m_pSocketInfo[socketid].is_client) { + for(int i = 0;i < m_pSocketInfo[socketid].max_clients;i++) { + if(m_pSocketInfo[socketid].connected_clients[i] != (-1)) { + close_socket(m_pSocketInfo[socketid].connected_clients[i]); + } + } + free(m_pSocketInfo[socketid].connected_clients); + } + close_socket(m_pSocket[socketid]); + m_pSocket[socketid] = (-1); + m_pSocketInfo[socketid].listen = false; + } + //g_pThread->Kill(m_pSocketInfo[socketid].con); + return 1; +} + +int CSocket::close_remote_connection(int socketid, int remote_client_id) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("close_remote_connection(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + int *rem_client = &m_pSocketInfo[socketid].connected_clients[remote_client_id]; + if(*rem_client != (-1)) { + //shutdown(*rem_client, SD_BOTH); + close_socket(*rem_client); + *rem_client = (-1); + } + return 1; +} + +int CSocket::send_socket(int socketid, char* data, int len) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("socket_send(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + //strcat(data, "\r\n"); + return send(m_pSocket[socketid], data, len, 0); +} + +int CSocket::sendto_remote_client(int socketid, int remote_clientid, char* data) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("sendto_remote_client(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + if(m_pSocketInfo[socketid].connected_clients[remote_clientid] != (-1)) + return send(m_pSocketInfo[socketid].connected_clients[remote_clientid], data, strlen(data), 0); + return 0; +} + +int CSocket::is_remote_client_connected(int socketid, int remote_clientid) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("is_remote_client_connected(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + char tempbuf[1024]; + if(remote_clientid <= m_pSocketInfo[socketid].max_clients && m_pSocketInfo[socketid].connected_clients[remote_clientid] != (-1)) { + if(recv(m_pSocketInfo[socketid].connected_clients[remote_clientid], tempbuf, sizeof(tempbuf), NULL) != 0) + return 1; + } + return 0; +} + +// (c) Bjorn Reese +int CSocket::set_nonblocking_socket(int fd) +{ + DWORD flags; +#ifdef WIN32 + flags = 1; + return ioctlsocket(fd, FIONBIO, &flags); +#else + /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */ + if (-1 == (flags = fcntl(fd, F_GETFL, 0))) + flags = 0; + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); + fcntl(fd, F_SETFL, O_NONBLOCK); +#endif +} + +int CSocket::is_socket_valid(int socketid) +{ + if(socketid > MAX_SOCK || socketid < 0) + return 0; + return ((m_pSocket[socketid] != -1) ? 1 : 0); +} + +char* CSocket::get_remote_client_ip(int socketid, int remote_clientid) +{ + if(m_pSocket[socketid] == -1 || !m_pSocketInfo[socketid].success) { + logprintf("get_remote_client_ip(): Socket ID %d doesn't exist or hasn't been created yet", socketid); + return 0; + } + struct sockaddr_in peer_addr; +#ifdef WIN32 + int cLen = sizeof(peer_addr); +#else + size_t cLen = sizeof(peer_addr); +#endif + getpeername(m_pSocketInfo[socketid].connected_clients[remote_clientid], (struct sockaddr *)&peer_addr, &cLen); + return inet_ntoa(peer_addr.sin_addr); +} + +void CSocket::close_socket(int socket) +{ +#ifdef WIN32 + closesocket(socket); +#else + close(socket); +#endif +} + +#ifdef WIN32 +DWORD socket_connection_thread(void* lpParam) +#else +void* socket_connection_thread(void* lpParam) +#endif +{ + int sockID = (int)lpParam; + sockaddr_in remote_client; +#ifdef WIN32 + int cLen = sizeof(remote_client); +#else + size_t cLen = sizeof(remote_client); +#endif + do { + if(g_pSocket->m_pSocketInfo[sockID].listen) { + int client = accept(g_pSocket->m_pSocket[sockID], (sockaddr*)&remote_client, &cLen); + int slot = g_pSocket->find_free_slot(g_pSocket->m_pSocketInfo[sockID].connected_clients, g_pSocket->m_pSocketInfo[sockID].max_clients); + if(client != NULL && client != SOCKET_ERROR && slot != 0xFFFF) { + g_pSocket->set_nonblocking_socket(client); + g_pSocket->m_pSocketInfo[sockID].connected_clients[slot] = client; + remoteConnect pData; + pData.remote_client = (char*)malloc(sizeof(char*)*15); + strcpy(pData.remote_client, inet_ntoa(remote_client.sin_addr)); + pData.remote_clientid = slot; + pData.socketid = sockID; + onRemoteConnect.push(pData); + } else { + if(slot == 0xFFFF) { + send(client, "-- Server is full", 17, 0); + logprintf("onSocketRemoteFail(Socket: %d)\r\n -- Maximum connection limit exceeded. (Limit is %d)\r\n -- Consider using 'socket_set_max_connections()' to increase the limit", sockID, g_pSocket->m_pSocketInfo[sockID].max_clients); + } + g_pSocket->close_socket(client); + /* + onSocketRemoteFail(Socket:id, remote_client[], remote_clientid) + */ + } + } + SLEEP(500); + } while(g_pSocket->m_pSocketInfo[sockID].active_thread); +#ifdef WIN32 + return 1; +#endif +} + +#ifdef WIN32 +DWORD socket_receive_thread(void* lpParam) +#else +void* socket_receive_thread(void* lpParam) +#endif +{ + int sockID = (int)lpParam, + curConnection, + maxClients; + bool sockType = g_pSocket->m_pSocketInfo[sockID].is_client; + bool tcpProtocol = g_pSocket->m_pSocketInfo[sockID].tcp; + char szRecBuffer[1024]; + curConnection = g_pSocket->m_pSocket[sockID]; + // logprintf("Thread started with socket id %d", sockID); + memset(szRecBuffer, '\0', sizeof(szRecBuffer)); + if(!sockType) maxClients = g_pSocket->m_pSocketInfo[sockID].max_clients; + do { + if(!tcpProtocol) { + // udp + struct sockaddr_in remote_client; +#ifdef WIN32 + int client_len = sizeof(remote_client); +#else + size_t client_len = sizeof(remote_client); +#endif + int byte_len = recvfrom(curConnection, szRecBuffer, sizeof(szRecBuffer), 0, (struct sockaddr*)&remote_client, &client_len); + if(byte_len > 0) { + socketUDP pData; + pData.data = (char*)malloc(sizeof(char)*byte_len); + strcpy(pData.data, szRecBuffer); + pData.data_len = byte_len; + pData.remote_ip = (char*)malloc(sizeof(char*)*15); + strcpy(pData.remote_ip, inet_ntoa(remote_client.sin_addr)); + pData.remote_port = ntohs(remote_client.sin_port); + pData.socketid = sockID; + onUDPReceiveData.push(pData); + memset(szRecBuffer, '\0', sizeof(szRecBuffer)); + } + SLEEP(200); + continue; // skip further processing + } + if(sockType) { + // tcp client + int byte_len = recv(curConnection, szRecBuffer, sizeof(szRecBuffer), NULL); + if(byte_len > 0) { + //remove_newline(szRecBuffer); + szRecBuffer[byte_len] = '\0'; + socketAnswer pData; + pData.socketid = sockID; + pData.data_len = byte_len; + pData.data = (char*)malloc(sizeof(char*)*byte_len); + strcpy(pData.data, szRecBuffer); + onSocketAnswer.push(pData); + memset(szRecBuffer, '\0', sizeof(szRecBuffer)); + } + if(!byte_len) { + socketClose pData; + pData.socketid = sockID; + onSocketClose.push(pData); + g_pSocket->destroy_socket(sockID); + } + } else { + // tcp server + for(int i = 0;i < maxClients;i++) { // loop through all connected clients + curConnection = g_pSocket->m_pSocketInfo[sockID].connected_clients[i]; + if(curConnection != (-1)) { + int byte_len = recv(curConnection, szRecBuffer, sizeof(szRecBuffer), NULL); + if(byte_len > 0) { + //remove_newline(szRecBuffer); + szRecBuffer[byte_len] = '\0'; + receiveData pData; + pData.data = (char*)malloc(sizeof(char*)*byte_len); + strcpy(pData.data, szRecBuffer); + pData.socketid = sockID; + pData.remote_clientid = i; + pData.data_len = byte_len; + onSocketReceiveData.push(pData); + } + if(!byte_len) { + remoteDisconnect pData; + pData.remote_clientid = i; + pData.socketid = sockID; + g_pSocket->close_socket(curConnection); + g_pSocket->m_pSocketInfo[sockID].connected_clients[i] = (-1); // connection has dropped + onRemoteDisconnect.push(pData); + } + } + } + } + SLEEP(200); + } while(g_pSocket->m_pSocketInfo[sockID].active_thread); +#ifdef WIN32 + return 1; +#endif +} \ No newline at end of file diff --git a/socket/CSocket.h b/socket/CSocket.h new file mode 100644 index 0000000..0502e18 --- /dev/null +++ b/socket/CSocket.h @@ -0,0 +1,51 @@ +#pragma once + +#include "main.h" + +#define MAX_SOCK 10 + +struct SocketStruct { + bool tcp; + bool listen; + bool success; + bool is_client; // true = client, false = server + bool active_thread; + int max_clients; + int* connected_clients; + char bind_ip[15]; +}; + +class CSocket +{ +public: + struct SocketStruct m_pSocketInfo[MAX_SOCK]; + int m_pSocket[MAX_SOCK]; + + CSocket(); + ~CSocket(); + int create_socket(int type); + int find_free_slot(int* arr, int size); + int set_max_connections(int socketid, int maxcon); + int connect_socket(int socketid, char* address, int port); + int listen_socket(int socketid, int port); + int stop_listen_socket(int socketid); + int bind_socket(int socketid, char* ip); + int destroy_socket(int socketid); + int close_remote_connection(int socketid, int remote_client_id); + int send_socket(int socketid, char* data, int len); + int sendto_remote_client(int socketid, int remote_clientid, char* data); + int set_nonblocking_socket(int fd); + int is_remote_client_connected(int socketid, int remote_clientid); + int is_socket_valid(int socketid); + char* get_remote_client_ip(int socketid, int remote_clientid); + void close_socket(int socket); +//private: +}; + +#ifdef WIN32 +DWORD socket_connection_thread(void* lpParam); +DWORD socket_receive_thread(void* lpParam); +#else +void* socket_connection_thread(void* lpParam); +void* socket_receive_thread(void* lpParam); +#endif \ No newline at end of file diff --git a/socket/CString.cpp b/socket/CString.cpp new file mode 100644 index 0000000..a10f5ae --- /dev/null +++ b/socket/CString.cpp @@ -0,0 +1,24 @@ +#pragma once + +#include "CString.h" + + +void CString::Set(AMX* amx, cell param, char* str) +{ + cell *destination; + amx_GetAddr(amx, param, &destination); + amx_SetString(destination, str, 0, 0, strlen(str)+1); + return; +} + +int CString::Get(AMX* amx, cell param, char*& dest) +{ + cell *ptr; + int len; + amx_GetAddr(amx, param, &ptr); + amx_StrLen(ptr, &len); + dest = (char*)malloc((len * sizeof(char))+1); + amx_GetString(dest, ptr, 0, UNLIMITED); + dest[len] = '\0'; + return len; +} \ No newline at end of file diff --git a/socket/CString.h b/socket/CString.h new file mode 100644 index 0000000..5b823fc --- /dev/null +++ b/socket/CString.h @@ -0,0 +1,12 @@ +#pragma once + +// taken from my sql plugin + +#include "main.h" + +class CString +{ +public: + void Set( AMX* amx, cell param, char* str); + int Get(AMX* amx, cell param, char*& dest); +}; \ No newline at end of file diff --git a/socket/CThread.cpp b/socket/CThread.cpp new file mode 100644 index 0000000..a34b83c --- /dev/null +++ b/socket/CThread.cpp @@ -0,0 +1,39 @@ +#pragma once + +#include "CThread.h" + +#ifdef WIN32 +HANDLE CThread::Start(ThreadFunc Function, void* param) +#else +pthread_t CThread::Start(ThreadFunc Function, void* param) +#endif +{ +#ifdef WIN32 + DWORD dwThreadId = 0; + HANDLE threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Function, param, 0, &dwThreadId); + CloseHandle(threadHandle); +#else + pthread_t threadHandle; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + int error = pthread_create(&threadHandle, &attr, Function, param); +#endif + return threadHandle; +} + +#ifdef WIN32 +int CThread::Kill(HANDLE thread) +#else +int CThread::Kill(pthread_t thread) +#endif +{ +#ifdef WIN32 + if(WaitForSingleObject(thread, NULL) == WAIT_TIMEOUT) { + TerminateThread(thread, NULL); + } +#else + pthread_join(thread, NULL); +#endif + return 1; +} \ No newline at end of file diff --git a/socket/CThread.h b/socket/CThread.h new file mode 100644 index 0000000..533722f --- /dev/null +++ b/socket/CThread.h @@ -0,0 +1,21 @@ +#pragma once + +#include "main.h" + +#ifdef WIN32 +typedef DWORD (*ThreadFunc)(void* lpParam); +#else +typedef void* (*ThreadFunc)(void* lpParam); +#endif + +class CThread +{ +public: +#ifdef WIN32 + HANDLE Start(ThreadFunc Function, void* param); + int Kill(HANDLE thread); +#else + pthread_t Start(ThreadFunc Function, void* param); + int Kill(pthread_t thread); +#endif +}; \ No newline at end of file diff --git a/socket/exports.def b/socket/exports.def new file mode 100644 index 0000000..9617a5a --- /dev/null +++ b/socket/exports.def @@ -0,0 +1,8 @@ +LIBRARY "socket" +EXPORTS + Supports + Load + Unload + AmxLoad + AmxUnload + ProcessTick \ No newline at end of file diff --git a/socket/main.cpp b/socket/main.cpp new file mode 100644 index 0000000..95d6caa --- /dev/null +++ b/socket/main.cpp @@ -0,0 +1,167 @@ +#pragma once +#include "main.h" + +std::list g_pAMX; +logprintf_t logprintf; +extern void *pAMXFunctions; +CSocket* g_pSocket; +std::queue onRemoteConnect; +std::queue onRemoteDisconnect; +std::queue onSocketReceiveData; +std::queue onSocketAnswer; +std::queue onSocketClose; +std::queue onUDPReceiveData; + +PLUGIN_EXPORT int PLUGIN_CALL ProcessTick( ) +{ + if(!onRemoteConnect.empty()) { + remoteConnect tempData = onRemoteConnect.front(); + cell amx_Address, amx_Ret, *phys_addr; + int amx_idx; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onSocketRemoteConnect", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.remote_clientid); + amx_PushString(* a, &amx_Address, &phys_addr, tempData.remote_client, 0, 0); + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + amx_Release(* a, amx_Address); + } + } + free(tempData.remote_client); + onRemoteConnect.pop(); + } + if(!onRemoteDisconnect.empty()) { + remoteDisconnect tempData = onRemoteDisconnect.front(); + cell amx_Ret; + int amx_idx; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onSocketRemoteDisconnect", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.remote_clientid); + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + } + } + onRemoteDisconnect.pop(); + } + if(!onSocketReceiveData.empty()) { + receiveData tempData = onSocketReceiveData.front(); + cell amx_Address, amx_Ret, *phys_addr; + int amx_idx; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onSocketReceiveData", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.data_len); + amx_PushString(* a, &amx_Address, &phys_addr, tempData.data, 0, 0); + amx_Push(* a, tempData.remote_clientid); + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + amx_Release(* a, amx_Address); + } + } + free(tempData.data); + onSocketReceiveData.pop(); + } + if(!onSocketAnswer.empty()) { + socketAnswer tempData = onSocketAnswer.front(); + cell amx_Address, amx_Ret, *phys_addr; + int amx_idx; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onSocketAnswer", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.data_len); + amx_PushString(* a, &amx_Address, &phys_addr, tempData.data, 0, 0); + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + amx_Release(* a, amx_Address); + } + } + free(tempData.data); + onSocketAnswer.pop(); + } + if(!onSocketClose.empty()) { + socketClose tempData = onSocketClose.front(); + int amx_idx; + cell amx_Ret; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onSocketClose", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + } + } + onSocketClose.pop(); + } + if(!onUDPReceiveData.empty()) { + socketUDP tempData = onUDPReceiveData.front(); + int amx_idx; + cell amx_Address[2], amx_Ret; + for (std::list::iterator a = g_pAMX.begin(); a != g_pAMX.end(); a++) { + if(amx_FindPublic(* a, "onUDPReceiveData", &amx_idx) == AMX_ERR_NONE) { + amx_Push(* a, tempData.remote_port); + amx_PushString(* a, &amx_Address[0], NULL, tempData.remote_ip, 0, 0); + amx_Push(* a, tempData.data_len); + amx_PushString(* a, &amx_Address[1], NULL, tempData.data, 0, 0); + amx_Push(* a, tempData.socketid); + amx_Exec(* a, &amx_Ret, amx_idx); + amx_Release(* a, amx_Address[0]); + amx_Release(* a, amx_Address[1]); + } + } + free(tempData.data); + free(tempData.remote_ip); + onUDPReceiveData.pop(); + } + return 1; +} + +PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() +{ + return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES | SUPPORTS_PROCESS_TICK; +} + +PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) +{ + pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS]; + logprintf = (logprintf_t) ppData[PLUGIN_DATA_LOGPRINTF]; + g_pSocket = new CSocket(); + logprintf(" Socket plugin v%s loaded.", VERSION); + return true; +} + +PLUGIN_EXPORT void PLUGIN_CALL Unload() +{ + logprintf(" Socket plugin successfully unloaded."); + delete g_pSocket; +} + +AMX_NATIVE_INFO PluginNatives[] = +{ + {"socket_create", n_create_socket}, + {"socket_bind", n_bind_socket}, + {"socket_set_max_connections", n_set_max_connections}, + {"socket_connect", n_connect_socket}, + {"socket_listen", n_listen_socket}, + {"socket_stop_listen", n_stop_listen_socket}, + {"socket_destroy", n_destroy_socket}, + {"socket_close_remote_client", n_close_remote_connection}, + {"socket_send", n_send_socket}, + {"socket_sendto_remote_client", n_sendto_remote_client}, + {"socket_remote_client_connected", n_is_remote_client_connected}, + {"is_socket_valid", n_is_socket_valid}, + {"get_remote_client_ip", n_get_remote_client_ip}, + {0, 0} +}; + +PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx ) +{ + g_pAMX.push_back(amx); + return amx_Register(amx, PluginNatives, -1); +} + +PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx ) +{ + for(std::list::iterator i = g_pAMX.begin();i != g_pAMX.end();i++) { + if(* i == amx) { + g_pAMX.erase(i); + break; + } + } + return AMX_ERR_NONE; +} \ No newline at end of file diff --git a/socket/main.h b/socket/main.h new file mode 100644 index 0000000..777b2c6 --- /dev/null +++ b/socket/main.h @@ -0,0 +1,83 @@ +#pragma once + +#pragma warning (disable:4005 4700 996) +#include "../SDK/amx/amx.h" +#include "../SDK/plugincommon.h" +#if (defined(WIN32) || defined(_WIN32) || defined(_WIN64)) + #include "winsock2.h" + #include "windows.h" + #include "ws2tcpip.h" + #pragma comment(lib, "ws2_32.lib") +#else + #include "sys/types.h" + #include "sys/socket.h" + #include "arpa/inet.h" + #include "netdb.h" + #include "fcntl.h" + #include "unistd.h" + #include "pthread.h" +#endif +#include "queue" +#include "list" +#include "string.h" +#include "CString.h" +#include "CThread.h" +#include "CSocket.h" +#include "CNatives.h" + +#define VERSION "0.1b" + +#ifdef WIN32 + #define SLEEP(x) { Sleep(x); } +#else + #define SLEEP(x) { usleep(x * 1000); } + #define SOCKET_ERROR (-1) + typedef unsigned long DWORD; + typedef unsigned int UINT; +#endif + +typedef void (*logprintf_t)(char* format, ...); + + +struct remoteConnect { + int socketid; + char* remote_client; + int remote_clientid; +}; + +struct remoteDisconnect { + int socketid; + int remote_clientid; +}; + +struct receiveData { + int socketid; + int remote_clientid; + char* data; + int data_len; +}; + +struct socketAnswer { + int socketid; + char* data; + int data_len; +}; + +struct socketClose { + int socketid; +}; + +struct socketUDP { + int socketid; + char* data; + int data_len; + char* remote_ip; + int remote_port; +}; + +extern std::queue onRemoteConnect; +extern std::queue onRemoteDisconnect; +extern std::queue onSocketReceiveData; +extern std::queue onSocketAnswer; +extern std::queue onSocketClose; +extern std::queue onUDPReceiveData; diff --git a/socket/makefile b/socket/makefile new file mode 100644 index 0000000..27ae958 --- /dev/null +++ b/socket/makefile @@ -0,0 +1,13 @@ +GPP=g++ +GCC=gcc + +OUTFILE="../socket.so" + +COMPILE_FLAGS=-c -m32 -O3 -fPIC -w -DLINUX -Wall -I../SDK/amx/ + +all: + $(GCC) $(COMPILE_FLAGS) ../SDK/amx/*.c + $(GPP) $(COMPILE_FLAGS) ../SDK/*.cpp + $(GPP) $(COMPILE_FLAGS) *.cpp + $(GPP) -m32 -O2 -fshort-wchar -shared -o $(OUTFILE) *.o -lpthread + rm -f *.o \ No newline at end of file diff --git a/socket/socket.vcproj b/socket/socket.vcproj new file mode 100644 index 0000000..1f20b87 --- /dev/null +++ b/socket/socket.vcproj @@ -0,0 +1,245 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +