From: Aaron Traas Date: Tue, 27 Jun 2017 14:46:26 +0000 (-0400) Subject: Allow cheat to generate save files with bogus numbers. X-Git-Tag: 1.1~76 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=0a30176a8c60832e140991c177e8924c7a9ad6d5;p=open-adventure.git Allow cheat to generate save files with bogus numbers. Also remove pre-built save file for resumefail.log --- diff --git a/advent.h b/advent.h index cf529ca..2c95e97 100644 --- a/advent.h +++ b/advent.h @@ -127,7 +127,7 @@ extern unsigned long get_next_lcg_value(void); extern long randrange(long); extern long score(enum termination); extern void terminate(enum termination) __attribute__((noreturn)); -extern int savefile(FILE *); +extern int savefile(FILE *, long); extern int suspend(void); extern int resume(void); extern int restore(FILE *); diff --git a/cheat.c b/cheat.c index cb20eb7..fecfdc2 100644 --- a/cheat.c +++ b/cheat.c @@ -25,10 +25,11 @@ int main(int argc, char *argv[]) char *savefilename = NULL; long numdie = 0; long saved = 1; + long version = 0; /* Options. */ - const char* opts = "d:s:o:"; - const char* usage = "Usage: %s [-d numdie] [-s numsaves] -o savefilename \n"; + const char* opts = "d:s:v:o:"; + const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n"; while ((ch = getopt(argc, argv, opts)) != EOF) { switch (ch) { case 'd': @@ -37,6 +38,9 @@ int main(int argc, char *argv[]) case 's': saved = (long)atoi(optarg); break; + case 'v': + version = (long)atoi(optarg);; + break; case 'o': savefilename = optarg; break; @@ -47,6 +51,8 @@ int main(int argc, char *argv[]) " -d number of deaths. Signed integer value.'\n"); fprintf(stderr, " -s number of saves. Signed integer value.\n"); + fprintf(stderr, + " -v version number of save format.\n"); fprintf(stderr, " -o file name of save game to write.\n"); exit(-1); @@ -87,11 +93,13 @@ int main(int argc, char *argv[]) fp = fopen(savefilename, WRITE_MODE); if (fp == NULL) { - printf("Can't open file %s. Exiting.\n", savefilename); + fprintf(stderr, + "Can't open file %s. Exiting.\n", savefilename); exit(-1); } - savefile(fp); + savefile(fp, version); + printf("cheat: %s created.\n", savefilename); return 0; } diff --git a/saveresume.c b/saveresume.c index 3546378..f4c694f 100644 --- a/saveresume.c +++ b/saveresume.c @@ -27,7 +27,7 @@ struct save_t { }; struct save_t save; -int savefile(FILE *fp) +int savefile(FILE *fp, long version) /* Save game to file. No input or output from user. */ { long i, k; @@ -35,7 +35,9 @@ int savefile(FILE *fp) k = i + 650 * k; save.savetime = k; save.mode = -1; - save.version = VRSION; + + save.version = (version == 0) ? VRSION : version; + memcpy(&save.game, &game, sizeof(struct game_t)); IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); return(0); @@ -68,8 +70,7 @@ int suspend(void) linenoiseFree(name); } - savefile(fp); - IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); + savefile(fp, VRSION); fclose(fp); rspeak(RESUME_HELP); exit(0); diff --git a/tests/Makefile b/tests/Makefile index cbef142..21fceb7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -37,8 +37,10 @@ buildregress: done; \ rm -f scratch.tmp regress: - $(ECHO) -n "Generate cheat file: " + $(ECHO) -n "Generate save file with -1000 deaths: " ../cheat -d -1000 -o cheat_numdie.adv + $(ECHO) -n "Generate save file with version -1337: " + ../cheat -v -1337 -o resume_badversion.adv @for file in $(TESTLOADS); do \ $(ECHO) -n " $${file} "; grep '##' $${file}.log || echo ' ## (no description)'; \ OPTS=`sed -n /#options:/s///p <$${file}.log`; \ diff --git a/tests/resume_badversion.testsave b/tests/resume_badversion.testsave deleted file mode 100644 index ce3206a..0000000 Binary files a/tests/resume_badversion.testsave and /dev/null differ diff --git a/tests/resumefail2.log b/tests/resumefail2.log index 92d7ab9..e8dcaf8 100644 --- a/tests/resumefail2.log +++ b/tests/resumefail2.log @@ -3,4 +3,4 @@ n seed 1240742801 resume y -resume_badversion.testsave \ No newline at end of file +resume_badversion.adv \ No newline at end of file