Fixing restart in games that have zcode in blorb files.
authorBill Lash <william.lash@gmail.com>
Fri, 15 May 2015 01:42:05 +0000 (20:42 -0500)
committerBill Lash <william.lash@gmail.com>
Fri, 15 May 2015 01:42:05 +0000 (20:42 -0500)
src/common/fastmem.c
src/common/quetzal.c
src/curses/ux_init.c
src/dos/bcinit.c
src/dumb/dumb_init.c

index 07a254e9840d48cdbc442514eddc60c59b5ac5f4..f29ff2da3b18836238104d81a8deec7cab1ca890 100644 (file)
@@ -59,6 +59,8 @@ extern void script_close (void);
 
 extern FILE *os_path_open (const char *, const char *);
 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 *);
@@ -327,9 +329,9 @@ void init_memory (void)
 
     } 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);
 
     }
 
@@ -539,7 +541,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");
@@ -697,7 +699,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);
@@ -986,7 +988,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) {
@@ -1112,7 +1114,8 @@ 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 b7bf62be7d2f7f4ac69263007a2b43948ffe4522..b11b195fddb96a80a5f37ae65e4f5d12353f12a0 100644 (file)
@@ -622,6 +622,53 @@ FILE *os_load_story(void)
     return fp;
 }
 
+/*
+ * 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);
+    }
+
+}
+
+/*
+ * 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..2d426bcfeeea85f8ffb02595c0d216a469f2f933 100644 (file)
@@ -949,3 +949,51 @@ int dos_init_blorb(void)
     return 0;\r
 }\r
 \r
+/*\r
+ * Seek into a storyfile, either a standalone file or the \r
+ * ZCODE chunk of a blorb file\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
+ * Tell the position in a storyfile, either a standalone file \r
+ * or the ZCODE chunk of a blorb file\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
+\r
index ec900dd1bba49f7524eb9b1d33ec705b71ab9282..2256687a7e9942036e191edb89ea791691a8e7fd 100644 (file)
@@ -175,6 +175,27 @@ 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;