summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--conf/common/map-index.conf34
-rw-r--r--src/common/mapindex.c57
-rw-r--r--src/common/mapindex.h4
3 files changed, 95 insertions, 0 deletions
diff --git a/conf/common/map-index.conf b/conf/common/map-index.conf
new file mode 100644
index 000000000..b3a1b4e8f
--- /dev/null
+++ b/conf/common/map-index.conf
@@ -0,0 +1,34 @@
+//================= Hercules Configuration ================================
+//= _ _ _
+//= | | | | | |
+//= | |_| | ___ _ __ ___ _ _| | ___ ___
+//= | _ |/ _ \ '__/ __| | | | |/ _ \/ __|
+//= | | | | __/ | | (__| |_| | | __/\__ \
+//= \_| |_/\___|_| \___|\__,_|_|\___||___/
+//================= License ===============================================
+//= This file is part of Hercules.
+//= http://herc.ws - http://github.com/HerculesWS/Hercules
+//=
+//= Copyright (C) 2014-2019 Hercules Dev Team
+//=
+//= Hercules is free software: you can redistribute it and/or modify
+//= it under the terms of the GNU General Public License as published by
+//= the Free Software Foundation, either version 3 of the License, or
+//= (at your option) any later version.
+//=
+//= This program is distributed in the hope that it will be useful,
+//= but WITHOUT ANY WARRANTY; without even the implied warranty of
+//= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//= GNU General Public License for more details.
+//=
+//= You should have received a copy of the GNU General Public License
+//= along with this program. If not, see <http://www.gnu.org/licenses/>.
+//=========================================================================
+//= Map Index configuration file.
+//=========================================================================
+
+mapindex_configuration: {
+ // Full path to the map_index.txt file
+ // Default: db/map_index.txt
+ file_path: "db/map_index.txt"
+}
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);
/* */