diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-08-20 16:27:11 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-20 16:27:11 +0300 |
commit | af77eec4f736f989703f81df903a1a7c971fa659 (patch) | |
tree | ecadc9763347f1c6a04507f714e58f37f778533a /src/map/map.c | |
parent | 68947c86d5fe3eb2686c9b3393e3db0df083bb11 (diff) | |
parent | 9e02b4ed69ce857e3bda57c7f1d3b2457559c534 (diff) | |
download | hercules-af77eec4f736f989703f81df903a1a7c971fa659.tar.gz hercules-af77eec4f736f989703f81df903a1a7c971fa659.tar.bz2 hercules-af77eec4f736f989703f81df903a1a7c971fa659.tar.xz hercules-af77eec4f736f989703f81df903a1a7c971fa659.zip |
Merge pull request #1399 from HerculesWS/settings_libconfig
Ported the configuration to libconfig format
Diffstat (limited to 'src/map/map.c')
-rw-r--r-- | src/map/map.c | 759 |
1 files changed, 533 insertions, 226 deletions
diff --git a/src/map/map.c b/src/map/map.c index d6425b94e..40bcd9263 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -79,6 +79,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #ifndef _WIN32 #include <unistd.h> #endif @@ -3330,20 +3331,42 @@ int map_readfromcache(struct map_data *m, char *buffer) { return 0; // Not found } -int map_addmap(const char* mapname) { +/** + * Adds a new empty map to the map list. + * + * Assumes that there's enough space in the map list. + * + * @param mapname The new map's name. + * @return success state. + */ +int map_addmap(const char *mapname) +{ map->list[map->count].instance_id = -1; mapindex->getmapname(mapname, map->list[map->count++].name); return 0; } -void map_delmapid(int id) { +/** + * Removes a map from the map list. + * + * @param id The map ID. + */ +void map_delmapid(int id) +{ Assert_retv(id >= 0 && id < map->count); ShowNotice("Removing map [ %s ] from maplist"CL_CLL"\n",map->list[id].name); memmove(map->list+id, map->list+id+1, sizeof(map->list[0])*(map->count-id-1)); map->count--; } -int map_delmap(char* mapname) { +/** + * Removes a map fromt he map list. + * + * @param mapname The name of the map to remove. + * @return the number of removed maps. + */ +int map_delmap(const char *mapname) +{ int i; char map_name[MAP_NAME_LENGTH]; @@ -3798,181 +3821,383 @@ int map_readallmaps (void) { return 0; } -/*========================================== - * Read map server configuration files (conf/map_server.conf...) - *------------------------------------------*/ -int map_config_read(char *cfgName) { - char line[1024], w1[1024], w2[1024]; - FILE *fp; +/** + * Reads 'map_configuration/console' and initializes required variables. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read_console(const char *filename, struct config_t *config, bool imported) +{ + struct config_setting_t *setting = NULL; - nullpo_retr(1, cfgName); + nullpo_retr(false, filename); + nullpo_retr(false, config); - fp = fopen(cfgName,"r"); - if( fp == NULL ) { - ShowError("Map configuration file not found at: %s\n", cfgName); - return 1; + if ((setting = libconfig->lookup(config, "map_configuration/console")) == NULL) { + if (imported) + return true; + ShowError("map_config_read: map_configuration/console was not found in %s!\n", filename); + return false; } - while (fgets(line, sizeof(line), fp)) { - char* ptr; + libconfig->setting_lookup_bool_real(setting, "stdout_with_ansisequence", &showmsg->stdout_with_ansisequence); + if (libconfig->setting_lookup_int(setting, "console_silent", &showmsg->silent) == CONFIG_TRUE) { + if (showmsg->silent) // only bother if its actually enabled + ShowInfo("Console Silent Setting: %d\n", showmsg->silent); + } + libconfig->setting_lookup_mutable_string(setting, "timestamp_format", showmsg->timestamp_format, sizeof(showmsg->timestamp_format)); + libconfig->setting_lookup_int(setting, "console_msg_log", &showmsg->console_log); - if (line[0] == '/' && line[1] == '/') - continue; - if ((ptr = strstr(line, "//")) != NULL) - *ptr = '\n'; //Strip comments - if (sscanf(line, "%1023[^:]: %1023[^\t\r\n]", w1, w2) < 2) - continue; + return true; +} - //Strip trailing spaces - ptr = w2 + strlen(w2); - while (--ptr >= w2 && *ptr == ' '); - ptr++; - *ptr = '\0'; - - if(strcmpi(w1,"timestamp_format")==0) - safestrncpy(showmsg->timestamp_format, w2, 20); - else if(strcmpi(w1,"stdout_with_ansisequence")==0) - showmsg->stdout_with_ansisequence = config_switch(w2) ? true : false; - else if(strcmpi(w1,"console_silent")==0) { - showmsg->silent = atoi(w2); - if (showmsg->silent) // only bother if its actually enabled - ShowInfo("Console Silent Setting: %d\n", atoi(w2)); - } else if (strcmpi(w1, "userid")==0) - chrif->setuserid(w2); - else if (strcmpi(w1, "passwd") == 0) - chrif->setpasswd(w2); - else if (strcmpi(w1, "char_ip") == 0) - map->char_ip_set = chrif->setip(w2); - else if (strcmpi(w1, "char_port") == 0) - chrif->setport(atoi(w2)); - else if (strcmpi(w1, "map_ip") == 0) - map->ip_set = clif->setip(w2); - else if (strcmpi(w1, "bind_ip") == 0) - clif->setbindip(w2); - else if (strcmpi(w1, "map_port") == 0) { - clif->setport(atoi(w2)); - map->port = (atoi(w2)); - } else if (strcmpi(w1, "map") == 0) - map->count++; - else if (strcmpi(w1, "delmap") == 0) - map->count--; - else if (strcmpi(w1, "npc") == 0) - npc->addsrcfile(w2); - else if (strcmpi(w1, "delnpc") == 0) - npc->delsrcfile(w2); - else if (strcmpi(w1, "autosave_time") == 0) { - map->autosave_interval = atoi(w2); - if (map->autosave_interval < 1) //Revert to default saving. - map->autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; - else - map->autosave_interval *= 1000; //Pass from sec to ms - } else if (strcmpi(w1, "minsave_time") == 0) { - map->minsave_interval= atoi(w2); - if (map->minsave_interval < 1) - map->minsave_interval = 1; - } else if (strcmpi(w1, "save_settings") == 0) - map->save_settings = atoi(w2); - else if (strcmpi(w1, "help_txt") == 0) - strcpy(map->help_txt, w2); - else if (strcmpi(w1, "help2_txt") == 0) - strcpy(map->help2_txt, w2); - else if (strcmpi(w1, "charhelp_txt") == 0) - strcpy(map->charhelp_txt, w2); - else if(strcmpi(w1,"db_path") == 0) - safestrncpy(map->db_path,w2,255); - else if (strcmpi(w1, "enable_spy") == 0) - map->enable_spy = config_switch(w2); - else if (strcmpi(w1, "use_grf") == 0) - map->enable_grf = config_switch(w2); - else if (strcmpi(w1, "console_msg_log") == 0) - showmsg->console_log = atoi(w2);//[Ind] - else if (strcmpi(w1, "default_language") == 0) - safestrncpy(map->default_lang_str, w2, sizeof(map->default_lang_str)); - else if (strcmpi(w1, "import") == 0) - map->config_read(w2); +/** + * Reads 'map_configuration/sql_connection' and initializes required variables. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read_connection(const char *filename, struct config_t *config, bool imported) +{ + struct config_setting_t *setting = NULL; + + nullpo_retr(false, filename); + nullpo_retr(false, config); + + if ((setting = libconfig->lookup(config, "map_configuration/sql_connection")) == NULL) { + if (imported) + return true; + ShowError("map_config_read: map_configuration/sql_connection was not found in %s!\n", filename); + ShowWarning("map_config_read_connection: Defaulting sql_connection...\n"); + return false; + } + + libconfig->setting_lookup_int(setting, "db_port", &map->server_port); + libconfig->setting_lookup_mutable_string(setting, "db_hostname", map->server_ip, sizeof(map->server_ip)); + libconfig->setting_lookup_mutable_string(setting, "db_username", map->server_id, sizeof(map->server_id)); + libconfig->setting_lookup_mutable_string(setting, "db_password", map->server_pw, sizeof(map->server_pw)); + libconfig->setting_lookup_mutable_string(setting, "db_database", map->server_db, sizeof(map->server_db)); + libconfig->setting_lookup_mutable_string(setting, "default_codepage", map->default_codepage, sizeof(map->default_codepage)); + return true; +} + +/** + * Reads 'map_configuration/inter' and initializes required variables. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read_inter(const char *filename, struct config_t *config, bool imported) +{ + struct config_setting_t *setting = NULL; + const char *str = NULL; + char temp[24]; + uint16 port; + + nullpo_retr(false, filename); + nullpo_retr(false, config); + + if ((setting = libconfig->lookup(config, "map_configuration/inter")) == NULL) { + if (imported) + return true; + ShowError("map_config_read: map_configuration/inter was not found in %s!\n", filename); + return false; + } + + // Login information + if (libconfig->setting_lookup_mutable_string(setting, "userid", temp, sizeof(temp)) == CONFIG_TRUE) + chrif->setuserid(temp); + if (libconfig->setting_lookup_mutable_string(setting, "passwd", temp, sizeof(temp)) == CONFIG_TRUE) + chrif->setpasswd(temp); + + // Char and map-server information + if (libconfig->setting_lookup_string(setting, "char_ip", &str) == CONFIG_TRUE) + map->char_ip_set = chrif->setip(str); + if (libconfig->setting_lookup_uint16(setting, "char_port", &port) == CONFIG_TRUE) + chrif->setport(port); + + if (libconfig->setting_lookup_string(setting, "map_ip", &str) == CONFIG_TRUE) + map->ip_set = clif->setip(str); + if (libconfig->setting_lookup_uint16(setting, "map_port", &port) == CONFIG_TRUE) { + clif->setport(port); + map->port = port; + } + if (libconfig->setting_lookup_string(setting, "bind_ip", &str) == CONFIG_TRUE) + clif->setbindip(str); + + return true; +} + +/** + * Reads 'map_configuration/database' and initializes required variables + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read_database(const char *filename, struct config_t *config, bool imported) +{ + struct config_setting_t *setting = NULL; + + nullpo_retr(false, filename); + nullpo_retr(false, config); + + if ((setting = libconfig->lookup(config, "map_configuration/database")) == NULL) { + if (imported) + return true; + ShowError("map_config_read: map_configuration/database was not found in %s!\n", filename); + return false; + } + libconfig->setting_lookup_mutable_string(setting, "db_path", map->db_path, sizeof(map->db_path)); + libconfig->setting_lookup_int(setting, "save_settings", &map->save_settings); + + if (libconfig->setting_lookup_int(setting, "autosave_time", &map->autosave_interval) == CONFIG_TRUE) { + if (map->autosave_interval < 1) // Revert to default saving + map->autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; else - ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName); + map->autosave_interval *= 1000; // Pass from s to ms + } + if (libconfig->setting_lookup_int(setting, "minsave_time", &map->minsave_interval) == CONFIG_TRUE) { + if (map->minsave_interval < 1) + map->minsave_interval = 1; } - fclose(fp); - return 0; + return true; } -int map_config_read_sub(char *cfgName) { - char line[1024], w1[1024], w2[1024]; - FILE *fp; - nullpo_retr(1, cfgName); - fp = fopen(cfgName,"r"); - if (fp == NULL) { - ShowError("Map configuration file not found at: %s\n", cfgName); - return 1; +/** + * Reads 'map_configuration/map_list'/'map_configuration/map_removed' and adds + * or removes maps from map-server. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read_map_list(const char *filename, struct config_t *config, bool imported) +{ + struct config_setting_t *setting = NULL; + int i, count = 0; + struct DBMap *deleted_maps; + + nullpo_retr(false, filename); + nullpo_retr(false, config); + + deleted_maps = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_KEY, MAP_NAME_LENGTH); + + // Remove maps + if ((setting = libconfig->lookup(config, "map_configuration/map_removed")) != NULL) { + count = libconfig->setting_length(setting); + for (i = 0; i < count; i++) { + const char *mapname; + + if ((mapname = libconfig->setting_get_string_elem(setting, i)) == NULL || mapname[0] == '\0') + continue; + + strdb_put(deleted_maps, mapname, NULL); + + if (imported) // Map list is empty on the first run, only do this for imported files. + map->delmap(mapname); + } + } + + if ((setting = libconfig->lookup(config, "map_configuration/map_list")) == NULL) { + db_destroy(deleted_maps); + if (imported) + return true; + ShowError("map_config_read_map_list: map_configuration/map_list was not found in %s!\n", filename); + return false; } - while (fgets(line, sizeof(line), fp)) { - char* ptr; + // Add maps to map->list + count = libconfig->setting_length(setting); - if (line[0] == '/' && line[1] == '/') + if (count <= 0) { + db_destroy(deleted_maps); + if (imported) + return true; + ShowWarning("map_config_read_map_list: no maps found in %s!\n", filename); + return false; + } + + RECREATE(map->list, struct map_data, map->count + count); // TODO: VECTOR candidate + + for (i = 0; i < count; i++) { + const char *mapname; + + if ((mapname = libconfig->setting_get_string_elem(setting, i)) == NULL || mapname[0] == '\0') continue; - if ((ptr = strstr(line, "//")) != NULL) - *ptr = '\n'; //Strip comments - if (sscanf(line, "%1023[^:]: %1023[^\t\r\n]", w1, w2) < 2) + + if (strdb_exists(deleted_maps, mapname)) continue; - //Strip trailing spaces - ptr = w2 + strlen(w2); - while (--ptr >= w2 && *ptr == ' '); - ptr++; - *ptr = '\0'; + map->addmap(mapname); + } + + RECREATE(map->list, struct map_data, map->count); + + db_destroy(deleted_maps); + return true; +} + +/** + * Reads map-server configuration files (map-server.conf) and initialises + * required variables. + * + * @param filename Path to configuration file. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_config_read(const char *filename, bool imported) +{ + struct config_t config; + struct config_setting_t *setting = NULL; + const char *import = NULL; + bool retval = true; + + nullpo_retr(false, filename); + + if (!libconfig->load_file(&config, filename)) + return false; - if (strcmpi(w1, "map") == 0) - map->addmap(w2); - else if (strcmpi(w1, "delmap") == 0) - map->delmap(w2); - else if (strcmpi(w1, "import") == 0) - map->config_read_sub(w2); + if ((setting = libconfig->lookup(&config, "map_configuration")) == NULL) { + libconfig->destroy(&config); + if (imported) + return true; + ShowError("map_config_read: map_configuration was not found in %s!\n", filename); + return false; } - fclose(fp); - return 0; + libconfig->setting_lookup_mutable_string(setting, "help_txt", map->help_txt, sizeof(map->help_txt)); + libconfig->setting_lookup_mutable_string(setting, "charhelp_txt", map->charhelp_txt, sizeof(map->charhelp_txt)); + libconfig->setting_lookup_bool(setting, "enable_spy", &map->enable_spy); + libconfig->setting_lookup_bool(setting, "use_grf", &map->enable_grf); + libconfig->setting_lookup_mutable_string(setting, "default_language", map->default_lang_str, sizeof(map->default_lang_str)); + + if (!map_config_read_console(filename, &config, imported)) + retval = false; + if (!map_config_read_connection(filename, &config, imported)) + retval = false; + if (!map_config_read_inter(filename, &config, imported)) + retval = false; + if (!map_config_read_database(filename, &config, imported)) + retval = false; + if (!map_config_read_map_list(filename, &config, imported)) + retval = false; + + // import should overwrite any previous configuration, so it should be called last + if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) { + if (strcmp(import, filename) == 0 || strcmp(import, map->MAP_CONF_NAME) == 0) { + ShowWarning("map_config_read: Loop detected! Skipping 'import'...\n"); + } else { + if (!map->config_read(import, true)) + retval = false; + } + } + + libconfig->destroy(&config); + return retval; } -void map_reloadnpc_sub(char *cfgName) { - char line[1024], w1[1024], w2[1024]; - FILE *fp; - nullpo_retv(cfgName); - fp = fopen(cfgName,"r"); - if (fp == NULL) { - ShowError("Map configuration file not found at: %s\n", cfgName); - return; +/** + * Reads 'npc_global_list'/'npc_removed_list' and adds or removes NPC sources + * from map-server. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool map_read_npclist(const char *filename, bool imported) +{ + struct config_t config; + struct config_setting_t *setting = NULL; + const char *import = NULL; + bool retval = true; + + struct DBMap *deleted_npcs; + + nullpo_retr(false, filename); + + if (!libconfig->load_file(&config, filename)) + return false; + + deleted_npcs = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_KEY, MAP_NAME_LENGTH); + + // Remove maps + if ((setting = libconfig->lookup(&config, "npc_removed_list")) != NULL) { + int i, del_count = libconfig->setting_length(setting); + for (i = 0; i < del_count; i++) { + const char *scriptname; + + if ((scriptname = libconfig->setting_get_string_elem(setting, i)) == NULL || scriptname[0] == '\0') + continue; + + strdb_put(deleted_npcs, scriptname, NULL); + + if (imported) // Map list is empty on the first run, only do this for imported files. + npc->delsrcfile(scriptname); + } } - while (fgets(line, sizeof(line), fp)) { - char* ptr; + if ((setting = libconfig->lookup(&config, "npc_global_list")) != NULL) { + int i, count = libconfig->setting_length(setting); + if (count <= 0) { + if (!imported) { + ShowWarning("map_read_npclist: no NPCs found in %s!\n", filename); + retval = false; + } + } + for (i = 0; i < count; i++) { + const char *scriptname; - if (line[0] == '/' && line[1] == '/') - continue; - if ((ptr = strstr(line, "//")) != NULL) - *ptr = '\n'; //Strip comments - if (sscanf(line, "%1023[^:]: %1023[^\t\r\n]", w1, w2) < 2) - continue; + if ((scriptname = libconfig->setting_get_string_elem(setting, i)) == NULL || scriptname[0] == '\0') + continue; - //Strip trailing spaces - ptr = w2 + strlen(w2); - while (--ptr >= w2 && *ptr == ' '); - ptr++; - *ptr = '\0'; - - if (strcmpi(w1, "npc") == 0) - npc->addsrcfile(w2); - else if (strcmpi(w1, "import") == 0) - map->reloadnpc_sub(w2); - else if (strcmpi(w1, "delnpc") == 0) - npc->delsrcfile(w2); - else - ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName); + if (strdb_exists(deleted_npcs, scriptname)) + continue; + + npc->addsrcfile(scriptname); + } + } else { + ShowError("map_read_npclist: npc_global_list was not found in %s!\n", filename); + retval = false; } - fclose(fp); + db_destroy(deleted_npcs); + + // import should overwrite any previous configuration, so it should be called last + if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) { + const char *base_npclist = NULL; +#ifdef RENEWAL + base_npclist = "npc/re/scripts_main.conf"; +#else + base_npclist = "npc/pre-re/scripts_main.conf"; +#endif + if (strcmp(import, filename) == 0 || strcmp(import, base_npclist) == 0) { + ShowWarning("map_read_npclist: Loop detected! Skipping 'import'...\n"); + } else { + if (!map->read_npclist(import, true)) + retval = false; + } + } + + libconfig->destroy(&config); + return retval; } /** @@ -3986,9 +4211,9 @@ void map_reloadnpc(bool clear) { npc->addsrcfile("clear"); // this will clear the current script list #ifdef RENEWAL - map->reloadnpc_sub("npc/re/scripts_main.conf"); + map->read_npclist("npc/re/scripts_main.conf", false); #else - map->reloadnpc_sub("npc/pre-re/scripts_main.conf"); + map->read_npclist("npc/pre-re/scripts_main.conf", false); #endif // Append extra scripts @@ -3997,63 +4222,125 @@ void map_reloadnpc(bool clear) { } } -int inter_config_read(char *cfgName) { - char line[1024],w1[1024],w2[1024]; - FILE *fp; +/** + * Reads inter-server.conf and initializes required variables. + * + * @param filename Path to configuration file + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool inter_config_read(const char *filename, bool imported) +{ + struct config_t config; + const struct config_setting_t *setting = NULL; + const char *import = NULL; + bool retval = true; + + nullpo_retr(false, filename); + + if (!libconfig->load_file(&config, filename)) + return false; + + if ((setting = libconfig->lookup(&config, "inter_configuration")) == NULL) { + libconfig->destroy(&config); + if (imported) + return true; + ShowError("inter_config_read: inter_configuration was not found in %s!\n", filename); + return false; + } + + if (!map->inter_config_read_database_names(filename, &config, imported)) + retval = false; + if (!map->inter_config_read_connection(filename, &config, imported)) + retval = false; + + // TODO HPM->parseConf(w1, w2, HPCT_MAP_INTER); + + // import should overwrite any previous configuration, so it should be called last + if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) { + if (strcmp(import, filename) == 0 || strcmp(import, map->INTER_CONF_NAME) == 0) { + ShowWarning("inter_config_read: Loop detected in %s! Skipping 'import'...\n", filename); + } else { + if (!map->inter_config_read(import, true)) + retval = false; + } + } + + libconfig->destroy(&config); + return retval; +} + +/** + * Reads the 'inter_configuration/log/sql_connection' config entry and initializes required variables. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool inter_config_read_connection(const char *filename, const struct config_t *config, bool imported) +{ + const struct config_setting_t *setting = NULL; - nullpo_retr(1, cfgName); - if (!(fp = fopen(cfgName,"r"))) { - ShowError("File not found: %s\n",cfgName); - return 1; + nullpo_retr(false, filename); + nullpo_retr(false, config); + + if ((setting = libconfig->lookup(config, "inter_configuration/log/sql_connection")) == NULL) { + if (imported) + return true; + ShowError("inter_config_read: inter_configuration/log/sql_connection was not found in %s!\n", filename); + return false; } - while (fgets(line, sizeof(line), fp)) { - if (line[0] == '/' && line[1] == '/') - continue; - if (sscanf(line,"%1023[^:]: %1023[^\r\n]", w1, w2) < 2) - continue; - /* map sql stuff */ - if(strcmpi(w1,"map_server_ip")==0) - safestrncpy(map->server_ip, w2, sizeof(map->server_ip)); - else if(strcmpi(w1,"map_server_port")==0) - map->server_port=atoi(w2); - else if(strcmpi(w1,"map_server_id")==0) - safestrncpy(map->server_id, w2, sizeof(map->server_id)); - else if(strcmpi(w1,"map_server_pw")==0) - safestrncpy(map->server_pw, w2, sizeof(map->server_pw)); - else if(strcmpi(w1,"map_server_db")==0) - safestrncpy(map->server_db, w2, sizeof(map->server_db)); - else if(strcmpi(w1,"default_codepage")==0) - safestrncpy(map->default_codepage, w2, sizeof(map->default_codepage)); - else if(strcmpi(w1,"autotrade_merchants_db")==0) - safestrncpy(map->autotrade_merchants_db, w2, sizeof(map->autotrade_merchants_db)); - else if(strcmpi(w1,"autotrade_data_db")==0) - safestrncpy(map->autotrade_data_db, w2, sizeof(map->autotrade_data_db)); - else if(strcmpi(w1,"npc_market_data_db")==0) - safestrncpy(map->npc_market_data_db, w2, sizeof(map->npc_market_data_db)); - /* sql log db */ - else if(strcmpi(w1,"log_db_ip")==0) - safestrncpy(logs->db_ip, w2, sizeof(logs->db_ip)); - else if(strcmpi(w1,"log_db_id")==0) - safestrncpy(logs->db_id, w2, sizeof(logs->db_id)); - else if(strcmpi(w1,"log_db_pw")==0) - safestrncpy(logs->db_pw, w2, sizeof(logs->db_pw)); - else if(strcmpi(w1,"log_db_port")==0) - logs->db_port = atoi(w2); - else if(strcmpi(w1,"log_db_db")==0) - safestrncpy(logs->db_name, w2, sizeof(logs->db_name)); - /* mapreg */ - else if( mapreg->config_read(w1,w2) ) - continue; - /* import */ - else if(strcmpi(w1,"import")==0) - map->inter_config_read(w2); - else - HPM->parseConf(w1, w2, HPCT_MAP_INTER); + libconfig->setting_lookup_int(setting, "port", &logs->db_port); + libconfig->setting_lookup_mutable_string(setting, "db_hostname", logs->db_ip, sizeof(logs->db_ip)); + libconfig->setting_lookup_mutable_string(setting, "db_username", logs->db_id, sizeof(logs->db_id)); + libconfig->setting_lookup_mutable_string(setting, "db_password", logs->db_pw, sizeof(logs->db_pw)); + libconfig->setting_lookup_mutable_string(setting, "db_database", logs->db_name, sizeof(logs->db_name)); + + return true; +} + +/** + * Reads the 'inter_configuration/database_names' config entry and initializes required variables. + * + * @param filename Path to configuration file (used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * + * @retval false in case of error. + */ +bool inter_config_read_database_names(const char *filename, const struct config_t *config, bool imported) +{ + const struct config_setting_t *setting = NULL; + bool retval = true; + + nullpo_retr(false, filename); + nullpo_retr(false, config); + + if ((setting = libconfig->lookup(config, "inter_configuration/database_names")) == NULL) { + if (imported) + return true; + ShowError("inter_config_read: inter_configuration/database_names was not found in %s!\n", filename); + return false; } - fclose(fp); - return 0; + libconfig->setting_lookup_mutable_string(setting, "autotrade_merchants_db", map->autotrade_merchants_db, sizeof(map->autotrade_merchants_db)); + libconfig->setting_lookup_mutable_string(setting, "autotrade_data_db", map->autotrade_data_db, sizeof(map->autotrade_data_db)); + libconfig->setting_lookup_mutable_string(setting, "npc_market_data_db", map->npc_market_data_db, sizeof(map->npc_market_data_db)); + + if (!mapreg->config_read(filename, setting, imported)) + retval = false; + + if ((setting = libconfig->lookup(config, "inter_configuration/database_names/registry")) == NULL) { + if (imported) + return retval; + ShowError("inter_config_read: inter_configuration/database_names/registry was not found in %s!\n", filename); + return false; + } + return retval; } /*======================================= @@ -6077,12 +6364,12 @@ int do_init(int argc, char *argv[]) map_load_defaults(); - map->INTER_CONF_NAME = aStrdup("conf/inter-server.conf"); - map->LOG_CONF_NAME = aStrdup("conf/logs.conf"); - map->MAP_CONF_NAME = aStrdup("conf/map-server.conf"); - map->BATTLE_CONF_FILENAME = aStrdup("conf/battle.conf"); + map->INTER_CONF_NAME = aStrdup("conf/common/inter-server.conf"); + map->LOG_CONF_NAME = aStrdup("conf/map/logs.conf"); + map->MAP_CONF_NAME = aStrdup("conf/map/map-server.conf"); + map->BATTLE_CONF_FILENAME = aStrdup("conf/map/battle.conf"); map->ATCOMMAND_CONF_FILENAME = aStrdup("conf/atcommand.conf"); - map->SCRIPT_CONF_NAME = aStrdup("conf/script.conf"); + map->SCRIPT_CONF_NAME = aStrdup("conf/map/script.conf"); map->MSG_CONF_NAME = aStrdup("conf/messages.conf"); map->GRF_PATH_FILENAME = aStrdup("conf/grf-files.txt"); @@ -6095,10 +6382,30 @@ int do_init(int argc, char *argv[]) cmdline->exec(argc, argv, CMDLINE_OPT_NORMAL); minimal = map->minimal;/* temp (perhaps make minimal a mask with options of what to load? e.g. plugin 1 does minimal |= mob_db; */ if (!minimal) { - map->config_read(map->MAP_CONF_NAME); - CREATE(map->list,struct map_data,map->count); - map->count = 0; - map->config_read_sub(map->MAP_CONF_NAME); + map->config_read(map->MAP_CONF_NAME, false); + + { + // TODO: Remove this when no longer needed. +#define CHECK_OLD_LOCAL_CONF(oldname, newname) do { \ + if (stat((oldname), &fileinfo) == 0 && fileinfo.st_size > 0) { \ + ShowWarning("An old configuration file \"%s\" was found.\n", (oldname)); \ + ShowWarning("If it contains settings you wish to keep, please merge them into \"%s\".\n", (newname)); \ + ShowWarning("Otherwise, just delete it.\n"); \ + ShowInfo("Resuming in 10 seconds...\n"); \ + HSleep(10); \ + } \ +} while (0) + struct stat fileinfo; + + CHECK_OLD_LOCAL_CONF("conf/import/map_conf.txt", "conf/import/map-server.conf"); + CHECK_OLD_LOCAL_CONF("conf/import/inter_conf.txt", "conf/import/inter-server.conf"); + CHECK_OLD_LOCAL_CONF("conf/import/log_conf.txt", "conf/import/logs.conf"); + CHECK_OLD_LOCAL_CONF("conf/import/script_conf.txt", "conf/import/script.conf"); + CHECK_OLD_LOCAL_CONF("conf/import/packet_conf.txt", "conf/import/socket.conf"); + CHECK_OLD_LOCAL_CONF("conf/import/battle_conf.txt", "conf/import/battle.conf"); + +#undef CHECK_OLD_LOCAL_CONF + } // loads npcs map->reloadnpc(false); @@ -6110,7 +6417,7 @@ int do_init(int argc, char *argv[]) sockt->ip2str(sockt->addr_[0], ip_str); #ifndef BUILDBOT - ShowWarning("Not all IP addresses in /conf/map-server.conf configured, auto-detecting...\n"); + ShowWarning("Not all IP addresses in /conf/map/map-server.conf configured, auto-detecting...\n"); #endif if (sockt->naddr_ == 0) @@ -6126,12 +6433,12 @@ int do_init(int argc, char *argv[]) chrif->setip(ip_str); } - battle->config_read(map->BATTLE_CONF_FILENAME); + battle->config_read(map->BATTLE_CONF_FILENAME, false); atcommand->msg_read(map->MSG_CONF_NAME, false); - map->inter_config_read(map->INTER_CONF_NAME); - logs->config_read(map->LOG_CONF_NAME); + map->inter_config_read(map->INTER_CONF_NAME, false); + logs->config_read(map->LOG_CONF_NAME, false); } - script->config_read(map->SCRIPT_CONF_NAME); + script->config_read(map->SCRIPT_CONF_NAME, false); map->id_db = idb_alloc(DB_OPT_BASE); map->pc_db = idb_alloc(DB_OPT_BASE); //Added for reliable map->id2sd() use. [Skotlex] @@ -6275,7 +6582,6 @@ void map_defaults(void) { sprintf(map->db_path ,"db"); sprintf(map->help_txt ,"conf/help.txt"); - sprintf(map->help2_txt ,"conf/help2.txt"); sprintf(map->charhelp_txt ,"conf/charhelp.txt"); sprintf(map->wisp_server_name ,"Server"); // can be modified in char-server configuration file @@ -6288,12 +6594,12 @@ void map_defaults(void) { map->night_flag = 0; // 0=day, 1=night [Yor] map->enable_spy = 0; //To enable/disable @spy commands, which consume too much cpu time when sending packets. [Skotlex] - map->INTER_CONF_NAME="conf/inter-server.conf"; - map->LOG_CONF_NAME="conf/logs.conf"; - map->MAP_CONF_NAME = "conf/map-server.conf"; - map->BATTLE_CONF_FILENAME = "conf/battle.conf"; + map->INTER_CONF_NAME="conf/common/inter-server.conf"; + map->LOG_CONF_NAME="conf/map/logs.conf"; + map->MAP_CONF_NAME = "conf/map/map-server.conf"; + map->BATTLE_CONF_FILENAME = "conf/map/battle.conf"; map->ATCOMMAND_CONF_FILENAME = "conf/atcommand.conf"; - map->SCRIPT_CONF_NAME = "conf/script.conf"; + map->SCRIPT_CONF_NAME = "conf/map/script.conf"; map->MSG_CONF_NAME = "conf/messages.conf"; map->GRF_PATH_FILENAME = "conf/grf-files.txt"; @@ -6499,9 +6805,10 @@ void map_defaults(void) { map->readgat = map_readgat; map->readallmaps = map_readallmaps; map->config_read = map_config_read; - map->config_read_sub = map_config_read_sub; - map->reloadnpc_sub = map_reloadnpc_sub; + map->read_npclist = map_read_npclist; map->inter_config_read = inter_config_read; + map->inter_config_read_database_names = inter_config_read_database_names; + map->inter_config_read_connection = inter_config_read_connection; map->sql_init = map_sql_init; map->sql_close = map_sql_close; map->zone_mf_cache = map_zone_mf_cache; |