2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2014-06-08 07:17:48 +02:00
|
|
|
+-------------------+
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| (c) 1998 - 2004 | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
+-------------------+
|
|
|
|
|
|
|
|
This program may not be used, modified or distributed
|
|
|
|
without prior permission by the authors of Eressea.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include "jsonconf.h"
|
|
|
|
|
|
|
|
/* kernel includes */
|
|
|
|
#include "building.h"
|
2014-06-16 20:30:23 +02:00
|
|
|
#include "direction.h"
|
|
|
|
#include "keyword.h"
|
2014-06-08 07:17:48 +02:00
|
|
|
#include "equipment.h"
|
|
|
|
#include "item.h"
|
2014-06-09 18:54:48 +02:00
|
|
|
#include "messages.h"
|
2014-06-08 07:17:48 +02:00
|
|
|
#include "race.h"
|
|
|
|
#include "region.h"
|
|
|
|
#include "resources.h"
|
|
|
|
#include "ship.h"
|
|
|
|
#include "terrain.h"
|
|
|
|
#include "skill.h"
|
|
|
|
#include "spell.h"
|
|
|
|
#include "spellbook.h"
|
|
|
|
#include "calendar.h"
|
|
|
|
|
2015-09-12 12:49:12 +02:00
|
|
|
/* game modules */
|
|
|
|
#include "prefix.h"
|
|
|
|
|
2014-06-08 07:17:48 +02:00
|
|
|
/* util includes */
|
|
|
|
#include <util/attrib.h>
|
|
|
|
#include <util/bsdstring.h>
|
|
|
|
#include <util/crmessage.h>
|
|
|
|
#include <util/functions.h>
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/log.h>
|
|
|
|
#include <util/message.h>
|
|
|
|
#include <util/nrmessage.h>
|
|
|
|
#include <util/xml.h>
|
|
|
|
|
|
|
|
/* external libraries */
|
|
|
|
#include <cJSON.h>
|
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2014-06-13 17:04:06 +02:00
|
|
|
#include <stdlib.h>
|
2014-06-08 07:17:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static int json_flags(cJSON *json, const char *flags[]) {
|
2014-06-17 07:43:40 +02:00
|
|
|
cJSON *entry;
|
|
|
|
int result = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
assert(json->type == cJSON_Array);
|
|
|
|
for (entry = json->child; entry; entry = entry->next) {
|
2014-06-17 07:43:40 +02:00
|
|
|
if (entry->type == cJSON_String) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; flags[i]; ++i) {
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(flags[i], entry->valuestring) == 0) {
|
|
|
|
result |= (1 << i);
|
2015-04-19 12:49:39 +02:00
|
|
|
break;
|
2014-06-17 07:43:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_requirements(cJSON *json, requirement **matp) {
|
2014-06-13 17:04:06 +02:00
|
|
|
cJSON *child;
|
2014-07-01 04:21:28 +02:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
requirement *mat = calloc(sizeof(requirement), 1 + cJSON_GetArraySize(json));
|
|
|
|
for (i = 0, child = json->child; child; child = child->next, ++i) {
|
2014-07-01 04:21:28 +02:00
|
|
|
mat[i].number = child->valueint;
|
|
|
|
mat[i].rtype = rt_get_or_create(child->string);
|
|
|
|
}
|
|
|
|
*matp = mat;
|
|
|
|
}
|
|
|
|
|
2014-07-05 06:48:17 +02:00
|
|
|
static void json_maintenance_i(cJSON *json, maintenance *mt) {
|
|
|
|
cJSON *child;
|
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case cJSON_Number:
|
|
|
|
if (strcmp(child->string, "amount") == 0) {
|
|
|
|
mt->number = child->valueint;
|
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("maintenance contains unknown attribute %s", child->string);
|
2014-07-05 06:48:17 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cJSON_String:
|
|
|
|
if (strcmp(child->string, "type") == 0) {
|
|
|
|
mt->rtype = rt_get_or_create(child->valuestring);
|
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("maintenance contains unknown attribute %s", child->string);
|
2014-07-05 06:48:17 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cJSON_Array:
|
|
|
|
if (strcmp(child->string, "flags") == 0) {
|
|
|
|
const char * flags[] = { "variable", "required", 0 };
|
|
|
|
mt->flags = json_flags(child, flags);
|
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("maintenance contains unknown array %s", child->string);
|
2014-07-05 06:48:17 +02:00
|
|
|
}
|
2014-07-06 03:21:20 +02:00
|
|
|
break;
|
2014-07-05 06:48:17 +02:00
|
|
|
default:
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("maintenance contains unknown attribute %s", child->string);
|
2014-07-05 06:48:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void json_maintenance(cJSON *json, maintenance **mtp) {
|
|
|
|
cJSON *child;
|
|
|
|
maintenance *mt;
|
|
|
|
int i, size = 1;
|
|
|
|
|
|
|
|
if (json->type == cJSON_Array) {
|
|
|
|
size = cJSON_GetArraySize(json);
|
|
|
|
}
|
|
|
|
else if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("maintenance is not a json object or array (%d)", json->type);
|
2014-07-05 06:48:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1);
|
|
|
|
if (json->type == cJSON_Array) {
|
|
|
|
for (i = 0, child = json->child; child; child = child->next, ++i) {
|
|
|
|
if (child->type == cJSON_Object) {
|
2015-01-30 20:37:14 +01:00
|
|
|
json_maintenance_i(child, mt + i);
|
2014-07-05 06:48:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json_maintenance_i(json, mt);
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_construction(cJSON *json, construction **consp) {
|
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type == cJSON_Array) {
|
2014-07-01 04:21:28 +02:00
|
|
|
int size = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-07-01 04:21:28 +02:00
|
|
|
construction *cons = 0;
|
|
|
|
json_construction(child, &cons);
|
|
|
|
if (cons) {
|
|
|
|
cons->maxsize -= size;
|
|
|
|
size += cons->maxsize + size;
|
|
|
|
*consp = cons;
|
|
|
|
consp = &cons->improvement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (json->type != cJSON_Object) {
|
2014-12-31 13:17:54 +01:00
|
|
|
log_error("construction %s is not a json object: %d", json->string, json->type);
|
2014-06-13 17:04:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
construction * cons = (construction *)calloc(sizeof(construction), 1);
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
2014-07-01 04:21:28 +02:00
|
|
|
case cJSON_Object:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "materials") == 0) {
|
2014-07-01 04:21:28 +02:00
|
|
|
json_requirements(child, &cons->materials);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-13 17:04:06 +02:00
|
|
|
case cJSON_Number:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "maxsize") == 0) {
|
2014-06-13 17:04:06 +02:00
|
|
|
cons->maxsize = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "reqsize") == 0) {
|
2014-06-13 17:04:06 +02:00
|
|
|
cons->reqsize = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "minskill") == 0) {
|
2014-06-13 17:04:06 +02:00
|
|
|
cons->minskill = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
break;
|
2014-06-13 17:04:06 +02:00
|
|
|
default:
|
2014-12-31 13:17:54 +01:00
|
|
|
log_error("construction %s contains unknown attribute %s", json->string, child->string);
|
2014-06-13 17:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*consp = cons;
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:12:53 +02:00
|
|
|
static void json_terrain_production(cJSON *json, terrain_production *prod) {
|
|
|
|
assert(json->type == cJSON_Object);
|
|
|
|
cJSON *child;
|
|
|
|
for (child = json->child; child; child = child->next) {
|
2015-09-13 09:46:40 +02:00
|
|
|
char **dst = 0;
|
2015-09-11 12:12:53 +02:00
|
|
|
switch (child->type) {
|
2015-09-13 09:46:40 +02:00
|
|
|
case cJSON_Number:
|
|
|
|
if (strcmp(child->string, "chance") == 0) {
|
|
|
|
prod->chance = (float)child->valuedouble;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("terrain_production %s contains unknown number %s", json->string, child->string);
|
|
|
|
}
|
|
|
|
break;
|
2015-09-11 12:12:53 +02:00
|
|
|
case cJSON_String:
|
|
|
|
if (strcmp(child->string, "base") == 0) {
|
2015-09-13 09:46:40 +02:00
|
|
|
dst = &prod->base;
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(child->string, "level") == 0) {
|
2015-09-13 09:46:40 +02:00
|
|
|
dst = &prod->startlevel;
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(child->string, "div") == 0) {
|
2015-09-13 09:46:40 +02:00
|
|
|
dst = &prod->divisor;
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-09-13 09:46:40 +02:00
|
|
|
log_error("terrain_production %s contains unknown string %s", json->string, child->string);
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error("terrain_production %s contains unknown attribute %s", json->string, child->string);
|
|
|
|
}
|
2015-09-13 09:46:40 +02:00
|
|
|
if (dst) {
|
|
|
|
free(*dst);
|
|
|
|
assert(child->type == cJSON_String);
|
|
|
|
*dst = _strdup(child->valuestring);
|
|
|
|
}
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_terrain(cJSON *json, terrain_type *ter) {
|
2014-06-14 02:36:05 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("terrain %s is not a json object: %d", json->string, json->type);
|
2014-06-14 02:36:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
2015-09-11 12:12:53 +02:00
|
|
|
case cJSON_Object:
|
|
|
|
if (strcmp(child->string, "production") == 0) {
|
|
|
|
cJSON *entry;
|
2015-09-11 12:52:18 +02:00
|
|
|
int size = cJSON_GetArraySize(child);
|
|
|
|
if (size > 0) {
|
|
|
|
int n;
|
|
|
|
ter->production = (terrain_production *)calloc(size + 1, sizeof(terrain_production));
|
|
|
|
ter->production[size].type = 0;
|
|
|
|
for (n = 0, entry = child->child; entry; entry = entry->next, ++n) {
|
|
|
|
ter->production[n].type = rt_get_or_create(entry->string);
|
|
|
|
if (entry->type != cJSON_Object) {
|
|
|
|
log_error("terrain %s contains invalid production %s", json->string, entry->string);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
json_terrain_production(entry, ter->production + n);
|
|
|
|
}
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("terrain %s contains unknown attribute %s", json->string, child->string);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-17 07:19:19 +02:00
|
|
|
case cJSON_Array:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "flags") == 0) {
|
2014-06-17 07:19:19 +02:00
|
|
|
const char * flags[] = {
|
|
|
|
"land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0
|
|
|
|
};
|
2014-06-17 07:43:40 +02:00
|
|
|
ter->flags = json_flags(child, flags);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
2015-09-11 12:12:53 +02:00
|
|
|
else if (strcmp(child->string, "herbs") == 0) {
|
|
|
|
cJSON *entry;
|
2015-09-11 12:52:18 +02:00
|
|
|
int size = cJSON_GetArraySize(child);
|
|
|
|
if (size > 0) {
|
|
|
|
int n;
|
2015-10-14 21:37:27 +02:00
|
|
|
free(ter->herbs);
|
2015-09-11 12:52:18 +02:00
|
|
|
ter->herbs = malloc(sizeof(const item_type *) * (size + 1));
|
|
|
|
ter->herbs[size] = 0;
|
|
|
|
for (n = 0, entry = child->child; entry; entry = entry->next) {
|
|
|
|
ter->herbs[n++] = it_get_or_create(rt_get_or_create(entry->valuestring));
|
|
|
|
}
|
2015-09-11 12:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("terrain %s contains unknown attribute %s", json->string, child->string);
|
2014-06-17 07:19:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-09-11 12:12:53 +02:00
|
|
|
case cJSON_Number:
|
|
|
|
if (strcmp(child->string, "size") == 0) {
|
|
|
|
ter->size = child->valueint;
|
|
|
|
}
|
|
|
|
else if (strcmp(child->string, "road") == 0) {
|
|
|
|
ter->max_road = (short)child->valueint;
|
|
|
|
}
|
|
|
|
else if (strcmp(child->string, "seed") == 0) {
|
|
|
|
ter->distribution = (short)child->valueint;
|
|
|
|
}
|
2015-09-11 21:14:10 +02:00
|
|
|
else {
|
|
|
|
log_error("terrain %s contains unknown attribute %s", json->string, child->string);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-17 07:19:19 +02:00
|
|
|
default:
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("terrain %s contains unknown attribute %s", json->string, child->string);
|
2014-06-17 07:19:19 +02:00
|
|
|
}
|
2014-06-14 02:36:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_building(cJSON *json, building_type *bt) {
|
2014-06-13 07:14:07 +02:00
|
|
|
cJSON *child;
|
2014-07-28 14:27:30 +02:00
|
|
|
const char *flags[] = {
|
|
|
|
"nodestroy", "nobuild", "unique", "decay", "dynamic", "magic", "oneperturn", "namechange", 0
|
|
|
|
};
|
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("building %s is not a json object: %d", json->string, json->type);
|
2014-06-13 07:14:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
2014-07-01 04:21:28 +02:00
|
|
|
case cJSON_Array:
|
2014-07-05 06:48:17 +02:00
|
|
|
if (strcmp(child->string, "construction") == 0) {
|
2014-07-01 04:21:28 +02:00
|
|
|
json_construction(child, &bt->construction);
|
|
|
|
}
|
2014-07-05 06:48:17 +02:00
|
|
|
else if (strcmp(child->string, "maintenance") == 0) {
|
|
|
|
json_maintenance(child, &bt->maintenance);
|
|
|
|
}
|
2014-07-28 14:27:30 +02:00
|
|
|
else if (strcmp(child->string, "flags") == 0) {
|
|
|
|
json_flags(child, flags);
|
|
|
|
}
|
2014-07-01 04:21:28 +02:00
|
|
|
break;
|
2014-06-14 02:36:05 +02:00
|
|
|
case cJSON_Object:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "construction") == 0) {
|
2014-06-14 02:36:05 +02:00
|
|
|
json_construction(child, &bt->construction);
|
|
|
|
}
|
2014-07-05 06:48:17 +02:00
|
|
|
else if (strcmp(child->string, "maintenance") == 0) {
|
|
|
|
json_maintenance(child, &bt->maintenance);
|
|
|
|
}
|
2014-06-14 02:36:05 +02:00
|
|
|
break;
|
2014-06-29 01:55:28 +02:00
|
|
|
case cJSON_String:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "name") == 0) {
|
2014-06-29 01:55:28 +02:00
|
|
|
bt->name = (const char *(*)(const struct building_type *,
|
2015-01-30 20:37:14 +01:00
|
|
|
const struct building *, int))get_function(child->valuestring);
|
2014-06-29 01:55:28 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("building %s contains unknown attribute %s", json->string, child->string);
|
2014-06-29 01:55:28 +02:00
|
|
|
break;
|
2014-06-14 02:36:05 +02:00
|
|
|
default:
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("building %s contains unknown attribute %s", json->string, child->string);
|
2014-06-14 02:36:05 +02:00
|
|
|
}
|
2014-06-13 07:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_item(cJSON *json, item_type *itype) {
|
2014-06-25 07:44:05 +02:00
|
|
|
cJSON *child;
|
2014-06-25 17:00:09 +02:00
|
|
|
const char *flags[] = {
|
|
|
|
"herb", "cursed", "nodrop", "big", "animal", "vehicle", 0
|
|
|
|
};
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ship %s is not a json object: %d", json->string, json->type);
|
2014-06-25 07:44:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
2014-06-25 17:00:09 +02:00
|
|
|
case cJSON_Number:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "weight") == 0) {
|
2014-06-25 17:00:09 +02:00
|
|
|
itype->weight = child->valueint;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "capacity") == 0) {
|
2014-06-25 17:00:09 +02:00
|
|
|
itype->capacity = child->valueint;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("item %s contains unknown attribute %s", json->string, child->string);
|
2014-06-25 17:00:09 +02:00
|
|
|
break;
|
|
|
|
case cJSON_Array:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "flags") == 0) {
|
2014-06-25 17:00:09 +02:00
|
|
|
itype->flags = json_flags(child, flags);
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("item %s contains unknown attribute %s", json->string, child->string);
|
2014-06-25 07:44:05 +02:00
|
|
|
case cJSON_Object:
|
|
|
|
default:
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("item %s contains unknown attribute %s", json->string, child->string);
|
2014-06-25 07:44:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_ship(cJSON *json, ship_type *st) {
|
2014-06-18 06:33:42 +02:00
|
|
|
cJSON *child, *iter;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ship %s is not a json object: %d", json->string, json->type);
|
2014-06-13 07:14:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-18 06:33:42 +02:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
switch (child->type) {
|
2014-06-13 17:04:06 +02:00
|
|
|
case cJSON_Object:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "construction") == 0) {
|
2014-06-13 17:04:06 +02:00
|
|
|
json_construction(child, &st->construction);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ship %s contains unknown attribute %s", json->string, child->string);
|
2014-06-17 07:19:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-06-18 06:33:42 +02:00
|
|
|
case cJSON_Array:
|
2014-12-31 01:00:10 +01:00
|
|
|
st->coasts = (terrain_type **)
|
2015-01-30 20:37:14 +01:00
|
|
|
malloc(sizeof(terrain_type *) * (1 + cJSON_GetArraySize(child)));
|
|
|
|
for (i = 0, iter = child->child; iter; iter = iter->next) {
|
|
|
|
if (iter->type == cJSON_String) {
|
2014-06-18 06:33:42 +02:00
|
|
|
terrain_type *ter = get_or_create_terrain(iter->valuestring);
|
|
|
|
if (ter) {
|
|
|
|
st->coasts[i++] = ter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
st->coasts[i] = 0;
|
|
|
|
break;
|
2014-06-17 07:19:19 +02:00
|
|
|
case cJSON_Number:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "range") == 0) {
|
2014-06-17 07:19:19 +02:00
|
|
|
st->range = child->valueint;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
2015-04-18 11:41:50 +02:00
|
|
|
else if (strcmp(child->string, "maxrange") == 0) {
|
|
|
|
st->range_max = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ship %s contains unknown attribute %s", json->string, child->string);
|
2014-06-13 17:04:06 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ship %s contains unknown attribute %s", json->string, child->string);
|
2014-06-13 17:04:06 +02:00
|
|
|
}
|
2014-06-13 07:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_race(cJSON *json, race *rc) {
|
2014-06-08 07:17:48 +02:00
|
|
|
cJSON *child;
|
2014-06-17 07:43:40 +02:00
|
|
|
const char *flags[] = {
|
2014-07-06 04:29:12 +02:00
|
|
|
"npc", "killpeasants", "scarepeasants",
|
|
|
|
"nosteal", "moverandom", "cannotmove",
|
2014-06-17 07:43:40 +02:00
|
|
|
"learn", "fly", "swim", "walk", "nolearn",
|
|
|
|
"noteach", "horse", "desert",
|
2015-01-30 20:37:14 +01:00
|
|
|
"illusionary", "absorbpeasants", "noheal",
|
2014-06-17 07:43:40 +02:00
|
|
|
"noweapons", "shapeshift", "", "undead", "dragon",
|
2014-07-06 09:29:52 +02:00
|
|
|
"coastal", "", "cansail", 0
|
2014-06-17 07:43:40 +02:00
|
|
|
};
|
2014-07-06 09:53:15 +02:00
|
|
|
const char *ecflags[] = {
|
2015-04-19 12:49:39 +02:00
|
|
|
"", "keepitem", "giveperson",
|
2014-07-06 09:53:15 +02:00
|
|
|
"giveunit", "getitem", 0
|
|
|
|
};
|
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("race %s is not a json object: %d", json->string, json->type);
|
2014-06-08 07:17:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
switch (child->type) {
|
2014-06-08 07:17:48 +02:00
|
|
|
case cJSON_String:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "damage") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->def_damage = _strdup(child->valuestring);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cJSON_Number:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "magres") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->magres = (float)child->valuedouble;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "maxaura") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->maxaura = (float)child->valuedouble;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "regaura") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->regaura = (float)child->valuedouble;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "speed") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->speed = (float)child->valuedouble;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "recruitcost") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->recruitcost = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "maintenance") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->maintenance = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "weight") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->weight = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "capacity") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->capacity = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "hp") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->hitpoints = child->valueint;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "ac") == 0) {
|
2014-06-08 07:17:48 +02:00
|
|
|
rc->armor = child->valueint;
|
|
|
|
}
|
|
|
|
// TODO: studyspeed (orcs only)
|
|
|
|
break;
|
2014-06-17 07:43:40 +02:00
|
|
|
case cJSON_Array:
|
2015-01-30 20:37:14 +01:00
|
|
|
if (strcmp(child->string, "flags") == 0) {
|
2014-06-17 07:43:40 +02:00
|
|
|
rc->flags = json_flags(child, flags);
|
2014-07-06 09:53:15 +02:00
|
|
|
rc->ec_flags = json_flags(child, ecflags);
|
2014-06-08 07:17:48 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:49:12 +02:00
|
|
|
static void json_prefixes(cJSON *json) {
|
|
|
|
cJSON *child;
|
|
|
|
if (json->type != cJSON_Array) {
|
|
|
|
log_error("prefixes is not a json array: %d", json->type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
add_raceprefix(child->valuestring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 17:37:29 +02:00
|
|
|
/** disable a feature.
|
|
|
|
* features are identified by eone of:
|
|
|
|
* 1. the keyword for their orders,
|
|
|
|
* 2. the name of the skill they use,
|
|
|
|
* 3. a "module.enabled" flag in the settings
|
|
|
|
*/
|
|
|
|
static void disable_feature(const char *str) {
|
2015-09-12 17:43:31 +02:00
|
|
|
char name[32];
|
2015-09-12 17:37:29 +02:00
|
|
|
int k;
|
2015-09-12 17:43:31 +02:00
|
|
|
skill_t sk;
|
|
|
|
sk = findskill(str);
|
|
|
|
if (sk != NOSKILL) {
|
|
|
|
enable_skill(sk, false);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-12 17:37:29 +02:00
|
|
|
for (k = 0; k != MAXKEYWORDS; ++k) {
|
2015-09-12 17:43:31 +02:00
|
|
|
// FIXME: this loop is slow as balls.
|
2015-09-12 17:37:29 +02:00
|
|
|
if (strcmp(keywords[k], str) == 0) {
|
|
|
|
log_info("disable keyword %s\n", str);
|
|
|
|
enable_keyword(k, false);
|
2015-09-12 17:43:31 +02:00
|
|
|
return;
|
2015-09-12 17:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-12 17:43:31 +02:00
|
|
|
_snprintf(name, sizeof(name), "%s.enabled", str);
|
|
|
|
log_info("disable feature %s\n", name);
|
|
|
|
set_param(&global.parameters, name, "0");
|
2015-09-12 17:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void json_disable_features(cJSON *json) {
|
2015-09-12 17:06:47 +02:00
|
|
|
cJSON *child;
|
|
|
|
if (json->type != cJSON_Array) {
|
2015-09-12 17:37:29 +02:00
|
|
|
log_error("disabled is not a json array: %d", json->type);
|
2015-09-12 17:06:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (child = json->child; child; child = child->next) {
|
2015-09-12 17:37:29 +02:00
|
|
|
disable_feature(child->valuestring);
|
2015-09-12 17:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_terrains(cJSON *json) {
|
2014-06-14 02:36:05 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("terrains is not a json object: %d", json->type);
|
2014-06-14 02:36:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-16 06:19:47 +02:00
|
|
|
json_terrain(child, get_or_create_terrain(child->string));
|
2014-06-14 02:36:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_buildings(cJSON *json) {
|
2014-06-13 07:14:07 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("buildings is not a json object: %d", json->type);
|
2014-06-13 07:14:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-13 07:14:07 +02:00
|
|
|
json_building(child, bt_get_or_create(child->string));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:15:22 +02:00
|
|
|
static void json_spells(cJSON *json) {
|
|
|
|
cJSON *child;
|
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("spells is not a json object: %d", json->type);
|
2014-07-03 06:15:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
if (child->type == cJSON_Object) {
|
|
|
|
spell *sp;
|
|
|
|
cJSON * item = cJSON_GetObjectItem(child, "index");
|
|
|
|
sp = create_spell(child->string, item ? item->valueint : 0);
|
|
|
|
for (item = child->child; item; item = item->next) {
|
|
|
|
if (strcmp(item->string, "index") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (strcmp(item->string, "cast") == 0) {
|
|
|
|
sp->cast = (spell_f)get_function(item->valuestring);
|
|
|
|
}
|
|
|
|
else if (strcmp(item->string, "fumble") == 0) {
|
|
|
|
sp->fumble = (fumble_f)get_function(item->valuestring);
|
|
|
|
}
|
|
|
|
else if (strcmp(item->string, "syntax") == 0) {
|
|
|
|
sp->syntax = _strdup(item->valuestring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_items(cJSON *json) {
|
2014-06-25 07:44:05 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("items is not a json object: %d", json->type);
|
2014-06-25 07:44:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-25 07:44:05 +02:00
|
|
|
resource_type *rtype = rt_get_or_create(child->string);
|
|
|
|
item_type *itype = rtype->itype;
|
|
|
|
if (!itype) {
|
|
|
|
rtype->itype = itype = it_get_or_create(rtype);
|
|
|
|
}
|
|
|
|
json_item(child, itype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_ships(cJSON *json) {
|
2014-06-13 07:14:07 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("ships is not a json object: %d", json->type);
|
2014-06-13 07:14:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-13 07:14:07 +02:00
|
|
|
json_ship(child, st_get_or_create(child->string));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_locale(cJSON *json, struct locale *lang) {
|
2014-06-28 19:37:40 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("strings is not a json object: %d", json->type);
|
2014-06-28 19:37:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
if (child->type == cJSON_String) {
|
2014-06-28 19:37:40 +02:00
|
|
|
locale_setstring(lang, child->string, child->valuestring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_strings(cJSON *json) {
|
2014-06-28 19:37:40 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("strings is not a json object: %d", json->type);
|
2014-06-28 19:37:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
if (child->type == cJSON_Object) {
|
|
|
|
struct locale *lang = get_or_create_locale(child->string);
|
2014-06-28 19:37:40 +02:00
|
|
|
json_locale(child, lang);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("strings for locale `%s` are not a json object: %d", child->string, child->type);
|
2014-06-28 19:37:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:17:08 +02:00
|
|
|
static void json_direction(cJSON *json, struct locale *lang) {
|
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("directions for locale `%s` not a json object: %d", locale_name(lang), json->type);
|
2014-06-16 07:17:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-16 07:17:08 +02:00
|
|
|
direction_t dir = finddirection(child->string);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (dir != NODIRECTION) {
|
|
|
|
if (child->type == cJSON_String) {
|
2014-06-16 07:17:08 +02:00
|
|
|
init_direction(lang, dir, child->valuestring);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (child->type == cJSON_Array) {
|
2014-06-16 07:17:08 +02:00
|
|
|
cJSON *entry;
|
2015-01-30 20:37:14 +01:00
|
|
|
for (entry = child->child; entry; entry = entry->next) {
|
2014-06-16 07:17:08 +02:00
|
|
|
init_direction(lang, dir, entry->valuestring);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("invalid type %d for direction `%s`", child->type, child->string);
|
2014-06-16 07:17:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_directions(cJSON *json) {
|
2014-06-16 07:17:08 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("directions is not a json object: %d", json->type);
|
2014-06-16 07:17:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-16 07:17:08 +02:00
|
|
|
struct locale * lang = get_or_create_locale(child->string);
|
|
|
|
json_direction(child, lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 17:53:14 +02:00
|
|
|
static void json_skill(cJSON *json, struct locale *lang) {
|
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("skill for locale `%s` not a json object: %d", locale_name(lang), json->type);
|
2014-06-21 17:53:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-21 17:53:14 +02:00
|
|
|
skill_t sk = findskill(child->string);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (sk != NOSKILL) {
|
|
|
|
if (child->type == cJSON_String) {
|
2014-06-21 17:53:14 +02:00
|
|
|
init_skill(lang, sk, child->valuestring);
|
|
|
|
locale_setstring(lang, mkname("skill", skillnames[sk]), child->valuestring);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (child->type == cJSON_Array) {
|
2014-06-21 17:53:14 +02:00
|
|
|
cJSON *entry;
|
2015-01-30 20:37:14 +01:00
|
|
|
for (entry = child->child; entry; entry = entry->next) {
|
2014-06-21 17:53:14 +02:00
|
|
|
init_skill(lang, sk, entry->valuestring);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (entry == child->child) {
|
|
|
|
locale_setstring(lang, mkname("skill", skillnames[sk]), entry->valuestring);
|
2014-06-21 17:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("invalid type %d for skill `%s`", child->type, child->string);
|
2014-06-21 17:53:14 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("unknown skill `%s` for locale `%s`", child->string, locale_name(lang));
|
2014-06-21 17:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 20:30:23 +02:00
|
|
|
static void json_keyword(cJSON *json, struct locale *lang) {
|
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("keywords for locale `%s` not a json object: %d", locale_name(lang), json->type);
|
2014-06-16 20:30:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-16 20:30:23 +02:00
|
|
|
keyword_t kwd = findkeyword(child->string);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (kwd != NOKEYWORD) {
|
|
|
|
if (child->type == cJSON_String) {
|
2014-06-16 20:30:23 +02:00
|
|
|
init_keyword(lang, kwd, child->valuestring);
|
2014-06-17 05:41:08 +02:00
|
|
|
locale_setstring(lang, mkname("keyword", keywords[kwd]), child->valuestring);
|
2014-06-16 20:30:23 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (child->type == cJSON_Array) {
|
2014-06-16 20:30:23 +02:00
|
|
|
cJSON *entry;
|
2015-01-30 20:37:14 +01:00
|
|
|
for (entry = child->child; entry; entry = entry->next) {
|
2014-06-16 20:30:23 +02:00
|
|
|
init_keyword(lang, kwd, entry->valuestring);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (entry == child->child) {
|
|
|
|
locale_setstring(lang, mkname("keyword", keywords[kwd]), entry->valuestring);
|
2014-06-17 05:41:08 +02:00
|
|
|
}
|
2014-06-16 20:30:23 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("invalid type %d for keyword `%s`", child->type, child->string);
|
2014-06-16 20:30:23 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("unknown keyword `%s` for locale `%s`", child->string, locale_name(lang));
|
2014-06-16 20:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_skills(cJSON *json) {
|
2014-06-21 17:53:14 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("skills is not a json object: %d", json->type);
|
2014-06-21 17:53:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-21 17:53:14 +02:00
|
|
|
struct locale * lang = get_or_create_locale(child->string);
|
|
|
|
json_skill(child, lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_keywords(cJSON *json) {
|
2014-06-16 20:30:23 +02:00
|
|
|
cJSON *child;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("keywords is not a json object: %d", json->type);
|
2014-06-16 20:30:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-16 20:30:23 +02:00
|
|
|
struct locale * lang = get_or_create_locale(child->string);
|
|
|
|
json_keyword(child, lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:16:17 +02:00
|
|
|
static void json_settings(cJSON *json) {
|
|
|
|
cJSON *child;
|
|
|
|
if (json->type != cJSON_Object) {
|
|
|
|
log_error("settings is not a json object: %d", json->type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
if (child->valuestring) {
|
|
|
|
set_param(&global.parameters, child->string, child->valuestring);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char value[32];
|
2015-09-11 09:17:07 +02:00
|
|
|
if (child->type == cJSON_Number && child->valuedouble && child->valueint<child->valuedouble) {
|
|
|
|
_snprintf(value, sizeof(value), "%lf", child->valuedouble);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_snprintf(value, sizeof(value), "%d", child->valueint);
|
|
|
|
}
|
2015-09-10 23:16:17 +02:00
|
|
|
set_param(&global.parameters, child->string, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:21:28 +02:00
|
|
|
static void json_races(cJSON *json) {
|
2014-06-08 07:17:48 +02:00
|
|
|
cJSON *child;
|
2014-10-29 07:50:06 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("races is not a json object: %d", json->type);
|
2014-06-08 07:17:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-29 07:50:06 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-06-13 07:14:07 +02:00
|
|
|
json_race(child, rc_get_or_create(child->string));
|
2014-06-08 07:17:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:40:09 +01:00
|
|
|
const char * json_relpath;
|
|
|
|
|
|
|
|
static void json_include(cJSON *json) {
|
2014-10-29 07:50:06 +01:00
|
|
|
cJSON *child;
|
|
|
|
if (json->type != cJSON_Array) {
|
|
|
|
log_error("config is not a json array: %d", json->type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (child = json->child; child; child = child->next) {
|
2014-10-29 19:40:09 +01:00
|
|
|
FILE *F;
|
|
|
|
if (json_relpath) {
|
|
|
|
char name[MAX_PATH];
|
|
|
|
_snprintf(name, sizeof(name), "%s/%s", json_relpath, child->valuestring);
|
|
|
|
F = fopen(name, "rt");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F = fopen(child->valuestring, "rt");
|
|
|
|
}
|
2014-10-29 07:50:06 +01:00
|
|
|
if (F) {
|
2015-11-04 12:29:51 +01:00
|
|
|
long pos;
|
2014-10-29 07:50:06 +01:00
|
|
|
fseek(F, 0, SEEK_END);
|
2015-11-04 12:29:51 +01:00
|
|
|
pos = ftell(F);
|
2014-10-29 07:50:06 +01:00
|
|
|
rewind(F);
|
2015-11-04 12:29:51 +01:00
|
|
|
if (pos > 0) {
|
|
|
|
cJSON *config;
|
|
|
|
char *data;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
data = malloc(pos + 1);
|
|
|
|
sz = fread(data, 1, (size_t)pos, F);
|
|
|
|
data[sz] = 0;
|
|
|
|
config = cJSON_Parse(data);
|
|
|
|
free(data);
|
|
|
|
if (config) {
|
|
|
|
json_config(config);
|
|
|
|
cJSON_Delete(config);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("invalid JSON, could not parse %s", child->valuestring);
|
|
|
|
}
|
2015-09-11 22:41:57 +02:00
|
|
|
}
|
2015-11-04 12:29:51 +01:00
|
|
|
fclose(F);
|
2014-10-29 07:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 07:17:48 +02:00
|
|
|
void json_config(cJSON *json) {
|
|
|
|
cJSON *child;
|
2015-09-11 22:41:57 +02:00
|
|
|
assert(json);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (json->type != cJSON_Object) {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("config is not a json object: %d", json->type);
|
2014-06-08 07:17:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-29 19:40:09 +01:00
|
|
|
reset_locales();
|
2015-01-30 20:37:14 +01:00
|
|
|
for (child = json->child; child; child = child->next) {
|
|
|
|
if (strcmp(child->string, "races") == 0) {
|
2014-06-14 02:36:05 +02:00
|
|
|
json_races(child);
|
|
|
|
}
|
2014-10-29 07:50:06 +01:00
|
|
|
else if (strcmp(child->string, "items") == 0) {
|
2014-06-25 07:54:10 +02:00
|
|
|
json_items(child);
|
|
|
|
}
|
2014-10-29 19:40:09 +01:00
|
|
|
else if (strcmp(child->string, "include") == 0) {
|
|
|
|
json_include(child);
|
2014-10-29 07:50:06 +01:00
|
|
|
}
|
|
|
|
else if (strcmp(child->string, "ships") == 0) {
|
2014-06-14 02:36:05 +02:00
|
|
|
json_ships(child);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "strings") == 0) {
|
2014-06-28 19:37:40 +02:00
|
|
|
json_strings(child);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "directions") == 0) {
|
2014-06-16 07:17:08 +02:00
|
|
|
json_directions(child);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "keywords") == 0) {
|
2014-06-16 20:30:23 +02:00
|
|
|
json_keywords(child);
|
|
|
|
}
|
2015-09-10 23:16:17 +02:00
|
|
|
else if (strcmp(child->string, "settings") == 0) {
|
|
|
|
json_settings(child);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (strcmp(child->string, "skills") == 0) {
|
2014-06-21 17:53:14 +02:00
|
|
|
json_skills(child);
|
|
|
|
}
|
2014-07-03 06:15:22 +02:00
|
|
|
else if (strcmp(child->string, "buildings") == 0) {
|
2014-06-14 02:36:05 +02:00
|
|
|
json_buildings(child);
|
|
|
|
}
|
2014-07-03 06:15:22 +02:00
|
|
|
else if (strcmp(child->string, "spells") == 0) {
|
|
|
|
json_spells(child);
|
|
|
|
}
|
2015-09-12 12:49:12 +02:00
|
|
|
else if (strcmp(child->string, "prefixes") == 0) {
|
|
|
|
json_prefixes(child);
|
|
|
|
}
|
2015-09-12 17:37:29 +02:00
|
|
|
else if (strcmp(child->string, "disabled") == 0) {
|
|
|
|
json_disable_features(child);
|
2015-09-12 17:06:47 +02:00
|
|
|
}
|
2014-07-03 06:15:22 +02:00
|
|
|
else if (strcmp(child->string, "terrains") == 0) {
|
2014-06-14 02:36:05 +02:00
|
|
|
json_terrains(child);
|
2015-09-11 21:14:10 +02:00
|
|
|
init_terrains();
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-08-11 12:02:16 +02:00
|
|
|
log_error("config contains unknown attribute %s", child->string);
|
2014-06-14 02:36:05 +02:00
|
|
|
}
|
2014-06-13 07:14:07 +02:00
|
|
|
}
|
2014-06-08 07:17:48 +02:00
|
|
|
}
|
2014-06-09 18:54:48 +02:00
|
|
|
|