2014-03-15 19:29:11 +01:00
|
|
|
#include "platform.h"
|
2014-03-07 17:03:53 +01:00
|
|
|
#include <util/base36.h>
|
|
|
|
|
2014-03-15 06:30:07 +01:00
|
|
|
#include "json.h"
|
2014-03-07 17:03:53 +01:00
|
|
|
|
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);
|
|
|
|
data = (char *)realloc(data, sz + len + 1);
|
|
|
|
memcpy(data + sz, buffer, len);
|
|
|
|
sz += len;
|
|
|
|
data[sz] = 0;
|
|
|
|
}
|
|
|
|
json = cJSON_Parse(data);
|
|
|
|
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;
|
|
|
|
unsigned int id = 0;
|
|
|
|
int x = 0, y = 0;
|
|
|
|
region * r;
|
|
|
|
|
|
|
|
id = (unsigned int)atol(j->string);
|
|
|
|
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)) {
|
2014-03-22 09:56:00 +01:00
|
|
|
char id[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;
|
|
|
|
_snprintf(id, sizeof(id), "%u", p->id);
|
|
|
|
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;
|
2014-03-15 19:29:11 +01:00
|
|
|
_snprintf(id, sizeof(id), "%u", r->uid);
|
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);
|
|
|
|
cJSON_AddNumberToObject(data, "score", f->score);
|
|
|
|
}
|
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
|
|
|
}
|