From 4ed2d6e51576dd5e770be1e28964f88fc2677a15 Mon Sep 17 00:00:00 2001 From: Scott Ehlert Date: Wed, 16 May 2007 04:09:31 +0000 Subject: [PATCH] Implemented amb296 - Added HookEventEx() which does not generate a runtime error if the event does not exist and returns false instead. --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40807 --- core/smn_events.cpp | 21 +++++++++++++++++++++ plugins/include/events.inc | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/core/smn_events.cpp b/core/smn_events.cpp index 679c4efe..2948dda6 100644 --- a/core/smn_events.cpp +++ b/core/smn_events.cpp @@ -38,6 +38,26 @@ static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params) return 1; } +static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params) +{ + char *name; + IPluginFunction *pFunction; + + pContext->LocalToString(params[1], &name); + + if (!pFunction) + { + return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); + } + + if (g_EventManager.HookEvent(name, pFunction, static_cast(params[3])) == EventHookErr_InvalidEvent) + { + return 0; + } + + return 1; +} + static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params) { char *name; @@ -313,6 +333,7 @@ static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params) REGISTER_NATIVES(gameEventNatives) { {"HookEvent", sm_HookEvent}, + {"HookEventEx", sm_HookEventEx}, {"UnhookEvent", sm_UnhookEvent}, {"CreateEvent", sm_CreateEvent}, {"FireEvent", sm_FireEvent}, diff --git a/plugins/include/events.inc b/plugins/include/events.inc index d1fd81d9..ede8c3e4 100644 --- a/plugins/include/events.inc +++ b/plugins/include/events.inc @@ -66,6 +66,17 @@ funcenum EventHook */ native HookEvent(const String:name[], EventHook:callback, EventHookMode:mode=EventHookMode_Post); +/** + * Creates a hook for when a game event is fired. + * + * @param name Name of event. + * @param callback An EventHook function pointer. + * @param mode Optional EventHookMode determining the type of hook. + * @return True if event exists and was hooked successfully, false otherwise. + * @error Invalid callback function. + */ +native bool:HookEventEx(const String:name[], EventHook:callback, EventHookMode:mode=EventHookMode_Post); + /** * Removes a hook for when a game event is fired. *