core/smn_console.cpp
changeset 2806 686c283b08fc
parent 2800 74c32429908e
child 2847 1daf209d9dd2
     1.1 --- a/core/smn_console.cpp	Thu Sep 17 13:08:18 2009 -0400
     1.2 +++ b/core/smn_console.cpp	Sat Sep 26 17:12:23 2009 -0400
     1.3 @@ -43,6 +43,8 @@
     1.4  #include <inetchannel.h>
     1.5  #include <bitbuf.h>
     1.6  #include <sm_trie_tpl.h>
     1.7 +#include "Logger.h"
     1.8 +#include "ConsoleDetours.h"
     1.9  
    1.10  #if SOURCE_ENGINE == SE_LEFT4DEAD
    1.11  #define NET_SETCONVAR	6
    1.12 @@ -668,7 +670,7 @@
    1.13  
    1.14  	if (strcasecmp(name, "sm") == 0)
    1.15  	{
    1.16 -		return pContext->ThrowNativeError("Cannot override \"sm\" command");
    1.17 +		return pContext->ThrowNativeError("Cannot register \"sm\" command");
    1.18  	}
    1.19  
    1.20  	pContext->LocalToString(params[3], &help);
    1.21 @@ -696,7 +698,7 @@
    1.22  
    1.23  	if (strcasecmp(name, "sm") == 0)
    1.24  	{
    1.25 -		return pContext->ThrowNativeError("Cannot override \"sm\" command");
    1.26 +		return pContext->ThrowNativeError("Cannot register \"sm\" command");
    1.27  	}
    1.28  
    1.29  	pContext->LocalToString(params[3], &help);
    1.30 @@ -727,7 +729,7 @@
    1.31  
    1.32  	if (strcasecmp(name, "sm") == 0)
    1.33  	{
    1.34 -		return pContext->ThrowNativeError("Cannot override \"sm\" command");
    1.35 +		return pContext->ThrowNativeError("Cannot register \"sm\" command");
    1.36  	}
    1.37  
    1.38  	pContext->LocalToString(params[4], &help);
    1.39 @@ -1332,6 +1334,46 @@
    1.40  	return 0;
    1.41  }
    1.42  
    1.43 +static cell_t AddCommandListener(IPluginContext *pContext, const cell_t *params)
    1.44 +{
    1.45 +	char *name;
    1.46 +	IPluginFunction *pFunction;
    1.47 +
    1.48 +	pContext->LocalToString(params[2], &name);
    1.49 +
    1.50 +	if (strcasecmp(name, "sm") == 0)
    1.51 +	{
    1.52 +		g_Logger.LogError("Request to register \"sm\" command denied.");
    1.53 +		return 0;
    1.54 +	}
    1.55 +
    1.56 +	pFunction = pContext->GetFunctionById(params[1]);
    1.57 +
    1.58 +	if (!pFunction)
    1.59 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
    1.60 +
    1.61 +	if (!g_ConsoleDetours.AddListener(pFunction, name[0] == '\0' ? NULL : name))
    1.62 +		return pContext->ThrowNativeError("This game does not support command listeners");
    1.63 +
    1.64 +	return 1;
    1.65 +}
    1.66 +
    1.67 +static cell_t RemoveCommandListener(IPluginContext *pContext, const cell_t *params)
    1.68 +{
    1.69 +	char *name;
    1.70 +	IPluginFunction *pFunction;
    1.71 +
    1.72 +	pContext->LocalToString(params[2], &name);
    1.73 +	pFunction = pContext->GetFunctionById(params[1]);
    1.74 +	if (!pFunction)
    1.75 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
    1.76 +
    1.77 +	if (!g_ConsoleDetours.RemoveListener(pFunction, name[0] == '\0' ? NULL : name))
    1.78 +		return pContext->ThrowNativeError("No matching callback was registered");
    1.79 +	
    1.80 +	return 1;
    1.81 +}
    1.82 +
    1.83  REGISTER_NATIVES(consoleNatives)
    1.84  {
    1.85  	{"CreateConVar",		sm_CreateConVar},
    1.86 @@ -1381,5 +1423,7 @@
    1.87  	{"SendConVarValue",		SendConVarValue},
    1.88  	{"AddServerTag",		AddServerTag},
    1.89  	{"RemoveServerTag",		RemoveServerTag},
    1.90 +	{"AddCommandListener",	AddCommandListener},
    1.91 +	{"RemoveCommandListener", RemoveCommandListener},
    1.92  	{NULL,					NULL}
    1.93  };