2014-06-13 02:34:57 +02:00
|
|
|
#include "bind_config.h"
|
|
|
|
|
|
|
|
#include <platform.h>
|
2016-02-05 23:10:05 +01:00
|
|
|
#include <kernel/config.h>
|
2014-06-13 02:34:57 +02:00
|
|
|
#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) {
|
2016-02-05 23:10:05 +01:00
|
|
|
join_path(relpath, filename, name, sizeof(name));
|
|
|
|
F = fopen(name, "r");
|
2014-10-29 19:40:09 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-02-05 23:10:05 +01:00
|
|
|
F = fopen(filename, "r");
|
2014-10-29 19:40:09 +01:00
|
|
|
}
|
2014-10-29 08:30:07 +01:00
|
|
|
if (F) {
|
2015-10-29 12:04:52 +01:00
|
|
|
long size;
|
2014-10-29 08:30:07 +01:00
|
|
|
|
|
|
|
fseek(F, 0, SEEK_END);
|
2015-10-29 12:04:52 +01:00
|
|
|
size = ftell(F);
|
2014-10-29 08:30:07 +01:00
|
|
|
rewind(F);
|
2015-10-29 12:04:52 +01:00
|
|
|
if (size > 0) {
|
2015-10-29 16:25:52 +01:00
|
|
|
int result;
|
|
|
|
char *data;
|
2015-10-29 12:04:52 +01:00
|
|
|
size_t sz = (size_t)size;
|
2015-10-29 16:25:52 +01:00
|
|
|
|
2015-10-30 12:08:09 +01:00
|
|
|
data = malloc(sz+1);
|
2015-10-30 11:59:05 +01:00
|
|
|
sz = fread(data, 1, sz, F);
|
|
|
|
data[sz] = 0;
|
2015-10-29 11:20:09 +01:00
|
|
|
fclose(F);
|
2015-10-29 16:25:52 +01:00
|
|
|
result = config_parse(data);
|
|
|
|
free(data);
|
|
|
|
return result;
|
2015-10-29 11:20:09 +01:00
|
|
|
}
|
2015-10-29 16:25:52 +01:00
|
|
|
fclose(F);
|
2014-10-29 08:30:07 +01:00
|
|
|
}
|
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
|
|
|
|