mirror of
https://github.com/fabiangreffrath/woof.git
synced 2025-09-24 04:29:34 -04:00
json lump: add type and version checking
This commit is contained in:
parent
e5dc32642c
commit
036af1d0c8
48
src/m_json.c
48
src/m_json.c
@ -13,11 +13,55 @@
|
|||||||
|
|
||||||
#include "m_json.h"
|
#include "m_json.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "i_printf.h"
|
||||||
|
|
||||||
#include "cjson/cJSON.h"
|
#include "cjson/cJSON.h"
|
||||||
|
|
||||||
json_t *JS_Open(const char *data)
|
json_t *JS_Open(const char *type, version_t version, const char *data)
|
||||||
{
|
{
|
||||||
return cJSON_Parse(data);
|
const char *s;
|
||||||
|
|
||||||
|
json_t *json = cJSON_Parse(data);
|
||||||
|
if (json == NULL)
|
||||||
|
{
|
||||||
|
I_Printf(VB_ERROR, "%s: error before %s", type, cJSON_GetErrorPtr());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_t *js_type = JS_GetObject(json, "type");
|
||||||
|
if (!JS_IsString(js_type))
|
||||||
|
{
|
||||||
|
I_Printf(VB_ERROR, "%s: no type string", type);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
s = JS_GetString(js_type);
|
||||||
|
if (strcmp(s, type))
|
||||||
|
{
|
||||||
|
I_Printf(VB_ERROR, "%s: wrong type %s", type, s);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_t *js_version = JS_GetObject(json, "version");
|
||||||
|
if (!JS_IsString(js_version))
|
||||||
|
{
|
||||||
|
I_Printf(VB_ERROR, "%s: no version string", type);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
s = JS_GetString(js_version);
|
||||||
|
version_t v = {0};
|
||||||
|
sscanf(s, "%d.%d.%d", &v.major, &v.minor, &v.revision);
|
||||||
|
if (v.major != version.major || v.minor != version.minor
|
||||||
|
|| v.revision != version.revision)
|
||||||
|
{
|
||||||
|
I_Printf(VB_ERROR, "%s: wrong version %d.%d.%d", type, v.major, v.minor,
|
||||||
|
v.revision);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JS_Close(json_t *json)
|
void JS_Close(json_t *json)
|
||||||
|
@ -18,7 +18,14 @@
|
|||||||
|
|
||||||
typedef struct cJSON json_t;
|
typedef struct cJSON json_t;
|
||||||
|
|
||||||
json_t *JS_Open(const char *data);
|
typedef struct
|
||||||
|
{
|
||||||
|
int major;
|
||||||
|
int minor;
|
||||||
|
int revision;
|
||||||
|
} version_t;
|
||||||
|
|
||||||
|
json_t *JS_Open(const char *type, version_t version, const char *data);
|
||||||
void JS_Close(json_t *json);
|
void JS_Close(json_t *json);
|
||||||
|
|
||||||
json_t *JS_GetObject(json_t *json, const char *string);
|
json_t *JS_GetObject(json_t *json, const char *string);
|
||||||
|
@ -136,10 +136,10 @@ skydefs_t *R_ParseSkyDefs(void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t *json = JS_Open(W_CacheLumpNum(lumpnum, PU_CACHE));
|
json_t *json = JS_Open("skydefs", (version_t){1, 0, 0},
|
||||||
|
W_CacheLumpNum(lumpnum, PU_CACHE));
|
||||||
if (json == NULL)
|
if (json == NULL)
|
||||||
{
|
{
|
||||||
I_Printf(VB_ERROR, "JSON: Error parsing SKYDEFS");
|
|
||||||
JS_Close(json);
|
JS_Close(json);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -153,10 +153,10 @@ static void ParseLevelLayer(json_t *json, interlevellayer_t *out)
|
|||||||
|
|
||||||
interlevel_t *WI_ParseInterlevel(const char *lumpname)
|
interlevel_t *WI_ParseInterlevel(const char *lumpname)
|
||||||
{
|
{
|
||||||
json_t *json = JS_Open(W_CacheLumpName(lumpname, PU_CACHE));
|
json_t *json = JS_Open("interlevel", (version_t){1, 0, 0},
|
||||||
|
W_CacheLumpName(lumpname, PU_CACHE));
|
||||||
if (json == NULL)
|
if (json == NULL)
|
||||||
{
|
{
|
||||||
I_Printf(VB_ERROR, "Error parsing %s", lumpname);
|
|
||||||
JS_Close(json);
|
JS_Close(json);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user