bool CReader::ReadBinaryFile( const char* pFileName )
{
	IPlatformOS::ISaveReaderPtr pOSSaveReader = gEnv->pSystem->GetPlatformOS()->SaveGetReader( pFileName, IPlatformOS::Unknown_User );
	if (!m_pZLibBuffer)
		m_pZLibBuffer = new uint8[ XMLCPB_ZLIB_BUFFER_SIZE ];
	if (!m_pZLibCompressedBuffer)
		m_pZLibCompressedBuffer = new uint8[ XMLCPB_ZLIB_BUFFER_SIZE ];
	m_errorReading = pOSSaveReader.get()==NULL;
	
	if (!m_errorReading)
	{
		size_t totalNumBytesInFile = 0;
		IPlatformOS::EFileOperationCode code = pOSSaveReader->GetNumBytes( totalNumBytesInFile );
		m_totalSize = totalNumBytesInFile;
		CheckErrorFlag( code );

		if (!m_errorReading)
		{
			SFileHeader fileHeader;
			pOSSaveReader->Seek( -int(sizeof(fileHeader)), IPlatformOS::ISaveReader::ESM_END );
			ReadDataFromFileInternal( pOSSaveReader, &fileHeader, sizeof(fileHeader) );
#ifdef XMLCPB_CHECK_FILE_INTEGRITY			
			m_errorReading = CheckFileCorruption( pOSSaveReader, fileHeader, m_totalSize );
#endif			
			
			pOSSaveReader->Seek( 0, IPlatformOS::ISaveReader::ESM_BEGIN );
			
			if (fileHeader.m_fileTypeCheck!=fileHeader.FILETYPECHECK)
			{
				CryWarning( VALIDATOR_MODULE_SYSTEM, VALIDATOR_ERROR, "XMLCPB ERROR: file type signature not correct. Savegame File corrupted!" );
				m_errorReading = true;
			}
			
			if (!m_errorReading)
			{
				m_nodesDataSize = fileHeader.m_sizeNodes;
				m_numNodes = fileHeader.m_numNodes;
				m_buffer.ReadFromFile( *this, pOSSaveReader, fileHeader.m_sizeNodes );
				m_tableTags.ReadFromFile( *this, pOSSaveReader, fileHeader.m_tags );
				m_tableAttrNames.ReadFromFile( *this, pOSSaveReader, fileHeader.m_attrNames );
				m_tableStrData.ReadFromFile( *this, pOSSaveReader, fileHeader.m_strData );
				m_tableAttrSets.ReadFromFile( *this, pOSSaveReader, fileHeader );
				CreateNodeAddressTables();
			}
			
			if (!m_errorReading)
			{
				const CNodeLiveReader& root = ActivateLiveNodeFromCompact( m_numNodes - 1 ); // the last node is always the root
				assert( root.GetLiveId()==XMLCPB_ROOTNODE_ID );

				pOSSaveReader->TouchFile();
			}
		}	

		pOSSaveReader->Close();
	}
	
	CryLog("[LOAD GAME] --Binary saveload: reading done--");
		
	if (m_errorReading)
		CryWarning( VALIDATOR_MODULE_SYSTEM, VALIDATOR_ERROR, "XMLCPB ERROR: while reading the file: '%s'", pFileName );

	return !m_errorReading;
}
Exemple #2
0
bool CXmlLoadGame::Init( const char * name )
{
#ifdef XML_LOADGAME_USE_COMPRESSION
	#if defined(XENON) || defined(PS3)
		CryWarning(VALIDATOR_MODULE_GAME,VALIDATOR_ERROR, "CXmlLoadGame::Init  not supported yet, strings cannot grow beyond 32767 chars\n");
		return false;
	#endif

	const unsigned int nFileSizeBits = GetISystem()->GetCompressedFileSize(name);
	const unsigned int nFileSizeBytes = nFileSizeBits / 8 + ((nFileSizeBits & 7) + 7) / 8;
	if (nFileSizeBytes <= 0)
	{
		return false;
	}

	char* const pXmlData = new char[nFileSizeBytes+16];
	GetISystem()->ReadCompressedFile(name, pXmlData,nFileSizeBits);

	m_pImpl->root = GetISystem()->LoadXmlFromBuffer(pXmlData, nFileSizeBytes);

	delete []pXmlData;
#else
	if (GetISystem()->GetPlatformOS()->UsePlatformSavingAPI() )
	{
		IPlatformOS::ISaveReaderPtr pSaveReader = GetISystem()->GetPlatformOS()->SaveGetReader(name);
		if (!pSaveReader)
		{
			return false;
		}

		size_t nFileSize;

		if ((pSaveReader->GetNumBytes(nFileSize) == IPlatformOS::eFOC_Failure) || (nFileSize <= 0))
		{
			return false;
		}

		std::vector<char> xmlData;
		xmlData.resize(nFileSize);
		
		if (pSaveReader->ReadBytes(&xmlData[0], nFileSize) == IPlatformOS::eFOC_Failure)
		{
			return false;
		}

		m_pImpl->root = GetISystem()->LoadXmlFromBuffer(&xmlData[0], nFileSize);
	}
	else
	{
		m_pImpl->root = GetISystem()->LoadXmlFromFile(name);
	}
#endif

	if (!m_pImpl->root)
		return false;

	m_pImpl->inputFile = name;

	m_pImpl->metadata = m_pImpl->root->findChild("Metadata");
	if (!m_pImpl->metadata)
		return false;

	return true;
}