2014-08-04 08:36:20 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2004-03-24 01:35:44 +00:00
|
|
|
#include "amxmodx.h"
|
2006-05-07 09:56:06 +00:00
|
|
|
#include "libraries.h"
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
#ifndef FAR
|
2005-09-10 20:09:14 +00:00
|
|
|
#define FAR
|
2004-02-04 19:21:42 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
// New
|
|
|
|
typedef void* (*PFN_REQ_FNPTR)(const char * /*name*/);
|
|
|
|
typedef int (FAR *QUERYMOD_NEW)(int * /*ifvers*/, amxx_module_info_s * /*modInfo*/);
|
2008-04-27 00:07:06 +00:00
|
|
|
typedef int (FAR *CHECKGAME_NEW)(const char *);
|
2004-03-31 18:02:20 +00:00
|
|
|
typedef int (FAR *ATTACHMOD_NEW)(PFN_REQ_FNPTR /*reqFnptrFunc*/);
|
|
|
|
typedef int (FAR *DETACHMOD_NEW)(void);
|
2004-04-03 19:15:06 +00:00
|
|
|
typedef void (FAR *PLUGINSLOADED_NEW)(void);
|
2006-05-07 09:56:06 +00:00
|
|
|
typedef void (*PLUGINSUNLOADED_NEW)(void);
|
|
|
|
typedef void (*PLUGINSUNLOADING_NEW)(void);
|
2004-01-31 20:56:22 +00:00
|
|
|
|
|
|
|
// *****************************************************
|
|
|
|
// class CModule
|
|
|
|
// *****************************************************
|
|
|
|
|
2004-09-12 03:48:21 +00:00
|
|
|
CModule::CModule(const char* fname)
|
2004-01-31 20:56:22 +00:00
|
|
|
{
|
2004-09-12 03:48:21 +00:00
|
|
|
m_Filename.assign(fname);
|
2004-03-31 18:02:20 +00:00
|
|
|
clear(false);
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
CModule::~CModule()
|
|
|
|
{
|
2004-03-31 18:02:20 +00:00
|
|
|
// old & new
|
2005-09-10 20:09:14 +00:00
|
|
|
if (m_Handle)
|
2004-03-31 18:02:20 +00:00
|
|
|
DLFREE(m_Handle);
|
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModule::clear(bool clearFilename)
|
|
|
|
{
|
|
|
|
// old & new
|
|
|
|
m_Metamod = false;
|
|
|
|
m_Handle = NULL;
|
|
|
|
m_Status = MODULE_NONE;
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
if (clearFilename)
|
2004-08-13 08:46:04 +00:00
|
|
|
m_Filename.assign("unknown");
|
2004-03-31 18:02:20 +00:00
|
|
|
|
|
|
|
// new
|
|
|
|
m_Amxx = false;
|
|
|
|
m_InfoNew.author = "unknown";
|
|
|
|
m_InfoNew.name = "unknown";
|
|
|
|
m_InfoNew.version = "unknown";
|
|
|
|
m_InfoNew.reload = 0;
|
|
|
|
m_MissingFunc = NULL;
|
|
|
|
|
2006-05-10 11:23:08 +00:00
|
|
|
for (size_t i=0; i<m_DestroyableIndexes.size(); i++)
|
|
|
|
{
|
|
|
|
delete [] m_Natives[m_DestroyableIndexes[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_DestroyableIndexes.clear();
|
2004-03-31 18:02:20 +00:00
|
|
|
m_Natives.clear();
|
2006-09-01 03:29:27 +00:00
|
|
|
m_NewNatives.clear();
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2005-07-22 19:32:16 +00:00
|
|
|
bool CModule::attachMetamod(const char *mmfile, PLUG_LOADTIME now)
|
|
|
|
{
|
|
|
|
void **handle;
|
|
|
|
void *dummy = NULL;
|
|
|
|
|
|
|
|
if (!m_Handle)
|
|
|
|
handle = &dummy;
|
|
|
|
else
|
|
|
|
handle = (void **)&m_Handle;
|
|
|
|
|
|
|
|
int res = LoadMetamodPlugin(mmfile, handle, now);
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
m_Metamod = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-05-10 11:23:08 +00:00
|
|
|
//this ugly function is ultimately something like O(n^4).
|
|
|
|
//sigh. it shouldn't be needed.
|
|
|
|
void CModule::rewriteNativeLists(AMX_NATIVE_INFO *list)
|
|
|
|
{
|
|
|
|
AMX_NATIVE_INFO *curlist;
|
|
|
|
for (size_t i=0; i<m_Natives.size(); i++)
|
|
|
|
{
|
|
|
|
curlist = m_Natives[i];
|
|
|
|
bool changed = false;
|
|
|
|
bool found = false;
|
|
|
|
CVector<size_t> newlist;
|
|
|
|
for (size_t j=0; curlist[j].func != NULL; j++)
|
|
|
|
{
|
|
|
|
found = false;
|
|
|
|
for (size_t k=0; list[k].func != NULL; k++)
|
|
|
|
{
|
|
|
|
if (strcmp(curlist[j].name, list[k].name) == 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
changed = true;
|
|
|
|
//don't break, we have to search it all
|
|
|
|
} else {
|
|
|
|
newlist.push_back(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
//now build the new list
|
|
|
|
AMX_NATIVE_INFO *rlist = new AMX_NATIVE_INFO[newlist.size()+1];
|
|
|
|
for (size_t j=0; j<newlist.size(); j++)
|
|
|
|
{
|
|
|
|
rlist[j].func = curlist[newlist[j]].func;
|
|
|
|
rlist[j].name = curlist[newlist[j]].name;
|
|
|
|
}
|
|
|
|
rlist[newlist.size()].func = NULL;
|
|
|
|
rlist[newlist.size()].name = NULL;
|
|
|
|
m_Natives[i] = rlist;
|
|
|
|
m_DestroyableIndexes.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
bool CModule::attachModule()
|
|
|
|
{
|
2004-03-31 18:02:20 +00:00
|
|
|
// old & new
|
|
|
|
if (m_Status != MODULE_QUERY || !m_Handle)
|
2004-01-31 20:56:22 +00:00
|
|
|
return false;
|
2004-03-31 18:02:20 +00:00
|
|
|
|
|
|
|
if (m_Amxx)
|
|
|
|
{
|
|
|
|
// new
|
|
|
|
ATTACHMOD_NEW AttachFunc_New = (ATTACHMOD_NEW)DLPROC(m_Handle, "AMXX_Attach");
|
|
|
|
|
|
|
|
if (!AttachFunc_New)
|
|
|
|
return false;
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-04-03 19:15:06 +00:00
|
|
|
g_ModuleCallReason = ModuleCall_Attach;
|
|
|
|
g_CurrentlyCalledModule = this;
|
2004-03-31 18:02:20 +00:00
|
|
|
int retVal = (*AttachFunc_New)(Module_ReqFnptr);
|
2004-04-03 19:15:06 +00:00
|
|
|
g_CurrentlyCalledModule = NULL;
|
|
|
|
g_ModuleCallReason = ModuleCall_NotCalled;
|
2004-03-31 18:02:20 +00:00
|
|
|
|
|
|
|
switch (retVal)
|
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
case AMXX_OK:
|
|
|
|
m_Status = MODULE_LOADED;
|
2006-05-07 09:56:06 +00:00
|
|
|
break;
|
2005-09-10 20:09:14 +00:00
|
|
|
case AMXX_PARAM:
|
|
|
|
AMXXLOG_Log("[AMXX] Internal Error: Module \"%s\" (version \"%s\") retured \"Invalid parameter\" from Attach func.", m_Filename.c_str(), getVersion());
|
|
|
|
m_Status = MODULE_INTERROR;
|
|
|
|
return false;
|
|
|
|
case AMXX_FUNC_NOT_PRESENT:
|
|
|
|
m_Status = MODULE_FUNCNOTPRESENT;
|
|
|
|
m_MissingFunc = g_LastRequestedFunc;
|
|
|
|
return false;
|
|
|
|
default:
|
2005-09-16 23:48:51 +00:00
|
|
|
AMXXLOG_Log("[AMXX] Module \"%s\" (version \"%s\") returned an invalid code.", m_Filename.c_str(), getVersion());
|
2005-09-10 20:09:14 +00:00
|
|
|
m_Status = MODULE_BADLOAD;
|
|
|
|
return false;
|
2004-03-31 18:02:20 +00:00
|
|
|
}
|
2005-07-25 06:03:43 +00:00
|
|
|
} else {
|
|
|
|
m_Status = MODULE_BADLOAD;
|
2004-03-31 18:02:20 +00:00
|
|
|
}
|
|
|
|
|
2006-05-07 09:56:06 +00:00
|
|
|
if (m_Status == MODULE_LOADED)
|
|
|
|
{
|
|
|
|
AddLibrariesFromString(m_InfoNew.library, LibType_Library, LibSource_Module, this);
|
|
|
|
AddLibrariesFromString(m_InfoNew.libclass, LibType_Class, LibSource_Module, this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-25 06:03:43 +00:00
|
|
|
return false;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
bool CModule::queryModule()
|
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
if (m_Status != MODULE_NONE) // don't check if already queried
|
2004-01-31 20:56:22 +00:00
|
|
|
return false;
|
|
|
|
|
2004-08-13 08:46:04 +00:00
|
|
|
m_Handle = DLLOAD(m_Filename.c_str()); // load file
|
2004-03-31 18:02:20 +00:00
|
|
|
if (!m_Handle)
|
|
|
|
{
|
2013-02-13 07:14:37 +00:00
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2007-11-12 23:48:26 +00:00
|
|
|
AMXXLOG_Log("[AMXX] Module \"%s\" failed to load (%s)", m_Filename.c_str(), dlerror());
|
|
|
|
#endif
|
2004-03-31 18:02:20 +00:00
|
|
|
m_Status = MODULE_BADLOAD;
|
2004-01-31 20:56:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
|
|
|
// Check whether the module uses metamod (for auto attach)
|
|
|
|
if (DLPROC(m_Handle, "Meta_Attach"))
|
|
|
|
m_Metamod = true;
|
|
|
|
|
|
|
|
// Try new interface first
|
|
|
|
QUERYMOD_NEW queryFunc_New = (QUERYMOD_NEW)DLPROC(m_Handle, "AMXX_Query");
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
if (queryFunc_New)
|
|
|
|
{
|
|
|
|
m_Amxx = true;
|
|
|
|
int ifVers = AMXX_INTERFACE_VERSION;
|
2004-04-03 19:15:06 +00:00
|
|
|
g_ModuleCallReason = ModuleCall_Query;
|
|
|
|
g_CurrentlyCalledModule = this;
|
|
|
|
int retVal = (*queryFunc_New)(&ifVers, &m_InfoNew);
|
|
|
|
g_CurrentlyCalledModule = NULL;
|
|
|
|
g_ModuleCallReason = ModuleCall_NotCalled;
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-04-03 19:15:06 +00:00
|
|
|
switch (retVal)
|
2004-03-31 18:02:20 +00:00
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
case AMXX_PARAM:
|
|
|
|
AMXXLOG_Log("[AMXX] Internal Error: Module \"%s\" (version \"%s\") retured \"Invalid parameter\" from Attach func.", m_Filename.c_str(), getVersion());
|
|
|
|
m_Status = MODULE_INTERROR;
|
|
|
|
return false;
|
|
|
|
case AMXX_IFVERS:
|
|
|
|
if (ifVers < AMXX_INTERFACE_VERSION)
|
2006-05-07 09:56:06 +00:00
|
|
|
{
|
|
|
|
//backwards compat for new defs
|
|
|
|
if (ifVers == 3)
|
|
|
|
{
|
|
|
|
g_ModuleCallReason = ModuleCall_Query;
|
|
|
|
g_CurrentlyCalledModule = this;
|
|
|
|
retVal = (*queryFunc_New)(&ifVers, &m_InfoNew);
|
|
|
|
g_CurrentlyCalledModule = NULL;
|
|
|
|
g_ModuleCallReason = ModuleCall_NotCalled;
|
|
|
|
if (retVal == AMXX_OK)
|
|
|
|
{
|
|
|
|
m_InfoNew.library = m_InfoNew.logtag;
|
|
|
|
if (StrCaseStr(m_InfoNew.library, "sql")
|
|
|
|
|| StrCaseStr(m_InfoNew.library, "dbi"))
|
|
|
|
{
|
|
|
|
m_InfoNew.libclass = "DBI";
|
|
|
|
} else {
|
|
|
|
m_InfoNew.libclass = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
m_Status = MODULE_OLD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2005-09-10 20:09:14 +00:00
|
|
|
m_Status = MODULE_NEWER;
|
2006-05-07 09:56:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-09-10 20:09:14 +00:00
|
|
|
case AMXX_OK:
|
|
|
|
break;
|
|
|
|
default:
|
2005-09-16 23:48:51 +00:00
|
|
|
AMXXLOG_Log("[AMXX] Module \"%s\" (version \"%s\") returned an invalid code.", m_Filename.c_str(), getVersion());
|
2005-09-10 20:09:14 +00:00
|
|
|
m_Status = MODULE_BADLOAD;
|
|
|
|
return false;
|
2004-03-31 18:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for attach
|
|
|
|
if (!DLPROC(m_Handle, "AMXX_Attach"))
|
|
|
|
{
|
|
|
|
m_Status = MODULE_NOATTACH;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-27 00:07:06 +00:00
|
|
|
|
|
|
|
// Lastly, check to see if this module is able to load on this game mod
|
|
|
|
CHECKGAME_NEW checkGame_New = (CHECKGAME_NEW)DLPROC(m_Handle, "AMXX_CheckGame");
|
|
|
|
|
|
|
|
if (checkGame_New)
|
|
|
|
{
|
|
|
|
// This is an optional check; do not fail modules that do not have it
|
|
|
|
int ret = checkGame_New(g_mod_name.c_str());
|
|
|
|
|
|
|
|
if (ret != AMXX_GAME_OK)
|
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case AMXX_GAME_BAD:
|
|
|
|
AMXXLOG_Log("[AMXX] Module \"%s\" (version \"%s\") reported that it cannot load on game \"%s\"", m_Filename.c_str(), getVersion(), g_mod_name.c_str());
|
|
|
|
m_Status = MODULE_BADGAME;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AMXXLOG_Log("[AMXX] Module \"%s\" (version \"%s\") returned an unknown CheckGame code (value: %d)", m_Filename.c_str(), getVersion(), ret);
|
|
|
|
m_Status = MODULE_BADLOAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
m_Status = MODULE_QUERY;
|
|
|
|
return true;
|
2005-09-10 20:09:14 +00:00
|
|
|
} else {
|
2005-07-25 06:03:43 +00:00
|
|
|
m_Status = MODULE_NOQUERY;
|
|
|
|
m_Amxx = false;
|
2004-04-29 17:09:57 +00:00
|
|
|
return false;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
bool CModule::detachModule()
|
|
|
|
{
|
2004-03-31 18:02:20 +00:00
|
|
|
if (m_Status != MODULE_LOADED)
|
2004-01-31 20:56:22 +00:00
|
|
|
return false;
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2006-05-07 09:56:06 +00:00
|
|
|
RemoveLibraries(this);
|
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
if (m_Amxx)
|
|
|
|
{
|
|
|
|
DETACHMOD_NEW detachFunc_New = (DETACHMOD_NEW)DLPROC(m_Handle, "AMXX_Detach");
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
if (detachFunc_New)
|
2004-04-03 19:15:06 +00:00
|
|
|
{
|
|
|
|
g_ModuleCallReason = ModuleCall_Detach;
|
|
|
|
g_CurrentlyCalledModule = this;
|
2004-03-31 18:02:20 +00:00
|
|
|
(*detachFunc_New)();
|
2004-04-03 19:15:06 +00:00
|
|
|
g_CurrentlyCalledModule = NULL;
|
|
|
|
g_ModuleCallReason = ModuleCall_NotCalled;
|
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
}
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2005-07-22 19:32:16 +00:00
|
|
|
#ifndef FAKEMETA
|
|
|
|
if (IsMetamod())
|
|
|
|
{
|
|
|
|
UnloadMetamodPlugin(m_Handle);
|
|
|
|
}
|
|
|
|
#endif
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
DLFREE(m_Handle);
|
|
|
|
clear();
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-03-31 18:02:20 +00:00
|
|
|
|
2006-05-07 09:56:06 +00:00
|
|
|
void CModule::CallPluginsUnloaded()
|
|
|
|
{
|
|
|
|
if (m_Status != MODULE_LOADED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_Handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PLUGINSUNLOADED_NEW func = (PLUGINSUNLOADED_NEW)DLPROC(m_Handle, "AMXX_PluginsUnloaded");
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModule::CallPluginsUnloading()
|
|
|
|
{
|
|
|
|
if (m_Status != MODULE_LOADED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_Handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PLUGINSUNLOADING_NEW func = (PLUGINSUNLOADING_NEW)DLPROC(m_Handle, "AMXX_PluginsUnloading");
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
|
2004-04-03 19:15:06 +00:00
|
|
|
void CModule::CallPluginsLoaded()
|
|
|
|
{
|
|
|
|
if (m_Status != MODULE_LOADED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_Handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PLUGINSLOADED_NEW func = (PLUGINSLOADED_NEW)DLPROC(m_Handle, "AMXX_PluginsLoaded");
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-04-03 19:15:06 +00:00
|
|
|
if (!func)
|
|
|
|
return;
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-04-03 19:15:06 +00:00
|
|
|
func();
|
|
|
|
}
|
|
|
|
|
2004-03-31 18:02:20 +00:00
|
|
|
const char* CModule::getStatus() const
|
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
switch (m_Status)
|
2004-03-31 18:02:20 +00:00
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
case MODULE_NONE: return "error";
|
|
|
|
case MODULE_QUERY: return "pending";
|
|
|
|
case MODULE_BADLOAD: return "bad load";
|
|
|
|
case MODULE_LOADED: return "running";
|
|
|
|
case MODULE_NOINFO: return "no info";
|
|
|
|
case MODULE_NOQUERY: return "no query";
|
|
|
|
case MODULE_NOATTACH: return "no attach";
|
|
|
|
case MODULE_OLD: return "old";
|
|
|
|
case MODULE_FUNCNOTPRESENT:
|
|
|
|
case MODULE_NEWER: return "newer";
|
|
|
|
case MODULE_INTERROR: return "internal err";
|
|
|
|
case MODULE_NOT64BIT: return "not 64bit";
|
2008-04-27 00:07:06 +00:00
|
|
|
case MODULE_BADGAME: return "bad game";
|
2005-09-10 20:09:14 +00:00
|
|
|
default: break;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-01-31 20:56:22 +00:00
|
|
|
return "unknown";
|
|
|
|
}
|