amxmodx/amxmodx/modules.cpp

1715 lines
36 KiB
C++
Raw Normal View History

2004-03-05 21:03:14 +00:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2005-09-16 23:48:51 +00:00
* along with this program; if not, write to the Free Software Foundation,
2004-03-05 21:03:14 +00:00
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
2005-09-16 23:48:51 +00:00
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
2004-03-05 21:03:14 +00:00
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
2004-01-31 20:56:22 +00:00
#ifdef __linux__
#include <malloc.h>
#include <stdlib.h>
#include <sys/mman.h>
2005-07-27 16:24:14 +00:00
#include "sclinux.h"
#endif
2005-09-16 23:48:51 +00:00
2004-03-24 01:35:44 +00:00
#include "amxmodx.h"
2004-01-31 20:56:22 +00:00
#include "osdep.h" // sleep, etc
#include "CFile.h"
2004-07-21 20:00:10 +00:00
#include "amxxfile.h"
2005-07-26 21:31:21 +00:00
#include "amxdbg.h"
2005-07-29 06:33:57 +00:00
#include "newmenus.h"
2005-07-31 20:11:58 +00:00
#include "natives.h"
2005-09-09 03:23:31 +00:00
#include "debugger.h"
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
CList<CModule, const char*> g_modules;
CList<CScript, AMX*> g_loadedscripts;
2004-01-31 20:56:22 +00:00
CModule *g_CurrentlyCalledModule = NULL; // The module we are in at the moment; NULL otherwise
2005-09-16 23:48:51 +00:00
// also NULL for non-amxx modules
// This is needed so we know which module called a function
ModuleCallReason g_ModuleCallReason;
2005-09-16 23:48:51 +00:00
extern const char* no_function; // stupid work around
2005-09-16 23:48:51 +00:00
void report_error(int code, char* fmt, ...)
2004-01-31 20:56:22 +00:00
{
va_list argptr;
char string[256];
*string = 0;
2005-09-16 23:48:51 +00:00
va_start(argptr, fmt);
vsnprintf(string, 255, fmt, argptr);
2004-01-31 20:56:22 +00:00
string[255] = 0;
2005-09-16 23:48:51 +00:00
va_end(argptr);
if (*string)
{
2004-06-28 18:12:26 +00:00
AMXXLOG_Log("Error:");
AMXXLOG_Log(string);
2005-09-16 23:48:51 +00:00
} else {
2004-06-28 18:12:26 +00:00
AMXXLOG_Log("!!! There was an unexpected module error.");
AMXXLOG_Log("The server may not work correctly.");
}
2004-01-31 20:56:22 +00:00
}
2005-09-16 23:48:51 +00:00
void print_srvconsole(char *fmt, ...)
2004-01-31 20:56:22 +00:00
{
va_list argptr;
static char string[384];
va_start(argptr, fmt);
2005-09-16 23:48:51 +00:00
vsnprintf(string, sizeof(string) - 1, fmt, argptr);
string[sizeof(string) - 1] = '\0';
va_end(argptr);
2005-09-16 23:48:51 +00:00
SERVER_PRINT(string);
2004-01-31 20:56:22 +00:00
}
void* alloc_amxmemory(void** p, int size)
{
2005-09-16 23:48:51 +00:00
*p = new unsigned char[size];
2004-01-31 20:56:22 +00:00
return *p;
}
void free_amxmemory(void **ptr)
{
2005-07-26 21:31:21 +00:00
delete[] (unsigned char *)(*ptr);
2004-01-31 20:56:22 +00:00
*ptr = 0;
}
2004-09-08 07:05:16 +00:00
int load_amxscript(AMX *amx, void **program, const char *filename, char error[64], int debug)
2004-07-21 20:00:10 +00:00
{
*error = 0;
CAmxxReader reader(filename, PAWN_CELL_SIZE / 8);
2005-09-16 23:48:51 +00:00
2004-07-21 20:00:10 +00:00
if (reader.GetStatus() == CAmxxReader::Err_None)
{
size_t bufSize = reader.GetBufferSize();
2005-09-16 23:48:51 +00:00
2004-07-21 20:00:10 +00:00
if (bufSize != 0)
{
*program = (void*) (new char[bufSize]);
2005-09-16 23:48:51 +00:00
2004-07-21 20:00:10 +00:00
if (!*program)
{
strcpy(error, "Failed to allocate memory");
return (amx->error = AMX_ERR_MEMORY);
}
2005-09-16 23:48:51 +00:00
2004-07-21 20:00:10 +00:00
reader.GetSection(*program);
}
}
2004-01-31 20:56:22 +00:00
2004-07-21 20:00:10 +00:00
switch (reader.GetStatus())
{
2005-09-16 23:48:51 +00:00
case CAmxxReader::Err_None:
break;
case CAmxxReader::Err_FileOpen:
strcpy(error, "Plugin file open error");
return (amx->error = AMX_ERR_NOTFOUND);
case CAmxxReader::Err_FileRead:
strcpy(error, "Plugin file read error");
return (amx->error = AMX_ERR_NOTFOUND);
case CAmxxReader::Err_InvalidParam:
strcpy(error, "Internal error: Invalid parameter");
return (amx->error = AMX_ERR_NOTFOUND);
case CAmxxReader::Err_FileInvalid:
strcpy(error, "Invalid Plugin");
return (amx->error = AMX_ERR_FORMAT);
case CAmxxReader::Err_SectionNotFound:
strcpy(error, "Searched section not found (.amxx)");
return (amx->error = AMX_ERR_NOTFOUND);
case CAmxxReader::Err_DecompressorInit:
strcpy(error, "Decompressor initialization failed");
return (amx->error = AMX_ERR_INIT);
case CAmxxReader::Err_Decompress:
strcpy(error, "Internal error: Decompress");
return (amx->error = AMX_ERR_NOTFOUND);
case CAmxxReader::Err_OldFile:
strcpy(error, "Plugin uses deprecated format. Update compiler");
default:
strcpy(error, "Unknown error");
return (amx->error = AMX_ERR_NOTFOUND);
2004-07-21 20:00:10 +00:00
}
// check for magic
AMX_HEADER *hdr = (AMX_HEADER*)*program;
uint16_t magic = hdr->magic;
amx_Align16(&magic);
2005-09-16 23:48:51 +00:00
2004-07-21 20:00:10 +00:00
if (magic != AMX_MAGIC)
{
strcpy(error, "Invalid Plugin");
return (amx->error = AMX_ERR_FORMAT);
}
int err;
memset(amx, 0, sizeof(*amx));
2005-07-26 21:31:21 +00:00
bool will_be_debugged = false;
tagAMX_DBG *pDbg = NULL;
if ((int)CVAR_GET_FLOAT("amx_debug") >= 2 || debug)
2004-09-08 07:05:16 +00:00
{
2005-07-26 21:31:21 +00:00
if ((hdr->file_version < CUR_FILE_VERSION))
{
2005-07-26 21:31:21 +00:00
sprintf(error, "Plugin needs newer debug version info");
return (amx->error = AMX_ERR_VERSION);
2005-09-16 23:48:51 +00:00
}
else if ((hdr->flags & AMX_FLAG_DEBUG) != 0)
{
2005-07-26 21:31:21 +00:00
will_be_debugged = true;
2005-09-16 23:48:51 +00:00
2005-07-26 21:31:21 +00:00
char *addr = (char *)hdr + hdr->size;
pDbg = new tagAMX_DBG;
2005-09-16 23:48:51 +00:00
2005-07-26 21:31:21 +00:00
memset(pDbg, 0, sizeof(AMX_DBG));
int err = dbg_LoadInfo(pDbg, addr);
if (err != AMX_ERR_NONE)
{
dbg_FreeInfo(pDbg);
delete pDbg;
sprintf(error, "Debug loading error %d", err);
return (amx->error = AMX_ERR_INIT);
}
amx->flags |= AMX_FLAG_DEBUG;
} else {
2005-09-16 23:48:51 +00:00
sprintf(error, "Plugin not compiled with debug option");
2005-07-26 21:31:21 +00:00
return (amx->error = AMX_ERR_INIT);
}
} else {
#ifdef JIT
//if (hdr->file_version == CUR_FILE_VERSION)
amx->flags |= AMX_FLAG_JITC;
#endif
2004-09-08 07:05:16 +00:00
}
2005-09-16 23:48:51 +00:00
if ((err = amx_Init(amx, *program)) != AMX_ERR_NONE)
{
2005-07-26 21:31:21 +00:00
if (pDbg)
{
dbg_FreeInfo(pDbg);
delete pDbg;
}
2005-09-16 23:48:51 +00:00
sprintf(error, "Load error %d (invalid file format or version)", err);
return (amx->error = AMX_ERR_INIT);
}
2004-01-31 20:56:22 +00:00
2005-09-09 23:13:34 +00:00
Handler *pHandler = new Handler(amx);
amx->userdata[UD_HANDLER] = (void *)pHandler;
2005-07-26 21:31:21 +00:00
if (will_be_debugged)
{
amx->flags |= AMX_FLAG_DEBUG;
2005-09-09 03:23:31 +00:00
amx->flags &= (~AMX_FLAG_JITC);
amx_SetDebugHook(amx, &Debugger::DebugHook);
Debugger *pDebugger = new Debugger(amx, pDbg);
amx->userdata[UD_DEBUGGER] = pDebugger;
2005-07-26 21:31:21 +00:00
} else {
#ifdef JIT
2005-09-09 03:23:31 +00:00
//set this again because amx_Init() erases it!
2005-07-26 21:31:21 +00:00
amx->flags |= AMX_FLAG_JITC;
2005-09-09 03:23:31 +00:00
amx->flags &= (~AMX_FLAG_DEBUG);
2005-07-26 21:31:21 +00:00
amx->sysreq_d = NULL;
#endif
2005-07-26 21:31:21 +00:00
}
#ifdef JIT
if (amx->flags & AMX_FLAG_JITC)
{
2005-09-16 23:48:51 +00:00
char *np = new char[amx->code_size];
char *rt = new char[amx->reloc_size];
if (!np || (!rt && amx->reloc_size > 0))
{
delete[] np;
delete[] rt;
2005-09-16 23:48:51 +00:00
strcpy(error, "Failed to initialize JIT'd plugin");
return (amx->error = AMX_ERR_INIT);
}
2005-09-16 23:48:51 +00:00
if ((err = amx_InitJIT(amx, (void *)rt, (void *)np)) == AMX_ERR_NONE)
{
2005-09-16 23:48:51 +00:00
//amx->base = (unsigned char FAR *)realloc(np, amx->code_size);
#ifndef __linux__
2005-08-21 19:30:27 +00:00
amx->base = (unsigned char *)VirtualAlloc(NULL, amx->code_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
#else
//posix_memalign((void **)&(amx->base), sysconf(_SC_PAGESIZE), amx->code_size);
amx->base = (unsigned char *)memalign(sysconf(_SC_PAGESIZE), amx->code_size);
mprotect((void *)amx->base, amx->code_size, PROT_READ|PROT_WRITE|PROT_EXEC);
#endif
2005-09-16 23:48:51 +00:00
if (amx->base)
memcpy(amx->base, np, amx->code_size);
delete[] np;
delete[] rt;
2005-09-16 23:48:51 +00:00
char *prg = (char *)(*program);
2005-09-16 23:48:51 +00:00
delete[] prg;
(*program) = amx->base;
2005-09-16 23:48:51 +00:00
if (*program == 0)
{
strcpy(error, "Failed to allocate memory");
return (amx->error = AMX_ERR_MEMORY);
}
2005-09-16 23:48:51 +00:00
} else {
delete[] np;
delete[] rt;
2005-09-16 23:48:51 +00:00
sprintf(error, "Failed to initialize plugin (%d)", err);
2005-09-16 23:48:51 +00:00
return (amx->error = AMX_ERR_INIT_JIT);
}
2004-01-31 20:56:22 +00:00
}
#endif
2005-09-16 23:48:51 +00:00
CScript* aa = new CScript(amx, *program, filename);
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
if (aa == 0)
{
2005-09-16 23:48:51 +00:00
strcpy(error, "Failed to allocate memory");
return (amx->error = AMX_ERR_MEMORY);
}
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
g_loadedscripts.put(aa);
2005-07-31 20:11:58 +00:00
2005-09-16 23:48:51 +00:00
set_amxnatives(amx, error);
2005-07-31 20:11:58 +00:00
if (g_plugins.m_Finalized)
{
amx_Register(amx, g_plugins.pNatives, -1);
2005-09-16 23:48:51 +00:00
2005-07-31 20:11:58 +00:00
if (CheckModules(amx, error))
{
2005-09-16 23:48:51 +00:00
if (amx_Register(amx, core_Natives, -1) != AMX_ERR_NONE)
2005-07-31 20:11:58 +00:00
{
sprintf(error, "Plugin uses an unknown function (name \"%s\") - check your modules.ini.", no_function);
return (amx->error = AMX_ERR_NOTFOUND);
}
} else {
return (amx->error = AMX_ERR_NOTFOUND);
}
}
return (amx->error = AMX_ERR_NONE);
2004-01-31 20:56:22 +00:00
}
2004-09-12 04:03:54 +00:00
const char *StrCaseStr(const char *as, const char *bs)
{
static char a[256];
static char b[256];
unsigned int i = 0;
unsigned int len = strlen(as);
if (len > 254)
len = 254;
2005-09-16 23:48:51 +00:00
for (i = 0; i < len; i++)
2004-09-12 04:03:54 +00:00
{
a[i] = tolower(as[i]);
}
2005-09-16 23:48:51 +00:00
2004-09-12 04:03:54 +00:00
a[len] = 0;
len = strlen(bs);
if (len > 254)
len = 254;
2005-09-16 23:48:51 +00:00
for (i = 0; i < len; i++)
2004-09-12 04:03:54 +00:00
{
b[i] = tolower(bs[i]);
}
2005-09-16 23:48:51 +00:00
2004-09-12 04:03:54 +00:00
b[len] = 0;
2005-09-16 23:48:51 +00:00
return strstr(a, b);
2004-09-12 04:03:54 +00:00
}
//returns 0 for module not found, 1 for "everything's okay"
int CheckModules(AMX *amx, char error[128])
2004-09-09 05:16:53 +00:00
{
int numLibraries = amx_GetLibraries(amx);
char buffer[32];
2005-09-16 23:48:51 +00:00
bool found = false;
bool isdbi = false;
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a;
const amxx_module_info_s *info;
2005-09-16 23:48:51 +00:00
2005-09-11 03:58:38 +00:00
Handler *pHandler = (Handler *)amx->userdata[UD_HANDLER];
2005-09-16 23:48:51 +00:00
for (int i = 0; i < numLibraries; i++)
2004-09-09 05:16:53 +00:00
{
2005-09-16 23:48:51 +00:00
amx_GetLibrary(amx, i, buffer, sizeof(buffer) - 1);
found = false;
2005-09-16 23:48:51 +00:00
if (stricmp(buffer, "float") == 0)
continue;
2005-09-16 23:48:51 +00:00
isdbi = false;
2005-09-16 23:48:51 +00:00
if (stricmp(buffer, "dbi") == 0)
isdbi = true;
2005-09-16 23:48:51 +00:00
for (a = g_modules.begin(); a; ++a)
2004-09-09 05:16:53 +00:00
{
2005-09-16 23:48:51 +00:00
if ((*a).getStatusValue() == MODULE_LOADED)
2004-09-09 05:16:53 +00:00
{
info = (*a).getInfoNew();
2005-09-16 23:48:51 +00:00
if (info)
2004-09-09 05:16:53 +00:00
{
if (isdbi)
2004-09-09 05:16:53 +00:00
{
2005-09-16 23:48:51 +00:00
if (info->logtag && (StrCaseStr(info->logtag, "sql") || StrCaseStr(info->logtag, "dbi")))
2004-09-09 05:16:53 +00:00
{
found = true;
2004-09-09 05:16:53 +00:00
break;
}
} else {
2005-07-27 16:24:14 +00:00
if (info->logtag && (stricmp(info->logtag, buffer) == 0))
2004-09-09 05:16:53 +00:00
{
found = true;
2004-09-09 05:16:53 +00:00
break;
}
}
}
}
}
2005-09-16 23:48:51 +00:00
2005-07-31 20:11:58 +00:00
if (!found)
found = LibraryExists(buffer);
2005-09-16 23:48:51 +00:00
if (!found)
2005-09-11 03:58:38 +00:00
{
if (pHandler->HandleModule(buffer))
found = true;
}
2005-09-16 23:48:51 +00:00
2005-09-11 03:58:38 +00:00
if (!found)
{
2005-09-16 23:48:51 +00:00
sprintf(error, "Module \"%s\" required for plugin. Check modules.ini.", buffer);
return 0;
}
2004-09-09 05:16:53 +00:00
}
return 1;
2004-09-09 05:16:53 +00:00
}
2005-09-16 23:48:51 +00:00
int set_amxnatives(AMX* amx, char error[128])
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
for (CList<CModule, const char *>::iterator a = g_modules.begin(); a ; ++a)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
for (CList<AMX_NATIVE_INFO*>::iterator cc = (*a).m_Natives.begin(); cc; ++cc)
amx_Register(amx, *cc, -1);
2004-01-31 20:56:22 +00:00
}
2004-01-31 20:56:22 +00:00
amx_Register(amx, string_Natives, -1);
amx_Register(amx, float_Natives, -1);
amx_Register(amx, file_Natives, -1);
2005-09-11 03:58:38 +00:00
amx_Register(amx, amxmodx_Natives, -1);
2004-01-31 20:56:22 +00:00
amx_Register(amx, power_Natives, -1);
amx_Register(amx, time_Natives, -1);
amx_Register(amx, vault_Natives, -1);
2005-07-29 06:33:57 +00:00
amx_Register(amx, g_NewMenuNatives, -1);
2005-07-31 20:11:58 +00:00
amx_Register(amx, g_NativeNatives, -1);
2005-09-09 23:13:34 +00:00
amx_Register(amx, g_DebugNatives, -1);
2005-07-31 20:11:58 +00:00
//we're not actually gonna check these here anymore
amx->flags |= AMX_FLAG_PRENIT;
2005-09-09 23:13:34 +00:00
int idx, err;
2005-07-31 20:11:58 +00:00
cell retval;
2005-09-16 23:48:51 +00:00
if (amx_FindPublic(amx, "plugin_natives", &idx) == AMX_ERR_NONE)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
if ((err = amx_Exec(amx, &retval, idx)) != AMX_ERR_NONE)
2004-09-09 05:16:53 +00:00
{
2005-09-09 23:13:34 +00:00
Debugger::GenericMessage(amx, err);
2005-09-16 23:48:51 +00:00
AMXXLOG_Log("An error occurred in plugins_native. This is dangerous!");
2004-09-09 05:16:53 +00:00
}
2004-01-31 20:56:22 +00:00
}
2005-07-31 20:11:58 +00:00
amx->flags &= ~(AMX_FLAG_PRENIT);
return (amx->error = AMX_ERR_NONE);
2004-01-31 20:56:22 +00:00
}
int unload_amxscript(AMX* amx, void** program)
{
2005-08-21 19:30:27 +00:00
int flags = amx->flags;
2005-09-16 23:48:51 +00:00
2005-09-09 03:23:31 +00:00
Debugger *pDebugger = (Debugger *)amx->userdata[UD_DEBUGGER];
if (pDebugger)
delete pDebugger;
2005-09-16 23:48:51 +00:00
2005-09-09 23:13:34 +00:00
Handler *pHandler = (Handler *)amx->userdata[UD_HANDLER];
if (pHandler)
delete pHandler;
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator a = g_loadedscripts.find(amx);
if (a)
a.remove();
char *prg = (char *)*program;
2005-09-16 23:48:51 +00:00
2005-08-21 19:30:27 +00:00
if (!prg)
return AMX_ERR_NONE;
2005-09-16 23:48:51 +00:00
2005-08-21 19:30:27 +00:00
#if defined JIT
#if defined __linux__
2005-09-16 23:48:51 +00:00
if ((flags & AMX_FLAG_JITC) != AMX_FLAG_JITC)
{
delete [] prg;
} else {
#ifdef free
#undef free
free(prg);
2005-09-16 23:48:51 +00:00
#define free(ptr) m_deallocator(__FILE__, __LINE__, __FUNCTION__, m_alloc_free, ptr)
#else
free(prg);
#endif
}
2005-08-21 19:30:27 +00:00
#elif defined WIN32
2005-09-16 23:48:51 +00:00
if ((flags & AMX_FLAG_JITC) != AMX_FLAG_JITC)
2005-08-21 19:30:27 +00:00
{
delete [] prg;
2005-09-16 23:48:51 +00:00
}
else if (!VirtualFree((LPVOID)prg, 0, MEM_RELEASE))
{
2005-08-21 19:30:27 +00:00
AMXXLOG_Log("[AMXX] Could not free plugin memory, failure %d.", GetLastError());
return AMX_ERR_PARAMS;
}
#endif //OS support
2005-08-15 21:38:03 +00:00
#else
2005-08-21 19:30:27 +00:00
//delete normally
delete [] prg;
2005-08-15 21:38:03 +00:00
#endif
*program = 0;
return AMX_ERR_NONE;
2004-01-31 20:56:22 +00:00
}
2005-09-16 23:48:51 +00:00
AMX* get_amxscript(int id, void** code, const char** filename)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator a = g_loadedscripts.begin();
while (a && id--)
2004-01-31 20:56:22 +00:00
++a;
2005-09-16 23:48:51 +00:00
if (a)
{
2004-01-31 20:56:22 +00:00
*filename = (*a).getName();
*code = (*a).getCode();
2005-09-16 23:48:51 +00:00
2004-01-31 20:56:22 +00:00
return (*a).getAMX();
}
2005-09-16 23:48:51 +00:00
2004-01-31 20:56:22 +00:00
return 0;
}
const char* get_amxscriptname(AMX* amx)
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator a = g_loadedscripts.find(amx);
2004-01-31 20:56:22 +00:00
return a ? (*a).getName() : "";
}
2005-09-16 23:48:51 +00:00
void get_modname(char* buffer)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
strcpy(buffer, g_mod_name.c_str());
2004-01-31 20:56:22 +00:00
}
2005-09-16 23:48:51 +00:00
char* build_pathname(char *fmt, ...)
2004-01-31 20:56:22 +00:00
{
static char string[256];
int b;
2005-09-16 23:48:51 +00:00
int a = b = snprintf(string, 255,
2004-01-31 20:56:22 +00:00
#ifndef __linux__
2005-09-16 23:48:51 +00:00
"%s\\",
2004-01-31 20:56:22 +00:00
#else
2005-09-16 23:48:51 +00:00
"%s/",
2004-01-31 20:56:22 +00:00
#endif
2004-08-13 08:46:04 +00:00
g_mod_name.c_str());
2004-01-31 20:56:22 +00:00
va_list argptr;
2005-09-16 23:48:51 +00:00
va_start(argptr, fmt);
a += vsnprintf (&string[a], 255 - a, fmt, argptr);
string[a] = 0;
va_end(argptr);
2004-01-31 20:56:22 +00:00
char* path = &string[b];
2004-01-31 20:56:22 +00:00
while (*path)
{
#ifndef __linux__
if (*path == '/') *path = '\\';
#else
if (*path == '\\') *path = '/';
#endif
++path;
}
2004-01-31 20:56:22 +00:00
return string;
}
char *build_pathname_r(char *buffer, size_t maxlen, char *fmt, ...)
{
snprintf(buffer, maxlen,
#ifdef __linux__
2005-09-16 23:48:51 +00:00
"%s/",
#else
2005-09-16 23:48:51 +00:00
"%s\\",
#endif
2005-09-16 23:48:51 +00:00
g_mod_name.c_str());
size_t len = strlen(buffer);
char *ptr = buffer + len;
va_list argptr;
va_start(argptr, fmt);
vsnprintf (ptr, maxlen-len, fmt, argptr);
va_end (argptr);
while (*ptr)
{
#ifndef __linux__
if (*ptr == '/') *ptr = '\\';
#else
if (*ptr == '\\') *ptr = '/';
#endif
++ptr;
}
return buffer;
}
// build pathname based on addons dir
2005-09-16 23:48:51 +00:00
char* build_pathname_addons(char *fmt, ...)
{
static char string[256];
va_list argptr;
2005-09-16 23:48:51 +00:00
va_start(argptr, fmt);
vsnprintf (string, 255, fmt, argptr);
2005-09-16 23:48:51 +00:00
va_end(argptr);
char* path = string;
while (*path)
{
#ifndef __linux__
if (*path == '/') *path = '\\';
#else
if (*path == '\\') *path = '/';
#endif
++path;
}
return string;
}
2004-01-31 20:56:22 +00:00
bool validFile(const char* file)
{
const char* a = 0;
2005-09-16 23:48:51 +00:00
while (*file)
if (*file++ == '.')
a = file;
2004-01-31 20:56:22 +00:00
#ifndef __linux__
2005-09-16 23:48:51 +00:00
return (a && !strcmp(a, "dll"));
2004-01-31 20:56:22 +00:00
#else
2005-09-16 23:48:51 +00:00
return (a && !strcmp(a, "so"));
2004-01-31 20:56:22 +00:00
#endif
}
void ConvertModuleName(const char *pathString, String &path)
{
#if PAWN_CELL_SIZE==64
char *ptr = strstr(pathString, "i386");
2005-09-16 23:48:51 +00:00
if (ptr)
{
//attempt to fix the binary name
*ptr = 0;
path.assign(pathString);
path.append("amd64.so");
} else {
ptr = strstr(pathString, ".dll");
2005-09-16 23:48:51 +00:00
if (ptr)
{
*ptr = 0;
path.assign(pathString);
path.append("_amd64.so");
} else {
ptr = strstr(pathString, ".so");
2005-09-16 23:48:51 +00:00
if (ptr)
{
path.assign(pathString);
} else {
//no extension at all
path.assign(pathString);
path.append("_amd64.so");
}
}
}
#else
2005-09-16 23:48:51 +00:00
2004-09-12 07:18:54 +00:00
#ifdef __linux__
char *ptr = strstr(pathString, "amd64");
2005-09-16 23:48:51 +00:00
if (ptr)
{
//attempt to fix the binary name
*ptr = 0;
path.assign(pathString);
path.append("i386.so");
} else {
ptr = strstr(pathString, ".dll");
2005-09-16 23:48:51 +00:00
if (ptr)
{
*ptr = 0;
path.assign(pathString);
path.append("_i386.so");
} else {
2004-09-12 07:18:54 +00:00
//check to see if this file even has an extension
ptr = strstr(pathString, ".so");
2005-09-16 23:48:51 +00:00
if (ptr)
{
path.assign(pathString);
} else {
path.assign(pathString);
path.append("_i386.so");
}
}
}
2004-09-12 07:18:54 +00:00
#else
char *ptr = strstr(pathString, ".dll");
2005-09-16 23:48:51 +00:00
2004-09-12 07:18:54 +00:00
if (ptr)
{
path.assign(pathString);
} else {
//prevent this from loading .so too
ptr = strstr(pathString, ".so");
2005-09-16 23:48:51 +00:00
2004-09-12 07:18:54 +00:00
if (ptr)
{
int i = 0, len = strlen(pathString), c = -1;
2005-09-16 23:48:51 +00:00
for (i = len - 1; i >= 0; i--)
2004-09-12 07:18:54 +00:00
{
//cut off at first _
if (pathString[i] == '_')
{
//make sure this is a valid _
2005-09-16 23:48:51 +00:00
if (i == len - 1 || strncmp(&(pathString[i + 1]), "amxx", 4) == 0)
2004-09-12 07:18:54 +00:00
break;
2005-09-16 23:48:51 +00:00
2004-09-12 07:18:54 +00:00
c = i;
break;
}
}
*ptr = 0;
2005-09-16 23:48:51 +00:00
2004-09-12 07:18:54 +00:00
if (c == -1)
{
path.assign(pathString);
path.append(".dll");
} else {
ptr = (char *)&(pathString[c]);
*ptr = 0;
path.assign(pathString);
path.append(".dll");
}
} else {
path.assign(pathString);
path.append(".dll");
}
}
#endif //__linux__
#endif //PAWN_CELL_SIZE==64
}
int loadModules(const char* filename, PLUG_LOADTIME now)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
FILE *fp = fopen(build_pathname("%s", filename), "rt");
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
if (!fp)
{
2005-09-16 23:48:51 +00:00
AMXXLOG_Log("[AMXX] Modules list not found (file \"%s\")", filename);
return 0;
}
2004-01-31 20:56:22 +00:00
char moduleName[256];
char pathString[512];
String line;
int loaded = 0;
2004-01-31 20:56:22 +00:00
String path;
while (!feof(fp))
{
if (!line._fread(fp) || line.size() < 1)
continue;
2005-09-16 23:48:51 +00:00
line.trim();
*moduleName = 0;
2005-09-16 23:48:51 +00:00
if (sscanf(line.c_str(), "%s", moduleName) == EOF)
continue;
2005-09-16 23:48:51 +00:00
if (moduleName[0] == ';')
continue;
char* pathname = build_pathname("%s/%s", get_localinfo("amxx_modulesdir", "addons/amxmodx/modules"), moduleName);
strcpy(pathString, pathname);
path.assign("");
ConvertModuleName(pathString, path);
if (!validFile(path.c_str()))
continue;
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a = g_modules.find(path.c_str());
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
if (a)
continue; // already loaded
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
CModule* cc = new CModule(path.c_str());
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
if (cc == 0)
{
fclose(fp);
return loaded;
}
2004-01-31 20:56:22 +00:00
cc->queryModule();
2005-09-16 23:48:51 +00:00
switch (cc->getStatusValue())
{
case MODULE_BADLOAD:
report_error(1, "[AMXX] Module is not a valid library (file \"%s\")", path.c_str());
break;
case MODULE_NOINFO:
report_error(1, "[AMXX] Couldn't find info. about module (file \"%s\")", path.c_str());
break;
case MODULE_NOQUERY:
report_error(1, "[AMXX] Couldn't find \"AMX_Query\" or \"AMXX_Query\" (file \"%s\")", path.c_str());
break;
case MODULE_NOATTACH:
report_error(1, "[AMXX] Couldn't find \"%s\" (file \"%s\")", cc->isAmxx() ? "AMXX_Attach" : "AMX_Attach", path.c_str());
break;
case MODULE_OLD:
report_error(1, "[AMXX] Module has a different interface version (file \"%s\")", path.c_str());
break;
case MODULE_NEWER:
report_error(1, "[AMXX] Module has a newer interface version (file \"%s\"). Please download a new amxmodx.", path.c_str());
break;
case MODULE_INTERROR:
report_error(1, "[AMXX] Internal error during module load (file \"%s\")", path.c_str());
break;
case MODULE_NOT64BIT:
report_error(1, "[AMXX] Module \"%s\" is not 64 bit compatible.", path.c_str());
break;
default:
++loaded;
}
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
g_modules.put(cc);
#ifndef FAKEMETA
2005-09-16 23:48:51 +00:00
if (cc->IsMetamod())
{
char* mmpathname = build_pathname_addons("%s/%s", get_localinfo("amxx_modulesdir", "addons/amxmodx/modules"), line.c_str());
2005-09-16 23:48:51 +00:00
ConvertModuleName(mmpathname, path);
cc->attachMetamod(path.c_str(), now);
}
bool retVal = cc->attachModule();
2005-09-16 23:48:51 +00:00
if (cc->isAmxx() && !retVal)
{
switch (cc->getStatusValue())
{
2005-09-16 23:48:51 +00:00
case MODULE_FUNCNOTPRESENT:
report_error(1, "[AMXX] Module requested a not exisitng function (file \"%s\")%s%s%s", cc->getFilename(), cc->getMissingFunc() ? " (func \"" : "",
cc->getMissingFunc() ? cc->getMissingFunc() : "", cc->getMissingFunc() ? "\")" : "");
break;
case MODULE_INTERROR:
report_error(1, "[AMXX] Internal error during module load (file \"%s\")", cc->getFilename());
break;
case MODULE_BADLOAD:
report_error(1, "[AMXX] Module is not a valid library (file \"%s\")", cc->getFilename());
break;
default:
break;
}
}
#endif
}
2004-01-31 20:56:22 +00:00
fclose(fp);
return loaded;
2004-01-31 20:56:22 +00:00
}
void detachModules()
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a = g_modules.begin();
2005-09-16 23:48:51 +00:00
while (a)
2004-01-31 20:56:22 +00:00
{
(*a).detachModule();
a.remove();
}
}
void detachReloadModules()
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a = g_modules.begin();
2005-09-16 23:48:51 +00:00
while (a)
2004-01-31 20:56:22 +00:00
{
#ifdef FAKEMETA
2005-09-16 23:48:51 +00:00
if ((*a).isReloadable())
#else
2005-09-16 23:48:51 +00:00
if ((*a).isReloadable() && !(*a).IsMetamod())
#endif
2004-01-31 20:56:22 +00:00
{
(*a).detachModule();
a.remove();
2005-09-16 23:48:51 +00:00
2004-01-31 20:56:22 +00:00
continue;
}
++a;
}
}
#ifdef FAKEMETA
2004-01-31 20:56:22 +00:00
void attachModules()
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a = g_modules.begin();
2005-09-16 23:48:51 +00:00
while (a)
2004-01-31 20:56:22 +00:00
{
bool retVal = (*a).attachModule();
2005-09-16 23:48:51 +00:00
if ((*a).isAmxx() && !retVal)
{
switch ((*a).getStatusValue())
{
2005-09-16 23:48:51 +00:00
case MODULE_FUNCNOTPRESENT:
report_error(1, "[AMXX] Module requested a not exisitng function (file \"%s\")%s%s%s", (*a).getFilename(), (*a).getMissingFunc() ? " (func \"" : "",
(*a).getMissingFunc() ? (*a).getMissingFunc() : "", (*a).getMissingFunc() ? "\")" : "");
break;
case MODULE_INTERROR:
report_error(1, "[AMXX] Internal error during module load (file \"%s\")", (*a).getFilename());
break;
case MODULE_BADLOAD:
report_error(1, "[AMXX] Module is not a valid library (file \"%s\")", (*a).getFilename());
break;
default:
break;
}
}
2004-01-31 20:56:22 +00:00
++a;
}
}
#endif
2004-01-31 20:56:22 +00:00
2005-09-16 23:48:51 +00:00
const char* strip_name(const char* a)
2004-01-31 20:56:22 +00:00
{
const char* ret = a;
2005-09-16 23:48:51 +00:00
while (*a)
{
if (*a == '/' || *a == '\\')
{
ret = ++a;
continue;
}
++a;
}
2005-09-16 23:48:51 +00:00
return ret;
2004-01-31 20:56:22 +00:00
}
#ifdef FAKEMETA
void attachMetaModModules(PLUG_LOADTIME now, const char* filename)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
File fp(build_pathname("%s", filename), "r");
2005-09-16 23:48:51 +00:00
if (!fp)
{
2005-09-16 23:48:51 +00:00
AMXXLOG_Log("[AMXX] Modules list not found (file \"%s\")", filename);
return;
}
char line[256], moduleName[256];
String modPath, mmPath;
DLHANDLE module;
2005-09-16 23:48:51 +00:00
while (fp.getline(line, 255))
{
*moduleName = 0;
2005-09-16 23:48:51 +00:00
sscanf(line, "%s", moduleName);
2005-09-16 23:48:51 +00:00
if (!isalnum(*moduleName))
continue;
char* pathname = build_pathname("%s/%s", get_localinfo("amxx_modulesdir", "addons/amxmodx/modules"), line);
char* mmpathname = build_pathname_addons("%s/%s", get_localinfo("amxx_modulesdir", "addons/amxmodx/modules"), line);
ConvertModuleName(pathname, modPath);
ConvertModuleName(mmpathname, mmPath);
2004-09-12 07:18:54 +00:00
CList<CFakeMeta::CFakeMetaPlugin>::iterator iter = g_FakeMeta.m_Plugins.begin();
//prevent double loading
int foundFlag = 0;
while (iter)
{
2005-09-16 23:48:51 +00:00
if (strcmp((*iter).GetPath(), mmPath.c_str()) == 0)
2004-09-12 07:18:54 +00:00
{
foundFlag = 1;
break;
}
++iter;
}
if (foundFlag)
continue;
2005-09-16 23:48:51 +00:00
module = DLLOAD(modPath.c_str()); // link dll
2005-09-16 23:48:51 +00:00
if (module)
2004-01-31 20:56:22 +00:00
{
2005-09-16 23:48:51 +00:00
int a = (int)DLPROC(module, "Meta_Attach");
DLFREE(module);
2005-09-16 23:48:51 +00:00
if (a)
{
g_FakeMeta.AddPlugin(mmPath.c_str());
}
2004-01-31 20:56:22 +00:00
}
}
2005-09-16 23:48:51 +00:00
g_FakeMeta.Meta_Query(gpMetaUtilFuncs);
g_FakeMeta.Meta_Attach(now, gpMetaGlobals, gpGamedllFuncs);
2004-01-31 20:56:22 +00:00
}
#endif
// Get the number of running modules
int countModules(CountModulesMode mode)
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator iter;
int num;
2005-09-16 23:48:51 +00:00
switch (mode)
{
2005-09-16 23:48:51 +00:00
case CountModules_All:
return g_modules.size();
case CountModules_Running:
iter = g_modules.begin();
num = 0;
while (iter)
{
if ((*iter).getStatusValue() == MODULE_LOADED)
++num;
++iter;
}
return num;
case CountModules_Stopped:
iter = g_modules.begin();
num = 0;
while (iter)
{
if ((*iter).getStatusValue() != MODULE_LOADED)
++num;
++iter;
}
return num;
}
2005-09-16 23:48:51 +00:00
return 0;
}
// Call all modules' AMXX_PluginsLoaded functions
void modules_callPluginsLoaded()
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator iter = g_modules.begin();
2004-05-03 19:46:49 +00:00
while (iter)
{
(*iter).CallPluginsLoaded();
2004-05-03 19:46:49 +00:00
++iter;
}
}
// new functions
int MNF_AddNatives(AMX_NATIVE_INFO* natives)
{
2005-09-16 23:48:51 +00:00
CList<CModule, const char *>::iterator a = g_modules.begin();
if (!g_CurrentlyCalledModule || g_ModuleCallReason != ModuleCall_Attach)
return FALSE; // may only be called from attach
// This is needed so that CList can free it ;]
AMX_NATIVE_INFO** pPtr = new AMX_NATIVE_INFO*(natives);
if (!pPtr)
return FALSE;
g_CurrentlyCalledModule->m_Natives.put(pPtr);
2005-09-16 23:48:51 +00:00
return TRUE;
}
const char *MNF_GetModname(void)
{
// :TODO: Do we have to do this??
// I dunno who wrote the above comment but no
#if 0
static char buffer[64];
2004-08-13 08:46:04 +00:00
strcpy(buffer, g_mod_name.c_str());
return buffer;
#endif
return g_mod_name.c_str();
}
AMX *MNF_GetAmxScript(int id)
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator iter = g_loadedscripts.begin();
while (iter && id--)
++iter;
if (iter == NULL)
2004-08-13 11:05:38 +00:00
return NULL;
2005-09-16 23:48:51 +00:00
return (*iter).getAMX();
}
const char *MNF_GetAmxScriptName(int id)
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator iter = g_loadedscripts.begin();
while (iter && id--)
++iter;
if (iter == NULL)
2004-08-13 10:46:13 +00:00
return NULL;
2005-09-16 23:48:51 +00:00
return (*iter).getName();
}
int MNF_FindAmxScriptByName(const char *name)
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator iter = g_loadedscripts.begin();
bool found = false;
int i = 0;
2005-09-16 23:48:51 +00:00
while (iter)
{
if (stricmp((*iter).getName(), name) == 0)
{
found = true;
break;
}
++iter;
++i;
}
2005-09-16 23:48:51 +00:00
if (!found)
return -1;
2005-09-16 23:48:51 +00:00
return i;
}
int MNF_FindAmxScriptByAmx(const AMX *amx)
{
2005-09-16 23:48:51 +00:00
CList<CScript, AMX*>::iterator iter = g_loadedscripts.begin();
bool found = false;
int i = 0;
2005-09-16 23:48:51 +00:00
while (iter)
2005-09-16 23:48:51 +00:00
{
if (amx == (*iter).getAMX())
{
found = true;
break;
}
++iter;
++i;
}
2005-09-16 23:48:51 +00:00
if (!found)
return -1;
2005-09-16 23:48:51 +00:00
return i;
}
char *MNF_GetAmxString(AMX *amx, cell amx_addr, int bufferId, int *pLen)
{
int len;
char *retVal = get_amxstring(amx, amx_addr, bufferId, len);
2005-09-16 23:48:51 +00:00
if (pLen)
*pLen = len;
2005-09-16 23:48:51 +00:00
return retVal;
}
int MNF_GetAmxStringLen(const cell *ptr)
{
register int c = 0;
2005-09-16 23:48:51 +00:00
while (ptr[c])
++c;
2005-09-16 23:48:51 +00:00
return c;
}
char *MNF_FormatAmxString(AMX *amx, cell *params, int startParam, int *pLen)
{
int len;
char *retVal = format_amxstring(amx, params, startParam, len);
2005-09-16 23:48:51 +00:00
if (pLen)
*pLen = len;
2005-09-16 23:48:51 +00:00
return retVal;
}
2004-09-01 21:13:30 +00:00
int MNF_GetPlayerFlags(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
2004-09-01 21:13:30 +00:00
CPlayer *pPlayer = GET_PLAYER_POINTER_I(id);
2005-09-16 23:48:51 +00:00
2004-09-01 21:13:30 +00:00
return (pPlayer->flags[0]);
}
void MNF_CopyAmxMemory(cell * dest, const cell * src, int len)
{
2005-09-16 23:48:51 +00:00
memcpy((void*)dest, (const void *)src, (size_t)len * sizeof(cell));
}
int MNF_IsPlayerValid(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
CPlayer *pPlayer = GET_PLAYER_POINTER_I(id);
2005-09-16 23:48:51 +00:00
return (pPlayer->initialized) ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
const char * MNF_GetPlayerName(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return NULL;
2005-09-16 23:48:51 +00:00
2004-08-13 08:46:04 +00:00
return GET_PLAYER_POINTER_I(id)->name.c_str();
}
2005-09-16 23:48:51 +00:00
const char * MNF_GetPlayerIP(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return NULL;
2005-09-16 23:48:51 +00:00
2004-08-13 08:46:04 +00:00
return GET_PLAYER_POINTER_I(id)->ip.c_str();
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerInGame(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->ingame ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerBot(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->IsBot() ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerAuthorized(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->authorized ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
float MNF_GetPlayerTime(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0.0f;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->time;
}
2005-09-16 23:48:51 +00:00
float MNF_GetPlayerPlayTime(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0.0f;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->playtime;
}
2005-09-16 23:48:51 +00:00
int MNF_GetPlayerCurweapon(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->current;
}
2005-09-16 23:48:51 +00:00
int MNF_GetPlayerTeamID(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->teamId;
}
2005-09-16 23:48:51 +00:00
int MNF_GetPlayerDeaths(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->deaths;
}
2005-09-16 23:48:51 +00:00
int MNF_GetPlayerMenu(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->menu;
}
2005-09-16 23:48:51 +00:00
int MNF_GetPlayerKeys(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->keys;
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerAlive(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->IsAlive() ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
float MNF_GetPlayerFrags(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0.0f;
2005-09-16 23:48:51 +00:00
return GET_PLAYER_POINTER_I(id)->pEdict->v.frags;
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerConnecting(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
CPlayer * pPlayer = GET_PLAYER_POINTER_I(id);
2005-09-16 23:48:51 +00:00
return (!pPlayer->ingame && pPlayer->initialized && (GETPLAYERUSERID(pPlayer->pEdict) > 0)) ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
int MNF_IsPlayerHLTV(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return (GET_PLAYER_POINTER_I(id)->pEdict->v.flags & FL_PROXY) ? 1 : 0;
}
2005-09-16 23:48:51 +00:00
float MNF_GetPlayerArmor(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0.0f;
2005-09-16 23:48:51 +00:00
return (GET_PLAYER_POINTER_I(id)->pEdict->v.armorvalue);
}
2005-09-16 23:48:51 +00:00
float MNF_GetPlayerHealth(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return 0;
2005-09-16 23:48:51 +00:00
return (GET_PLAYER_POINTER_I(id)->pEdict->v.health);
}
cell MNF_RealToCell(REAL x)
{
return *(cell*)&x;
}
REAL MNF_CellToReal(cell x)
{
return *(REAL*)&x;
}
2004-07-08 16:02:01 +00:00
void MNF_Log(const char *fmt, ...)
{
char msg[3072];
va_list arglst;
va_start(arglst, fmt);
2005-09-16 23:48:51 +00:00
_vsnprintf(msg, sizeof(msg) - 1, fmt, arglst);
//vsprintf(msg, fmt, arglst);
2004-07-08 16:02:01 +00:00
va_end(arglst);
2005-09-16 23:48:51 +00:00
2004-07-08 16:02:01 +00:00
AMXXLOG_Log("%s", msg);
}
2004-07-28 19:28:36 +00:00
2004-09-15 21:21:46 +00:00
//by BAILOPAN
// debugger engine front end
void LogError(AMX *amx, int err, const char *fmt, ...)
{
2005-09-09 03:23:31 +00:00
Debugger *pDebugger = (Debugger *)amx->userdata[UD_DEBUGGER];
amx->error = err;
char msg_buffer[2048];
2005-07-26 21:31:21 +00:00
msg_buffer[0] = '\0';
2005-09-16 23:48:51 +00:00
2005-09-09 03:23:31 +00:00
if (fmt != NULL)
2005-07-26 21:31:21 +00:00
{
2005-09-09 03:23:31 +00:00
va_list ap;
va_start(ap, fmt);
2005-09-16 23:48:51 +00:00
_vsnprintf(msg_buffer, sizeof(msg_buffer) - 1, fmt, ap);
2005-09-09 03:23:31 +00:00
va_end(ap);
}
2005-07-26 21:31:21 +00:00
//give the plugin first chance to handle any sort of error
Handler *pHandler = (Handler *)amx->userdata[UD_HANDLER];
2005-09-11 03:58:38 +00:00
if (pHandler->InNativeFilter())
{
2005-09-11 03:58:38 +00:00
if (pDebugger)
pDebugger->EndExec();
} else {
if (pHandler)
2005-09-09 23:13:34 +00:00
{
2005-09-11 03:58:38 +00:00
if (pHandler->IsHandling())
{
if (fmt != NULL)
pHandler->SetErrorMsg(msg_buffer);
return;
}
2005-09-16 23:48:51 +00:00
2005-09-11 03:58:38 +00:00
//give the user a first-chance at blocking the error from displaying
if (pHandler->HandleError(fmt ? msg_buffer : NULL) != 0)
{
amx->error = -1;
return;
}
2005-09-09 23:13:34 +00:00
}
}
2005-07-26 21:31:21 +00:00
2005-09-09 03:23:31 +00:00
if (!pDebugger)
{
2005-09-09 23:13:34 +00:00
if (fmt)
AMXXLOG_Log("%s", msg_buffer);
2005-09-16 23:48:51 +00:00
2005-09-09 23:13:34 +00:00
Debugger::GenericMessage(amx, err);
2005-09-09 03:23:31 +00:00
AMXXLOG_Log("[AMXX] To enable debug mode, add \"debug\" after the plugin name in plugins.ini (without quotes).");
//destroy original error code so the original is not displayed again
} else {
pDebugger->SetTracedError(err);
//we can display error now
pDebugger->DisplayTrace(fmt ? msg_buffer : NULL);
2005-09-09 03:23:31 +00:00
}
2005-09-09 23:13:34 +00:00
amx->error = -1;
2004-09-15 21:21:46 +00:00
}
2004-07-28 19:28:36 +00:00
void MNF_MergeDefinitionFile(const char *file)
{
g_langMngr.MergeDefinitionFile(file);
}
2004-09-05 22:09:45 +00:00
edict_t* MNF_GetPlayerEdict(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return NULL;
2005-09-16 23:48:51 +00:00
2004-09-05 22:09:45 +00:00
return (GET_PLAYER_POINTER_I(id)->pEdict);
}
const char *MNF_Format(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
const char *retVal = g_langMngr.FormatString(fmt, ap);
va_end(ap);
2005-09-16 23:48:51 +00:00
return retVal;
}
2004-09-14 16:18:52 +00:00
const char *MNF_GetPlayerTeam(int id)
{
if (id < 1 || id > gpGlobals->maxClients)
return NULL;
return (GET_PLAYER_POINTER_I(id)->team.c_str());
}
#ifndef MEMORY_TEST
void *MNF_Allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int allocationType, const size_t reportedSize)
{
return malloc(reportedSize);
}
void *MNF_Reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress)
{
return realloc(reportedAddress, reportedSize);
}
void MNF_Deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int deallocationType, void *reportedAddress)
{
free(reportedAddress);
}
#endif
// 09/18/2004 : added these two funcs that default to copyBack=false so we don't break all modules
cell MNF_PrepareCellArray(cell *ptr, unsigned int size)
{
return prepareCellArray(ptr, size, false);
}
cell MNF_PrepareCharArray(char *ptr, unsigned int size)
{
return prepareCharArray(ptr, size, false);
}
inline bool operator ==(func_s &arg1, const char *desc)
{
if (strcmp(arg1.desc, desc) == 0)
return true;
2005-09-16 23:48:51 +00:00
return false;
}
CList<func_s, const char *> g_functions;
// Fnptr Request function for the new interface
const char *g_LastRequestedFunc = NULL;
#define REGISTER_FUNC(name, func) \
{ \
pFunc = new func_s; \
pFunc->pfn = (void *)func; \
pFunc->desc = name; \
g_functions.put(pFunc); \
}
void MNF_RegisterFunction(void *pfn, const char *description)
{
func_s *pFunc;
REGISTER_FUNC(description, pfn);
}
void Module_UncacheFunctions()
{
g_functions.clear();
}
int amx_Execv()
{
return AMX_ERR_NOTFOUND;
}
void Module_CacheFunctions()
{
func_s *pFunc;
REGISTER_FUNC("BuildPathname", build_pathname)
REGISTER_FUNC("BuildPathnameR", build_pathname_r)
REGISTER_FUNC("PrintSrvConsole", print_srvconsole)
REGISTER_FUNC("GetModname", MNF_GetModname)
REGISTER_FUNC("Log", MNF_Log)
REGISTER_FUNC("LogError", LogError)
REGISTER_FUNC("MergeDefinitionFile", MNF_MergeDefinitionFile)
REGISTER_FUNC("Format", MNF_Format)
REGISTER_FUNC("RegisterFunction", MNF_RegisterFunction);
// Amx scripts loading / unloading / managing
REGISTER_FUNC("GetAmxScript", MNF_GetAmxScript)
REGISTER_FUNC("GetAmxScriptName", MNF_GetAmxScriptName)
REGISTER_FUNC("FindAmxScriptByName", MNF_FindAmxScriptByName)
REGISTER_FUNC("FindAmxScriptByAmx", MNF_FindAmxScriptByAmx)
REGISTER_FUNC("LoadAmxScript", load_amxscript)
REGISTER_FUNC("UnloadAmxScript", unload_amxscript)
// String / mem in amx scripts support
REGISTER_FUNC("SetAmxString", set_amxstring)
REGISTER_FUNC("GetAmxString", MNF_GetAmxString)
REGISTER_FUNC("GetAmxStringLen", MNF_GetAmxStringLen)
REGISTER_FUNC("FormatAmxString", MNF_FormatAmxString)
REGISTER_FUNC("CopyAmxMemory", MNF_CopyAmxMemory)
REGISTER_FUNC("GetAmxAddr", get_amxaddr)
// other amx stuff
REGISTER_FUNC("amx_Exec", amx_Exec)
2005-08-02 07:01:25 +00:00
REGISTER_FUNC("amx_Push", amx_Push)
REGISTER_FUNC("amx_Execv", amx_Execv) //I HOPE NO ONE USES THIS!!!!
REGISTER_FUNC("amx_Allot", amx_Allot)
REGISTER_FUNC("amx_FindPublic", amx_FindPublic)
REGISTER_FUNC("amx_FindNative", amx_FindNative)
// Natives / Forwards
REGISTER_FUNC("AddNatives", MNF_AddNatives)
REGISTER_FUNC("RaiseAmxError", amx_RaiseError)
REGISTER_FUNC("RegisterForward", registerForward)
REGISTER_FUNC("RegisterSPForward", registerSPForward)
REGISTER_FUNC("RegisterSPForwardByName", registerSPForwardByName)
REGISTER_FUNC("UnregisterSPForward", unregisterSPForward)
REGISTER_FUNC("ExecuteForward", executeForwards)
REGISTER_FUNC("PrepareCellArray", MNF_PrepareCellArray)
REGISTER_FUNC("PrepareCharArray", MNF_PrepareCharArray)
REGISTER_FUNC("PrepareCellArrayA", prepareCellArray)
REGISTER_FUNC("PrepareCharArrayA", prepareCharArray)
// Player
REGISTER_FUNC("GetPlayerFlags", MNF_GetPlayerFlags)
REGISTER_FUNC("IsPlayerValid", MNF_IsPlayerValid)
REGISTER_FUNC("GetPlayerName", MNF_GetPlayerName)
REGISTER_FUNC("GetPlayerIP", MNF_GetPlayerIP)
REGISTER_FUNC("IsPlayerInGame", MNF_IsPlayerInGame)
REGISTER_FUNC("IsPlayerBot", MNF_IsPlayerBot)
REGISTER_FUNC("IsPlayerAuthorized", MNF_IsPlayerAuthorized)
REGISTER_FUNC("GetPlayerTime", MNF_GetPlayerTime)
REGISTER_FUNC("GetPlayerPlayTime", MNF_GetPlayerPlayTime)
REGISTER_FUNC("GetPlayerCurweapon", MNF_GetPlayerCurweapon)
REGISTER_FUNC("GetPlayerTeamID", MNF_GetPlayerTeamID)
REGISTER_FUNC("GetPlayerTeam", MNF_GetPlayerTeam)
REGISTER_FUNC("GetPlayerDeaths", MNF_GetPlayerDeaths)
REGISTER_FUNC("GetPlayerFrags", MNF_GetPlayerFrags)
REGISTER_FUNC("GetPlayerMenu", MNF_GetPlayerMenu)
REGISTER_FUNC("GetPlayerKeys", MNF_GetPlayerKeys)
REGISTER_FUNC("IsPlayerAlive", MNF_IsPlayerAlive)
REGISTER_FUNC("IsPlayerConnecting", MNF_IsPlayerConnecting)
REGISTER_FUNC("IsPlayerHLTV", MNF_IsPlayerHLTV)
REGISTER_FUNC("GetPlayerArmor", MNF_GetPlayerArmor)
REGISTER_FUNC("GetPlayerHealth", MNF_GetPlayerHealth)
REGISTER_FUNC("GetPlayerEdict", MNF_GetPlayerEdict)
REGISTER_FUNC("CellToReal", MNF_CellToReal)
REGISTER_FUNC("RealToCell", MNF_RealToCell)
#ifdef MEMORY_TEST
REGISTER_FUNC("Allocator", m_allocator)
REGISTER_FUNC("Deallocator", m_deallocator)
REGISTER_FUNC("Reallocator", m_reallocator)
#else
REGISTER_FUNC("Allocator", MNF_Allocator)
REGISTER_FUNC("Deallocator", MNF_Deallocator)
REGISTER_FUNC("Reallocator", MNF_Reallocator)
#endif // MEMORY_TEST
}
void *Module_ReqFnptr(const char *funcName)
{
// code
// ^---- really? wow!
2005-09-16 23:48:51 +00:00
if (!g_CurrentlyCalledModule)
2004-05-03 19:46:49 +00:00
{
return NULL;
}
g_LastRequestedFunc = funcName;
CList<func_s, const char *>::iterator iter;
2005-09-16 23:48:51 +00:00
for (iter = g_functions.begin(); iter; ++iter)
{
if (strcmp(funcName, iter->desc) == 0)
return iter->pfn;
}
return NULL;
2004-09-02 02:16:38 +00:00
}
2005-08-18 06:34:34 +00:00
2005-08-18 06:37:05 +00:00
#if !defined MEMORY_TEST && !defined WIN32
2005-09-16 23:48:51 +00:00
void * ::operator new(size_t size)
{
return (calloc(1, size));
2005-08-18 06:34:34 +00:00
}
2005-09-16 23:48:51 +00:00
void * ::operator new[](size_t size)
{
return (calloc(1, size));
2005-08-18 06:34:34 +00:00
}
2005-09-16 23:48:51 +00:00
void ::operator delete(void * ptr)
{
if (ptr)
2005-08-18 06:34:34 +00:00
free(ptr);
}
2005-09-16 23:48:51 +00:00
void ::operator delete[](void * ptr)
{
if (ptr)
2005-08-18 06:34:34 +00:00
free(ptr);
}
#endif