From cc899d298dee01d1f148b6420e7e9174604d1a6f Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 5 Jul 2005 22:01:29 +0000 Subject: [PATCH] Modules can now intercommunicate through RegisterFunction() - UNTESTED Registering a function will make it available through ReqFunc, --- amxmodx/amxmodx.h | 8 ++ amxmodx/meta_api.cpp | 3 + amxmodx/modules.cpp | 195 +++++++++++++++++++++---------------- amxmodx/sdk/amxxmodule.cpp | 3 + amxmodx/sdk/amxxmodule.h | 4 + 5 files changed, 127 insertions(+), 86 deletions(-) diff --git a/amxmodx/amxmodx.h b/amxmodx/amxmodx.h index 614b4cdd..da178383 100755 --- a/amxmodx/amxmodx.h +++ b/amxmodx/amxmodx.h @@ -276,6 +276,7 @@ extern ModuleCallReason g_ModuleCallReason; // modules.cpp extern CModule *g_CurrentlyCalledModule; // modules.cpp extern const char *g_LastRequestedFunc; // modules.cpp extern CQueue CurModuleList; +void Module_CacheFunctions(); void *Module_ReqFnptr(const char *funcName); // modules.cpp @@ -295,5 +296,12 @@ extern int FF_InconsistentFile; extern int FF_ClientAuthorized; extern CFakeMeta g_FakeMeta; + +struct func_s +{ + void *pfn; + const char *desc; +}; + #endif // AMXMODX_H diff --git a/amxmodx/meta_api.cpp b/amxmodx/meta_api.cpp index 9b179a95..74a359aa 100755 --- a/amxmodx/meta_api.cpp +++ b/amxmodx/meta_api.cpp @@ -1077,6 +1077,9 @@ C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, m memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS)); gpGamedllFuncs=pGamedllFuncs; + + Module_CacheFunctions(); + CVAR_REGISTER(&init_amxmodx_version); CVAR_REGISTER(&init_amxmodx_modules); CVAR_REGISTER(&init_amxmodx_debug); diff --git a/amxmodx/modules.cpp b/amxmodx/modules.cpp index c2318a56..530125b1 100755 --- a/amxmodx/modules.cpp +++ b/amxmodx/modules.cpp @@ -1316,114 +1316,137 @@ 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; + return false; +} + +CList g_functions; // Fnptr Request function for the new interface const char *g_LastRequestedFunc = NULL; -#define REGISTER_FUNC(name, func) { name, (void*)func }, -void *Module_ReqFnptr(const char *funcName) +#define REGISTER_FUNC(name, func) \ + { \ + pFunc = new func_s; \ + pFunc->pfn = func; \ + pFunc->desc = name; \ + g_functions.put(pFunc); \ + } + +void MNF_RegisterFunction(void *pfn, const char *description) { - // func table - 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) + func_s *pFunc; - // 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) + REGISTER_FUNC(description, pfn); +} - // 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) +void Module_CacheFunctions() +{ + func_s *pFunc; - // other amx stuff - REGISTER_FUNC("amx_Exec", amx_Exec) - REGISTER_FUNC("amx_Execv", amx_Execv) - REGISTER_FUNC("amx_Allot", amx_Allot) - REGISTER_FUNC("amx_FindPublic", amx_FindPublic) - REGISTER_FUNC("amx_FindNative", amx_FindNative) + 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) + REGISTER_FUNC("RegisterFunction", MNF_RegisterFunction); - // 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) + // 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) - // 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) + // 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) + REGISTER_FUNC("amx_Execv", amx_Execv) + 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) + 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) + REGISTER_FUNC("Allocator", MNF_Allocator) + REGISTER_FUNC("Deallocator", MNF_Deallocator) + REGISTER_FUNC("Reallocator", MNF_Reallocator) #endif // MEMORY_TEST - REGISTER_FUNC("Haha_HiddenStuff", MNF_HiddenStuff) - }; + REGISTER_FUNC("Haha_HiddenStuff", MNF_HiddenStuff) +} +void *Module_ReqFnptr(const char *funcName) +{ // code - if (!g_CurrentlyCalledModule || g_ModuleCallReason != ModuleCall_Attach) + // ^---- really? wow! + if (!g_CurrentlyCalledModule) { return NULL; } g_LastRequestedFunc = funcName; - for (unsigned int i = 0; i < (sizeof(functions) / sizeof(Func_s)); ++i) + + CList::iterator iter; + for (iter = g_functions.begin(); iter; ++iter) { - if (strcmp(funcName, functions[i].name) == 0) - return functions[i].ptr; + if (strcmp(funcName, iter->desc) == 0) + return iter->pfn; } + return NULL; } diff --git a/amxmodx/sdk/amxxmodule.cpp b/amxmodx/sdk/amxxmodule.cpp index 5bd6e75a..d5d49b62 100755 --- a/amxmodx/sdk/amxxmodule.cpp +++ b/amxmodx/sdk/amxxmodule.cpp @@ -2501,6 +2501,7 @@ PFN_AMX_FINDNATIVE g_fn_AmxFindNative; PFN_GETPLAYERFLAGS g_fn_GetPlayerFlags; PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict; PFN_FORMAT g_fn_Format; +PFN_REGISTERFUNCTION g_fn_RegisterFunction; // *** Exports *** 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("MergeDefinitionFile", g_fn_MergeDefinition_File, PFN_MERGEDEFINITION_FILE); REQFUNC("Format", g_fn_Format, PFN_FORMAT); + REQFUNC("RegisterFunction", g_fn_RegisterFunction, PFN_REGISTERFUNCTION); // Amx scripts REQFUNC("GetAmxScript", g_fn_GetAmxScript, PFN_GET_AMXSCRIPT); @@ -2723,6 +2725,7 @@ void ValidateMacros_DontCallThis_Smiley() MF_GetPlayerFrags(0); MF_GetPlayerEdict(0); MF_Format("", 4, "str"); + MF_RegisterFunction(NULL, ""); } #endif diff --git a/amxmodx/sdk/amxxmodule.h b/amxmodx/sdk/amxxmodule.h index e0b0e8fa..090df9ff 100755 --- a/amxmodx/sdk/amxxmodule.h +++ b/amxmodx/sdk/amxxmodule.h @@ -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_MERGEDEFINITION_FILE) (const char * /*filename*/); 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_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_FORMAT g_fn_Format; extern PFN_GET_PLAYER_TEAM g_fn_GetPlayerTeam; +extern PFN_REGISTERFUNCTION g_fn_RegisterFunction; #ifdef MAY_NEVER_BE_DEFINED // Function prototypes for intellisense and similar systems @@ -2089,6 +2091,7 @@ void MF_UnregisterSPForward (int id) { } int MF_GetPlayerFlags (int id) { } edict_t* MF_GetPlayerEdict (int id) { } const char * MF_Format (const char *fmt, ...) { } +void MF_RegisterFunction (void *pfn, const char *description) { } #endif // MAY_NEVER_BE_DEFINED #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_GetPlayerEdict g_fn_GetPlayerEdict #define MF_Format g_fn_Format +#define MF_RegisterFunction g_fn_RegisterFunction /*** Memory ***/ void *operator new(size_t reportedSize);