Add requireStrMin save-JSON shorthand
Replaces the hand-rolled mapName missing/null/empty check in saveSlotVer1ReadJSON with a reusable macro. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -481,6 +481,26 @@
|
|||||||
#define requireString(key) \
|
#define requireString(key) \
|
||||||
if(!hasString(key)) errorThrow("Save JSON missing '%s' key", key)
|
if(!hasString(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Errors if the given key is missing, not a string, or a string shorter
|
||||||
|
* than the given minimum length - e.g. requireStrMin(key, 1) to reject a
|
||||||
|
* missing/null/empty string outright.
|
||||||
|
*
|
||||||
|
* @param key The key to check.
|
||||||
|
* @param len The minimum string length required.
|
||||||
|
*/
|
||||||
|
#define requireStrMin(key, len) { \
|
||||||
|
yyjson_val *saveJsonStrMinVal = yyjson_obj_get(object, key); \
|
||||||
|
if( \
|
||||||
|
saveJsonStrMinVal == NULL || !yyjson_is_str(saveJsonStrMinVal) || \
|
||||||
|
yyjson_get_len(saveJsonStrMinVal) < (size_t)(len) \
|
||||||
|
) { \
|
||||||
|
errorThrow( \
|
||||||
|
"Save JSON '%s' must be a string of at least length %d", key, (int)(len) \
|
||||||
|
); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a string to the current JSON object. The string is copied, so it
|
* Writes a string to the current JSON object. The string is copied, so it
|
||||||
* does not need to outlive the JSON document.
|
* does not need to outlive the JSON document.
|
||||||
|
|||||||
@@ -62,18 +62,7 @@ errorret_t saveSlotVer1ReadJSON(saveslotver1_t *slot, yyjson_val *object) {
|
|||||||
|
|
||||||
readTime("time", slot->time);
|
readTime("time", slot->time);
|
||||||
readInt32("playerLevel", slot->playerLevel, 1);
|
readInt32("playerLevel", slot->playerLevel, 1);
|
||||||
|
requireStrMin("mapName", 1);
|
||||||
// A blank/never-used slot returned early above (see saveSlotInUse -
|
|
||||||
// keyed off "name"), so name is guaranteed non-empty here - and an
|
|
||||||
// in-use slot always has a map name too (see saveSlotNewGame). Missing,
|
|
||||||
// null, or empty means this save is corrupt.
|
|
||||||
yyjson_val *mapNameVal = yyjson_obj_get(object, "mapName");
|
|
||||||
if(
|
|
||||||
mapNameVal == NULL || !yyjson_is_str(mapNameVal) ||
|
|
||||||
yyjson_get_len(mapNameVal) == 0
|
|
||||||
) {
|
|
||||||
errorThrow("Save JSON 'mapName' must be a non-empty string");
|
|
||||||
}
|
|
||||||
readString(
|
readString(
|
||||||
"mapName", slot->mapName, "", SAVE_SLOT_VER1_MAP_NAME_MAX - 1
|
"mapName", slot->mapName, "", SAVE_SLOT_VER1_MAP_NAME_MAX - 1
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user