From 0a30176a8c60832e140991c177e8924c7a9ad6d5 Mon Sep 17 00:00:00 2001 From: Aaron Traas Date: Tue, 27 Jun 2017 10:46:26 -0400 Subject: [PATCH] Allow cheat to generate save files with bogus numbers. Also remove pre-built save file for resumefail.log --- advent.h | 2 +- cheat.c | 16 ++++++++++++---- saveresume.c | 9 +++++---- tests/Makefile | 4 +++- tests/resume_badversion.testsave | Bin 7880 -> 0 bytes tests/resumefail2.log | 2 +- 6 files changed, 22 insertions(+), 11 deletions(-) delete mode 100644 tests/resume_badversion.testsave 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 ce3206ade86e3054a2c0eae4fcb092cdde2f6b1d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7880 zcmeI1+e%eY6ozNn$<&TQCp#gAg_(Au6J?Y)Jwtc^MJUn=L=r6Ua2Hhc23-_g_5fW* z*FA;r&_%V|`~T8@8oF1w!FF@s{Nw+}7<0`rV6VBxS~KI{o~F|8yMCX)R*e2lKh>08 z4>wedw5yDtK5i}JW~m#5z+)NDg-3*E35al8zio6PU@ zfC>AJpA$bXydb=2aS=TfIsOxUrcUa-WOX!(Ulwi`9=15>RT(V^!X3t;m8VMB67G}v zO!(WwR7pF%6#7T&n5jpN&xy~Q?zBU~4R?sQ2m0U2CxXP2(r*U-^(uF3W4CRCuZ$O^ z{Y4XM6u(#Na{oIn-X-i4G8c)i8Swv%7TjqL<~(FL`-kIqT+JWFlXpu2@Nr8(2cO74 zZAcn%5mp;V{(BOH=#Cb)=9IcwnCrtcuH1o*a`XT6=rb+^yfe_R1$u9w^Zp=k7nZMB z#&vP-Kjuz-F4Txe?|B~9=z4kC+<|I3@J2xk)dc65+yQlfc^zafXAbhYn>mO1XF%%= z2|15;i;oIP1m=C_s0r!qmN0C5N_^6guvrXY`Qt&!=6yfzAsH`( zTW}aY5`QbCpL@jV1Dxm7Hu;%D`OY9Y-IXyYq|Om>`c6D`GjCGoO9BkviGMIX)7*jF Uf%@qHbJu#s`ssABO8or$0U{EIp#T5? 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 -- 2.31.1