From 224952a962edce0e5f4cbc928e9a6fdc0efa3258 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 29 Apr 2018 07:47:37 +0100 Subject: [PATCH] Check for possible corrupt save files and prevent loading. --- locale/blobwarsAttrition.pot | 8 ++- src/defs.h | 2 + src/game/game.c | 114 +++++++++++++++++++---------------- src/game/title.c | 8 ++- src/system/strings.c | 3 + 5 files changed, 81 insertions(+), 54 deletions(-) diff --git a/locale/blobwarsAttrition.pot b/locale/blobwarsAttrition.pot index e35ea6f..a3c3c66 100644 --- a/locale/blobwarsAttrition.pot +++ b/locale/blobwarsAttrition.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Blob Wars : Attrition\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-04-24 19:04:13+0100\n" +"POT-Creation-Date: 2018-04-29 07:46:49+0100\n" "PO-Revision-Date: ???\n" "Last-Translator: ???\n" "Language-Team: ???\n" @@ -265,6 +265,12 @@ msgstr "" msgid "Cells: %d / %d" msgstr "" +msgid "! Corrupt data" +msgstr "" + +msgid "- empty -" +msgstr "" + msgid "This is an information point. Bob can stand by them for a few moments to see what they have to say. There are many scattered throughout this tutorial map, to help you learn how to play. They will also appear from time to time during the actual game. Stand by them for a short time, to get more information. If you get lost, tap the radar icon in the top left, to view a map of the local area." msgstr "" diff --git a/src/defs.h b/src/defs.h index 729a868..3f0da94 100644 --- a/src/defs.h +++ b/src/defs.h @@ -429,5 +429,7 @@ enum ST_HUB_KEYS, ST_HUB_HEARTS, ST_HUB_CELLS, + ST_CORRUPT_SAVE, + ST_EMPTY_SAVE, ST_MAX }; diff --git a/src/game/game.c b/src/game/game.c index 4de71cc..bb1cac5 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -296,48 +296,55 @@ void loadGame(int slot) root = cJSON_Parse(text); - game.cells = cJSON_GetObjectItem(root, "cells")->valueint; - game.hearts = cJSON_GetObjectItem(root, "hearts")->valueint; - - statsJSON = cJSON_GetObjectItem(root, "stats"); - - for (i = 0 ; i < STAT_MAX ; i++) + if (root) { - statName = getLookupName("STAT_", i); - - if (cJSON_GetObjectItem(statsJSON, statName)) + game.cells = cJSON_GetObjectItem(root, "cells")->valueint; + game.hearts = cJSON_GetObjectItem(root, "hearts")->valueint; + + statsJSON = cJSON_GetObjectItem(root, "stats"); + + for (i = 0 ; i < STAT_MAX ; i++) { - game.stats[i] = cJSON_GetObjectItem(statsJSON, statName)->valueint; + statName = getLookupName("STAT_", i); + + if (cJSON_GetObjectItem(statsJSON, statName)) + { + game.stats[i] = cJSON_GetObjectItem(statsJSON, statName)->valueint; + } } - } - i = 0; - for (node = cJSON_GetObjectItem(root, "keys")->child ; node != NULL ; node = node->next) + i = 0; + for (node = cJSON_GetObjectItem(root, "keys")->child ; node != NULL ; node = node->next) + { + STRNCPY(game.keys[i].key, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); + game.keys[i].value.i = cJSON_GetObjectItem(node, "num")->valueint; + + i++; + } + + for (node = cJSON_GetObjectItem(root, "missions")->child ; node != NULL ; node = node->next) + { + t = malloc(sizeof(Tuple)); + memset(t, 0, sizeof(Tuple)); + game.missionStatusTail->next = t; + game.missionStatusTail = t; + + STRNCPY(t->key, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH); + t->value.i = lookup(cJSON_GetObjectItem(node, "status")->valuestring); + } + + for (node = cJSON_GetObjectItem(root, "trophies")->child ; node != NULL ; node = node->next) + { + trophy = getTrophy(cJSON_GetObjectItem(node, "id")->valuestring); + trophy->awardDate = cJSON_GetObjectItem(node, "awardDate")->valueint; + } + + cJSON_Delete(root); + } + else { - STRNCPY(game.keys[i].key, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); - game.keys[i].value.i = cJSON_GetObjectItem(node, "num")->valueint; - - i++; + printf("Corrupt save file\n"); } - - for (node = cJSON_GetObjectItem(root, "missions")->child ; node != NULL ; node = node->next) - { - t = malloc(sizeof(Tuple)); - memset(t, 0, sizeof(Tuple)); - game.missionStatusTail->next = t; - game.missionStatusTail = t; - - STRNCPY(t->key, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH); - t->value.i = lookup(cJSON_GetObjectItem(node, "status")->valuestring); - } - - for (node = cJSON_GetObjectItem(root, "trophies")->child ; node != NULL ; node = node->next) - { - trophy = getTrophy(cJSON_GetObjectItem(node, "id")->valuestring); - trophy->awardDate = cJSON_GetObjectItem(node, "awardDate")->valueint; - } - - cJSON_Delete(root); free(text); } @@ -482,29 +489,32 @@ char *getSaveWidgetLabel(char *filename) root = cJSON_Parse(text); - statsJSON = cJSON_GetObjectItem(root, "stats"); - - memset(stats, 0, sizeof(int) * STAT_MAX); - - for (i = 0 ; i < STAT_MAX ; i++) + if (root) { - statName = getLookupName("STAT_", i); + statsJSON = cJSON_GetObjectItem(root, "stats"); - if (cJSON_GetObjectItem(statsJSON, statName)) + memset(stats, 0, sizeof(int) * STAT_MAX); + + for (i = 0 ; i < STAT_MAX ; i++) { - stats[i] = cJSON_GetObjectItem(statsJSON, statName)->valueint; - } - } + statName = getLookupName("STAT_", i); - cJSON_Delete(root); + if (cJSON_GetObjectItem(statsJSON, statName)) + { + stats[i] = cJSON_GetObjectItem(statsJSON, statName)->valueint; + } + } + + cJSON_Delete(root); + + gameDone = stats[STAT_MIAS_RESCUED] + stats[STAT_TARGETS_DEFEATED] + stats[STAT_KEYS_FOUND] + stats[STAT_HEARTS_FOUND] + stats[STAT_CELLS_FOUND]; + gameTotal = game.totalMIAs + game.totalTargets + game.totalKeys + game.totalHearts + game.totalCells; + + sprintf(label, "%d%%%% - %s", getPercent(gameDone, gameTotal), timeToString(stats[STAT_TIME_PLAYED], 1)); + } free(text); - gameDone = stats[STAT_MIAS_RESCUED] + stats[STAT_TARGETS_DEFEATED] + stats[STAT_KEYS_FOUND] + stats[STAT_HEARTS_FOUND] + stats[STAT_CELLS_FOUND]; - gameTotal = game.totalMIAs + game.totalTargets + game.totalKeys + game.totalHearts + game.totalCells; - - sprintf(label, "%d%%%% - %s", getPercent(gameDone, gameTotal), timeToString(stats[STAT_TIME_PLAYED], 1)); - return label; } diff --git a/src/game/title.c b/src/game/title.c index e8c6147..61d957b 100644 --- a/src/game/title.c +++ b/src/game/title.c @@ -208,10 +208,16 @@ static void populateSaveSlotWidgets(void) { strcpy(save[i]->label, getSaveWidgetLabel(filename)); save[i]->value[0] = 1; + + if (strlen(save[i]->label) == 0) + { + STRNCPY(save[i]->label, app.strings[ST_CORRUPT_SAVE], MAX_NAME_LENGTH); + save[i]->value[0] = 0; + } } else { - strcpy(save[i]->label, "(empty)"); + STRNCPY(save[i]->label, app.strings[ST_EMPTY_SAVE], MAX_NAME_LENGTH); save[i]->value[0] = 0; } diff --git a/src/system/strings.c b/src/system/strings.c index b110666..a6bfa78 100644 --- a/src/system/strings.c +++ b/src/system/strings.c @@ -93,4 +93,7 @@ void initStrings(void) app.strings[ST_HUB_KEYS] = _("Keys: %d / %d"); app.strings[ST_HUB_HEARTS] = _("Hearts: %d / %d"); app.strings[ST_HUB_CELLS] = _("Cells: %d / %d"); + + app.strings[ST_CORRUPT_SAVE] = _("! Corrupt data"); + app.strings[ST_EMPTY_SAVE] = _("- empty -"); }