From ce08d6238d902590dbfb650f889a8ab8887356bf Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 9 Nov 2014 02:07:09 +0100 Subject: Command line arguments handling overhaul - login_server, char_server, map_server as well as the tools (mapcache) now have a common command line arguments handling mechanism. - All of them now accept `--help` (`-h`), `--version` (`-v`) and `--load-plugin`. - login_server now accepts `--login-config` and `--lan-config` instead of relying on positional arguments to override those files. The old syntax will no longer work, please update your custom startup scripts. - char_server now accepts `--char-config`, `--inter-config`, `--lan-config` instead of relying on positional arguments. The old syntax will no longer work, please update your custom startup scripts. - mapcache now accepts `--grf-list`, `--map-list`, `--map-cache`, `--rebuild` in place of, respectively, `-grf`, `-list`, `-cache`, `-rebuild`. - A new macro `CMDLINEARG()` is provided, to help defining new command line argument handlers (i.e. in plugins). the `addArg()` call is still required, but its syntax has changed. The `help` argument is now of type `const char *` rather than a function pointer, and it is supposed to contain the message to show in the `--help` screen. Pass `NULL` if no help message is desired. Signed-off-by: Haru --- src/char/char.c | 90 +++++++++++++++++++++++++++++++++++++++----------------- src/char/char.h | 5 ++++ src/char/inter.h | 2 -- 3 files changed, 68 insertions(+), 29 deletions(-) (limited to 'src/char') diff --git a/src/char/char.c b/src/char/char.c index f29bf8cff..1301f2cc4 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -50,10 +50,6 @@ #endif // private declarations -#define CHAR_CONF_NAME "conf/char-server.conf" -#define LAN_CONF_NAME "conf/subnet.conf" -#define SQL_CONF_NAME "conf/inter-server.conf" - char char_db[256] = "char"; char scdata_db[256] = "sc_data"; char cart_db[256] = "cart_inventory"; @@ -5707,6 +5703,11 @@ int do_final(void) { if( chr->server[i].map ) aFree(chr->server[i].map); + aFree(chr->CHAR_CONF_NAME); + aFree(chr->LAN_CONF_NAME); + aFree(chr->SQL_CONF_NAME); + aFree(chr->INTER_CONF_NAME); + HPM->event(HPET_POST_FINAL); ShowStatus("Finished.\n"); @@ -5763,45 +5764,80 @@ void char_hp_symbols(void) { HPM->share(inter->sql_handle, "sql_handle"); } +/** + * --char-config handler + * + * Overrides the default char configuration file. + * @see cmdline->exec + */ +static CMDLINEARG(charconfig) +{ + aFree(chr->CHAR_CONF_NAME); + chr->CHAR_CONF_NAME = aStrdup(params); + return true; +} +/** + * --inter-config handler + * + * Overrides the default inter-server configuration file. + * @see cmdline->exec + */ +static CMDLINEARG(interconfig) +{ + aFree(chr->INTER_CONF_NAME); + chr->INTER_CONF_NAME = aStrdup(params); + return true; +} +/** + * --lan-config handler + * + * Overrides the default subnet configuration file. + * @see cmdline->exec + */ +static CMDLINEARG(lanconfig) +{ + aFree(chr->LAN_CONF_NAME); + chr->LAN_CONF_NAME = aStrdup(params); + return true; +} +/** + * Initializes the command line arguments handlers. + */ +void cmdline_args_init_local(void) +{ + CMDLINEARG_DEF2(char-config, charconfig, "Alternative char-server configuration.", CMDLINE_OPT_PARAM); + CMDLINEARG_DEF2(inter-config, interconfig, "Alternative inter-server configuration.", CMDLINE_OPT_PARAM); + CMDLINEARG_DEF2(lan-config, lanconfig, "Alternative subnet configuration.", CMDLINE_OPT_PARAM); +} + int do_init(int argc, char **argv) { int i; memset(&skillid2idx, 0, sizeof(skillid2idx)); char_load_defaults(); + chr->CHAR_CONF_NAME = aStrdup("conf/char-server.conf"); + chr->LAN_CONF_NAME = aStrdup("conf/subnet.conf"); + chr->SQL_CONF_NAME = aStrdup("conf/inter-server.conf"); + chr->INTER_CONF_NAME = aStrdup("conf/inter-server.conf"); + for(i = 0; i < MAX_MAP_SERVERS; i++ ) chr->server[i].map = NULL; HPM_char_do_init(); HPM->symbol_defaults_sub = char_hp_symbols; -#if 0 - /* TODO: Move to common code */ - for( i = 1; i < argc; i++ ) { - const char* arg = argv[i]; - if( strcmp(arg, "--load-plugin") == 0 ) { - if( map->arg_next_value(arg, i, argc, true) ) { - RECREATE(load_extras, char *, ++load_extras_count); - load_extras[load_extras_count-1] = argv[++i]; - } - } - } - HPM->config_read((const char * const *)load_extras, load_extras_count); - if (load_extras) { - aFree(load_extras); - load_extras = NULL; - load_extras_count = 0; - } -#endif - HPM->config_read(NULL, 0); + cmdline->exec(argc, argv, CMDLINE_OPT_PREINIT); + HPM->config_read(); HPM->event(HPET_PRE_INIT); //Read map indexes mapindex->init(); start_point.map = mapindex->name2id("new_zone01"); - chr->config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); - chr->lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME); - chr->sql_config_read(SQL_CONF_NAME); + cmdline->exec(argc, argv, CMDLINE_OPT_NORMAL); + chr->config_read(chr->CHAR_CONF_NAME); + chr->lan_config_read(chr->LAN_CONF_NAME); + chr->sql_config_read(chr->SQL_CONF_NAME); if (strcmp(chr->userid, "s1")==0 && strcmp(chr->passwd, "p1")==0) { ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n"); @@ -5809,7 +5845,7 @@ int do_init(int argc, char **argv) { ShowNotice("And then change the user/password to use in conf/char-server.conf (or conf/import/char_conf.txt)\n"); } - inter->init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server configuration + inter->init_sql(chr->INTER_CONF_NAME); // inter server configuration auth_db = idb_alloc(DB_OPT_RELEASE_DATA); chr->online_char_db = idb_alloc(DB_OPT_RELEASE_DATA); diff --git a/src/char/char.h b/src/char/char.h index 4b68dc43c..e27aa0b44 100644 --- a/src/char/char.h +++ b/src/char/char.h @@ -144,6 +144,11 @@ struct char_interface { int server_type; int new_display; + char *CHAR_CONF_NAME; + char *LAN_CONF_NAME; + char *SQL_CONF_NAME; + char *INTER_CONF_NAME; + int (*waiting_disconnect) (int tid, int64 tick, int id, intptr_t data); int (*delete_char_sql) (int char_id); DBData (*create_online_char_data) (DBKey key, va_list args); diff --git a/src/char/inter.h b/src/char/inter.h index 7f5b5fc22..a58d3b3a9 100644 --- a/src/char/inter.h +++ b/src/char/inter.h @@ -11,8 +11,6 @@ struct accreg; -#define inter_cfgName "conf/inter-server.conf" - #ifdef HERCULES_CORE extern unsigned int party_share_level; -- cgit v1.2.3-60-g2f50