summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSmokexyz <sagunxp@gmail.com>2015-08-29 15:04:04 +0530
committerSmokexyz <sagunxp@gmail.com>2015-10-02 00:42:13 +0530
commit993138361b5d712afbd7afee6aba5d5c52bdcc4f (patch)
treed5572d3a5291044e7f890c8f7b9b160c5a1d733f /src
parentf7a97b556e05373711998d4f819cf7d89e4b0ce8 (diff)
downloadhercules-993138361b5d712afbd7afee6aba5d5c52bdcc4f.tar.gz
hercules-993138361b5d712afbd7afee6aba5d5c52bdcc4f.tar.bz2
hercules-993138361b5d712afbd7afee6aba5d5c52bdcc4f.tar.xz
hercules-993138361b5d712afbd7afee6aba5d5c52bdcc4f.zip
Convert refine_db.txt to refine_db.conf (libconfig)
Thanks for Haru and 4414 for pointing out inefficiency in code, edited as suggested. Rework - Changed refine_db.conf format to a more concise one, thanks to Haru for suggesting. [2/10/15] Updated with corrections pointed out by Haru. Updated warning and error messages.
Diffstat (limited to 'src')
-rw-r--r--src/map/status.c143
-rw-r--r--src/map/status.h3
-rw-r--r--src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc12
-rw-r--r--src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc3
-rw-r--r--src/plugins/HPMHooking/HPMHooking_map.Hooks.inc53
5 files changed, 164 insertions, 50 deletions
diff --git a/src/map/status.c b/src/map/status.c
index 720a02494..4f1813af6 100644
--- a/src/map/status.c
+++ b/src/map/status.c
@@ -35,6 +35,7 @@
#include "common/strlib.h"
#include "common/timer.h"
#include "common/utils.h"
+#include "common/conf.h"
#include <math.h>
#include <memory.h>
@@ -12278,38 +12279,117 @@ bool status_readdb_sizefix(char* fields[], int columns, int current)
return true;
}
-bool status_readdb_refine(char* fields[], int columns, int current)
-{
- int i, bonus_per_level, random_bonus, random_bonus_start_level;
-
- current = atoi(fields[0]);
-
- if (current < 0 || current >= REFINE_TYPE_MAX)
- return false;
-
- bonus_per_level = atoi(fields[1]);
- random_bonus_start_level = atoi(fields[2]);
- random_bonus = atoi(fields[3]);
-
- for(i = 0; i < MAX_REFINE; i++)
- {
- char* delim;
-
- if (!(delim = strchr(fields[4+i], ':')))
- return false;
-
- *delim = '\0';
-
- status->dbs->refine_info[current].chance[i] = atoi(fields[4+i]);
+/**
+ * Processes a refine_db.conf entry.
+ *
+ * @param *r Libconfig setting entry. It is expected to be valid and it
+ * won't be freed (it is care of the caller to do so if
+ * necessary)
+ * @param n Ordinal number of the entry, to be displayed in case of
+ * validation errors.
+ * @param *source Source of the entry (file name), to be displayed in case of
+ * validation errors.
+ * @return # of the validated entry, or 0 in case of failure.
+ */
+int status_readdb_refine_libconfig_sub(config_setting_t *r, const char *name, const char *source) {
+ config_setting_t *rate = NULL;
+ int i=0, type=0, bonus_per_level=0, rnd_bonus_v=0, rnd_bonus_lv=0;
+ char lv[4];
+
+ if(!strncmp(name, "Armors", 6)) {
+ type = 0;
+ } else if(strncmp(name, "WeaponLevel", 11) || !strspn(&name[strlen(name)-1], "0123456789") || !(type = atoi(strncpy(lv, name+11, 2)))) {
+ ShowError("status_readdb_refine_libconfig_sub: Invalid key name for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ }
+ if(type < REFINE_TYPE_ARMOR || type >= REFINE_TYPE_MAX) {
+ ShowError("status_readdb_refine_libconfig_sub: Out of range level for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ } else if(!libconfig->setting_lookup_int(r, "StatsPerLevel", &bonus_per_level)) {
+ ShowWarning("status_readdb_refine_libconfig_sub: Missing StatsPerLevel for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ } else if(!libconfig->setting_lookup_int(r, "RandomBonusStartLevel", &rnd_bonus_lv)) {
+ ShowWarning("status_readdb_refine_libconfig_sub: Missing RandomBonusStartLevel for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ } else if(!libconfig->setting_lookup_int(r, "RandomBonusValue", &rnd_bonus_v)) {
+ ShowWarning("status_readdb_refine_libconfig_sub: Missing RandomBonusValue for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ }
- if (i >= random_bonus_start_level - 1)
- status->dbs->refine_info[current].randombonus_max[i] = random_bonus * (i - random_bonus_start_level + 2);
+ if((rate=libconfig->setting_get_member(r, "Rates")) && config_setting_is_group(rate)) {
+ config_setting_t *t = NULL;
+ bool duplicate[MAX_REFINE];
+ int bonus_[MAX_REFINE], rnd_bonus[MAX_REFINE], chance_[MAX_REFINE];
+
+ memset(&duplicate,0,sizeof(duplicate));
+ memset(&bonus_,0,sizeof(bonus_));
+ memset(&rnd_bonus,0,sizeof(rnd_bonus));
+ memset(&chance_,0,sizeof(chance_));
+ i=0;
+ while( (t = libconfig->setting_get_elem(rate,i++)) && config_setting_is_group(t) ) {
+ int level=0, chance=0, bonus=0;
+ char *rlvl = config_setting_name(t);
+ memset(&lv,0, sizeof(lv));
+ if(!strspn(&rlvl[strlen(rlvl)-1], "0123456789") || (level = atoi(strncpy(lv, rlvl+2, 3))) <= 0) {
+ ShowError("status_readdb_refine_libconfig_sub: Invalid refine level format '%s' for entry %s in \"%s\"... skipping.\n", rlvl, name, source);
+ continue;
+ } else if(level <= 0 || level > MAX_REFINE) {
+ ShowError("status_readdb_refine_libconfig_sub: Out of range refine level '%s' for entry %s in \"%s\"... skipping.\n", rlvl, name, source);
+ continue;
+ } else if(duplicate[level-1]) {
+ ShowWarning("status_readdb_refine_libconfig_sub: duplicate rate '%s' for entry %s in \"%s\", overwriting previous entry...\n", rlvl, name, source);
+ } else duplicate[level-1] = true;
+ level--;
+ chance_[level] = libconfig->setting_lookup_int(t, "Chance", &chance)?chance:100;
+ bonus_[level] = bonus_per_level + libconfig->setting_lookup_int(t, "Bonus", &bonus)?bonus:0;
+ if ( level >= rnd_bonus_lv - 1 ) rnd_bonus[level] = rnd_bonus_v * (level - rnd_bonus_lv + 2);
+ }
+ for(i=0; i < MAX_REFINE; i++) {
+ status->dbs->refine_info[type].chance[i] = chance_[i]?chance_[i]:100;
+ status->dbs->refine_info[type].randombonus_max[i] = rnd_bonus[i];
+ bonus_[i] += i?bonus_[i-1]:0;
+ status->dbs->refine_info[type].bonus[i] = bonus_[i]?bonus_[i]:0;
+ }
+ } else {
+ ShowWarning("status_readdb_refine_libconfig_sub: Missing refine rates for entry '%s' in \"%s\", skipping.\n", name, source);
+ return 0;
+ }
+ return type+1;
+}
- status->dbs->refine_info[current].bonus[i] = bonus_per_level + atoi(delim+1);
- if (i > 0)
- status->dbs->refine_info[current].bonus[i] += status->dbs->refine_info[current].bonus[i-1];
+/**
+ * Reads from a libconfig-formatted refine_db.conf file.
+ *
+ * @param *filename File name, relative to the database path.
+ * @return The number of found entries.
+ */
+int status_readdb_refine_libconfig(const char *filename) {
+ bool duplicate[REFINE_TYPE_MAX];
+ config_t refine_db_conf;
+ config_setting_t *r;
+ char filepath[256];
+ int i = 0, count = 0,type = 0;
+
+ sprintf(filepath, "%s/%s", map->db_path, filename);
+ memset(&duplicate,0,sizeof(duplicate));
+ if( libconfig->read_file(&refine_db_conf, filepath) ) {
+ ShowError("can't read %s\n", filepath);
+ return 0;
}
- return true;
+
+ while((r = libconfig->setting_get_elem(refine_db_conf.root,i++))) {
+ char *name = config_setting_name(r);
+ if((type=status->readdb_refine_libconfig_sub(r, name, filename))) {
+ if( duplicate[type-1] ) {
+ ShowWarning("status_readdb_refine_libconfig: duplicate entry for %s in \"%s\", overwriting previous entry...\n", name, filename);
+ } else duplicate[type-1] = true;
+ count++;
+ }
+ }
+ libconfig->destroy(&refine_db_conf);
+ ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename);
+
+ return count;
}
bool status_readdb_scconfig(char* fields[], int columns, int current) {
@@ -12372,7 +12452,7 @@ int status_readdb(void)
//
sv->readdb(map->db_path, "job_db2.txt", ',', 1, 1+MAX_LEVEL, -1, status->readdb_job2);
sv->readdb(map->db_path, DBPATH"size_fix.txt", ',', MAX_WEAPON_TYPE, MAX_WEAPON_TYPE, ARRAYLENGTH(status->dbs->atkmods), status->readdb_sizefix);
- sv->readdb(map->db_path, DBPATH"refine_db.txt", ',', 4+MAX_REFINE, 4+MAX_REFINE, ARRAYLENGTH(status->dbs->refine_info), status->readdb_refine);
+ status->readdb_refine_libconfig(DBPATH"refine_db.conf");
sv->readdb(map->db_path, "sc_config.txt", ',', 2, 2, SC_MAX, status->readdb_scconfig);
status->read_job_db();
@@ -12547,7 +12627,8 @@ void status_defaults(void) {
status->natural_heal_timer = status_natural_heal_timer;
status->readdb_job2 = status_readdb_job2;
status->readdb_sizefix = status_readdb_sizefix;
- status->readdb_refine = status_readdb_refine;
+ status->readdb_refine_libconfig = status_readdb_refine_libconfig;
+ status->readdb_refine_libconfig_sub = status_readdb_refine_libconfig_sub;
status->readdb_scconfig = status_readdb_scconfig;
status->read_job_db = status_read_job_db;
status->read_job_db_sub = status_read_job_db_sub;
diff --git a/src/map/status.h b/src/map/status.h
index 274c64c5b..d49bca8b4 100644
--- a/src/map/status.h
+++ b/src/map/status.h
@@ -2077,7 +2077,8 @@ struct status_interface {
int (*natural_heal_timer) (int tid, int64 tick, int id, intptr_t data);
bool (*readdb_job2) (char *fields[], int columns, int current);
bool (*readdb_sizefix) (char *fields[], int columns, int current);
- bool (*readdb_refine) (char *fields[], int columns, int current);
+ int (*readdb_refine_libconfig) (const char *filename);
+ int (*readdb_refine_libconfig_sub) (config_setting_t *r, const char *name, const char *source);
bool (*readdb_scconfig) (char *fields[], int columns, int current);
void (*read_job_db) (void);
void (*read_job_db_sub) (int idx, const char *name, config_setting_t *jdb);
diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc
index eab1d007e..bf1aee21b 100644
--- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc
+++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc
@@ -5427,8 +5427,10 @@ struct {
struct HPMHookPoint *HP_status_readdb_job2_post;
struct HPMHookPoint *HP_status_readdb_sizefix_pre;
struct HPMHookPoint *HP_status_readdb_sizefix_post;
- struct HPMHookPoint *HP_status_readdb_refine_pre;
- struct HPMHookPoint *HP_status_readdb_refine_post;
+ struct HPMHookPoint *HP_status_readdb_refine_libconfig_pre;
+ struct HPMHookPoint *HP_status_readdb_refine_libconfig_post;
+ struct HPMHookPoint *HP_status_readdb_refine_libconfig_sub_pre;
+ struct HPMHookPoint *HP_status_readdb_refine_libconfig_sub_post;
struct HPMHookPoint *HP_status_readdb_scconfig_pre;
struct HPMHookPoint *HP_status_readdb_scconfig_post;
struct HPMHookPoint *HP_status_read_job_db_pre;
@@ -11140,8 +11142,10 @@ struct {
int HP_status_readdb_job2_post;
int HP_status_readdb_sizefix_pre;
int HP_status_readdb_sizefix_post;
- int HP_status_readdb_refine_pre;
- int HP_status_readdb_refine_post;
+ int HP_status_readdb_refine_libconfig_pre;
+ int HP_status_readdb_refine_libconfig_post;
+ int HP_status_readdb_refine_libconfig_sub_pre;
+ int HP_status_readdb_refine_libconfig_sub_post;
int HP_status_readdb_scconfig_pre;
int HP_status_readdb_scconfig_post;
int HP_status_read_job_db_pre;
diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc
index 4656f01c2..d9d8dc230 100644
--- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc
+++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc
@@ -2763,7 +2763,8 @@ struct HookingPointData HookingPoints[] = {
{ HP_POP(status->natural_heal_timer, HP_status_natural_heal_timer) },
{ HP_POP(status->readdb_job2, HP_status_readdb_job2) },
{ HP_POP(status->readdb_sizefix, HP_status_readdb_sizefix) },
- { HP_POP(status->readdb_refine, HP_status_readdb_refine) },
+ { HP_POP(status->readdb_refine_libconfig, HP_status_readdb_refine_libconfig) },
+ { HP_POP(status->readdb_refine_libconfig_sub, HP_status_readdb_refine_libconfig_sub) },
{ HP_POP(status->readdb_scconfig, HP_status_readdb_scconfig) },
{ HP_POP(status->read_job_db, HP_status_read_job_db) },
{ HP_POP(status->read_job_db_sub, HP_status_read_job_db_sub) },
diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc
index e6aad6dad..2454846f8 100644
--- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc
+++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc
@@ -72994,15 +72994,15 @@ bool HP_status_readdb_sizefix(char *fields[], int columns, int current) {
}
return retVal___;
}
-bool HP_status_readdb_refine(char *fields[], int columns, int current) {
+int HP_status_readdb_refine_libconfig_sub(config_setting_t *r, const char *name, const char *source) {
int hIndex = 0;
- bool retVal___ = false;
- if( HPMHooks.count.HP_status_readdb_refine_pre ) {
- bool (*preHookFunc) (char *fields[], int *columns, int *current);
+ int retVal___ = 0;
+ if( HPMHooks.count.HP_status_readdb_refine_libconfig_sub_pre ) {
+ int (*preHookFunc) (config_setting_t *r, const char *name, const char *source);
*HPMforce_return = false;
- for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_pre; hIndex++ ) {
- preHookFunc = HPMHooks.list.HP_status_readdb_refine_pre[hIndex].func;
- retVal___ = preHookFunc(fields, &columns, &current);
+ for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_libconfig_sub_pre; hIndex++ ) {
+ preHookFunc = HPMHooks.list.HP_status_readdb_refine_libconfig_sub_pre[hIndex].func;
+ retVal___ = preHookFunc(r, name, source);
}
if( *HPMforce_return ) {
*HPMforce_return = false;
@@ -73010,13 +73010,40 @@ bool HP_status_readdb_refine(char *fields[], int columns, int current) {
}
}
{
- retVal___ = HPMHooks.source.status.readdb_refine(fields, columns, current);
+ retVal___ = HPMHooks.source.status.readdb_refine_libconfig_sub(r, name, source);
}
- if( HPMHooks.count.HP_status_readdb_refine_post ) {
- bool (*postHookFunc) (bool retVal___, char *fields[], int *columns, int *current);
- for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_post; hIndex++ ) {
- postHookFunc = HPMHooks.list.HP_status_readdb_refine_post[hIndex].func;
- retVal___ = postHookFunc(retVal___, fields, &columns, &current);
+ if( HPMHooks.count.HP_status_readdb_refine_libconfig_sub_post ) {
+ int (*postHookFunc) (int retVal___, config_setting_t *r, const char *name, const char *source);
+ for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_libconfig_sub_post; hIndex++ ) {
+ postHookFunc = HPMHooks.list.HP_status_readdb_refine_libconfig_sub_post[hIndex].func;
+ retVal___ = postHookFunc(retVal___, r, name, source);
+ }
+ }
+ return retVal___;
+}
+int HP_status_readdb_refine_libconfig(const char *filename) {
+ int hIndex = 0;
+ int retVal___ = 0;
+ if( HPMHooks.count.HP_status_readdb_refine_libconfig_pre ) {
+ int (*preHookFunc) (const char *filename);
+ *HPMforce_return = false;
+ for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_libconfig_pre; hIndex++ ) {
+ preHookFunc = HPMHooks.list.HP_status_readdb_refine_libconfig_pre[hIndex].func;
+ retVal___ = preHookFunc(filename);
+ }
+ if( *HPMforce_return ) {
+ *HPMforce_return = false;
+ return retVal___;
+ }
+ }
+ {
+ retVal___ = HPMHooks.source.status.readdb_refine_libconfig(filename);
+ }
+ if( HPMHooks.count.HP_status_readdb_refine_libconfig_post ) {
+ int (*postHookFunc) (int retVal___, const char *filename);
+ for(hIndex = 0; hIndex < HPMHooks.count.HP_status_readdb_refine_libconfig_post; hIndex++ ) {
+ postHookFunc = HPMHooks.list.HP_status_readdb_refine_libconfig_post[hIndex].func;
+ retVal___ = postHookFunc(retVal___, filename);
}
}
return retVal___;