Modules can now intercommunicate through RegisterFunction() - UNTESTED

Registering a function will make it available through ReqFunc,
This commit is contained in:
David Anderson 2005-07-05 22:01:29 +00:00
parent 3f2c117039
commit cc899d298d
5 changed files with 127 additions and 86 deletions

View File

@ -276,6 +276,7 @@ extern ModuleCallReason g_ModuleCallReason; // modules.cpp
extern CModule *g_CurrentlyCalledModule; // modules.cpp extern CModule *g_CurrentlyCalledModule; // modules.cpp
extern const char *g_LastRequestedFunc; // modules.cpp extern const char *g_LastRequestedFunc; // modules.cpp
extern CQueue<String> CurModuleList; extern CQueue<String> CurModuleList;
void Module_CacheFunctions();
void *Module_ReqFnptr(const char *funcName); // modules.cpp void *Module_ReqFnptr(const char *funcName); // modules.cpp
@ -295,5 +296,12 @@ extern int FF_InconsistentFile;
extern int FF_ClientAuthorized; extern int FF_ClientAuthorized;
extern CFakeMeta g_FakeMeta; extern CFakeMeta g_FakeMeta;
struct func_s
{
void *pfn;
const char *desc;
};
#endif // AMXMODX_H #endif // AMXMODX_H

View File

@ -1077,6 +1077,9 @@ C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, m
memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS)); memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS));
gpGamedllFuncs=pGamedllFuncs; gpGamedllFuncs=pGamedllFuncs;
Module_CacheFunctions();
CVAR_REGISTER(&init_amxmodx_version); CVAR_REGISTER(&init_amxmodx_version);
CVAR_REGISTER(&init_amxmodx_modules); CVAR_REGISTER(&init_amxmodx_modules);
CVAR_REGISTER(&init_amxmodx_debug); CVAR_REGISTER(&init_amxmodx_debug);

View File

@ -1316,114 +1316,137 @@ cell MNF_PrepareCharArray(char *ptr, unsigned int size)
return prepareCharArray(ptr, size, false); return prepareCharArray(ptr, size, false);
} }
inline bool operator ==(func_s &arg1, const char *desc)
{
if (strcmp(arg1.desc, desc) == 0)
return true;
return false;
}
CList<func_s, const char *> g_functions;
// Fnptr Request function for the new interface // Fnptr Request function for the new interface
const char *g_LastRequestedFunc = NULL; const char *g_LastRequestedFunc = NULL;
#define REGISTER_FUNC(name, func) { name, (void*)func }, #define REGISTER_FUNC(name, func) \
void *Module_ReqFnptr(const char *funcName) { \
pFunc = new func_s; \
pFunc->pfn = func; \
pFunc->desc = name; \
g_functions.put(pFunc); \
}
void MNF_RegisterFunction(void *pfn, const char *description)
{ {
// func table func_s *pFunc;
struct Func_s
{
const char *name;
void *ptr;
};
static Func_s functions[] = {
// Misc
REGISTER_FUNC("BuildPathname", build_pathname)
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)
// Amx scripts loading / unloading / managing REGISTER_FUNC(description, pfn);
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 void Module_CacheFunctions()
REGISTER_FUNC("SetAmxString", set_amxstring) {
REGISTER_FUNC("GetAmxString", MNF_GetAmxString) func_s *pFunc;
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("BuildPathname", build_pathname)
REGISTER_FUNC("amx_Exec", amx_Exec) REGISTER_FUNC("PrintSrvConsole", print_srvconsole)
REGISTER_FUNC("amx_Execv", amx_Execv) REGISTER_FUNC("GetModname", MNF_GetModname)
REGISTER_FUNC("amx_Allot", amx_Allot) REGISTER_FUNC("Log", MNF_Log)
REGISTER_FUNC("amx_FindPublic", amx_FindPublic) REGISTER_FUNC("LogError", LogError)
REGISTER_FUNC("amx_FindNative", amx_FindNative) REGISTER_FUNC("MergeDefinitionFile", MNF_MergeDefinitionFile)
REGISTER_FUNC("Format", MNF_Format)
REGISTER_FUNC("RegisterFunction", MNF_RegisterFunction);
// Natives / Forwards // Amx scripts loading / unloading / managing
REGISTER_FUNC("AddNatives", MNF_AddNatives) REGISTER_FUNC("GetAmxScript", MNF_GetAmxScript)
REGISTER_FUNC("RaiseAmxError", amx_RaiseError) REGISTER_FUNC("GetAmxScriptName", MNF_GetAmxScriptName)
REGISTER_FUNC("RegisterForward", registerForward) REGISTER_FUNC("FindAmxScriptByName", MNF_FindAmxScriptByName)
REGISTER_FUNC("RegisterSPForward", registerSPForward) REGISTER_FUNC("FindAmxScriptByAmx", MNF_FindAmxScriptByAmx)
REGISTER_FUNC("RegisterSPForwardByName", registerSPForwardByName) REGISTER_FUNC("LoadAmxScript", load_amxscript)
REGISTER_FUNC("UnregisterSPForward", unregisterSPForward) REGISTER_FUNC("UnloadAmxScript", unload_amxscript)
REGISTER_FUNC("ExecuteForward", executeForwards)
REGISTER_FUNC("PrepareCellArray", MNF_PrepareCellArray)
REGISTER_FUNC("PrepareCharArray", MNF_PrepareCharArray)
REGISTER_FUNC("PrepareCellArrayA", prepareCellArray)
REGISTER_FUNC("PrepareCharArrayA", prepareCharArray)
// Player // String / mem in amx scripts support
REGISTER_FUNC("GetPlayerFlags", MNF_GetPlayerFlags) REGISTER_FUNC("SetAmxString", set_amxstring)
REGISTER_FUNC("IsPlayerValid", MNF_IsPlayerValid) REGISTER_FUNC("GetAmxString", MNF_GetAmxString)
REGISTER_FUNC("GetPlayerName", MNF_GetPlayerName) REGISTER_FUNC("GetAmxStringLen", MNF_GetAmxStringLen)
REGISTER_FUNC("GetPlayerIP", MNF_GetPlayerIP) REGISTER_FUNC("FormatAmxString", MNF_FormatAmxString)
REGISTER_FUNC("IsPlayerInGame", MNF_IsPlayerInGame) REGISTER_FUNC("CopyAmxMemory", MNF_CopyAmxMemory)
REGISTER_FUNC("IsPlayerBot", MNF_IsPlayerBot) REGISTER_FUNC("GetAmxAddr", get_amxaddr)
REGISTER_FUNC("IsPlayerAuthorized", MNF_IsPlayerAuthorized)
REGISTER_FUNC("GetPlayerTime", MNF_GetPlayerTime) // other amx stuff
REGISTER_FUNC("GetPlayerPlayTime", MNF_GetPlayerPlayTime) REGISTER_FUNC("amx_Exec", amx_Exec)
REGISTER_FUNC("GetPlayerCurweapon", MNF_GetPlayerCurweapon) REGISTER_FUNC("amx_Execv", amx_Execv)
REGISTER_FUNC("GetPlayerTeamID", MNF_GetPlayerTeamID) REGISTER_FUNC("amx_Allot", amx_Allot)
REGISTER_FUNC("GetPlayerTeam", MNF_GetPlayerTeam) REGISTER_FUNC("amx_FindPublic", amx_FindPublic)
REGISTER_FUNC("GetPlayerDeaths", MNF_GetPlayerDeaths) REGISTER_FUNC("amx_FindNative", amx_FindNative)
REGISTER_FUNC("GetPlayerFrags", MNF_GetPlayerFrags)
REGISTER_FUNC("GetPlayerMenu", MNF_GetPlayerMenu) // Natives / Forwards
REGISTER_FUNC("GetPlayerKeys", MNF_GetPlayerKeys) REGISTER_FUNC("AddNatives", MNF_AddNatives)
REGISTER_FUNC("IsPlayerAlive", MNF_IsPlayerAlive) REGISTER_FUNC("RaiseAmxError", amx_RaiseError)
REGISTER_FUNC("IsPlayerConnecting", MNF_IsPlayerConnecting) REGISTER_FUNC("RegisterForward", registerForward)
REGISTER_FUNC("IsPlayerHLTV", MNF_IsPlayerHLTV) REGISTER_FUNC("RegisterSPForward", registerSPForward)
REGISTER_FUNC("GetPlayerArmor", MNF_GetPlayerArmor) REGISTER_FUNC("RegisterSPForwardByName", registerSPForwardByName)
REGISTER_FUNC("GetPlayerHealth", MNF_GetPlayerHealth) REGISTER_FUNC("UnregisterSPForward", unregisterSPForward)
REGISTER_FUNC("GetPlayerEdict", MNF_GetPlayerEdict) REGISTER_FUNC("ExecuteForward", executeForwards)
REGISTER_FUNC("CellToReal", MNF_CellToReal) REGISTER_FUNC("PrepareCellArray", MNF_PrepareCellArray)
REGISTER_FUNC("RealToCell", MNF_RealToCell) 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 #ifdef MEMORY_TEST
REGISTER_FUNC("Allocator", m_allocator) REGISTER_FUNC("Allocator", m_allocator)
REGISTER_FUNC("Deallocator", m_deallocator) REGISTER_FUNC("Deallocator", m_deallocator)
REGISTER_FUNC("Reallocator", m_reallocator) REGISTER_FUNC("Reallocator", m_reallocator)
#else #else
REGISTER_FUNC("Allocator", MNF_Allocator) REGISTER_FUNC("Allocator", MNF_Allocator)
REGISTER_FUNC("Deallocator", MNF_Deallocator) REGISTER_FUNC("Deallocator", MNF_Deallocator)
REGISTER_FUNC("Reallocator", MNF_Reallocator) REGISTER_FUNC("Reallocator", MNF_Reallocator)
#endif // MEMORY_TEST #endif // MEMORY_TEST
REGISTER_FUNC("Haha_HiddenStuff", MNF_HiddenStuff) REGISTER_FUNC("Haha_HiddenStuff", MNF_HiddenStuff)
}; }
void *Module_ReqFnptr(const char *funcName)
{
// code // code
if (!g_CurrentlyCalledModule || g_ModuleCallReason != ModuleCall_Attach) // ^---- really? wow!
if (!g_CurrentlyCalledModule)
{ {
return NULL; return NULL;
} }
g_LastRequestedFunc = funcName; g_LastRequestedFunc = funcName;
for (unsigned int i = 0; i < (sizeof(functions) / sizeof(Func_s)); ++i)
CList<func_s, const char *>::iterator iter;
for (iter = g_functions.begin(); iter; ++iter)
{ {
if (strcmp(funcName, functions[i].name) == 0) if (strcmp(funcName, iter->desc) == 0)
return functions[i].ptr; return iter->pfn;
} }
return NULL; return NULL;
} }

View File

@ -2501,6 +2501,7 @@ PFN_AMX_FINDNATIVE g_fn_AmxFindNative;
PFN_GETPLAYERFLAGS g_fn_GetPlayerFlags; PFN_GETPLAYERFLAGS g_fn_GetPlayerFlags;
PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict; PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict;
PFN_FORMAT g_fn_Format; PFN_FORMAT g_fn_Format;
PFN_REGISTERFUNCTION g_fn_RegisterFunction;
// *** Exports *** // *** Exports ***
C_DLLEXPORT int AMXX_Query(int *interfaceVersion, amxx_module_info_s *moduleInfo) C_DLLEXPORT int AMXX_Query(int *interfaceVersion, amxx_module_info_s *moduleInfo)
@ -2547,6 +2548,7 @@ C_DLLEXPORT int AMXX_Attach(PFN_REQ_FNPTR reqFnptrFunc)
REQFUNC("LogError", g_fn_LogErrorFunc, PFN_LOG_ERROR); REQFUNC("LogError", g_fn_LogErrorFunc, PFN_LOG_ERROR);
REQFUNC("MergeDefinitionFile", g_fn_MergeDefinition_File, PFN_MERGEDEFINITION_FILE); REQFUNC("MergeDefinitionFile", g_fn_MergeDefinition_File, PFN_MERGEDEFINITION_FILE);
REQFUNC("Format", g_fn_Format, PFN_FORMAT); REQFUNC("Format", g_fn_Format, PFN_FORMAT);
REQFUNC("RegisterFunction", g_fn_RegisterFunction, PFN_REGISTERFUNCTION);
// Amx scripts // Amx scripts
REQFUNC("GetAmxScript", g_fn_GetAmxScript, PFN_GET_AMXSCRIPT); REQFUNC("GetAmxScript", g_fn_GetAmxScript, PFN_GET_AMXSCRIPT);
@ -2723,6 +2725,7 @@ void ValidateMacros_DontCallThis_Smiley()
MF_GetPlayerFrags(0); MF_GetPlayerFrags(0);
MF_GetPlayerEdict(0); MF_GetPlayerEdict(0);
MF_Format("", 4, "str"); MF_Format("", 4, "str");
MF_RegisterFunction(NULL, "");
} }
#endif #endif

View File

@ -1974,6 +1974,7 @@ typedef int (*PFN_REGISTER_SPFORWARD_BYNAME) (AMX * /*amx*/, const char * /*f
typedef void (*PFN_UNREGISTER_SPFORWARD) (int /*id*/); typedef void (*PFN_UNREGISTER_SPFORWARD) (int /*id*/);
typedef void (*PFN_MERGEDEFINITION_FILE) (const char * /*filename*/); typedef void (*PFN_MERGEDEFINITION_FILE) (const char * /*filename*/);
typedef const char * (*PFN_FORMAT) (const char * /*fmt*/, ... /*params*/); typedef const char * (*PFN_FORMAT) (const char * /*fmt*/, ... /*params*/);
typedef void (*PFN_REGISTERFUNCTION) (void * /*pfn*/, const char * /*desc*/);
extern PFN_ADD_NATIVES g_fn_AddNatives; extern PFN_ADD_NATIVES g_fn_AddNatives;
extern PFN_BUILD_PATHNAME g_fn_BuildPathname; extern PFN_BUILD_PATHNAME g_fn_BuildPathname;
@ -2034,6 +2035,7 @@ extern PFN_GETPLAYERFLAGS g_fn_GetPlayerFlags;
extern PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict; extern PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict;
extern PFN_FORMAT g_fn_Format; extern PFN_FORMAT g_fn_Format;
extern PFN_GET_PLAYER_TEAM g_fn_GetPlayerTeam; extern PFN_GET_PLAYER_TEAM g_fn_GetPlayerTeam;
extern PFN_REGISTERFUNCTION g_fn_RegisterFunction;
#ifdef MAY_NEVER_BE_DEFINED #ifdef MAY_NEVER_BE_DEFINED
// Function prototypes for intellisense and similar systems // Function prototypes for intellisense and similar systems
@ -2089,6 +2091,7 @@ void MF_UnregisterSPForward (int id) { }
int MF_GetPlayerFlags (int id) { } int MF_GetPlayerFlags (int id) { }
edict_t* MF_GetPlayerEdict (int id) { } edict_t* MF_GetPlayerEdict (int id) { }
const char * MF_Format (const char *fmt, ...) { } const char * MF_Format (const char *fmt, ...) { }
void MF_RegisterFunction (void *pfn, const char *description) { }
#endif // MAY_NEVER_BE_DEFINED #endif // MAY_NEVER_BE_DEFINED
#define MF_AddNatives g_fn_AddNatives #define MF_AddNatives g_fn_AddNatives
@ -2150,6 +2153,7 @@ void MF_LogError(AMX *amx, int err, const char *fmt, ...);
#define MF_GetPlayerFlags g_fn_GetPlayerFlags #define MF_GetPlayerFlags g_fn_GetPlayerFlags
#define MF_GetPlayerEdict g_fn_GetPlayerEdict #define MF_GetPlayerEdict g_fn_GetPlayerEdict
#define MF_Format g_fn_Format #define MF_Format g_fn_Format
#define MF_RegisterFunction g_fn_RegisterFunction
/*** Memory ***/ /*** Memory ***/
void *operator new(size_t reportedSize); void *operator new(size_t reportedSize);