shithub: choc

Download patch

ref: 717673ef80aff9ae1a271b73d3c7f43f2bb27a1a
parent: 505de5022c942d06d9d508578289ec9f23df57cd
author: Simon Howard <fraggle@gmail.com>
date: Thu Jan 19 13:46:24 EST 2006

Move savegame header read/write code into p_saveg.c

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 300

--- a/src/g_game.c
+++ b/src/g_game.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: g_game.c 291 2006-01-13 23:56:00Z fraggle $
+// $Id: g_game.c 300 2006-01-19 18:46:24Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -22,6 +22,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.20  2006/01/19 18:46:24  fraggle
+// Move savegame header read/write code into p_saveg.c
+//
 // Revision 1.19  2006/01/13 23:56:00  fraggle
 // Add text-mode I/O functions.
 // Use text-mode screen for the waiting screen.
@@ -102,7 +105,7 @@
 
 
 static const char
-rcsid[] = "$Id: g_game.c 291 2006-01-13 23:56:00Z fraggle $";
+rcsid[] = "$Id: g_game.c 300 2006-01-19 18:46:24Z fraggle $";
 
 #include <string.h>
 #include <stdlib.h>
@@ -155,7 +158,6 @@
 
 
 #define SAVEGAMESIZE	0x2c000
-#define SAVESTRINGSIZE	24
 
 
 
@@ -1318,38 +1320,27 @@
 
 void G_DoLoadGame (void) 
 { 
-    int		length; 
-    int		i; 
-    int		a,b,c; 
-    char        vcheck[VERSIONSIZE]; 
+    int	length; 
+    int savedleveltime;
 	 
     gameaction = ga_nothing; 
 	 
     length = M_ReadFile (savename, &savebuffer); 
-    save_p = savebuffer + SAVESTRINGSIZE;
-    
-    // skip the description field 
-    memset (vcheck,0,sizeof(vcheck)); 
-    sprintf (vcheck,"version %i",DOOM_VERSION); 
-    if (strcmp ((char *) save_p, vcheck)) 
-	return;				// bad version 
-    save_p += VERSIONSIZE; 
-			 
-    gameskill = *save_p++; 
-    gameepisode = *save_p++; 
-    gamemap = *save_p++; 
-    for (i=0 ; i<MAXPLAYERS ; i++) 
-	playeringame[i] = *save_p++; 
+    save_p = savebuffer;
 
+    if (!P_ReadSaveGameHeader())
+    {
+        Z_Free(savebuffer);
+        return;
+    }
+
+    savedleveltime = leveltime;
+    
     // load a base level 
     G_InitNew (gameskill, gameepisode, gamemap); 
  
-    // get the times 
-    a = *save_p++; 
-    b = *save_p++; 
-    c = *save_p++; 
-    leveltime = (a<<16) + (b<<8) + c; 
-	 
+    leveltime = savedleveltime;
+
     // dearchive all the modifications
     P_UnArchivePlayers (); 
     P_UnArchiveWorld (); 
@@ -1356,7 +1347,7 @@
     P_UnArchiveThinkers (); 
     P_UnArchiveSpecials (); 
  
-    if (*save_p != 0x1d) 
+    if (!P_ReadSaveGameEOF())
 	I_Error ("Bad savegame");
     
     // done 
@@ -1388,10 +1379,8 @@
 void G_DoSaveGame (void) 
 { 
     char	name[100]; 
-    char	name2[VERSIONSIZE]; 
     char*	description; 
     int		length; 
-    int		i; 
 	
     strcpy(name, P_SaveGameFile(savegameslot));
 
@@ -1399,21 +1388,7 @@
 	 
     save_p = savebuffer = screens[1]+0x4000; 
 	 
-    memcpy (save_p, description, SAVESTRINGSIZE); 
-    save_p += SAVESTRINGSIZE; 
-    memset (name2,0,sizeof(name2)); 
-    sprintf (name2,"version %i",DOOM_VERSION); 
-    memcpy (save_p, name2, VERSIONSIZE); 
-    save_p += VERSIONSIZE; 
-	 
-    *save_p++ = gameskill; 
-    *save_p++ = gameepisode; 
-    *save_p++ = gamemap; 
-    for (i=0 ; i<MAXPLAYERS ; i++) 
-	*save_p++ = playeringame[i]; 
-    *save_p++ = leveltime>>16; 
-    *save_p++ = leveltime>>8; 
-    *save_p++ = leveltime; 
+    P_WriteSaveGameHeader(description);
  
     P_ArchivePlayers (); 
     P_ArchiveWorld (); 
@@ -1420,7 +1395,7 @@
     P_ArchiveThinkers (); 
     P_ArchiveSpecials (); 
 	 
-    *save_p++ = 0x1d;		// consistancy marker 
+    P_WriteSaveGameEOF();
 	 
     length = save_p - savebuffer; 
     if (length > SAVEGAMESIZE) 
@@ -1427,7 +1402,7 @@
 	I_Error ("Savegame buffer overrun"); 
     M_WriteFile (name, savebuffer, length); 
     gameaction = ga_nothing; 
-    savedescription[0] = 0;		 
+    strcpy(savedescription, "");
 	 
     players[consoleplayer].message = DEH_String(GGSAVED);
 
--- a/src/p_saveg.c
+++ b/src/p_saveg.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: p_saveg.c 299 2006-01-19 00:17:01Z fraggle $
+// $Id: p_saveg.c 300 2006-01-19 18:46:24Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -22,6 +22,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.7  2006/01/19 18:46:24  fraggle
+// Move savegame header read/write code into p_saveg.c
+//
 // Revision 1.6  2006/01/19 00:17:01  fraggle
 // Remove now-redundant note about structure packing.
 //
@@ -52,7 +55,7 @@
 //-----------------------------------------------------------------------------
 
 static const char
-rcsid[] = "$Id: p_saveg.c 299 2006-01-19 00:17:01Z fraggle $";
+rcsid[] = "$Id: p_saveg.c 300 2006-01-19 18:46:24Z fraggle $";
 
 #include "dstrings.h"
 #include "deh_main.h"
@@ -59,11 +62,15 @@
 #include "i_system.h"
 #include "z_zone.h"
 #include "p_local.h"
+#include "p_saveg.h"
 
 // State.
 #include "doomstat.h"
 #include "r_state.h"
 
+#define SAVEGAME_EOF 0x1d
+#define VERSIONSIZE 16 
+
 byte*		save_p;
 
 
@@ -1305,6 +1312,91 @@
     saveg_write32(str->direction);
 }
 
+//
+// Write the header for a savegame
+//
+
+void P_WriteSaveGameHeader(char *description)
+{
+    char name[VERSIONSIZE]; 
+    int i; 
+	
+    memcpy (save_p, description, SAVESTRINGSIZE); 
+    save_p += SAVESTRINGSIZE; 
+
+    memset (name,0,sizeof(name)); 
+    sprintf (name,"version %i",DOOM_VERSION); 
+    memcpy (save_p, name, VERSIONSIZE); 
+    save_p += VERSIONSIZE; 
+	 
+    saveg_write8(gameskill);
+    saveg_write8(gameepisode);
+    saveg_write8(gamemap);
+
+    for (i=0 ; i<MAXPLAYERS ; i++) 
+        saveg_write8(playeringame[i]);
+
+    saveg_write8((leveltime >> 16) & 0xff);
+    saveg_write8((leveltime >> 8) & 0xff);
+    saveg_write8(leveltime & 0xff);
+}
+
+// 
+// Read the header for a savegame
+//
+
+boolean P_ReadSaveGameHeader(void)
+{
+    int	 i; 
+    byte a, b, c; 
+    char vcheck[VERSIONSIZE]; 
+	 
+    save_p += SAVESTRINGSIZE;
+    
+    // skip the description field 
+    memset (vcheck,0,sizeof(vcheck)); 
+    sprintf (vcheck,"version %i",DOOM_VERSION); 
+    if (strcmp ((char *) save_p, vcheck)) 
+	return false;				// bad version 
+    save_p += VERSIONSIZE; 
+			 
+    gameskill = saveg_read8();
+    gameepisode = saveg_read8();
+    gamemap = saveg_read8();
+
+    for (i=0 ; i<MAXPLAYERS ; i++) 
+	playeringame[i] = saveg_read8();
+
+    // get the times 
+    a = saveg_read8();
+    b = saveg_read8();
+    c = saveg_read8();
+    leveltime = (a<<16) + (b<<8) + c; 
+
+    return true;
+}
+
+//
+// Read the end of file marker.  Returns true if read successfully.
+// 
+
+boolean P_ReadSaveGameEOF(void)
+{
+    int value;
+
+    value = saveg_read8();
+
+    return value == SAVEGAME_EOF;
+}
+
+//
+// Write the end of file marker
+//
+
+void P_WriteSaveGameEOF(void)
+{
+    saveg_write8(SAVEGAME_EOF);
+}
 
 //
 // P_ArchivePlayers
--- a/src/p_saveg.h
+++ b/src/p_saveg.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: p_saveg.h 202 2005-10-16 01:18:10Z fraggle $
+// $Id: p_saveg.h 300 2006-01-19 18:46:24Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -30,11 +30,24 @@
 #ifndef __P_SAVEG__
 #define __P_SAVEG__
 
+// maximum size of a savegame description
+
+#define SAVESTRINGSIZE 24
+
 // filename to use for a savegame slot
 
 char *P_SaveGameFile(int slot);
 
+// Savegame file header read/write functions
 
+boolean P_ReadSaveGameHeader(void);
+void P_WriteSaveGameHeader(char *description);
+
+// Savegame end-of-file read/write functions
+
+boolean P_ReadSaveGameEOF(void);
+void P_WriteSaveGameEOF(void);
+
 // Persistent storage/archiving.
 // These are the load / save game routines.
 void P_ArchivePlayers (void);
@@ -53,6 +66,9 @@
 //-----------------------------------------------------------------------------
 //
 // $Log$
+// Revision 1.5  2006/01/19 18:46:24  fraggle
+// Move savegame header read/write code into p_saveg.c
+//
 // Revision 1.4  2005/10/16 01:18:10  fraggle
 // Global "configdir" variable with directory to store config files in.
 // Create a function to find the filename for a savegame slot.  Store