diff --git a/src/m_json.c b/src/m_json.c index e4a65e43..833c7956 100644 --- a/src/m_json.c +++ b/src/m_json.c @@ -13,11 +13,55 @@ #include "m_json.h" +#include +#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) diff --git a/src/m_json.h b/src/m_json.h index d7082bd2..e595f0c7 100644 --- a/src/m_json.h +++ b/src/m_json.h @@ -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); diff --git a/src/r_skydefs.c b/src/r_skydefs.c index 8efaff50..f7e87c13 100644 --- a/src/r_skydefs.c +++ b/src/r_skydefs.c @@ -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; } diff --git a/src/wi_interlvl.c b/src/wi_interlvl.c index bff85690..59ad0a02 100644 --- a/src/wi_interlvl.c +++ b/src/wi_interlvl.c @@ -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; }