2014-06-13 02:34:57 +02:00
|
|
|
#include "bind_config.h"
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/jsonconf.h>
|
2014-06-13 17:04:06 +02:00
|
|
|
#include <util/log.h>
|
2014-06-17 17:46:22 +02:00
|
|
|
#include <util/language.h>
|
2014-06-13 02:34:57 +02:00
|
|
|
#include <cJSON.h>
|
2014-06-13 22:02:03 +02:00
|
|
|
#include <string.h>
|
2014-10-29 08:30:07 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-13 02:34:57 +02:00
|
|
|
|
2014-06-17 17:46:22 +02:00
|
|
|
#include "kernel/building.h"
|
|
|
|
#include "kernel/race.h"
|
|
|
|
#include "kernel/ship.h"
|
|
|
|
#include "kernel/spell.h"
|
|
|
|
#include "kernel/spellbook.h"
|
|
|
|
#include "kernel/terrain.h"
|
|
|
|
|
|
|
|
void config_reset(void) {
|
|
|
|
default_locale = 0;
|
|
|
|
free_locales();
|
|
|
|
free_spells();
|
|
|
|
free_buildingtypes();
|
|
|
|
free_shiptypes();
|
|
|
|
free_races();
|
|
|
|
free_spellbooks();
|
|
|
|
}
|
|
|
|
|
2014-06-13 22:02:03 +02:00
|
|
|
int config_parse(const char *json)
|
2014-06-13 02:34:57 +02:00
|
|
|
{
|
|
|
|
cJSON * conf = cJSON_Parse(json);
|
|
|
|
if (conf) {
|
2015-01-30 20:37:14 +01:00
|
|
|
json_config(conf);
|
2014-06-13 02:34:57 +02:00
|
|
|
cJSON_Delete(conf);
|
2015-01-08 20:55:29 +01:00
|
|
|
init_locales();
|
2014-06-13 22:02:03 +02:00
|
|
|
return 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-06-13 22:02:03 +02:00
|
|
|
int line;
|
|
|
|
char buffer[10];
|
|
|
|
const char *xp = json, *lp, *ep = cJSON_GetErrorPtr();
|
2015-01-30 20:37:14 +01:00
|
|
|
for (line = 1, lp = xp; xp && xp<ep; ++line, lp = xp + 1) {
|
2014-06-18 06:33:42 +02:00
|
|
|
xp = strchr(lp, '\n');
|
2015-01-30 20:37:14 +01:00
|
|
|
if (xp >= ep) break;
|
2014-06-13 22:02:03 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
xp = (ep > json + 10) ? ep - 10 : json;
|
2014-06-13 22:02:03 +02:00
|
|
|
strncpy(buffer, xp, sizeof(buffer));
|
|
|
|
buffer[9] = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
log_error("json parse error in line %d, position %d, near `%s`\n", line, ep - lp, buffer);
|
2014-06-13 02:34:57 +02:00
|
|
|
}
|
2014-06-17 17:46:22 +02:00
|
|
|
return 1;
|
2014-06-13 02:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:40:09 +01:00
|
|
|
int config_read(const char *filename, const char * relpath)
|
2014-06-13 02:34:57 +02:00
|
|
|
{
|
2014-10-29 19:40:09 +01:00
|
|
|
char name[MAX_PATH];
|
|
|
|
FILE *F;
|
|
|
|
|
|
|
|
json_relpath = relpath;
|
|
|
|
if (relpath) {
|
|
|
|
_snprintf(name, sizeof(name), "%s/%s", relpath, filename);
|
|
|
|
F = fopen(name, "rt");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F = fopen(filename, "rt");
|
|
|
|
}
|
2014-10-29 08:30:07 +01:00
|
|
|
if (F) {
|
|
|
|
int result;
|
|
|
|
char *data;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
fseek(F, 0, SEEK_END);
|
|
|
|
sz = ftell(F);
|
|
|
|
rewind(F);
|
|
|
|
data = malloc(sz);
|
|
|
|
fread(data, 1, sz, F);
|
|
|
|
fclose(F);
|
|
|
|
result = config_parse(data);
|
|
|
|
free(data);
|
|
|
|
return result;
|
|
|
|
}
|
2014-06-13 22:02:03 +02:00
|
|
|
return 1;
|
2014-06-13 02:34:57 +02:00
|
|
|
}
|
2014-06-13 22:02:03 +02:00
|
|
|
|