X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=saveresume.c;h=4cac0343a2a41f7c7a59a573ba61caa44414cf30;hb=eebc87f889b0fa1404684aa6e72dda5a5e53d96b;hp=858498235fae8d5835e4638edf02f334911ec596;hpb=3640e5cb96db9057dec008b05e69d27676ecbfbb;p=open-adventure.git diff --git a/saveresume.c b/saveresume.c index 8584982..4cac034 100644 --- a/saveresume.c +++ b/saveresume.c @@ -18,16 +18,23 @@ #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) -int savefile(FILE *fp, int32_t version) +int savefile(FILE *fp) /* Save game to file. No input or output from user. */ { - save.savetime = time(NULL); - save.mode = -1; - save.version = (version == 0) ? SAVE_VERSION : version; + 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)); @@ -85,7 +92,7 @@ int suspend(void) free(name); } - savefile(fp, SAVE_VERSION); + savefile(fp); fclose(fp); rspeak(RESUME_HELP); exit(EXIT_SUCCESS); @@ -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,9 @@ int restore(FILE* fp) IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); - if (save.version != SAVE_VERSION) { + 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)); } else if (!is_valid(save.game)) { rspeak(SAVE_TAMPERING); @@ -181,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 } } @@ -209,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 (valgame.objects[treasure].prop == STATE_NOTFOUND) { ++temp_tally; } } @@ -221,7 +230,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 (valgame.objects[obj].prop < -2 || valgame.objects[obj].prop > 1) { switch (obj) { case RUG: case DRAGON: @@ -234,11 +243,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: @@ -249,7 +258,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 } }