2014-03-15 19:29:11 +01:00
|
|
|
#include "platform.h"
|
2014-03-07 17:03:53 +01:00
|
|
|
|
2014-03-15 06:30:07 +01:00
|
|
|
#include "json.h"
|
2014-03-07 17:03:53 +01:00
|
|
|
|
2016-11-23 18:56:40 +01:00
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/log.h>
|
2014-03-22 09:56:00 +01:00
|
|
|
#include <kernel/plane.h>
|
2014-03-06 16:15:43 +01:00
|
|
|
#include <kernel/region.h>
|
2014-03-07 17:03:53 +01:00
|
|
|
#include <kernel/faction.h>
|
2014-03-06 16:15:43 +01:00
|
|
|
#include <kernel/terrain.h>
|
2014-03-06 17:05:26 +01:00
|
|
|
#include <stream.h>
|
2014-03-06 16:15:43 +01:00
|
|
|
#include "cJSON.h"
|
|
|
|
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-03-13 15:33:44 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-03-15 06:30:07 +01:00
|
|
|
int json_import(struct stream * out) {
|
2014-03-16 09:51:08 +01:00
|
|
|
cJSON *json, *child;
|
|
|
|
char buffer[1024], *data = 0;
|
|
|
|
size_t sz = 0;
|
|
|
|
assert(out && out->api);
|
|
|
|
while (!out->api->readln(out->handle, buffer, sizeof(buffer))) {
|
|
|
|
size_t len = strlen(buffer);
|
2016-11-23 18:56:40 +01:00
|
|
|
char *tmp;
|
|
|
|
tmp = (char *)realloc(data, sz + len + 1);
|
|
|
|
if (!tmp) {
|
|
|
|
log_fatal("allocation failure in json_import");
|
|
|
|
free(data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
data = tmp;
|
2014-03-16 09:51:08 +01:00
|
|
|
memcpy(data + sz, buffer, len);
|
|
|
|
sz += len;
|
|
|
|
data[sz] = 0;
|
|
|
|
}
|
|
|
|
json = cJSON_Parse(data);
|
2016-11-23 18:56:40 +01:00
|
|
|
free(data);
|
2014-03-16 09:51:08 +01:00
|
|
|
child = cJSON_GetObjectItem(json, "regions");
|
2015-01-30 20:37:14 +01:00
|
|
|
if (child && child->type == cJSON_Object) {
|
2014-03-16 09:51:08 +01:00
|
|
|
cJSON *j;
|
|
|
|
for (j = child->child; j; j = j->next) {
|
|
|
|
cJSON *attr;
|
2015-05-15 11:19:26 +02:00
|
|
|
int id = 0;
|
2014-03-16 09:51:08 +01:00
|
|
|
int x = 0, y = 0;
|
|
|
|
region * r;
|
|
|
|
|
2015-05-15 11:19:26 +02:00
|
|
|
id = atoi(j->string);
|
2014-03-16 09:51:08 +01:00
|
|
|
if ((attr = cJSON_GetObjectItem(j, "x")) != 0 && attr->type == cJSON_Number) x = attr->valueint;
|
|
|
|
if ((attr = cJSON_GetObjectItem(j, "y")) != 0 && attr->type == cJSON_Number) y = attr->valueint;
|
|
|
|
r = new_region(x, y, 0, id);
|
|
|
|
if ((attr = cJSON_GetObjectItem(j, "type")) != 0 && attr->type == cJSON_String) {
|
|
|
|
const terrain_type *terrain = get_terrain(attr->valuestring);
|
|
|
|
terraform_region(r, terrain);
|
|
|
|
}
|
|
|
|
if ((attr = cJSON_GetObjectItem(j, "name")) != 0 && attr->type == cJSON_String) {
|
|
|
|
region_setname(r, attr->valuestring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
2014-03-15 06:30:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-21 16:34:36 +02:00
|
|
|
int json_export(stream * out, int flags) {
|
2014-03-06 16:15:43 +01:00
|
|
|
cJSON *json, *root = cJSON_CreateObject();
|
2014-03-13 15:33:44 +01:00
|
|
|
assert(out && out->api);
|
2014-03-16 19:17:34 +01:00
|
|
|
if (regions && (flags & EXPORT_REGIONS)) {
|
2017-01-07 21:09:39 +01:00
|
|
|
char id[32]; // TODO: static_assert(INT_MAX < 10^32)
|
2014-03-07 17:03:53 +01:00
|
|
|
region * r;
|
2014-03-22 09:56:00 +01:00
|
|
|
plane * p;
|
|
|
|
cJSON_AddItemToObject(root, "planes", json = cJSON_CreateObject());
|
2015-01-30 20:37:14 +01:00
|
|
|
for (p = planes; p; p = p->next) {
|
|
|
|
cJSON *data;
|
2017-01-07 21:09:39 +01:00
|
|
|
sprintf(id, "%d", p->id); // safe, unless int is bigger than 64 bit
|
2015-01-30 20:37:14 +01:00
|
|
|
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
|
|
|
|
cJSON_AddNumberToObject(data, "x", p->minx);
|
|
|
|
cJSON_AddNumberToObject(data, "y", p->miny);
|
|
|
|
cJSON_AddNumberToObject(data, "width", p->maxx - p->minx);
|
|
|
|
cJSON_AddNumberToObject(data, "height", p->maxy - p->miny);
|
|
|
|
if (p->name) cJSON_AddStringToObject(data, "name", p->name);
|
2014-03-22 09:56:00 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 17:03:53 +01:00
|
|
|
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
|
|
|
|
for (r = regions; r; r = r->next) {
|
|
|
|
cJSON *data;
|
2017-01-07 21:09:39 +01:00
|
|
|
sprintf(id, "%d", r->uid); // safe, unless int is bigger than 64 bit
|
2014-03-07 17:03:53 +01:00
|
|
|
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
|
|
|
|
cJSON_AddNumberToObject(data, "x", r->x);
|
|
|
|
cJSON_AddNumberToObject(data, "y", r->y);
|
|
|
|
cJSON_AddStringToObject(data, "type", r->terrain->_name);
|
2014-03-16 22:08:37 +01:00
|
|
|
if (r->land) {
|
|
|
|
cJSON_AddStringToObject(data, "name", r->land->name);
|
|
|
|
}
|
2014-03-07 17:03:53 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 19:17:34 +01:00
|
|
|
if (factions && (flags & EXPORT_FACTIONS)) {
|
2014-03-07 17:03:53 +01:00
|
|
|
faction *f;
|
|
|
|
cJSON_AddItemToObject(root, "factions", json = cJSON_CreateObject());
|
|
|
|
for (f = factions; f; f = f->next) {
|
|
|
|
cJSON *data;
|
|
|
|
cJSON_AddItemToObject(json, itoa36(f->no), data = cJSON_CreateObject());
|
|
|
|
cJSON_AddStringToObject(data, "name", f->name);
|
|
|
|
cJSON_AddStringToObject(data, "email", f->email);
|
2015-09-06 19:04:04 +02:00
|
|
|
cJSON_AddNumberToObject(data, "score", (double)f->score);
|
2014-03-07 17:03:53 +01:00
|
|
|
}
|
2014-03-06 17:05:26 +01:00
|
|
|
}
|
2014-03-07 17:03:53 +01:00
|
|
|
if (flags) {
|
2014-03-06 17:05:26 +01:00
|
|
|
char *tok, *output;
|
2014-03-16 10:12:49 +01:00
|
|
|
output = cJSON_Print(root);
|
2014-03-06 17:05:26 +01:00
|
|
|
tok = strtok(output, "\n\r");
|
|
|
|
while (tok) {
|
|
|
|
if (tok[0]) {
|
|
|
|
out->api->writeln(out->handle, tok);
|
|
|
|
}
|
|
|
|
tok = strtok(NULL, "\n\r");
|
|
|
|
}
|
|
|
|
free(output);
|
2014-03-06 16:15:43 +01:00
|
|
|
}
|
2014-03-07 17:03:53 +01:00
|
|
|
cJSON_Delete(root);
|
2014-03-13 15:33:44 +01:00
|
|
|
return 0;
|
2014-03-06 16:15:43 +01:00
|
|
|
}
|