diff -r 0a6c27d24411 -r 686c283b08fc core/smn_console.cpp --- a/core/smn_console.cpp Thu Sep 17 13:08:18 2009 -0400 +++ b/core/smn_console.cpp Sat Sep 26 17:12:23 2009 -0400 @@ -43,6 +43,8 @@ #include #include #include +#include "Logger.h" +#include "ConsoleDetours.h" #if SOURCE_ENGINE == SE_LEFT4DEAD #define NET_SETCONVAR 6 @@ -668,7 +670,7 @@ if (strcasecmp(name, "sm") == 0) { - return pContext->ThrowNativeError("Cannot override \"sm\" command"); + return pContext->ThrowNativeError("Cannot register \"sm\" command"); } pContext->LocalToString(params[3], &help); @@ -696,7 +698,7 @@ if (strcasecmp(name, "sm") == 0) { - return pContext->ThrowNativeError("Cannot override \"sm\" command"); + return pContext->ThrowNativeError("Cannot register \"sm\" command"); } pContext->LocalToString(params[3], &help); @@ -727,7 +729,7 @@ if (strcasecmp(name, "sm") == 0) { - return pContext->ThrowNativeError("Cannot override \"sm\" command"); + return pContext->ThrowNativeError("Cannot register \"sm\" command"); } pContext->LocalToString(params[4], &help); @@ -1332,6 +1334,46 @@ return 0; } +static cell_t AddCommandListener(IPluginContext *pContext, const cell_t *params) +{ + char *name; + IPluginFunction *pFunction; + + pContext->LocalToString(params[2], &name); + + if (strcasecmp(name, "sm") == 0) + { + g_Logger.LogError("Request to register \"sm\" command denied."); + return 0; + } + + pFunction = pContext->GetFunctionById(params[1]); + + if (!pFunction) + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]); + + if (!g_ConsoleDetours.AddListener(pFunction, name[0] == '\0' ? NULL : name)) + return pContext->ThrowNativeError("This game does not support command listeners"); + + return 1; +} + +static cell_t RemoveCommandListener(IPluginContext *pContext, const cell_t *params) +{ + char *name; + IPluginFunction *pFunction; + + pContext->LocalToString(params[2], &name); + pFunction = pContext->GetFunctionById(params[1]); + if (!pFunction) + return pContext->ThrowNativeError("Invalid function id (%X)", params[1]); + + if (!g_ConsoleDetours.RemoveListener(pFunction, name[0] == '\0' ? NULL : name)) + return pContext->ThrowNativeError("No matching callback was registered"); + + return 1; +} + REGISTER_NATIVES(consoleNatives) { {"CreateConVar", sm_CreateConVar}, @@ -1381,5 +1423,7 @@ {"SendConVarValue", SendConVarValue}, {"AddServerTag", AddServerTag}, {"RemoveServerTag", RemoveServerTag}, + {"AddCommandListener", AddCommandListener}, + {"RemoveCommandListener", RemoveCommandListener}, {NULL, NULL} };