Use custom URI schemes for config files.

TODO: XML files need to be rebased, XIncludes replaced.
This commit is contained in:
Enno Rehling 2018-02-05 03:44:26 +01:00
parent 92f82c3608
commit da02c1b92e
5 changed files with 69 additions and 51 deletions

View File

@ -1,11 +1,30 @@
{ {
"include": [ "include": [
"keywords.json", "config://keywords.json",
"calendar.json", "config://calendar.json",
"prefixes.json", "config://prefixes.json",
"e2/terrains.json", "config://e2/terrains.json",
"e2/rules.xml", "config://e2/locales.xml",
"e2/locales.xml" "config://e2/rules.xml",
"rules://core/ships.xml",
"rules://core/spoils.xml",
"rules://core/common/buildings.xml",
"rules://core/common/items.xml",
"rules://core/common/resources.xml",
"rules://core/common/luxuries.xml",
"rules://core/common/herbs.xml",
"rules://core/common/potions.xml",
"rules://core/common/armor.xml",
"rules://core/common/weapons.xml",
"rules://eressea/races.xml",
"rules://eressea/artrewards.xml",
"rules://eressea/buildings.xml",
"rules://eressea/familiars.xml",
"rules://eressea/buildings.xml",
"rules://eressea/equipment.xml",
"rules://eressea/items.xml",
"rules://eressea/spells.xml",
"rules://adamantium.xml"
], ],
"disabled": [ "disabled": [
"jsreport" "jsreport"

View File

@ -1,24 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<eressea xmlns:xi="http://www.w3.org/2001/XInclude"> <eressea>
<xi:include href="config://core/common/items.xml" />
<xi:include href="config://core/common/armor.xml" />
<xi:include href="config://core/common/weapons.xml" />
<xi:include href="config://core/common/resources.xml" />
<xi:include href="config://core/common/luxuries.xml" />
<xi:include href="config://core/common/herbs.xml" />
<xi:include href="config://core/common/potions.xml" />
<xi:include href="config://core/spoils.xml"/>
<xi:include href="config://game/races.xml"/>
<xi:include href="config://core/ships.xml"/>
<xi:include href="config://core/common/buildings.xml"/>
<xi:include href="config://game/familiars.xml"/>
<xi:include href="config://game/artrewards.xml"/>
<xi:include href="config://game/buildings.xml"/>
<xi:include href="config://game/equipment.xml"/>
<xi:include href="config://game/items.xml"/>
<xi:include href="config://game/spells.xml"/>
<xi:include href="config://default/adamantium.xml"/>
<equipment> <equipment>
<set name="first_unit"> <set name="first_unit">
<item name="money" amount="2500"/> <item name="money" amount="2500"/>

View File

@ -1,4 +1,5 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<eressea>
<resources> <resources>
<resource name="money"> <resource name="money">
@ -133,3 +134,4 @@
</resource> </resource>
</resources> </resources>
</eressea>

View File

@ -1,11 +1,10 @@
local confdir = 'conf/' local rules = 'conf'
if config.install then
confdir = config.install .. '/' .. confdir
end
if config.rules then if config.rules then
local rules = config.rules .. '/' rules = rules .. '/' .. config.rules
assert(0 == eressea.config.read(rules .. 'config.json', confdir), "could not read JSON data") assert(0 == eressea.config.read(rules .. '/config.json', config.install), "could not read JSON data")
-- assert(0 == read_xml(confdir .. rules .. 'rules.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?") -- assert(0 == read_xml(confdir .. rules .. 'rules.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
-- assert(0 == read_xml(confdir .. rules .. 'locales.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?") -- assert(0 == read_xml(confdir .. rules .. 'locales.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
end end
eressea.game.reset() eressea.game.reset()

View File

@ -887,16 +887,32 @@ static void json_races(cJSON *json) {
const char * json_relpath; const char * json_relpath;
static void include_json(const char *filename) { static const char * uri_to_file(const char * uri, char *name, size_t size) {
const char *pos, *path = json_relpath;
pos = strstr(uri, "://");
if (pos) {
size_t slen = pos - uri;
/* identify scheme */
if (strncmp(uri, "config", slen) == 0) {
path = path_join(path, "conf", name, size);
}
else if (strncmp(uri, "rules", slen) == 0) {
path = path_join(path, "res", name, size);
}
if (path) {
return path_join(path, pos + 3, name, size);
}
}
return uri;
}
static void include_json(const char *uri) {
FILE *F; FILE *F;
if (json_relpath) {
char name[PATH_MAX]; char name[PATH_MAX];
path_join(json_relpath, filename, name, sizeof(name)); const char *filename = uri_to_file(uri, name, sizeof(name));
F = fopen(name, "r");
}
else {
F = fopen(filename, "r"); F = fopen(filename, "r");
}
if (F) { if (F) {
long pos; long pos;
fseek(F, 0, SEEK_END); fseek(F, 0, SEEK_END);
@ -917,21 +933,20 @@ static void include_json(const char *filename) {
cJSON_Delete(config); cJSON_Delete(config);
} }
else { else {
log_error("invalid JSON, could not parse %s", filename); log_error("could not parse JSON from %s", uri);
} }
} }
fclose(F); fclose(F);
} }
} }
static void include_xml(const char *filename) { static void include_xml(const char *uri) {
char name[PATH_MAX]; char name[PATH_MAX];
const char *filename = uri_to_file(uri, name, sizeof(name));
if (json_relpath) { int err = read_xml(filename, NULL);
path_join(json_relpath, filename, name, sizeof(name)); if (err != 0) {
filename = name; log_error("could not parse XML from %s", uri);
} }
read_xml(filename, NULL);
} }
static void json_include(cJSON *json) { static void json_include(cJSON *json) {
@ -941,12 +956,13 @@ static void json_include(cJSON *json) {
return; return;
} }
for (child = json->child; child; child = child->next) { for (child = json->child; child; child = child->next) {
const char * filename = child->valuestring; const char *uri = child->valuestring;
if (strstr(filename, ".xml") != NULL) {
include_xml(filename); if (strstr(uri, ".xml") != NULL) {
include_xml(uri);
} }
else { else {
include_json(filename); include_json(uri);
} }
} }
} }