X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=saveresume.c;h=9c788d1c59f99a523a535e139c3842cc12b50aa3;hb=1af01ff91fd0fee206d096449a564996ea85abed;hp=0b71319c0dc835368eb95f0df39d1dc6e8f5d292;hpb=4b08b726f9735b5a6a17e588753a496700ff25f7;p=open-adventure.git diff --git a/saveresume.c b/saveresume.c index 0b71319..9c788d1 100644 --- a/saveresume.c +++ b/saveresume.c @@ -18,6 +18,11 @@ #include "advent.h" #include "dungeon.h" +/* + * Use this to detect endianness mismatch. Can't be unchanged by byte-swapping. + */ +#define ENDIAN_MAGIC 2317 + struct save_t save; #define IGNORE(r) do{if (r){}}while(0) @@ -28,6 +33,8 @@ int savefile(FILE *fp) memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)); if (save.version == 0) save.version = SAVE_VERSION; + if (save.canary == 0) + save.canary = ENDIAN_MAGIC; save.game = game; IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); @@ -102,7 +109,7 @@ int resume(void) #endif FILE *fp = NULL; - if (game.loc != LOC_START || game.abbrev[LOC_START] != 1) { + if (game.loc != LOC_START || game.locs[LOC_START].abbrev != 1) { rspeak(RESUME_ABANDON); if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ; @@ -136,7 +143,7 @@ int restore(FILE* fp) IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); - if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0) + if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0 || save.canary != ENDIAN_MAGIC) rspeak(BAD_SAVE); else if (save.version != SAVE_VERSION) { rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10)); @@ -183,15 +190,15 @@ bool is_valid(struct game_t valgame) } /* Bounds check for location arrays */ for (int i = 0; i <= NDWARVES; i++) { - if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS || - valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) { + if (valgame.dwarves[i].loc < -1 || valgame.dwarves[i].loc > NLOCATIONS || + valgame.dwarves[i].oldloc < -1 || valgame.dwarves[i].oldloc > NLOCATIONS) { return false; // LCOV_EXCL_LINE } } for (int i = 0; i <= NOBJECTS; i++) { - if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS || - valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) { + if (valgame.objects[i].place < -1 || valgame.objects[i].place > NLOCATIONS || + valgame.objects[i].fixed < -1 || valgame.objects[i].fixed > NLOCATIONS) { return false; // LCOV_EXCL_LINE } } @@ -211,7 +218,7 @@ bool is_valid(struct game_t valgame) int temp_tally = 0; for (int treasure = 1; treasure <= NOBJECTS; treasure++) { if (objects[treasure].is_treasure) { - if (valgame.prop[treasure] == STATE_NOTFOUND) { + if (PROP_IS_NOTFOUND2(valgame, treasure)) { ++temp_tally; } } @@ -222,8 +229,7 @@ bool is_valid(struct game_t valgame) /* Check that properties of objects aren't beyond expected */ for (obj_t obj = 0; obj <= NOBJECTS; obj++) { - /* Magic number -2 allows a STASHED version of state 1 */ - if (valgame.prop[obj] < -2 || valgame.prop[obj] > 1) { + if (PROP_IS_INVALID(valgame.objects[obj].prop)) { switch (obj) { case RUG: case DRAGON: @@ -236,11 +242,11 @@ bool is_valid(struct game_t valgame) case EGGS: case VASE: case CHAIN: - if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together + if (valgame.objects[obj].prop == 2) // There are multiple different states, but it's convenient to clump them together continue; // LCOV_EXCL_LINE /* FALLTHRU */ case BEAR: - if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD) + if (valgame.objects[BEAR].prop == CONTENTED_BEAR || valgame.objects[BEAR].prop == BEAR_DEAD) continue; /* FALLTHRU */ default: @@ -251,7 +257,7 @@ bool is_valid(struct game_t valgame) /* Check that values in linked lists for objects in locations are inside bounds */ for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) { - if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) { + if (valgame.locs[loc].atloc < NO_OBJECT || valgame.locs[loc].atloc > NOBJECTS * 2) { return false; // LCOV_EXCL_LINE } }