diff options
Diffstat (limited to 'src/engine')
| -rw-r--r-- | src/engine/client/ec_client.c | 40 | ||||
| -rw-r--r-- | src/engine/e_config.h | 8 | ||||
| -rw-r--r-- | src/engine/e_console.c | 111 | ||||
| -rw-r--r-- | src/engine/e_console.h | 45 | ||||
| -rw-r--r-- | src/engine/e_engine.c | 35 | ||||
| -rw-r--r-- | src/engine/e_engine.h | 3 | ||||
| -rw-r--r-- | src/engine/e_if_modc.h | 4 | ||||
| -rw-r--r-- | src/engine/e_if_mods.h | 5 | ||||
| -rw-r--r-- | src/engine/e_server_interface.h | 2 | ||||
| -rw-r--r-- | src/engine/e_snapshot.h | 2 | ||||
| -rw-r--r-- | src/engine/e_system.h | 21 | ||||
| -rw-r--r-- | src/engine/server/es_server.c | 22 |
12 files changed, 176 insertions, 122 deletions
diff --git a/src/engine/client/ec_client.c b/src/engine/client/ec_client.c index b1399645..3c0180b9 100644 --- a/src/engine/client/ec_client.c +++ b/src/engine/client/ec_client.c @@ -18,6 +18,7 @@ #include <engine/e_packer.h> #include <engine/e_memheap.h> #include <engine/e_datafile.h> +#include <engine/e_console.h> #include <mastersrv/mastersrv.h> @@ -1207,20 +1208,49 @@ static void client_run() snd_shutdown(); } +static void connect_command(void *result, void *user_data) +{ + const char *address; + console_result_string(result, 1, &address); + client_connect(address); +} + +static void disconnect_command(void *result, void *user_data) +{ + client_disconnect(); +} + +static void quit_command(void *result, void *user_data) +{ + client_quit(); +} + +static void client_register_commands() +{ + MACRO_REGISTER_COMMAND("quit", "", quit_command, 0x0); + MACRO_REGISTER_COMMAND("connect", "s", connect_command, 0x0); + MACRO_REGISTER_COMMAND("disconnect", "", disconnect_command, 0x0); +} int editor_main(int argc, char **argv); int main(int argc, char **argv) { - /* preinit the mod */ - modc_preinit(); - /* init the engine */ dbg_msg("client", "starting..."); - engine_init("Teewars", argc, argv); + engine_init("Teewars"); + /* register all console commands */ + client_register_commands(); + modc_console_init(); + + /* parse the command line arguments */ + engine_parse_arguments(argc, argv); + + /* run the client*/ client_run(); - + + /* write down the config and quit */ engine_writeconfig(); return 0; } diff --git a/src/engine/e_config.h b/src/engine/e_config.h index b6fbc93d..f1145f6a 100644 --- a/src/engine/e_config.h +++ b/src/engine/e_config.h @@ -23,10 +23,10 @@ void config_reset(); void config_load(const char *filename); void config_save(const char *filename); -typedef int (*config_int_getter)(CONFIGURATION *c); -typedef const char *(*config_str_getter)(CONFIGURATION *c); -typedef void (*config_int_setter)(CONFIGURATION *c, int val); -typedef void (*config_str_setter)(CONFIGURATION *c, const char *str); +typedef int (*CONFIG_INT_GETTER)(CONFIGURATION *c); +typedef const char *(*CONFIG_STR_GETTER)(CONFIGURATION *c); +typedef void (*CONFIG_INT_SETTER)(CONFIGURATION *c, int val); +typedef void (*CONFIG_STR_SETTER)(CONFIGURATION *c, const char *str); #define MACRO_CONFIG_INT(name,def,min,max) int config_get_ ## name (CONFIGURATION *c); #define MACRO_CONFIG_STR(name,len,def) const char *config_get_ ## name (CONFIGURATION *c); diff --git a/src/engine/e_console.c b/src/engine/e_console.c index 929a0eca..8582815b 100644 --- a/src/engine/e_console.c +++ b/src/engine/e_console.c @@ -1,9 +1,37 @@ +#include "e_system.h" #include "e_console.h" #include "e_config.h" #include <stdio.h> #include <string.h> #include <stdlib.h> + +#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; + +typedef struct +{ + char string_storage[CONSOLE_MAX_STR_LENGTH+1]; + char *next_string; + + TOKEN tokens[MAX_TOKENS]; + unsigned int num_tokens; +} LEXER_RESULT; + enum { STATE_START, @@ -15,7 +43,7 @@ enum STATE_ESCAPE }; -static const char *store_string(struct lexer_result *res, const char *str, int len) +static const char *store_string(LEXER_RESULT *res, const char *str, int len) { const char *ptr = res->next_string; int escaped = 0; @@ -48,10 +76,10 @@ static const char *store_string(struct lexer_result *res, const char *str, int l return ptr; } -static void save_token(struct lexer_result *res, int *index, const char **start, const char *end, int *state, int type) +static void save_token(LEXER_RESULT *res, int *index, const char **start, const char *end, int *state, int type) { /* printf("Saving token with length %d\n", end - *start); */ - struct token *tok = &res->tokens[*index]; + TOKEN *tok = &res->tokens[*index]; tok->stored_string = store_string(res, *start, end - *start); tok->type = type; ++res->num_tokens; @@ -66,14 +94,14 @@ static int digit(char c) return '0' <= c && c <= '9'; } -static int lex(const char *line, struct lexer_result *res) +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; - memset(res, 0, sizeof(*res)); + 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) @@ -170,33 +198,35 @@ static int lex(const char *line, struct lexer_result *res) return 0; } -int extract_result_string(struct lexer_result *result, int index, const char **str) +int console_result_string(void *res, int index, const char **str) { + LEXER_RESULT *result = (LEXER_RESULT *)res; + if (index < 0 || index >= result->num_tokens) return -1; else { - struct token *t = &result->tokens[index]; - + TOKEN *t = &result->tokens[index]; *str = t->stored_string; - return 0; } } -int extract_result_int(struct lexer_result *result, int index, int *i) +int console_result_int(void *res, int index, int *i) { + LEXER_RESULT *result = (LEXER_RESULT *)res; + if (index < 0 || index >= result->num_tokens) return -1; else { - struct token *t = &result->tokens[index]; + TOKEN *t = &result->tokens[index]; const char *str; if (t->type != TOKEN_INT) return -2; - extract_result_string(result, index, &str); + console_result_string(result, index, &str); *i = atoi(str); @@ -204,19 +234,21 @@ int extract_result_int(struct lexer_result *result, int index, int *i) } } -int extract_result_float(struct lexer_result *result, int index, float *f) +int console_result_float(void *res, int index, float *f) { + LEXER_RESULT *result = (LEXER_RESULT *)res; + if (index < 0 || index >= result->num_tokens) return -1; else { - struct token *t = &result->tokens[index]; + TOKEN *t = &result->tokens[index]; const char *str; if (t->type != TOKEN_INT && t->type != TOKEN_FLOAT) return -2; - extract_result_string(result, index, &str); + console_result_string(result, index, &str); *f = atof(str); @@ -243,7 +275,7 @@ void console_register(COMMAND *cmd) } -static int console_validate(COMMAND *command, struct lexer_result *result) +static int console_validate(COMMAND *command, LEXER_RESULT *result) { const char *c = command->params; int i = 1; @@ -257,15 +289,15 @@ static int console_validate(COMMAND *command, struct lexer_result *result) switch (*c) { case 's': - if (extract_result_string(result, i, &dummy_s)) + if (console_result_string(result, i, &dummy_s)) return -1; break; case 'i': - if (extract_result_int(result, i, &dummy_i)) + if (console_result_int(result, i, &dummy_i)) return -1; break; case 'f': - if (extract_result_float(result, i, &dummy_f)) + if (console_result_float(result, i, &dummy_f)) return -1; break; default: @@ -296,7 +328,7 @@ void console_print(const char *str) void console_execute(const char *str) { - struct lexer_result result; + LEXER_RESULT result; int error; if ((error = lex(str, &result))) @@ -305,7 +337,7 @@ void console_execute(const char *str) { const char *name; COMMAND *command; - extract_result_string(&result, 0, &name); + console_result_string(&result, 0, &name); command = console_find_command(name); @@ -329,33 +361,32 @@ void console_execute(const char *str) } } -static void echo_command(struct lexer_result *result, void *user_data) +static void echo_command(void *result, void *user_data) { const char *str; - extract_result_string(result, 1, &str); - + console_result_string(result, 1, &str); console_print(str); } -struct int_variable_data +typedef struct { - config_int_getter getter; - config_int_setter setter; -}; + CONFIG_INT_GETTER getter; + CONFIG_INT_SETTER setter; +} INT_VARIABLE_DATA; -struct str_variable_data +typedef struct { - config_str_getter getter; - config_str_setter setter; -}; + CONFIG_STR_GETTER getter; + CONFIG_STR_SETTER setter; +} STR_VARIABLE_DATA; -static void int_variable_command(struct lexer_result *result, void *user_data) +static void int_variable_command(void *result, void *user_data) { - struct int_variable_data *data = (struct int_variable_data *)user_data; + INT_VARIABLE_DATA *data = (INT_VARIABLE_DATA *)user_data; int new_val; - if (extract_result_int(result, 1, &new_val)) + if (console_result_int(result, 1, &new_val)) { char buf[256]; sprintf(buf, "Value: %d", data->getter(&config)); @@ -367,12 +398,12 @@ static void int_variable_command(struct lexer_result *result, void *user_data) } } -static void str_variable_command(struct lexer_result *result, void *user_data) +static void str_variable_command(void *result, void *user_data) { - struct str_variable_data *data = (struct str_variable_data *)user_data; + STR_VARIABLE_DATA *data = (STR_VARIABLE_DATA *)user_data; const char *new_val; - if (extract_result_string(result, 1, &new_val)) + if (console_result_string(result, 1, &new_val)) { char buf[256]; sprintf(buf, "Value: %s", data->getter(&config)); @@ -388,8 +419,8 @@ void console_init() { MACRO_REGISTER_COMMAND("echo", "s", echo_command, 0x0); - #define MACRO_CONFIG_INT(name,def,min,max) { static struct 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 struct str_variable_data data = { &config_get_ ## name, &config_set_ ## name }; MACRO_REGISTER_COMMAND(#name, "?s", str_variable_command, &data) } + #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) } #include "e_config_variables.h" diff --git a/src/engine/e_console.h b/src/engine/e_console.h index 77057df3..10d77a3a 100644 --- a/src/engine/e_console.h +++ b/src/engine/e_console.h @@ -5,46 +5,15 @@ extern "C"{ #endif -#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 -}; - -struct token -{ - int type; - const char *stored_string; -}; - -struct lexer_result -{ - char string_storage[CONSOLE_MAX_STR_LENGTH+1]; - char *next_string; - - struct token tokens[MAX_TOKENS]; - unsigned int num_tokens; -}; - -int extract_result_string(struct lexer_result *result, int index, const char **str); -int extract_result_int(struct lexer_result *result, int index, int *i); -int extract_result_float(struct lexer_result *result, int index, float *f); - -typedef void (*console_callback)(struct lexer_result *result, void *user_data); - -typedef struct COMMAND +typedef void (*CONSOLE_CALLBACK)(void *result, void *user_data); + +typedef struct COMMAND_t { const char *name; const char *params; - console_callback callback; + CONSOLE_CALLBACK callback; void *user_data; - struct COMMAND *next; - + struct COMMAND_t *next; } COMMAND; void console_init(); @@ -53,6 +22,10 @@ void console_execute(const char *str); 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_int(void *result, int index, int *i); +int console_result_float(void *result, int index, float *f); + #define MACRO_REGISTER_COMMAND(name, params, func, ptr) { static COMMAND cmd = { name, params, func, ptr, 0x0 }; console_register(&cmd); } #ifdef __cplusplus diff --git a/src/engine/e_engine.c b/src/engine/e_engine.c index 5cd5d6bd..3168020d 100644 --- a/src/engine/e_engine.c +++ b/src/engine/e_engine.c @@ -47,28 +47,29 @@ void engine_init(const char *appname, int argc, char **argv) /* reset the config */ config_reset(); - +} + +void engine_parse_arguments(int argc, char **argv) +{ /* load the configuration */ + int i; + int abs = 0; + const char *config_filename = "default.cfg"; + char buf[1024]; + for(i = 1; i < argc; i++) { - int i; - int abs = 0; - const char *config_filename = "default.cfg"; - char buf[1024]; - for(i = 1; i < argc; i++) + if(argv[i][0] == '-' && argv[i][1] == 'f' && argv[i][2] == 0 && argc - i > 1) { - if(argv[i][0] == '-' && argv[i][1] == 'f' && argv[i][2] == 0 && argc - i > 1) - { - config_filename = argv[i+1]; - abs = 1; - i++; - } + config_filename = argv[i+1]; + abs = 1; + i++; } - - if(abs) - config_load(config_filename); - else - config_load(engine_savepath(config_filename, buf, sizeof(buf))); } + + if(abs) + config_load(config_filename); + else + config_load(engine_savepath(config_filename, buf, sizeof(buf))); /* search arguments for overrides */ { diff --git a/src/engine/e_engine.h b/src/engine/e_engine.h index 56fec20e..ba880bb0 100644 --- a/src/engine/e_engine.h +++ b/src/engine/e_engine.h @@ -1,5 +1,6 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ const char *engine_savepath(const char *filename, char *buffer, int max); -void engine_init(const char *appname, int argc, char **argv); +void engine_init(const char *appname); +void engine_parse_arguments(int argc, char **argv); void engine_writeconfig(); diff --git a/src/engine/e_if_modc.h b/src/engine/e_if_modc.h index 581b0a67..33069a6f 100644 --- a/src/engine/e_if_modc.h +++ b/src/engine/e_if_modc.h @@ -6,10 +6,10 @@ Section: Client Hooks *********************************************************************************/ /* - Function: modc_preinit + Function: modc_console_init TODO */ -void modc_preinit(); +void modc_console_init(); /* Function: modc_init diff --git a/src/engine/e_if_mods.h b/src/engine/e_if_mods.h index 1e6d9694..eff909f3 100644 --- a/src/engine/e_if_mods.h +++ b/src/engine/e_if_mods.h @@ -5,6 +5,11 @@ /********************************************************************************** Section: Server Hooks **********************************************************************************/ +/* + Function: mods_console_init + TODO +*/ +void mods_console_init(); /* Function: mods_init diff --git a/src/engine/e_server_interface.h b/src/engine/e_server_interface.h index f1749fcd..5b1e6327 100644 --- a/src/engine/e_server_interface.h +++ b/src/engine/e_server_interface.h @@ -11,6 +11,8 @@ extern "C" { #include "e_if_msg.h" #include "e_if_mods.h" +#include "e_console.h" /* TODO: clean this up*/ + #ifdef __cplusplus } #endif diff --git a/src/engine/e_snapshot.h b/src/engine/e_snapshot.h index 0a74b0dc..1396f6ee 100644 --- a/src/engine/e_snapshot.h +++ b/src/engine/e_snapshot.h @@ -70,7 +70,7 @@ int snapstorage_get(SNAPSTORAGE *ss, int tick, int64 *tagtime, SNAPSHOT **data); enum { - SNAPBUILD_MAX_ITEMS = 512 + SNAPBUILD_MAX_ITEMS = 1024*2 }; typedef struct SNAPBUILD diff --git a/src/engine/e_system.h b/src/engine/e_system.h index 72a4ff7b..69fa5c63 100644 --- a/src/engine/e_system.h +++ b/src/engine/e_system.h @@ -161,8 +161,7 @@ typedef struct IOINTERNAL *IOHANDLE; /**** Function: io_open - - Opens a file. + Opens a file. Parameters: filename - File to open. @@ -176,8 +175,7 @@ IOHANDLE io_open(const char *filename, int flags); /**** Function: io_read - - Reads data into a buffer from a file. + Reads data into a buffer from a file. Parameters: io - Handle to the file to read data from. @@ -192,8 +190,7 @@ unsigned io_read(IOHANDLE io, void *buffer, unsigned size); /***** Function: io_skip - - Skips data in a file. + Skips data in a file. Parameters: io - Handle to the file. @@ -221,8 +218,7 @@ unsigned io_write(IOHANDLE io, const void *buffer, unsigned size); /***** Function: io_seek - - Seeks to a specified offset in the file. + Seeks to a specified offset in the file. Parameters: io - Handle to the file. @@ -236,8 +232,7 @@ int io_seek(IOHANDLE io, int offset, int origin); /***** Function: io_tell - - Gets the current position in the file. + Gets the current position in the file. Parameters: io - Handle to the file. @@ -249,8 +244,7 @@ long int io_tell(IOHANDLE io); /***** Function: io_length - - Gets the total length of the file. Resetting cursor to the beginning + Gets the total length of the file. Resetting cursor to the beginning Parameters: io - Handle to the file. @@ -262,8 +256,7 @@ long int io_length(IOHANDLE io); /***** Function: io_close - - Closes a file. + Closes a file. Parameters: io - Handle to the file. diff --git a/src/engine/server/es_server.c b/src/engine/server/es_server.c index d781c8da..8d805e42 100644 --- a/src/engine/server/es_server.c +++ b/src/engine/server/es_server.c @@ -225,6 +225,9 @@ void server_setbrowseinfo(int game_type, int progression) void server_kick(int client_id, const char *reason) { + if(client_id < 0 || client_id > MAX_CLIENTS) + return; + if(clients[client_id].state != SRVCLIENT_STATE_EMPTY) netserver_drop(net, client_id, reason); } @@ -647,7 +650,7 @@ static void server_process_client_packet(NETPACKET *packet) if(config.rcon_password[0] != 0 && strcmp(pw, config.rcon_password) == 0) { dbg_msg("server", "cid=%d rcon='%s'", cid, cmd); - config_set(cmd); + console_execute(cmd); } } else @@ -1039,11 +1042,26 @@ static int server_run() return 0; } +static void server_register_commands() +{ + +} + int main(int argc, char **argv) { + /* init the engine */ dbg_msg("server", "starting..."); - engine_init("Teewars", argc, argv); + engine_init("Teewars"); + + /* register all console commands */ + server_register_commands(); + mods_console_init(); + + /* parse the command line arguments */ + engine_parse_arguments(argc, argv); + + /* run the server */ server_run(); return 0; } |