From a8154726935b2d6a2273b75126a675e6e706edab Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 10 Jan 2016 02:01:55 +0100 Subject: Converted const.txt to libconfig format - This is necessary for upcoming improvements Signed-off-by: Haru --- src/map/npc.c | 2 +- src/map/script.c | 72 +++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 23 deletions(-) (limited to 'src/map') diff --git a/src/map/npc.c b/src/map/npc.c index 411e52c29..acecff6d0 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -2551,7 +2551,7 @@ void npc_parsename(struct npc_data* nd, const char* name, const char* start, con // Support for using Constants in place of NPC View IDs. int npc_parseview(const char* w4, const char* start, const char* buffer, const char* filepath) { int val = FAKE_NPC, i = 0; - char viewid[1024]; // Max size of name from const.txt, see script->read_constdb. + char viewid[1024]; // Max size of name from constants.conf, see script->read_constdb. // Extract view ID / constant while (w4[i] != '\0') { diff --git a/src/map/script.c b/src/map/script.c index 5d87e6495..8ddfb0717 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -2277,32 +2277,57 @@ void script_set_constant2(const char *name, int value, bool isparameter) { script->str_data[n].val = value; } -/*========================================== - * Reading constant databases - * const.txt - *------------------------------------------*/ -void read_constdb(void) { - FILE *fp; - char line[1024],name[1024],val[1024]; - int type; - sprintf(line, "%s/const.txt", map->db_path); - fp=fopen(line, "r"); - if(fp==NULL) { - ShowError("can't read %s\n", line); - return ; +/** + * Loads the constants database from constants.conf + */ +void read_constdb(void) +{ + config_t constants_conf; + char filepath[256]; + config_setting_t *cdb; + config_setting_t *t; + int i = 0; + + sprintf(filepath, "%s/constants.conf", map->db_path); + + if (libconfig->read_file(&constants_conf, filepath) || !(cdb = libconfig->setting_get_member(constants_conf.root, "constants_db"))) { + ShowError("can't read %s\n", filepath); + return; } - while (fgets(line, sizeof(line), fp)) { - if (line[0] == '/' && line[1] == '/') + + while ((t = libconfig->setting_get_elem(cdb, i++))) { + bool is_parameter = false; + int value = 0; + const char *name = config_setting_name(t); + const char *p = name; + + while (*p != '\0') { + if (!ISALNUM(*p) && *p != '_') + break; + p++; + } + if (*p != '\0') { + ShowWarning("read_constdb: Invalid constant name %s. Skipping.\n", name); continue; - type = 0; - if (sscanf(line, "%1023[A-Za-z0-9_],%1023[-0-9xXA-Fa-f],%d", name, val, &type) >=2 - || sscanf(line, "%1023[A-Za-z0-9_] %1023[-0-9xXA-Fa-f] %d", name, val, &type) >=2 - ) { - script->set_constant(name, (int)strtol(val, NULL, 0), (bool)type); } + if (config_setting_is_aggregate(t)) { + int i32; + if (!libconfig->setting_lookup_int(t, "Value", &i32)) { + ShowWarning("read_constdb: Invalid entry for %s. Skipping.\n", name); + continue; + } + value = i32; + if (libconfig->setting_lookup_bool(t, "Parameter", &i32)) { + if (i32 != 0) + is_parameter = true; + } + } else { + value = libconfig->setting_get_int(t); + } + script->set_constant(name, value, is_parameter); } - fclose(fp); + libconfig->destroy(&constants_conf); } // Standard UNIX tab size is 8 @@ -20778,7 +20803,10 @@ void script_label_add(int key, int pos) { /** * Sets source-end constants for scripts to play with **/ -void script_hardcoded_constants(void) { +void script_hardcoded_constants(void) +{ + script->set_constant("true", 1, false); + script->set_constant("false", 0, false); /* server defines */ script->set_constant("PACKETVER",PACKETVER,false); script->set_constant("MAX_LEVEL",MAX_LEVEL,false); -- cgit v1.2.3-60-g2f50