PageRenderTime 45ms CodeModel.GetById 37ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

/ghost-legacy/savegame.cpp

http://ghostcb.googlecode.com/
C++ | 118 lines | 68 code | 17 blank | 33 comment | 6 complexity | 2ab5ef931b18283263529fcb65e1eee5 MD5 | raw file
  1/*
  2
  3   Copyright [2008] [Trevor Hogan]
  4
  5   Licensed under the Apache License, Version 2.0 (the "License");
  6   you may not use this file except in compliance with the License.
  7   You may obtain a copy of the License at
  8
  9       http://www.apache.org/licenses/LICENSE-2.0
 10
 11   Unless required by applicable law or agreed to in writing, software
 12   distributed under the License is distributed on an "AS IS" BASIS,
 13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14   See the License for the specific language governing permissions and
 15   limitations under the License.
 16
 17   CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
 18
 19*/
 20
 21#include "ghost.h"
 22#include "util.h"
 23#include "packed.h"
 24#include "savegame.h"
 25
 26//
 27// CSaveGame
 28//
 29
 30CSaveGame :: CSaveGame( ) : CPacked( )
 31{
 32	m_NumSlots = 0;
 33	m_RandomSeed = 0;
 34}
 35
 36CSaveGame :: ~CSaveGame( )
 37{
 38
 39}
 40
 41#define READB( x, y, z )	(x).read( (char *)(y), (z) )
 42#define READSTR( x, y )		getline( (x), (y), '\0' )
 43
 44void CSaveGame :: ParseSaveGame( )
 45{
 46	m_MapPath.clear( );
 47	m_GameName.clear( );
 48	m_NumSlots = 0;
 49	m_Slots.clear( );
 50	m_RandomSeed = 0;
 51	m_MagicNumber.clear( );
 52
 53	if( m_Flags != 0 )
 54	{
 55		CONSOLE_Print( "[SAVEGAME] invalid replay (flags mismatch)" );
 56		m_Valid = false;
 57		return;
 58	}
 59
 60	istringstream ISS( m_Decompressed );
 61
 62	// savegame format figured out by Varlock:
 63	// string		-> map path
 64	// 0 (string?)	-> ??? (no idea what this is)
 65	// string		-> game name
 66	// 0 (string?)	-> ??? (maybe original game password)
 67	// string		-> stat string
 68	// 4 bytes		-> ??? (seems to be # of slots)
 69	// 4 bytes		-> ??? (seems to be 0x01 0x28 0x49 0x00 on both of the savegames examined)
 70	// 2 bytes		-> ??? (no idea what this is)
 71	// slot structure
 72	// 4 bytes		-> magic number
 73
 74	unsigned char Garbage1;
 75	uint16_t Garbage2;
 76	uint32_t Garbage4;
 77	string GarbageString;
 78	uint32_t MagicNumber;
 79
 80	READSTR( ISS, m_MapPath );				// map path
 81	READSTR( ISS, GarbageString );			// ???
 82	READSTR( ISS, m_GameName );				// game name
 83	READSTR( ISS, GarbageString );			// ???
 84	READSTR( ISS, GarbageString );			// stat string
 85	READB( ISS, &Garbage4, 4 );				// ???
 86	READB( ISS, &Garbage4, 4 );				// ???
 87	READB( ISS, &Garbage2, 2 );				// ???
 88	READB( ISS, &m_NumSlots, 1 );			// number of slots
 89
 90	if( m_NumSlots > 12 )
 91	{
 92		CONSOLE_Print( "[SAVEGAME] invalid savegame (too many slots)" );
 93		m_Valid = false;
 94		return;
 95	}
 96
 97	for( unsigned char i = 0; i < m_NumSlots; i++ )
 98	{
 99		unsigned char SlotData[9];
100		READB( ISS, SlotData, 9 );			// slot data
101		m_Slots.push_back( CGameSlot( SlotData[0], SlotData[1], SlotData[2], SlotData[3], SlotData[4], SlotData[5], SlotData[6], SlotData[7], SlotData[8] ) );
102	}
103
104	READB( ISS, &m_RandomSeed, 4 );			// random seed
105	READB( ISS, &Garbage1, 1 );				// GameType
106	READB( ISS, &Garbage1, 1 );				// number of player slots (non observer)
107	READB( ISS, &MagicNumber, 4 );			// magic number
108
109	if( ISS.eof( ) || ISS.fail( ) )
110	{
111		CONSOLE_Print( "[SAVEGAME] failed to parse savegame header" );
112		m_Valid = false;
113		return;
114	}
115
116	m_MagicNumber = UTIL_CreateByteArray( MagicNumber, false );
117	m_Valid = true;
118}