Skip to content
Permalink
Browse files

Merge pull request #2547 from guilherme-gm/201910-dehardcode-db

Dehardcode DB files path
  • Loading branch information...
MishimaHaruna committed Oct 20, 2019
2 parents ddd0e46 + be955e5 commit 275edef51c858e9e55934ec3af92ec19db74f3d3
@@ -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"
}
@@ -120,8 +120,6 @@ char char_achievement_db[256] = "char_achievements";
static struct char_interface char_s;
struct char_interface *chr;

char db_path[1024] = "db";

static char wisp_server_name[NAME_LENGTH] = "Server";
static char login_ip_str[128];
static uint32 login_ip = 0;
@@ -5792,7 +5790,8 @@ static bool char_config_read_database(const char *filename, const struct config_
if (autosave_interval <= 0)
autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
}
libconfig->setting_lookup_mutable_string(setting, "db_path", db_path, sizeof(db_path));
libconfig->setting_lookup_mutable_string(setting, "db_path", chr->db_path, sizeof(chr->db_path));
libconfig->set_db_path(chr->db_path);
libconfig->setting_lookup_bool_real(setting, "log_char", &chr->enable_logs);
return true;
}
@@ -6454,6 +6453,8 @@ void char_defaults(void)
chr = &char_s;

memset(chr->server, 0, sizeof(chr->server));
sprintf(chr->db_path, "db");
libconfig->set_db_path(chr->db_path);

chr->login_fd = 0;
chr->char_fd = -1;
@@ -124,6 +124,8 @@ struct char_interface {
bool show_save_log; ///< Show loading/saving messages.
bool enable_logs; ///< Whether to log char server operations.

char db_path[256]; //< Database directory (db)

int (*waiting_disconnect) (int tid, int64 tick, int id, intptr_t data);
int (*delete_char_sql) (int char_id);
struct DBData (*create_online_char_data) (union DBKey key, va_list args);
@@ -23,6 +23,7 @@
#include "geoip.h"

#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/memmgr.h"
#include "common/showmsg.h"

@@ -141,7 +142,9 @@ static void geoip_init(void)

geoip->data->active = true;

db = fopen("./db/GeoIP.dat","rb");
char file_path[256];
libconfig->format_db_path("GeoIP.dat", file_path, sizeof(file_path));
db = fopen(file_path, "rb");
if (db == NULL) {
ShowError("geoip_readdb: Error reading GeoIP.dat!\n");
geoip->final(false);
@@ -747,7 +747,7 @@ static int inter_guild_sql_init(void)
inter_guild->castle_db = idb_alloc(DB_OPT_RELEASE_DATA);

//Read exp file
sv->readdb("db", DBPATH"exp_guild.txt", ',', 1, 1, MAX_GUILDLEVEL, inter_guild->exp_parse_row);
sv->readdb(chr->db_path, DBPATH"exp_guild.txt", ',', 1, 1, MAX_GUILDLEVEL, inter_guild->exp_parse_row);

timer->add_func_list(inter_guild->save_timer, "inter_guild->save_timer");
timer->add(timer->gettick() + 10000, inter_guild->save_timer, 0, 0);
@@ -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
@@ -32,6 +33,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.
*
@@ -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;
/* */
@@ -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);
/* */
@@ -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;
@@ -233,6 +287,9 @@ void mapindex_defaults(void)
mapindex->default_y = MAP_DEFAULT_Y;
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;
@@ -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);
/* */
@@ -49,10 +49,13 @@
/**
* Path within the /db folder to (non-)renewal specific db files
**/
#define DBPATH_RE "re/"
#define DBPATH_PRE "pre-re/"

#ifdef RENEWAL
#define DBPATH "re/"
#define DBPATH DBPATH_RE
#else
#define DBPATH "pre-re/"
#define DBPATH DBPATH_PRE
#endif

/**
@@ -1099,7 +1099,8 @@ static bool achievement_get_rewards(struct map_session_data *sd, const struct ac
*/
static void achievement_readdb_ranks(void)
{
const char *filename = "db/achievement_rank_db.conf";
char filename[256];
libconfig->format_db_path("achievement_rank_db.conf", filename, sizeof(filename));
struct config_t ar_conf = { 0 };
struct config_setting_t *ardb = NULL, *conf = NULL;
int entry = 0;
@@ -1777,7 +1778,8 @@ static void achievement_readdb_additional_fields(const struct config_setting_t *
*/
static void achievement_readb(void)
{
const char *filename = "db/"DBPATH"achievement_db.conf";
char filename[256];
libconfig->format_db_path(DBPATH"achievement_db.conf", filename, sizeof(filename));
struct config_t ach_conf = { 0 };
struct config_setting_t *achdb = NULL, *conf = NULL;
int entry = 0, count = 0;
@@ -19794,7 +19794,8 @@ static void clif_cashshop_db(void)
{
struct config_t cashshop_conf;
struct config_setting_t *cashshop = NULL, *cats = NULL;
const char *config_filename = "db/cashshop_db.conf"; // FIXME hardcoded name
char config_filename[256];
libconfig->format_db_path("cashshop_db.conf", config_filename, sizeof(config_filename));
int i, item_count_t = 0;
for( i = 0; i < CASHSHOP_TAB_MAX; i++ ) {
CREATE(clif->cs.data[i], struct hCSData *, 1);
@@ -21065,7 +21066,8 @@ static bool clif_parse_roulette_db(void)
{
struct config_t roulette_conf;
struct config_setting_t *roulette = NULL, *levels = NULL;
const char *config_filename = "db/roulette_db.conf"; // FIXME hardcoded name
char config_filename[256];
libconfig->format_db_path("roulette_db.conf", config_filename, sizeof(config_filename));
int i, j, item_count_t = 0;

for( i = 0; i < MAX_ROULETTE_LEVEL; i++ ) {
@@ -22549,7 +22551,8 @@ static bool clif_parse_attendance_db(void)
{
struct config_t attendance_conf;
struct config_setting_t *attendance = NULL, *it = NULL;
const char *config_filename = "db/attendance_db.conf"; // FIXME hardcoded name
char config_filename[256];
libconfig->format_db_path("attendance_db.conf", config_filename, sizeof(config_filename));
int i = 0;

if (!libconfig->load_file(&attendance_conf, config_filename))
@@ -152,7 +152,8 @@ static bool guild_read_castledb_libconfig(void)
{
struct config_t castle_conf;
struct config_setting_t *castle_db = NULL, *it = NULL;
const char *config_filename = "db/castle_db.conf"; // FIXME hardcoded name
char config_filename[256];
libconfig->format_db_path("castle_db.conf", config_filename, sizeof(config_filename));
int i = 0;

if (libconfig->load_file(&castle_conf, config_filename) == 0)
@@ -846,11 +846,8 @@ static void itemdb_read_groups(void)
{
struct config_t item_group_conf;
struct config_setting_t *itg = NULL, *it = NULL;
#ifdef RENEWAL
const char *config_filename = "db/re/item_group.conf"; // FIXME hardcoded name
#else
const char *config_filename = "db/pre-re/item_group.conf"; // FIXME hardcoded name
#endif
char config_filename[256];
libconfig->format_db_path(DBPATH"item_group.conf", config_filename, sizeof(config_filename));
const char *itname;
int i = 0, count = 0, c;
unsigned int *gsize = NULL;
@@ -1144,11 +1141,8 @@ static void itemdb_read_packages(void)
{
struct config_t item_packages_conf;
struct config_setting_t *itg = NULL, *it = NULL, *t = NULL;
#ifdef RENEWAL
const char *config_filename = "db/re/item_packages.conf"; // FIXME hardcoded name
#else
const char *config_filename = "db/pre-re/item_packages.conf"; // FIXME hardcoded name
#endif
char config_filename[256];
libconfig->format_db_path(DBPATH"item_packages.conf", config_filename, sizeof(config_filename));
const char *itname;
int i = 0, count = 0, c = 0, highest_gcount = 0;
unsigned int *must = NULL, *random = NULL, *rgroup = NULL, **rgroups = NULL;
@@ -1393,7 +1387,8 @@ static void itemdb_read_options(void)
struct config_t item_options_db;
struct config_setting_t *ito = NULL, *conf = NULL;
int index = 0, count = 0;
const char *filepath = "db/item_options.conf";
char filepath[256];
libconfig->format_db_path("item_options.conf", filepath, sizeof(filepath));
VECTOR_DECL(int) duplicate_id;

if (!libconfig->load_file(&item_options_db, filepath))
@@ -1494,11 +1489,8 @@ static void itemdb_read_chains(void)
{
struct config_t item_chain_conf;
struct config_setting_t *itc = NULL;
#ifdef RENEWAL
const char *config_filename = "db/re/item_chain.conf"; // FIXME hardcoded name
#else
const char *config_filename = "db/pre-re/item_chain.conf"; // FIXME hardcoded name
#endif
char config_filename[256];
libconfig->format_db_path(DBPATH"item_chain.conf", config_filename, sizeof(config_filename));
int i = 0, count = 0;

if (!libconfig->load_file(&item_chain_conf, config_filename))

0 comments on commit 275edef

Please sign in to comment.
You can’t perform that action at this time.