mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
redone the console parsing to be more userfriendly
This commit is contained in:
parent
9e84f8d01f
commit
41b8f4f165
|
@ -204,7 +204,7 @@ function build(settings)
|
|||
if family == "windows" then
|
||||
settings.cc.flags = "/wd4244"
|
||||
else
|
||||
settings.cc.flags = "-Wall -fstack-protector -fstack-protector-all"
|
||||
settings.cc.flags = "-Wall -fstack-protector -fstack-protector-all -fno-exceptions"
|
||||
settings.linker.flags = ""
|
||||
end
|
||||
|
||||
|
|
|
@ -1412,9 +1412,7 @@ static void client_run()
|
|||
|
||||
static void con_connect(void *result, void *user_data)
|
||||
{
|
||||
const char *address;
|
||||
console_result_string(result, 1, &address);
|
||||
client_connect(address);
|
||||
client_connect(console_arg_string(result, 0));
|
||||
}
|
||||
|
||||
static void con_disconnect(void *result, void *user_data)
|
||||
|
@ -1454,7 +1452,9 @@ void client_save_line(const char *line)
|
|||
engine_config_write_line(line);
|
||||
}
|
||||
|
||||
int editor_main(int argc, char **argv);
|
||||
/*int editor_main(int argc, char **argv);*/
|
||||
|
||||
/*extern void test_parser();*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
@ -1462,6 +1462,9 @@ int main(int argc, char **argv)
|
|||
dbg_msg("client", "starting...");
|
||||
engine_init("Teewars");
|
||||
|
||||
/* test_parser();
|
||||
return 0;*/
|
||||
|
||||
/* register all console commands */
|
||||
client_register_commands();
|
||||
modc_console_init();
|
||||
|
|
|
@ -87,7 +87,7 @@ void config_save()
|
|||
char linebuf[512];
|
||||
|
||||
#define MACRO_CONFIG_INT(name,def,min,max) { str_format(linebuf, sizeof(linebuf), "%s %i", #name, config.name); engine_config_write_line(linebuf); }
|
||||
#define MACRO_CONFIG_STR(name,len,def) { str_format(linebuf, sizeof(linebuf), "%s \"%s\"", #name, config.name); engine_config_write_line(linebuf); }
|
||||
#define MACRO_CONFIG_STR(name,len,def) { str_format(linebuf, sizeof(linebuf), "%s %s", #name, config.name); engine_config_write_line(linebuf); }
|
||||
|
||||
#include "e_config_variables.h"
|
||||
|
||||
|
|
|
@ -9,265 +9,137 @@
|
|||
|
||||
#define CONSOLE_MAX_STR_LENGTH 255
|
||||
/* the maximum number of tokens occurs in a string of length CONSOLE_MAX_STR_LENGTH with tokens size 1 separated by single spaces */
|
||||
#define MAX_TOKENS (CONSOLE_MAX_STR_LENGTH+1)/2
|
||||
|
||||
enum
|
||||
{
|
||||
TOKEN_INT,
|
||||
TOKEN_FLOAT,
|
||||
TOKEN_STRING
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int type;
|
||||
const char *stored_string;
|
||||
} TOKEN;
|
||||
#define MAX_PARTS (CONSOLE_MAX_STR_LENGTH+1)/2
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char string_storage[CONSOLE_MAX_STR_LENGTH+1];
|
||||
char *next_string;
|
||||
char *args_start;
|
||||
|
||||
TOKEN tokens[MAX_TOKENS];
|
||||
unsigned int num_tokens;
|
||||
} LEXER_RESULT;
|
||||
const char *command;
|
||||
const char *args[MAX_PARTS];
|
||||
unsigned int num_args;
|
||||
} PARSE_RESULT;
|
||||
|
||||
enum
|
||||
static char *str_skipblanks(char *str)
|
||||
{
|
||||
STATE_START,
|
||||
STATE_INT,
|
||||
STATE_FLOAT,
|
||||
STATE_POT_FLOAT,
|
||||
STATE_POT_NEGATIVE,
|
||||
STATE_STRING,
|
||||
STATE_QUOTED,
|
||||
STATE_ESCAPE
|
||||
};
|
||||
|
||||
static const char *store_string(LEXER_RESULT *res, const char *str, int len)
|
||||
{
|
||||
const char *ptr = res->next_string;
|
||||
int escaped = 0;
|
||||
|
||||
while (len)
|
||||
{
|
||||
if (!escaped && *str == '\\')
|
||||
{
|
||||
escaped = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
escaped = 0;
|
||||
|
||||
*res->next_string++ = *str;
|
||||
}
|
||||
|
||||
while(*str && (*str == ' ' || *str == '\t' || *str == '\n'))
|
||||
str++;
|
||||
len--;
|
||||
}
|
||||
|
||||
*res->next_string++ = 0;
|
||||
|
||||
/*
|
||||
memcpy(res->next_string, str, len);
|
||||
res->next_string[len] = 0;
|
||||
res->next_string += len+1;
|
||||
*/
|
||||
|
||||
return ptr;
|
||||
return str;
|
||||
}
|
||||
|
||||
static void save_token(LEXER_RESULT *res, int *index, const char **start, const char *end, int *state, int type)
|
||||
static char *str_skiptoblank(char *str)
|
||||
{
|
||||
/* printf("Saving token with length %d\n", end - *start); */
|
||||
TOKEN *tok = &res->tokens[*index];
|
||||
tok->stored_string = store_string(res, *start, end - *start);
|
||||
tok->type = type;
|
||||
++res->num_tokens;
|
||||
|
||||
*start = end + 1;
|
||||
*state = STATE_START;
|
||||
++*index;
|
||||
while(*str && (*str != ' ' && *str != '\t' && *str != '\n'))
|
||||
str++;
|
||||
return str;
|
||||
}
|
||||
|
||||
static int digit(char c)
|
||||
/* static int digit(char c) { return '0' <= c && c <= '9'; } */
|
||||
|
||||
static int console_parse_start(PARSE_RESULT *result, const char *string)
|
||||
{
|
||||
return '0' <= c && c <= '9';
|
||||
}
|
||||
char *str;
|
||||
str_copy(result->string_storage, string, sizeof(result->string_storage));
|
||||
str = result->string_storage;
|
||||
|
||||
static int lex(const char *line, LEXER_RESULT *res)
|
||||
{
|
||||
int state = STATE_START, i = 0;
|
||||
int length_left = CONSOLE_MAX_STR_LENGTH;
|
||||
const char *start, *c;
|
||||
res->num_tokens = 0;
|
||||
/* get command */
|
||||
str = str_skipblanks(str);
|
||||
result->command = str;
|
||||
str = str_skiptoblank(str);
|
||||
|
||||
mem_zero(res, sizeof(*res));
|
||||
res->next_string = res->string_storage;
|
||||
|
||||
for (c = start = line; *c != '\0' && res->num_tokens < MAX_TOKENS && length_left; ++c, --length_left)
|
||||
if(*str)
|
||||
{
|
||||
/* printf("State: %d.. c: %c\n", state, *c); */
|
||||
switch (state)
|
||||
{
|
||||
case STATE_START:
|
||||
if (*c == ' ')
|
||||
start = c + 1;
|
||||
else if (digit(*c))
|
||||
state = STATE_INT;
|
||||
else if (*c == '-')
|
||||
state = STATE_POT_NEGATIVE;
|
||||
else if (*c == '.')
|
||||
state = STATE_POT_FLOAT;
|
||||
else if (*c == '"')
|
||||
state = STATE_QUOTED;
|
||||
else
|
||||
state = STATE_STRING;
|
||||
break;
|
||||
|
||||
case STATE_POT_NEGATIVE:
|
||||
if (digit(*c))
|
||||
state = STATE_INT;
|
||||
else if (*c == '.')
|
||||
state = STATE_POT_FLOAT;
|
||||
else if (*c == ' ')
|
||||
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
||||
else
|
||||
state = STATE_STRING;
|
||||
break;
|
||||
|
||||
case STATE_INT:
|
||||
if (digit(*c))
|
||||
;
|
||||
else if (*c == '.')
|
||||
state = STATE_FLOAT;
|
||||
else if (*c == ' ')
|
||||
save_token(res, &i, &start, c, &state, TOKEN_INT);
|
||||
else
|
||||
state = STATE_STRING;
|
||||
break;
|
||||
|
||||
case STATE_FLOAT:
|
||||
if (digit(*c))
|
||||
;
|
||||
else if (*c == ' ')
|
||||
save_token(res, &i, &start, c, &state, TOKEN_FLOAT);
|
||||
else
|
||||
state = STATE_STRING;
|
||||
break;
|
||||
|
||||
case STATE_POT_FLOAT:
|
||||
if (digit(*c))
|
||||
state = STATE_FLOAT;
|
||||
else if (*c == ' ')
|
||||
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
||||
else
|
||||
state = STATE_STRING;
|
||||
break;
|
||||
|
||||
case STATE_STRING:
|
||||
if (*c == ' ')
|
||||
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
||||
break;
|
||||
|
||||
case STATE_QUOTED:
|
||||
if (*c == '"')
|
||||
{
|
||||
++start;
|
||||
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
||||
}
|
||||
else if (*c == '\\')
|
||||
state = STATE_ESCAPE;
|
||||
break;
|
||||
|
||||
case STATE_ESCAPE:
|
||||
if (*c != ' ')
|
||||
state = STATE_QUOTED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case STATE_INT:
|
||||
save_token(res, &i, &start, c, &state, TOKEN_INT);
|
||||
break;
|
||||
case STATE_FLOAT:
|
||||
save_token(res, &i, &start, c, &state, TOKEN_FLOAT);
|
||||
break;
|
||||
case STATE_STRING:
|
||||
case STATE_QUOTED:
|
||||
case STATE_POT_FLOAT:
|
||||
case STATE_POT_NEGATIVE:
|
||||
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
||||
break;
|
||||
case STATE_ESCAPE:
|
||||
dbg_msg("console/lexer", "Misplaced escape character");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
str[0] = 0;
|
||||
str++;
|
||||
}
|
||||
|
||||
result->args_start = str;
|
||||
result->num_args = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int console_result_string(void *res, int index, const char **str)
|
||||
static int console_parse_args(PARSE_RESULT *result, const char *format)
|
||||
{
|
||||
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
||||
char command;
|
||||
char *str;
|
||||
int optional = 0;
|
||||
int error = 0;
|
||||
|
||||
if (index < 0 || index >= result->num_tokens)
|
||||
return -1;
|
||||
else
|
||||
str = result->args_start;
|
||||
|
||||
while(1)
|
||||
{
|
||||
TOKEN *t = &result->tokens[index];
|
||||
*str = t->stored_string;
|
||||
return 0;
|
||||
/* fetch command */
|
||||
command = *format;
|
||||
format++;
|
||||
|
||||
if(!command)
|
||||
break;
|
||||
|
||||
if(command == '?')
|
||||
optional = 1;
|
||||
else
|
||||
{
|
||||
str = str_skipblanks(str);
|
||||
|
||||
if(!(*str)) /* error, non optional command needs value */
|
||||
{
|
||||
if(!optional)
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* add token */
|
||||
result->args[result->num_args++] = str;
|
||||
|
||||
if(command == 'r') /* rest of the string */
|
||||
break;
|
||||
else if(command == 'i') /* validate int */
|
||||
str = str_skiptoblank(str);
|
||||
else if(command == 'f') /* validate float */
|
||||
str = str_skiptoblank(str);
|
||||
else if(command == 's') /* validate string */
|
||||
str = str_skiptoblank(str);
|
||||
|
||||
if(str[0] != 0) /* check for end of string */
|
||||
{
|
||||
str[0] = 0;
|
||||
str++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
int console_result_int(void *res, int index, int *i)
|
||||
const char *console_arg_string(void *res, int index)
|
||||
{
|
||||
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
||||
|
||||
if (index < 0 || index >= result->num_tokens)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
TOKEN *t = &result->tokens[index];
|
||||
const char *str;
|
||||
|
||||
if (t->type != TOKEN_INT)
|
||||
return -2;
|
||||
|
||||
console_result_string(result, index, &str);
|
||||
|
||||
*i = atoi(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
PARSE_RESULT *result = (PARSE_RESULT *)res;
|
||||
if (index < 0 || index >= result->num_args)
|
||||
return "";
|
||||
return result->args[index];
|
||||
}
|
||||
|
||||
int console_result_float(void *res, int index, float *f)
|
||||
int console_arg_int(void *res, int index)
|
||||
{
|
||||
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
||||
|
||||
if (index < 0 || index >= result->num_tokens)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
TOKEN *t = &result->tokens[index];
|
||||
const char *str;
|
||||
|
||||
if (t->type != TOKEN_INT && t->type != TOKEN_FLOAT)
|
||||
return -2;
|
||||
|
||||
console_result_string(result, index, &str);
|
||||
|
||||
*f = atof(str);
|
||||
|
||||
PARSE_RESULT *result = (PARSE_RESULT *)res;
|
||||
if (index < 0 || index >= result->num_args)
|
||||
return 0;
|
||||
}
|
||||
return atoi(result->args[index]);
|
||||
}
|
||||
|
||||
float console_arg_float(void *res, int index)
|
||||
{
|
||||
PARSE_RESULT *result = (PARSE_RESULT *)res;
|
||||
if (index < 0 || index >= result->num_args)
|
||||
return 0.0f;
|
||||
return atof(result->args[index]);
|
||||
}
|
||||
|
||||
int console_arg_num(void *result)
|
||||
{
|
||||
return ((PARSE_RESULT *)result)->num_args;
|
||||
}
|
||||
|
||||
static COMMAND *first_command = 0x0;
|
||||
|
@ -276,8 +148,10 @@ COMMAND *console_find_command(const char *name)
|
|||
{
|
||||
COMMAND *cmd;
|
||||
for (cmd = first_command; cmd; cmd = cmd->next)
|
||||
{
|
||||
if (strcmp(cmd->name, name) == 0)
|
||||
return cmd;
|
||||
}
|
||||
|
||||
return 0x0;
|
||||
}
|
||||
|
@ -288,45 +162,6 @@ void console_register(COMMAND *cmd)
|
|||
first_command = cmd;
|
||||
}
|
||||
|
||||
|
||||
static int console_validate(COMMAND *command, LEXER_RESULT *result)
|
||||
{
|
||||
const char *c = command->params;
|
||||
int i = 1;
|
||||
|
||||
const char *dummy_s;
|
||||
int dummy_i;
|
||||
float dummy_f;
|
||||
|
||||
while (*c && *c != '?')
|
||||
{
|
||||
switch (*c)
|
||||
{
|
||||
case 's':
|
||||
if (console_result_string(result, i, &dummy_s))
|
||||
return -1;
|
||||
break;
|
||||
case 'i':
|
||||
if (console_result_int(result, i, &dummy_i))
|
||||
return -1;
|
||||
break;
|
||||
case 'f':
|
||||
if (console_result_float(result, i, &dummy_f))
|
||||
return -1;
|
||||
break;
|
||||
default:
|
||||
/* unknown char, so just continue... */
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
c++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void (*print_callback)(const char *) = 0x0;
|
||||
|
||||
void console_register_print_callback(void (*callback)(const char *))
|
||||
|
@ -340,63 +175,49 @@ void console_print(const char *str)
|
|||
print_callback(str);
|
||||
}
|
||||
|
||||
|
||||
void console_execute_line_stroked(int stroke, const char *str)
|
||||
{
|
||||
LEXER_RESULT result;
|
||||
int error;
|
||||
char strokestr[8] = {'0', 0};
|
||||
PARSE_RESULT result;
|
||||
COMMAND *command;
|
||||
|
||||
char strokestr[2] = {'0', 0};
|
||||
if(stroke)
|
||||
strokestr[0] = '1';
|
||||
|
||||
if ((error = lex(str, &result)))
|
||||
printf("ERROR: %d\n", error);
|
||||
else if (result.num_tokens > 0)
|
||||
if(console_parse_start(&result, str) != 0)
|
||||
return;
|
||||
|
||||
command = console_find_command(result.command);
|
||||
|
||||
if(command)
|
||||
{
|
||||
const char *name;
|
||||
COMMAND *command;
|
||||
console_result_string(&result, 0, &name);
|
||||
|
||||
command = console_find_command(name);
|
||||
|
||||
if(command)
|
||||
int is_stroke_command = 0;
|
||||
if(result.command[0] == '+')
|
||||
{
|
||||
int is_stroke_command = 0;
|
||||
if(name[0] == '+')
|
||||
{
|
||||
/* insert the stroke direction token */
|
||||
int i;
|
||||
for(i = result.num_tokens-2; i > 1; i--)
|
||||
{
|
||||
result.tokens[i+1] = result.tokens[i];
|
||||
}
|
||||
|
||||
result.tokens[1].type = TOKEN_INT;
|
||||
result.tokens[1].stored_string = strokestr;
|
||||
result.num_tokens++;
|
||||
|
||||
is_stroke_command = 1;
|
||||
}
|
||||
|
||||
if(stroke || is_stroke_command)
|
||||
{
|
||||
if (console_validate(command, &result))
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "Invalid arguments... Usage: %s %s", command->name, command->params);
|
||||
console_print(buf);
|
||||
}
|
||||
else
|
||||
command->callback(&result, command->user_data);
|
||||
}
|
||||
/* insert the stroke direction token */
|
||||
result.args[result.num_args] = strokestr;
|
||||
result.num_args++;
|
||||
is_stroke_command = 1;
|
||||
}
|
||||
else
|
||||
|
||||
if(stroke || is_stroke_command)
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "No such command: %s.", name);
|
||||
console_print(buf);
|
||||
if(console_parse_args(&result, command->params))
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "Invalid arguments... Usage: %s %s", command->name, command->params);
|
||||
console_print(buf);
|
||||
}
|
||||
else
|
||||
command->callback(&result, command->user_data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "No such command: %s.", result.command);
|
||||
console_print(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void console_execute_line(const char *str)
|
||||
|
@ -404,7 +225,6 @@ void console_execute_line(const char *str)
|
|||
console_execute_line_stroked(1, str);
|
||||
}
|
||||
|
||||
|
||||
void console_execute_file(const char *filename)
|
||||
{
|
||||
IOHANDLE file;
|
||||
|
@ -429,9 +249,7 @@ void console_execute_file(const char *filename)
|
|||
|
||||
static void echo_command(void *result, void *user_data)
|
||||
{
|
||||
const char *str;
|
||||
console_result_string(result, 1, &str);
|
||||
console_print(str);
|
||||
console_print(console_arg_string(result, 0));
|
||||
}
|
||||
|
||||
|
||||
|
@ -450,40 +268,34 @@ typedef struct
|
|||
static void int_variable_command(void *result, void *user_data)
|
||||
{
|
||||
INT_VARIABLE_DATA *data = (INT_VARIABLE_DATA *)user_data;
|
||||
int new_val;
|
||||
|
||||
if (console_result_int(result, 1, &new_val))
|
||||
if(console_arg_num(result))
|
||||
data->setter(&config, console_arg_int(result, 0));
|
||||
else
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "Value: %d", data->getter(&config));
|
||||
console_print(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
data->setter(&config, new_val);
|
||||
}
|
||||
}
|
||||
|
||||
static void str_variable_command(void *result, void *user_data)
|
||||
{
|
||||
STR_VARIABLE_DATA *data = (STR_VARIABLE_DATA *)user_data;
|
||||
const char *new_val;
|
||||
|
||||
if (console_result_string(result, 1, &new_val))
|
||||
if(console_arg_num(result))
|
||||
data->setter(&config, console_arg_string(result, 0));
|
||||
else
|
||||
{
|
||||
char buf[256];
|
||||
str_format(buf, sizeof(buf), "Value: %s", data->getter(&config));
|
||||
console_print(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
data->setter(&config, new_val);
|
||||
}
|
||||
}
|
||||
|
||||
void console_init()
|
||||
{
|
||||
MACRO_REGISTER_COMMAND("echo", "s", echo_command, 0x0);
|
||||
MACRO_REGISTER_COMMAND("echo", "r", echo_command, 0x0);
|
||||
|
||||
#define MACRO_CONFIG_INT(name,def,min,max) { static INT_VARIABLE_DATA data = { &config_get_ ## name, &config_set_ ## name }; MACRO_REGISTER_COMMAND(#name, "?i", int_variable_command, &data) }
|
||||
#define MACRO_CONFIG_STR(name,len,def) { static STR_VARIABLE_DATA data = { &config_get_ ## name, &config_set_ ## name }; MACRO_REGISTER_COMMAND(#name, "?s", str_variable_command, &data) }
|
||||
|
|
|
@ -24,9 +24,14 @@ void console_execute_file(const char *filename);
|
|||
void console_print(const char *str);
|
||||
void console_register_print_callback(void (*callback)(const char *));
|
||||
|
||||
int console_result_string(void *result, int index, const char **str);
|
||||
/*int console_result_string(void *result, int index, const char **str);
|
||||
int console_result_int(void *result, int index, int *i);
|
||||
int console_result_float(void *result, int index, float *f);
|
||||
int console_result_float(void *result, int index, float *f);*/
|
||||
|
||||
const char *console_arg_string(void *result, int index);
|
||||
int console_arg_int(void *result, int index);
|
||||
float console_arg_float(void *result, int index);
|
||||
int console_arg_num(void *result);
|
||||
|
||||
#define MACRO_REGISTER_COMMAND(name, params, func, ptr) { static COMMAND cmd = { name, params, func, ptr, 0x0 }; console_register(&cmd); }
|
||||
|
||||
|
|
|
@ -1065,9 +1065,7 @@ static int server_run()
|
|||
|
||||
static void con_kick(void *result, void *user_data)
|
||||
{
|
||||
int cid;
|
||||
console_result_int(result, 1, &cid);
|
||||
server_kick(cid, "kicked by console");
|
||||
server_kick(console_arg_int(result, 0), "kicked by console");
|
||||
}
|
||||
|
||||
static void con_status(void *result, void *user_data)
|
||||
|
|
|
@ -202,23 +202,17 @@ void console_rcon_print(const char *line)
|
|||
|
||||
static void con_team(void *result, void *user_data)
|
||||
{
|
||||
int new_team;
|
||||
console_result_int(result, 1, &new_team);
|
||||
send_switch_team(new_team);
|
||||
send_switch_team(console_arg_int(result, 0));
|
||||
}
|
||||
|
||||
static void con_say(void *result, void *user_data)
|
||||
{
|
||||
const char *str;
|
||||
console_result_string(result, 1, &str);
|
||||
chat_say(0, str);
|
||||
chat_say(0, console_arg_string(result, 0));
|
||||
}
|
||||
|
||||
static void con_sayteam(void *result, void *user_data)
|
||||
{
|
||||
const char *str;
|
||||
console_result_string(result, 1, &str);
|
||||
chat_say(1, str);
|
||||
chat_say(1, console_arg_string(result, 0));
|
||||
}
|
||||
|
||||
void send_kill(int client_id);
|
||||
|
@ -267,10 +261,7 @@ static int get_key_id(const char *key_name)
|
|||
|
||||
static void con_binds_set(void *result, void *user_data)
|
||||
{
|
||||
const char *key_name;
|
||||
const char *bind_str;
|
||||
console_result_string(result, 1, &key_name);
|
||||
console_result_string(result, 2, &bind_str);
|
||||
const char *key_name = console_arg_string(result, 0);
|
||||
int id = get_key_id(key_name);
|
||||
|
||||
if(!id)
|
||||
|
@ -279,14 +270,13 @@ static void con_binds_set(void *result, void *user_data)
|
|||
return;
|
||||
}
|
||||
|
||||
binds_set(id, bind_str);
|
||||
binds_set(id, console_arg_string(result, 1));
|
||||
}
|
||||
|
||||
|
||||
static void con_binds_remove(void *result, void *user_data)
|
||||
{
|
||||
const char *key_name;
|
||||
console_result_string(result, 1, &key_name);
|
||||
const char *key_name = console_arg_string(result, 0);
|
||||
int id = get_key_id(key_name);
|
||||
|
||||
if(!id)
|
||||
|
@ -315,24 +305,20 @@ void binds_save()
|
|||
{
|
||||
if(keybindings[i][0] == 0)
|
||||
continue;
|
||||
str_format(buffer, sizeof(buffer), "binds_set %s \"%s\"", inp_key_name(i), keybindings[i]);
|
||||
str_format(buffer, sizeof(buffer), "binds_set %s %s", inp_key_name(i), keybindings[i]);
|
||||
client_save_line(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
static void con_key_input_state(void *result, void *user_data)
|
||||
{
|
||||
int i;
|
||||
console_result_int(result, 1, &i);
|
||||
((int *)user_data)[0] = i;
|
||||
((int *)user_data)[0] = console_arg_int(result, 0);
|
||||
}
|
||||
|
||||
static void con_key_input_counter(void *result, void *user_data)
|
||||
{
|
||||
int stroke;
|
||||
int *v = (int *)user_data;
|
||||
console_result_int(result, 1, &stroke);
|
||||
if(((*v)&1) != stroke)
|
||||
if(((*v)&1) != console_arg_int(result, 0))
|
||||
(*v)++;
|
||||
*v &= INPUT_STATE_MASK;
|
||||
}
|
||||
|
@ -340,16 +326,13 @@ static void con_key_input_counter(void *result, void *user_data)
|
|||
static void con_key_input_weapon(void *result, void *user_data)
|
||||
{
|
||||
int w = (char *)user_data - (char *)0;
|
||||
int stroke;
|
||||
console_result_int(result, 1, &stroke);
|
||||
if(stroke)
|
||||
if(console_arg_int(result, 0))
|
||||
input_data.wanted_weapon = w;
|
||||
}
|
||||
|
||||
static void con_chat(void *result, void *user_data)
|
||||
{
|
||||
const char *mode;
|
||||
console_result_string(result, 1, &mode);
|
||||
const char *mode = console_arg_string(result, 0);
|
||||
if(strcmp(mode, "all") == 0)
|
||||
chat_enable_mode(0);
|
||||
else if(strcmp(mode, "team") == 0)
|
||||
|
@ -368,12 +351,9 @@ static void con_toggle_remote_console(void *result, void *user_data)
|
|||
console_toggle(1);
|
||||
}
|
||||
|
||||
|
||||
static void con_emote(void *result, void *user_data)
|
||||
{
|
||||
int emote;
|
||||
console_result_int(result, 1, &emote);
|
||||
send_emoticon(emote);
|
||||
send_emoticon(console_arg_int(result, 0));
|
||||
}
|
||||
|
||||
void client_console_init()
|
||||
|
@ -389,33 +369,33 @@ void client_console_init()
|
|||
MACRO_REGISTER_COMMAND("kill", "", con_kill, 0x0);
|
||||
|
||||
// bindings
|
||||
MACRO_REGISTER_COMMAND("binds_set", "ss", con_binds_set, 0x0);
|
||||
MACRO_REGISTER_COMMAND("binds_set", "sr", con_binds_set, 0x0);
|
||||
MACRO_REGISTER_COMMAND("binds_remove", "s", con_binds_remove, 0x0);
|
||||
MACRO_REGISTER_COMMAND("binds_dump", "", con_binds_dump, 0x0);
|
||||
|
||||
// chatting
|
||||
MACRO_REGISTER_COMMAND("say", "s", con_say, 0x0);
|
||||
MACRO_REGISTER_COMMAND("say_team", "s", con_sayteam, 0x0);
|
||||
MACRO_REGISTER_COMMAND("say", "r", con_say, 0x0);
|
||||
MACRO_REGISTER_COMMAND("say_team", "r", con_sayteam, 0x0);
|
||||
MACRO_REGISTER_COMMAND("chat", "s", con_chat, 0x0);
|
||||
MACRO_REGISTER_COMMAND("emote", "i", con_emote, 0);
|
||||
|
||||
// game commands
|
||||
MACRO_REGISTER_COMMAND("+left", "i", con_key_input_state, &input_data.left);
|
||||
MACRO_REGISTER_COMMAND("+right", "i", con_key_input_state, &input_data.right);
|
||||
MACRO_REGISTER_COMMAND("+jump", "i", con_key_input_state, &input_data.jump);
|
||||
MACRO_REGISTER_COMMAND("+hook", "i", con_key_input_state, &input_data.hook);
|
||||
MACRO_REGISTER_COMMAND("+fire", "i", con_key_input_counter, &input_data.fire);
|
||||
MACRO_REGISTER_COMMAND("+weapon1", "i", con_key_input_weapon, (void *)1);
|
||||
MACRO_REGISTER_COMMAND("+weapon2", "i", con_key_input_weapon, (void *)2);
|
||||
MACRO_REGISTER_COMMAND("+weapon3", "i", con_key_input_weapon, (void *)3);
|
||||
MACRO_REGISTER_COMMAND("+weapon4", "i", con_key_input_weapon, (void *)4);
|
||||
MACRO_REGISTER_COMMAND("+weapon5", "i", con_key_input_weapon, (void *)5);
|
||||
MACRO_REGISTER_COMMAND("+left", "", con_key_input_state, &input_data.left);
|
||||
MACRO_REGISTER_COMMAND("+right", "", con_key_input_state, &input_data.right);
|
||||
MACRO_REGISTER_COMMAND("+jump", "", con_key_input_state, &input_data.jump);
|
||||
MACRO_REGISTER_COMMAND("+hook", "", con_key_input_state, &input_data.hook);
|
||||
MACRO_REGISTER_COMMAND("+fire", "", con_key_input_counter, &input_data.fire);
|
||||
MACRO_REGISTER_COMMAND("+weapon1", "", con_key_input_weapon, (void *)1);
|
||||
MACRO_REGISTER_COMMAND("+weapon2", "", con_key_input_weapon, (void *)2);
|
||||
MACRO_REGISTER_COMMAND("+weapon3", "", con_key_input_weapon, (void *)3);
|
||||
MACRO_REGISTER_COMMAND("+weapon4", "", con_key_input_weapon, (void *)4);
|
||||
MACRO_REGISTER_COMMAND("+weapon5", "", con_key_input_weapon, (void *)5);
|
||||
|
||||
MACRO_REGISTER_COMMAND("+nextweapon", "i", con_key_input_counter, &input_data.next_weapon);
|
||||
MACRO_REGISTER_COMMAND("+prevweapon", "i", con_key_input_counter, &input_data.prev_weapon);
|
||||
MACRO_REGISTER_COMMAND("+nextweapon", "", con_key_input_counter, &input_data.next_weapon);
|
||||
MACRO_REGISTER_COMMAND("+prevweapon", "", con_key_input_counter, &input_data.prev_weapon);
|
||||
|
||||
MACRO_REGISTER_COMMAND("+emote", "i", con_key_input_state, &emoticon_selector_active);
|
||||
MACRO_REGISTER_COMMAND("+scoreboard", "i", con_key_input_state, &scoreboard_active);
|
||||
MACRO_REGISTER_COMMAND("+emote", "", con_key_input_state, &emoticon_selector_active);
|
||||
MACRO_REGISTER_COMMAND("+scoreboard", "", con_key_input_state, &scoreboard_active);
|
||||
|
||||
// set default key bindings
|
||||
binds_set(KEY_F1, "toggle_local_console");
|
||||
|
|
|
@ -2235,26 +2235,16 @@ extern unsigned char internal_data[];
|
|||
|
||||
static void con_tune_param(void *result, void *user_data)
|
||||
{
|
||||
const char *param_name;
|
||||
float new_value;
|
||||
const char *param_name = console_arg_string(result, 0);
|
||||
float new_value = console_arg_float(result, 1);
|
||||
|
||||
if(console_result_string(result, 1, ¶m_name) == 0)
|
||||
if(tuning.set(param_name, new_value))
|
||||
{
|
||||
if(console_result_float(result, 2, &new_value) == 0)
|
||||
{
|
||||
if(tuning.set(param_name, new_value))
|
||||
{
|
||||
dbg_msg("tuning", "%s changed to %.2f", param_name, new_value);
|
||||
send_tuning_params(-1);
|
||||
}
|
||||
else
|
||||
console_print("No such tuning parameter");
|
||||
}
|
||||
else
|
||||
{
|
||||
//console_print("");
|
||||
}
|
||||
dbg_msg("tuning", "%s changed to %.2f", param_name, new_value);
|
||||
send_tuning_params(-1);
|
||||
}
|
||||
else
|
||||
console_print("No such tuning parameter");
|
||||
}
|
||||
|
||||
static void con_tune_reset(void *result, void *user_data)
|
||||
|
@ -2278,20 +2268,15 @@ static void con_tune_dump(void *result, void *user_data)
|
|||
|
||||
static void con_restart(void *result, void *user_data)
|
||||
{
|
||||
int time = 0;
|
||||
console_result_int(result, 1, &time);
|
||||
|
||||
if(time)
|
||||
gameobj->do_warmup(time);
|
||||
if(console_arg_num(result))
|
||||
gameobj->do_warmup(console_arg_int(result, 0));
|
||||
else
|
||||
gameobj->startround();
|
||||
}
|
||||
|
||||
static void con_broadcast(void *result, void *user_data)
|
||||
{
|
||||
const char *message;
|
||||
console_result_string(result, 1, &message);
|
||||
send_chat(-1, -1, message);
|
||||
send_chat(-1, -1, console_arg_string(result, 0));
|
||||
}
|
||||
|
||||
void mods_console_init()
|
||||
|
@ -2301,7 +2286,7 @@ void mods_console_init()
|
|||
MACRO_REGISTER_COMMAND("tune_dump", "", con_tune_dump, 0);
|
||||
|
||||
MACRO_REGISTER_COMMAND("restart", "?i", con_restart, 0);
|
||||
MACRO_REGISTER_COMMAND("broadcast", "s", con_broadcast, 0);
|
||||
MACRO_REGISTER_COMMAND("broadcast", "r", con_broadcast, 0);
|
||||
}
|
||||
|
||||
void mods_init()
|
||||
|
|
Loading…
Reference in a new issue