json lump: add type and version checking

This commit is contained in:
Roman Fomin 2024-09-15 15:44:31 +07:00
parent e5dc32642c
commit 036af1d0c8
4 changed files with 58 additions and 7 deletions

View File

@ -13,11 +13,55 @@
#include "m_json.h"
#include <stdio.h>
#include "i_printf.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)

View File

@ -18,7 +18,14 @@
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);
json_t *JS_GetObject(json_t *json, const char *string);

View File

@ -136,10 +136,10 @@ skydefs_t *R_ParseSkyDefs(void)
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)
{
I_Printf(VB_ERROR, "JSON: Error parsing SKYDEFS");
JS_Close(json);
return NULL;
}

View File

@ -153,10 +153,10 @@ static void ParseLevelLayer(json_t *json, interlevellayer_t *out)
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)
{
I_Printf(VB_ERROR, "Error parsing %s", lumpname);
JS_Close(json);
return NULL;
}