mirror of https://github.com/OpenTTD/OpenTTD
(svn r19514) -Codechange: Allow console hooks to deny existance of commands.
parent
7b4dd765d3
commit
9a00f6961f
|
@ -461,12 +461,17 @@ void IConsoleCmdExec(const char *cmdstr)
|
||||||
*/
|
*/
|
||||||
cmd = IConsoleCmdGet(tokens[0]);
|
cmd = IConsoleCmdGet(tokens[0]);
|
||||||
if (cmd != NULL) {
|
if (cmd != NULL) {
|
||||||
if (cmd->hook == NULL || cmd->hook()) {
|
ConsoleHookResult chr = (cmd->hook == NULL ? CHR_ALLOW : cmd->hook(true));
|
||||||
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
switch (chr) {
|
||||||
cmd->proc(0, NULL); // if command failed, give help
|
case CHR_ALLOW:
|
||||||
}
|
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
||||||
|
cmd->proc(0, NULL); // if command failed, give help
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case CHR_DISALLOW: return;
|
||||||
|
case CHR_HIDE: break;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t_index--;
|
t_index--;
|
||||||
|
|
|
@ -46,7 +46,7 @@ static bool _script_running;
|
||||||
|
|
||||||
/* console command defines */
|
/* console command defines */
|
||||||
#define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[])
|
#define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[])
|
||||||
#define DEF_CONSOLE_HOOK(function) static bool function()
|
#define DEF_CONSOLE_HOOK(function) static ConsoleHookResult function(bool echo)
|
||||||
|
|
||||||
|
|
||||||
/****************
|
/****************
|
||||||
|
@ -55,10 +55,10 @@ static bool _script_running;
|
||||||
|
|
||||||
#ifdef ENABLE_NETWORK
|
#ifdef ENABLE_NETWORK
|
||||||
|
|
||||||
static inline bool NetworkAvailable()
|
static inline bool NetworkAvailable(bool echo)
|
||||||
{
|
{
|
||||||
if (!_network_available) {
|
if (!_network_available) {
|
||||||
IConsoleError("You cannot use this command because there is no network available.");
|
if (echo) IConsoleError("You cannot use this command because there is no network available.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -66,44 +66,44 @@ static inline bool NetworkAvailable()
|
||||||
|
|
||||||
DEF_CONSOLE_HOOK(ConHookServerOnly)
|
DEF_CONSOLE_HOOK(ConHookServerOnly)
|
||||||
{
|
{
|
||||||
if (!NetworkAvailable()) return false;
|
if (!NetworkAvailable(echo)) return CHR_DISALLOW;
|
||||||
|
|
||||||
if (!_network_server) {
|
if (!_network_server) {
|
||||||
IConsoleError("This command is only available to a network server.");
|
if (echo) IConsoleError("This command is only available to a network server.");
|
||||||
return false;
|
return CHR_DISALLOW;
|
||||||
}
|
}
|
||||||
return true;
|
return CHR_ALLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEF_CONSOLE_HOOK(ConHookClientOnly)
|
DEF_CONSOLE_HOOK(ConHookClientOnly)
|
||||||
{
|
{
|
||||||
if (!NetworkAvailable()) return false;
|
if (!NetworkAvailable(echo)) return CHR_DISALLOW;
|
||||||
|
|
||||||
if (_network_server) {
|
if (_network_server) {
|
||||||
IConsoleError("This command is not available to a network server.");
|
if (echo) IConsoleError("This command is not available to a network server.");
|
||||||
return false;
|
return CHR_DISALLOW;
|
||||||
}
|
}
|
||||||
return true;
|
return CHR_ALLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEF_CONSOLE_HOOK(ConHookNeedNetwork)
|
DEF_CONSOLE_HOOK(ConHookNeedNetwork)
|
||||||
{
|
{
|
||||||
if (!NetworkAvailable()) return false;
|
if (!NetworkAvailable(echo)) return CHR_DISALLOW;
|
||||||
|
|
||||||
if (!_networking) {
|
if (!_networking) {
|
||||||
IConsoleError("Not connected. This command is only available in multiplayer.");
|
if (echo) IConsoleError("Not connected. This command is only available in multiplayer.");
|
||||||
return false;
|
return CHR_DISALLOW;
|
||||||
}
|
}
|
||||||
return true;
|
return CHR_ALLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEF_CONSOLE_HOOK(ConHookNoNetwork)
|
DEF_CONSOLE_HOOK(ConHookNoNetwork)
|
||||||
{
|
{
|
||||||
if (_networking) {
|
if (_networking) {
|
||||||
IConsoleError("This command is forbidden in multiplayer.");
|
if (echo) IConsoleError("This command is forbidden in multiplayer.");
|
||||||
return false;
|
return CHR_DISALLOW;
|
||||||
}
|
}
|
||||||
return true;
|
return CHR_ALLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -1365,7 +1365,7 @@ DEF_CONSOLE_CMD(ConListCommands)
|
||||||
|
|
||||||
for (cmd = _iconsole_cmds; cmd != NULL; cmd = cmd->next) {
|
for (cmd = _iconsole_cmds; cmd != NULL; cmd = cmd->next) {
|
||||||
if (argv[1] == NULL || strstr(cmd->name, argv[1]) != NULL) {
|
if (argv[1] == NULL || strstr(cmd->name, argv[1]) != NULL) {
|
||||||
IConsolePrintF(CC_DEFAULT, "%s", cmd->name);
|
if (cmd->hook == NULL || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,13 @@ enum {
|
||||||
ICON_MAX_STREAMSIZE = 2048, ///< maximum length of a totally expanded command
|
ICON_MAX_STREAMSIZE = 2048, ///< maximum length of a totally expanded command
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Return values of console hooks (#IConsoleHook). */
|
||||||
|
enum ConsoleHookResult {
|
||||||
|
CHR_ALLOW, ///< Allow command execution.
|
||||||
|
CHR_DISALLOW, ///< Disallow command execution.
|
||||||
|
CHR_HIDE, ///< Hide the existance of the command.
|
||||||
|
};
|
||||||
|
|
||||||
/** --Commands--
|
/** --Commands--
|
||||||
* Commands are commands, or functions. They get executed once and any
|
* Commands are commands, or functions. They get executed once and any
|
||||||
* effect they produce are carried out. The arguments to the commands
|
* effect they produce are carried out. The arguments to the commands
|
||||||
|
@ -27,7 +34,7 @@ enum {
|
||||||
* eg. 'say "hello sexy boy"'
|
* eg. 'say "hello sexy boy"'
|
||||||
*/
|
*/
|
||||||
typedef bool IConsoleCmdProc(byte argc, char *argv[]);
|
typedef bool IConsoleCmdProc(byte argc, char *argv[]);
|
||||||
typedef bool IConsoleHook();
|
typedef ConsoleHookResult IConsoleHook(bool echo);
|
||||||
struct IConsoleCmd {
|
struct IConsoleCmd {
|
||||||
char *name; ///< name of command
|
char *name; ///< name of command
|
||||||
IConsoleCmd *next; ///< next command in list
|
IConsoleCmd *next; ///< next command in list
|
||||||
|
|
Loading…
Reference in New Issue