diff options
author | Haru <haru@dotalux.com> | 2019-10-20 17:43:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-20 17:43:44 +0200 |
commit | 275edef51c858e9e55934ec3af92ec19db74f3d3 (patch) | |
tree | 81c4367dfb4d51fec9c71c2a407f502a514b6a67 /src/common | |
parent | ddd0e461fb8e451912a6aee90869023e2c2d8272 (diff) | |
parent | be955e59bcd950d6430189a8a23a608927665c02 (diff) | |
download | hercules-275edef51c858e9e55934ec3af92ec19db74f3d3.tar.gz hercules-275edef51c858e9e55934ec3af92ec19db74f3d3.tar.bz2 hercules-275edef51c858e9e55934ec3af92ec19db74f3d3.tar.xz hercules-275edef51c858e9e55934ec3af92ec19db74f3d3.zip |
Merge pull request #2547 from guilherme-gm/201910-dehardcode-db
Dehardcode DB files path
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/conf.c | 30 | ||||
-rw-r--r-- | src/common/conf.h | 4 | ||||
-rw-r--r-- | src/common/mapindex.c | 57 | ||||
-rw-r--r-- | src/common/mapindex.h | 4 |
4 files changed, 95 insertions, 0 deletions
diff --git a/src/common/conf.c b/src/common/conf.c index 0ad350057..d81a6636b 100644 --- a/src/common/conf.c +++ b/src/common/conf.c @@ -22,6 +22,7 @@ #include "conf.h" +#include "common/nullpo.h" // nullpo_retv #include "common/showmsg.h" // ShowError #include "common/strlib.h" // safestrncpy #include "common/utils.h" // exists @@ -33,6 +34,31 @@ static struct libconfig_interface libconfig_s; struct libconfig_interface *libconfig; /** + * Sets the server's db_path to be used by config_format_db_path + * @param db_path path to the folder where the db files are at + */ +static void config_set_db_path(const char *db_path) +{ + nullpo_retv(db_path); + safestrncpy(libconfig->db_path, db_path, sizeof(libconfig->db_path)); +} + +/** + * Writes into path_buf the fullpath to the db file in filename. + * Basically this prepends map->db_path to filename. + * @param filename File name to format (e.g. re/item_db.conf) + * @param path_buf Where to save the path to + * @param buffer_len Maximun length of path_buf + */ +static void config_format_db_path(const char *filename, char *path_buf, int buffer_len) +{ + nullpo_retv(filename); + nullpo_retv(path_buf); + + safesnprintf(path_buf, buffer_len, "%s/%s", libconfig->db_path, filename); +} + +/** * Initializes 'config' and loads a configuration file. * * Shows error and destroys 'config' in case of failure. @@ -450,6 +476,10 @@ static int config_lookup_int64_real(const struct config_t *config, const char *f void libconfig_defaults(void) { libconfig = &libconfig_s; + snprintf(libconfig->db_path, sizeof(libconfig->db_path), "db"); + libconfig->set_db_path = config_set_db_path; + libconfig->format_db_path = config_format_db_path; + /* */ libconfig->read = config_read; libconfig->write = config_write; /* */ diff --git a/src/common/conf.h b/src/common/conf.h index 66960c0ec..ccab6dc17 100644 --- a/src/common/conf.h +++ b/src/common/conf.h @@ -29,6 +29,10 @@ * The libconfig interface -- specially for plugins, but we enforce it throughout the core to be consistent **/ struct libconfig_interface { + char db_path[256]; + void (*set_db_path) (const char *db_path); + void (*format_db_path) (const char *filename, char *path_buf, int buffer_len); + /* */ int (*read) (struct config_t *config, FILE *stream); void (*write) (const struct config_t *config, FILE *stream); /* */ diff --git a/src/common/mapindex.c b/src/common/mapindex.c index d5cda5c22..f6097bb06 100644 --- a/src/common/mapindex.c +++ b/src/common/mapindex.c @@ -23,6 +23,7 @@ #include "mapindex.h" #include "common/cbasetypes.h" +#include "common/conf.h" #include "common/db.h" #include "common/mmo.h" #include "common/nullpo.h" @@ -159,8 +160,61 @@ static const char *mapindex_id2name_sub(uint16 id, const char *file, int line, c return mapindex->list[id].name; } +/** + * Reads the db_path config of mapindex configuration file + * @param filename File being read (used when displaying errors) + * @param config Config structure being read + * @returns true if it read the all the configs, false otherwise + */ +static bool mapindex_config_read_dbpath(const char *filename, const struct config_t *config) +{ + nullpo_retr(false, config); + + const struct config_setting_t *setting = NULL; + + if ((setting = libconfig->lookup(config, "mapindex_configuration")) == NULL) { + ShowError("mapindex_config_read: mapindex_configuration was not found in %s!\n", filename); + return false; + } + + // mapindex_configuration/file_path + if (libconfig->setting_lookup_mutable_string(setting, "file_path", mapindex->config_file, sizeof(mapindex->config_file)) == CONFIG_TRUE) { + ShowInfo("map_index file %s\n", mapindex->config_file); + } else { + ShowInfo("Failed to load map_index path, defaulting to db/map_index.txt\n"); + safestrncpy(mapindex->config_file, "db/map_index.txt", sizeof(mapindex->config_file)); + } + + return true; +} + +/** + * Reads conf/common/map-index.conf config file + * @returns true if it successfully read the file and configs, false otherwise + */ +static bool mapindex_config_read(void) +{ + struct config_t config; + const char *filename = "conf/common/map-index.conf"; + + if (!libconfig->load_file(&config, filename)) + return false; // Error message is already shown by libconfig->load_file + + if (!mapindex_config_read_dbpath(filename, &config)) { + libconfig->destroy(&config); + return false; + } + + ShowInfo("Done reading %s.\n", filename); + libconfig->destroy(&config); + return true; +} + static int mapindex_init(void) { + if (!mapindex_config_read()) + ShowError("Failed to load map_index configuration. Continuing with default values...\n"); + FILE *fp; char line[1024]; int last_index = -1; @@ -234,6 +288,9 @@ void mapindex_defaults(void) memset (&mapindex->list, 0, sizeof (mapindex->list)); /* */ + mapindex->config_read = mapindex_config_read; + mapindex->config_read_dbpath = mapindex_config_read_dbpath; + /* */ mapindex->init = mapindex_init; mapindex->final = mapindex_final; /* */ diff --git a/src/common/mapindex.h b/src/common/mapindex.h index 27cbc5e37..b7e02447d 100644 --- a/src/common/mapindex.h +++ b/src/common/mapindex.h @@ -21,6 +21,7 @@ #ifndef COMMON_MAPINDEX_H #define COMMON_MAPINDEX_H +#include "common/conf.h" #include "common/hercules.h" #include "common/mmo.h" @@ -98,6 +99,9 @@ struct mapindex_interface { char name[MAP_NAME_LENGTH]; } list[MAX_MAPINDEX]; /* */ + bool (*config_read_dbpath) (const char *filename, const struct config_t *config); + bool (*config_read) (void); + /* */ int (*init) (void); void (*final) (void); /* */ |