Fixing restart in games that have zcode in blorb files.
authorDavid Griffith <dave@661.org>
Fri, 15 May 2015 04:30:56 +0000 (21:30 -0700)
committerDavid Griffith <dave@661.org>
Fri, 15 May 2015 04:30:56 +0000 (21:30 -0700)
Copied from pull request from Bill Lash <william.lash@gmail.com> for the
master branch.

src/common/fastmem.c
src/common/quetzal.c
src/curses/ux_init.c
src/dos/bcinit.c
src/dumb/dumb_init.c

index 4fd0570252daaaf8cda8e28f21873193c1d01b5d..c1a0dfa53e0502c5359358a2cb4734b29a4d1c97 100644 (file)
@@ -58,6 +58,8 @@ extern void script_open (void);
 extern void script_close (void);
 
 extern FILE *os_load_story (void);
+extern int os_storyfile_seek (FILE * fp, long offset, int whence);
+extern int os_storyfile_tell (FILE * fp);
 
 extern zword save_quetzal (FILE *, FILE *);
 extern zword restore_quetzal (FILE *, FILE *);
@@ -321,11 +323,9 @@ void init_memory (void)
            story_size *= 2;
 
     } else {           /* some old games lack the file size entry */
-
-       fseek (story_fp, 0, SEEK_END);
-       story_size = ftell (story_fp);
-       fseek (story_fp, 64, SEEK_SET);
-
+       os_storyfile_seek (story_fp, 0, SEEK_END);
+       story_size = os_storyfile_tell (story_fp);
+       os_storyfile_seek (story_fp, 64, SEEK_SET);
     }
 
     LOW_WORD (H_CHECKSUM, h_checksum);
@@ -534,7 +534,7 @@ void z_restart (void)
 
     if (!first_restart) {
 
-       fseek (story_fp, 0, SEEK_SET);
+       os_storyfile_seek (story_fp, 0, SEEK_SET);
 
        if (fread (zmp, 1, h_dynamic_size, story_fp) != h_dynamic_size)
            os_fatal ("Story file read error");
@@ -692,7 +692,7 @@ void z_restore (void)
                    stack[i] |= fgetc (gfp);
                }
 
-               fseek (story_fp, 0, SEEK_SET);
+               os_storyfile_seek (story_fp, 0, SEEK_SET);
 
                for (addr = 0; addr < h_dynamic_size; addr++) {
                    int skip = fgetc (gfp);
@@ -981,7 +981,7 @@ void z_save (void)
                fputc ((int) lo (stack[i]), gfp);
            }
 
-           fseek (story_fp, 0, SEEK_SET);
+           os_storyfile_seek (story_fp, 0, SEEK_SET);
 
            for (addr = 0, skip = 0; addr < h_dynamic_size; addr++)
                if (zmp[addr] != fgetc (story_fp) || skip == 255 || addr + 1 == h_dynamic_size) {
@@ -1107,7 +1107,7 @@ void z_verify (void)
 
     /* Sum all bytes in story file except header bytes */
 
-    fseek (story_fp, 64, SEEK_SET);
+    os_storyfile_seek (story_fp, 64, SEEK_SET);
 
     for (i = 64; i < story_size; i++)
        checksum += fgetc (story_fp);
index c2e9f1e89559467e822749fdffcedc460e32a913..ac06055869bdaed22c9e6eacf55a907dcfce6c86 100644 (file)
 #define get_c fgetc
 #define put_c fputc
 
+/*
+ * externs
+ */
+
+extern int os_storyfile_seek(FILE * fp, long offset, int whence);
+
 typedef unsigned long zlong;
 
 /*
@@ -321,7 +327,7 @@ zword restore_quetzal (FILE *svf, FILE *stf)
            case ID_CMem:
                if (!(progress & GOT_MEMORY))   /* Don't complain if two. */
                {
-                   (void) fseek (stf, 0, SEEK_SET);
+                   (void) os_storyfile_seek (stf, 0, SEEK_SET);
                    i=0;        /* Bytes written to data area. */
                    for (; currlen > 0; --currlen)
                    {
@@ -442,7 +448,7 @@ zword save_quetzal (FILE *svf, FILE *stf)
     /* Write `CMem' chunk. */
     if ((cmempos = ftell (svf)) < 0)                   return 0;
     if (!write_chnk (svf, ID_CMem, 0))                 return 0;
-    (void) fseek (stf, 0, SEEK_SET);
+    (void) os_storyfile_seek (stf, 0, SEEK_SET);
     /* j holds current run length. */
     for (i=0, j=0, cmemlen=0; i < h_dynamic_size; ++i)
     {
index 2952704e35bc8686a8ffb760dd0c0677e73d1613..814d5dda0811c29023e4da008077a2c589c8060b 100644 (file)
@@ -568,6 +568,62 @@ FILE *os_load_story(void)
 }
 
 
+/*
+ * os_storyfile_seek
+ *
+ * Seek into a storyfile, either a standalone file or the
+ * ZCODE chunk of a blorb file.
+ *
+ */
+int os_storyfile_seek(FILE * fp, long offset, int whence)
+{
+    /* Is this a Blorb file containing Zcode? */
+    if (u_setup.exec_in_blorb)
+    {
+       switch (whence)
+       {
+           case SEEK_END:
+               return fseek(fp, blorb_res.data.startpos + blorb_res.length + offset, SEEK_SET);
+               break;
+           case SEEK_CUR:
+               return fseek(fp, offset, SEEK_CUR);
+               break;
+           case SEEK_SET:
+           default:
+               return fseek(fp, blorb_res.data.startpos + offset, SEEK_SET);
+               break;
+       }
+    }
+    else
+    {
+       return fseek(fp, offset, whence);
+    }
+
+}
+
+
+/*
+ * os_storyfile_tell
+ *
+ * Tell the position in a storyfile, either a standalone file
+ * or the ZCODE chunk of a blorb file.
+ *
+ */
+int os_storyfile_tell(FILE * fp)
+{
+    /* Is this a Blorb file containing Zcode? */
+    if (u_setup.exec_in_blorb)
+    {
+       return ftell(fp) - blorb_res.data.startpos;
+    }
+    else
+    {
+       return ftell(fp);
+    }
+
+}
+
+
 /*
  * pathopen
  *
index 4671e855d3becb5693fc562c2c2ded2d48a51ebd..f81125a3487a739f738cd0c052d23d5aa3bd326b 100644 (file)
@@ -949,3 +949,58 @@ int dos_init_blorb(void)
     return 0;\r
 }\r
 \r
+\r
+/*\r
+ * os_storyfile_seek\r
+ *\r
+ * Seek into a storyfile, either a standalone file or the \r
+ * ZCODE chunk of a blorb file\r
+ *\r
+ */\r
+int os_storyfile_seek(FILE * fp, long offset, int whence)\r
+{\r
+    /* Is this a Blorb file containing Zcode? */\r
+    if (exec_in_blorb)\r
+    {\r
+       switch (whence)\r
+       {\r
+           case SEEK_END:\r
+               return fseek(fp, blorb_res.data.startpos + blorb_res.length + offset, SEEK_SET);\r
+               break;\r
+           case SEEK_CUR:\r
+               return fseek(fp, offset, SEEK_CUR);\r
+               break;\r
+           case SEEK_SET:\r
+           default:\r
+               return fseek(fp, blorb_res.data.startpos + offset, SEEK_SET);\r
+               break;\r
+       }\r
+    }\r
+    else\r
+    {\r
+       return fseek(fp, offset, whence);\r
+    }\r
+\r
+}\r
+\r
+\r
+/*\r
+ * os_storyfile_seek\r
+ *\r
+ * Tell the position in a storyfile, either a standalone file \r
+ * or the ZCODE chunk of a blorb file\r
+ *\r
+ */\r
+int os_storyfile_tell(FILE * fp)\r
+{\r
+    /* Is this a Blorb file containing Zcode? */\r
+    if (exec_in_blorb)\r
+    {\r
+       return ftell(fp) - blorb_res.data.startpos;\r
+    }\r
+    else\r
+    {\r
+       return ftell(fp);\r
+    }\r
+\r
+}\r
index e6a8f008726647fcb45023a517dcd80163282ce2..d20b540b2682aa90afec9af3570b617fc533cff9 100644 (file)
@@ -172,6 +172,26 @@ FILE *os_load_story(void)
     return fopen(f_setup.story_file, "rb");
 }
 
+/*
+ * Seek into a storyfile, either a standalone file or the
+ * ZCODE chunk of a blorb file (dumb does not support blorb
+ * so this is just a wrapper for fseek)
+ */
+int os_storyfile_seek(FILE * fp, long offset, int whence)
+{
+    return fseek(fp, offset, whence);
+}
+
+/*
+ * Tell the position in a storyfile, either a standalone file
+ * or the ZCODE chunk of a blorb file (dumb does not support
+ * blorb so this is just a wrapper for fseek)
+ */
+int os_storyfile_tell(FILE * fp)
+{
+    return ftell(fp);
+}
+
 void os_init_setup(void)
 {
        f_setup.attribute_assignment = 0;