doom3-bfg/neo/framework/File_SaveGame.cpp

1326 lines
31 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
#include "File_SaveGame.h"
/*
TODO: CRC on each block
*/
/*
========================
ZlibAlloc
========================
*/
void* ZlibAlloc( void* opaque, uInt items, uInt size )
{
2012-11-26 18:58:24 +00:00
return Mem_Alloc( items * size, TAG_SAVEGAMES );
}
/*
========================
ZlibFree
========================
*/
void ZlibFree( void* opaque, void* address )
{
2012-11-26 18:58:24 +00:00
Mem_Free( address );
}
idCVar sgf_threads( "sgf_threads", "2", CVAR_INTEGER, "0 = all foreground, 1 = background write, 2 = background write + compress" );
idCVar sgf_checksums( "sgf_checksums", "1", CVAR_BOOL, "enable save game file checksums" );
idCVar sgf_testCorruption( "sgf_testCorruption", "-1", CVAR_INTEGER, "test corruption at the 128 kB compressed block" );
// this is supposed to get faster going from -15 to -9, but it gets slower as well as worse compression
idCVar sgf_windowBits( "sgf_windowBits", "-15", CVAR_INTEGER, "zlib window bits" );
bool idFile_SaveGamePipelined::cancelToTerminate = false;
class idSGFcompressThread : public idSysThread
{
2012-11-26 18:58:24 +00:00
public:
virtual int Run()
{
sgf->CompressBlock();
return 0;
}
idFile_SaveGamePipelined* sgf;
2012-11-26 18:58:24 +00:00
};
class idSGFdecompressThread : public idSysThread
{
2012-11-26 18:58:24 +00:00
public:
virtual int Run()
{
sgf->DecompressBlock();
return 0;
}
idFile_SaveGamePipelined* sgf;
2012-11-26 18:58:24 +00:00
};
class idSGFwriteThread : public idSysThread
{
2012-11-26 18:58:24 +00:00
public:
virtual int Run()
{
sgf->WriteBlock();
return 0;
}
idFile_SaveGamePipelined* sgf;
2012-11-26 18:58:24 +00:00
};
class idSGFreadThread : public idSysThread
{
2012-11-26 18:58:24 +00:00
public:
virtual int Run()
{
sgf->ReadBlock();
return 0;
}
idFile_SaveGamePipelined* sgf;
2012-11-26 18:58:24 +00:00
};
/*
============================
idFile_SaveGamePipelined::idFile_SaveGamePipelined
============================
*/
idFile_SaveGamePipelined::idFile_SaveGamePipelined() :
mode( CLOSED ),
compressedLength( 0 ),
uncompressedProducedBytes( 0 ),
uncompressedConsumedBytes( 0 ),
compressedProducedBytes( 0 ),
compressedConsumedBytes( 0 ),
dataZlib( NULL ),
bytesZlib( 0 ),
dataIO( NULL ),
bytesIO( 0 ),
zLibFlushType( Z_NO_FLUSH ),
zStreamEndHit( false ),
numChecksums( 0 ),
nativeFile( NULL ),
nativeFileEndHit( false ),
finished( false ),
readThread( NULL ),
writeThread( NULL ),
decompressThread( NULL ),
compressThread( NULL ),
blockFinished( true ),
buildVersion( "" ),
saveFormatVersion( 0 )
{
2012-11-26 18:58:24 +00:00
memset( &zStream, 0, sizeof( zStream ) );
memset( compressed, 0, sizeof( compressed ) );
memset( uncompressed, 0, sizeof( uncompressed ) );
zStream.zalloc = ZlibAlloc;
zStream.zfree = ZlibFree;
}
/*
============================
idFile_SaveGamePipelined::~idFile_SaveGamePipelined
============================
*/
idFile_SaveGamePipelined::~idFile_SaveGamePipelined()
{
2012-11-26 18:58:24 +00:00
Finish();
2012-11-26 18:58:24 +00:00
// free the threads
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
delete compressThread;
compressThread = NULL;
}
if( decompressThread != NULL )
{
2012-11-26 18:58:24 +00:00
delete decompressThread;
decompressThread = NULL;
}
if( readThread != NULL )
{
2012-11-26 18:58:24 +00:00
delete readThread;
readThread = NULL;
}
if( writeThread != NULL )
{
2012-11-26 18:58:24 +00:00
delete writeThread;
writeThread = NULL;
}
2012-11-26 18:58:24 +00:00
// close the native file
/* if ( nativeFile != NULL ) {
delete nativeFile;
nativeFile = NULL;
} */
2012-11-26 18:58:24 +00:00
dataZlib = NULL;
dataIO = NULL;
}
/*
========================
idFile_SaveGamePipelined::ReadBuildVersion
========================
*/
bool idFile_SaveGamePipelined::ReadBuildVersion()
{
2012-11-26 18:58:24 +00:00
return ReadString( buildVersion ) != 0;
}
/*
========================
idFile_SaveGamePipelined::ReadSaveFormatVersion
========================
*/
bool idFile_SaveGamePipelined::ReadSaveFormatVersion()
{
if( ReadBig( pointerSize ) <= 0 )
{
return false;
2012-11-26 18:58:24 +00:00
}
return ReadBig( saveFormatVersion ) != 0;
}
/*
========================
idFile_SaveGamePipelined::GetPointerSize
========================
*/
int idFile_SaveGamePipelined::GetPointerSize() const
{
if( pointerSize == 0 )
{
2012-11-26 18:58:24 +00:00
// in original savegames we weren't saving the pointer size, so the 2 high bytes of the save version will be 0
return 4;
}
else
{
2012-11-26 18:58:24 +00:00
return pointerSize;
}
}
/*
============================
idFile_SaveGamePipelined::Finish
============================
*/
void idFile_SaveGamePipelined::Finish()
{
if( mode == WRITE )
{
2012-11-26 18:58:24 +00:00
// wait for the compression thread to complete, which may kick off a write
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
compressThread->WaitForThread();
}
2012-11-26 18:58:24 +00:00
// force the next compression to emit everything
zLibFlushType = Z_FINISH;
FlushUncompressedBlock();
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
compressThread->WaitForThread();
}
if( writeThread != NULL )
{
2012-11-26 18:58:24 +00:00
// wait for the IO thread to exit
writeThread->WaitForThread();
}
else if( nativeFile == NULL && !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
// wait for the last block to be consumed
blockRequested.Wait();
finished = true;
blockAvailable.Raise();
blockFinished.Wait();
}
2012-11-26 18:58:24 +00:00
// free zlib tables
deflateEnd( &zStream );
}
else if( mode == READ )
{
2012-11-26 18:58:24 +00:00
// wait for the decompression thread to complete, which may kick off a read
if( decompressThread != NULL )
{
2012-11-26 18:58:24 +00:00
decompressThread->WaitForThread();
}
if( readThread != NULL )
{
2012-11-26 18:58:24 +00:00
// wait for the IO thread to exit
readThread->WaitForThread();
}
else if( nativeFile == NULL && !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
// wait for the last block to be consumed
blockAvailable.Wait();
finished = true;
blockRequested.Raise();
blockFinished.Wait();
}
2012-11-26 18:58:24 +00:00
// free zlib tables
inflateEnd( &zStream );
}
2012-11-26 18:58:24 +00:00
mode = CLOSED;
}
/*
============================
idFile_SaveGamePipelined::Abort
============================
*/
void idFile_SaveGamePipelined::Abort()
{
if( mode == WRITE )
{
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
compressThread->WaitForThread();
}
if( writeThread != NULL )
{
2012-11-26 18:58:24 +00:00
writeThread->WaitForThread();
}
else if( nativeFile == NULL && !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
blockRequested.Wait();
finished = true;
dataIO = NULL;
bytesIO = 0;
blockAvailable.Raise();
blockFinished.Wait();
}
}
else if( mode == READ )
{
2012-11-26 18:58:24 +00:00
if( decompressThread != NULL )
{
2012-11-26 18:58:24 +00:00
decompressThread->WaitForThread();
}
if( readThread != NULL )
{
2012-11-26 18:58:24 +00:00
readThread->WaitForThread();
}
else if( nativeFile == NULL && !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
blockAvailable.Wait();
finished = true;
dataIO = NULL;
bytesIO = 0;
blockRequested.Raise();
blockFinished.Wait();
}
}
2012-11-26 18:58:24 +00:00
mode = CLOSED;
}
/*
===================================================================================
WRITE PATH
===================================================================================
*/
/*
============================
idFile_SaveGamePipelined::OpenForWriting
============================
*/
bool idFile_SaveGamePipelined::OpenForWriting( const char* const filename, bool useNativeFile )
{
2012-11-26 18:58:24 +00:00
assert( mode == CLOSED );
2012-11-26 18:58:24 +00:00
name = filename;
osPath = filename;
mode = WRITE;
nativeFile = NULL;
numChecksums = 0;
if( useNativeFile )
{
2012-11-26 18:58:24 +00:00
nativeFile = fileSystem->OpenFileWrite( filename );
if( nativeFile == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
// raw deflate with no header / checksum
// use max memory for fastest compression
// optimize for higher speed
//mem.PushHeap();
int status = deflateInit2( &zStream, Z_BEST_SPEED, Z_DEFLATED, sgf_windowBits.GetInteger(), 9, Z_DEFAULT_STRATEGY );
//mem.PopHeap();
if( status != Z_OK )
{
2012-11-26 18:58:24 +00:00
idLib::FatalError( "idFile_SaveGamePipelined::OpenForWriting: deflateInit2() error %i", status );
}
2012-11-26 18:58:24 +00:00
// initial buffer setup
zStream.avail_out = COMPRESSED_BLOCK_SIZE;
zStream.next_out = ( Bytef* )compressed;
if( sgf_checksums.GetBool() )
{
2012-11-26 18:58:24 +00:00
zStream.avail_out -= sizeof( uint32 );
}
if( sgf_threads.GetInteger() >= 1 )
{
compressThread = new( TAG_IDFILE ) idSGFcompressThread();
2012-11-26 18:58:24 +00:00
compressThread->sgf = this;
// DG: change threadname from "SGF_CompressThread" to "SGF_Compress", because Linux
// has a 15 (+ \0) char limit for threadnames. also, "thread" in a threadname is redundant
compressThread->StartWorkerThread( "SGF_Compress", CORE_2B, THREAD_NORMAL );
// DG end
2012-11-26 18:58:24 +00:00
}
if( nativeFile != NULL && sgf_threads.GetInteger() >= 2 )
{
writeThread = new( TAG_IDFILE ) idSGFwriteThread();
2012-11-26 18:58:24 +00:00
writeThread->sgf = this;
writeThread->StartWorkerThread( "SGF_WriteThread", CORE_2A, THREAD_NORMAL );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::OpenForWriting
============================
*/
bool idFile_SaveGamePipelined::OpenForWriting( idFile* file )
{
2012-11-26 18:58:24 +00:00
assert( mode == CLOSED );
if( file == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
name = file->GetName();
osPath = file->GetFullPath();
mode = WRITE;
nativeFile = file;
numChecksums = 0;
2012-11-26 18:58:24 +00:00
// raw deflate with no header / checksum
// use max memory for fastest compression
// optimize for higher speed
//mem.PushHeap();
int status = deflateInit2( &zStream, Z_BEST_SPEED, Z_DEFLATED, sgf_windowBits.GetInteger(), 9, Z_DEFAULT_STRATEGY );
//mem.PopHeap();
if( status != Z_OK )
{
2012-11-26 18:58:24 +00:00
idLib::FatalError( "idFile_SaveGamePipelined::OpenForWriting: deflateInit2() error %i", status );
}
2012-11-26 18:58:24 +00:00
// initial buffer setup
zStream.avail_out = COMPRESSED_BLOCK_SIZE;
zStream.next_out = ( Bytef* )compressed;
if( sgf_checksums.GetBool() )
{
2012-11-26 18:58:24 +00:00
zStream.avail_out -= sizeof( uint32 );
}
if( sgf_threads.GetInteger() >= 1 )
{
compressThread = new( TAG_IDFILE ) idSGFcompressThread();
2012-11-26 18:58:24 +00:00
compressThread->sgf = this;
// DG: change threadname from "SGF_CompressThread" to "SGF_Compress", because Linux
// has a 15 (+ \0) char limit for threadnames. also, "thread" in a threadname is redundant
compressThread->StartWorkerThread( "SGF_Compress", CORE_2B, THREAD_NORMAL );
// DG end
2012-11-26 18:58:24 +00:00
}
if( nativeFile != NULL && sgf_threads.GetInteger() >= 2 )
{
writeThread = new( TAG_IDFILE ) idSGFwriteThread();
2012-11-26 18:58:24 +00:00
writeThread->sgf = this;
writeThread->StartWorkerThread( "SGF_WriteThread", CORE_2A, THREAD_NORMAL );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::NextWriteBlock
Modifies:
dataIO
bytesIO
============================
*/
bool idFile_SaveGamePipelined::NextWriteBlock( blockForIO_t* block )
{
2012-11-26 18:58:24 +00:00
assert( mode == WRITE );
2012-11-26 18:58:24 +00:00
blockRequested.Raise(); // the background thread is done with the last block
if( nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
blockAvailable.Wait(); // wait for a new block to come through the pipeline
if( finished || block == NULL )
{
2012-11-26 18:58:24 +00:00
nativeFileEndHit = true;
blockRequested.Raise();
blockFinished.Raise();
return false;
}
2012-11-26 18:58:24 +00:00
compressedLength += bytesIO;
2012-11-26 18:58:24 +00:00
block->data = dataIO;
block->bytes = bytesIO;
2012-11-26 18:58:24 +00:00
dataIO = NULL;
bytesIO = 0;
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::WriteBlock
Modifies:
dataIO
bytesIO
nativeFile
============================
*/
void idFile_SaveGamePipelined::WriteBlock()
{
2012-11-26 18:58:24 +00:00
assert( nativeFile != NULL );
2012-11-26 18:58:24 +00:00
compressedLength += bytesIO;
2012-11-26 18:58:24 +00:00
nativeFile->Write( dataIO, bytesIO );
2012-11-26 18:58:24 +00:00
dataIO = NULL;
bytesIO = 0;
}
/*
============================
idFile_SaveGamePipelined::FlushCompressedBlock
Called when a compressed block fills up, and also to flush the final partial block.
Flushes everything from [compressedConsumedBytes -> compressedProducedBytes)
Reads:
compressed
compressedProducedBytes
Modifies:
dataZlib
bytesZlib
compressedConsumedBytes
============================
*/
void idFile_SaveGamePipelined::FlushCompressedBlock()
{
2012-11-26 18:58:24 +00:00
// block until the background thread is done with the last block
if( writeThread != NULL )
{
2012-11-26 18:58:24 +00:00
writeThread->WaitForThread();
}
if( nativeFile == NULL )
{
if( !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
blockRequested.Wait();
}
}
2012-11-26 18:58:24 +00:00
// prepare the next block to be written out
dataIO = &compressed[ compressedConsumedBytes & ( COMPRESSED_BUFFER_SIZE - 1 ) ];
bytesIO = compressedProducedBytes - compressedConsumedBytes;
compressedConsumedBytes = compressedProducedBytes;
if( writeThread != NULL )
{
2012-11-26 18:58:24 +00:00
// signal a new block is available to be written out
writeThread->SignalWork();
}
else if( nativeFile != NULL )
{
2012-11-26 18:58:24 +00:00
// write syncronously
WriteBlock();
}
else
{
2012-11-26 18:58:24 +00:00
// signal a new block is available to be written out
blockAvailable.Raise();
}
}
/*
============================
idFile_SaveGamePipelined::CompressBlock
Called when an uncompressed block fills up, and also to flush the final partial block.
Flushes everything from [uncompressedConsumedBytes -> uncompressedProducedBytes)
Modifies:
dataZlib
bytesZlib
compressed
compressedProducedBytes
zStream
zStreamEndHit
============================
*/
void idFile_SaveGamePipelined::CompressBlock()
{
zStream.next_in = ( Bytef* )dataZlib;
zStream.avail_in = ( uInt ) bytesZlib;
2012-11-26 18:58:24 +00:00
dataZlib = NULL;
bytesZlib = 0;
2012-11-26 18:58:24 +00:00
// if this is the finish block, we may need to write
// multiple buffers even after all input has been consumed
while( zStream.avail_in > 0 || zLibFlushType == Z_FINISH )
{
2012-11-26 18:58:24 +00:00
const int zstat = deflate( &zStream, zLibFlushType );
if( zstat != Z_OK && zstat != Z_STREAM_END )
{
2012-11-26 18:58:24 +00:00
idLib::FatalError( "idFile_SaveGamePipelined::CompressBlock: deflate() returned %i", zstat );
}
if( zStream.avail_out == 0 || zLibFlushType == Z_FINISH )
{
if( sgf_checksums.GetBool() )
{
2012-11-26 18:58:24 +00:00
size_t blockSize = zStream.total_out + numChecksums * sizeof( uint32 ) - compressedProducedBytes;
uint32 checksum = MD5_BlockChecksum( zStream.next_out - blockSize, blockSize );
zStream.next_out[0] = ( ( checksum >> 0 ) & 0xFF );
zStream.next_out[1] = ( ( checksum >> 8 ) & 0xFF );
zStream.next_out[2] = ( ( checksum >> 16 ) & 0xFF );
zStream.next_out[3] = ( ( checksum >> 24 ) & 0xFF );
numChecksums++;
}
2012-11-26 18:58:24 +00:00
// flush the output buffer IO
compressedProducedBytes = zStream.total_out + numChecksums * sizeof( uint32 );
FlushCompressedBlock();
if( zstat == Z_STREAM_END )
{
2012-11-26 18:58:24 +00:00
assert( zLibFlushType == Z_FINISH );
zStreamEndHit = true;
return;
}
2012-11-26 18:58:24 +00:00
assert( 0 == ( compressedProducedBytes & ( COMPRESSED_BLOCK_SIZE - 1 ) ) );
2012-11-26 18:58:24 +00:00
zStream.avail_out = COMPRESSED_BLOCK_SIZE;
zStream.next_out = ( Bytef* )&compressed[ compressedProducedBytes & ( COMPRESSED_BUFFER_SIZE - 1 ) ];
if( sgf_checksums.GetBool() )
{
2012-11-26 18:58:24 +00:00
zStream.avail_out -= sizeof( uint32 );
}
}
}
}
/*
============================
idFile_SaveGamePipelined::FlushUncompressedBlock
Called when an uncompressed block fills up, and also to flush the final partial block.
Flushes everything from [uncompressedConsumedBytes -> uncompressedProducedBytes)
Reads:
uncompressed
uncompressedProducedBytes
Modifies:
dataZlib
bytesZlib
uncompressedConsumedBytes
============================
*/
void idFile_SaveGamePipelined::FlushUncompressedBlock()
{
2012-11-26 18:58:24 +00:00
// block until the background thread has completed
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
// make sure thread has completed the last work
compressThread->WaitForThread();
}
2012-11-26 18:58:24 +00:00
// prepare the next block to be consumed by Zlib
dataZlib = &uncompressed[ uncompressedConsumedBytes & ( UNCOMPRESSED_BUFFER_SIZE - 1 ) ];
bytesZlib = uncompressedProducedBytes - uncompressedConsumedBytes;
uncompressedConsumedBytes = uncompressedProducedBytes;
if( compressThread != NULL )
{
2012-11-26 18:58:24 +00:00
// signal thread for more work
compressThread->SignalWork();
}
else
{
2012-11-26 18:58:24 +00:00
// run syncronously
CompressBlock();
}
}
/*
============================
idFile_SaveGamePipelined::Write
Modifies:
uncompressed
uncompressedProducedBytes
============================
*/
int idFile_SaveGamePipelined::Write( const void* buffer, int length )
{
if( buffer == NULL || length <= 0 )
{
2012-11-26 18:58:24 +00:00
return 0;
}
2012-11-26 18:58:24 +00:00
#if 1 // quick and dirty fix for user-initiated forced shutdown during a savegame
if( cancelToTerminate )
{
if( mode != CLOSED )
{
2012-11-26 18:58:24 +00:00
Abort();
}
return 0;
}
#endif
2012-11-26 18:58:24 +00:00
assert( mode == WRITE );
size_t lengthRemaining = length;
const byte* buffer_p = ( const byte* )buffer;
while( lengthRemaining > 0 )
{
2012-11-26 18:58:24 +00:00
const size_t ofsInBuffer = uncompressedProducedBytes & ( UNCOMPRESSED_BUFFER_SIZE - 1 );
const size_t ofsInBlock = uncompressedProducedBytes & ( UNCOMPRESSED_BLOCK_SIZE - 1 );
const size_t remainingInBlock = UNCOMPRESSED_BLOCK_SIZE - ofsInBlock;
const size_t copyToBlock = ( lengthRemaining < remainingInBlock ) ? lengthRemaining : remainingInBlock;
2012-11-26 18:58:24 +00:00
memcpy( uncompressed + ofsInBuffer, buffer_p, copyToBlock );
uncompressedProducedBytes += copyToBlock;
2012-11-26 18:58:24 +00:00
buffer_p += copyToBlock;
lengthRemaining -= copyToBlock;
if( copyToBlock == remainingInBlock )
{
2012-11-26 18:58:24 +00:00
FlushUncompressedBlock();
}
}
return length;
}
/*
===================================================================================
READ PATH
===================================================================================
*/
/*
============================
idFile_SaveGamePipelined::OpenForReading
============================
*/
bool idFile_SaveGamePipelined::OpenForReading( const char* const filename, bool useNativeFile )
{
2012-11-26 18:58:24 +00:00
assert( mode == CLOSED );
2012-11-26 18:58:24 +00:00
name = filename;
osPath = filename;
mode = READ;
nativeFile = NULL;
numChecksums = 0;
if( useNativeFile )
{
2012-11-26 18:58:24 +00:00
nativeFile = fileSystem->OpenFileRead( filename );
if( nativeFile == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
// init zlib for raw inflate with a 32k dictionary
//mem.PushHeap();
int status = inflateInit2( &zStream, sgf_windowBits.GetInteger() );
//mem.PopHeap();
if( status != Z_OK )
{
2012-11-26 18:58:24 +00:00
idLib::FatalError( "idFile_SaveGamePipelined::OpenForReading: inflateInit2() error %i", status );
}
2012-11-26 18:58:24 +00:00
// spawn threads
if( sgf_threads.GetInteger() >= 1 )
{
decompressThread = new( TAG_IDFILE ) idSGFdecompressThread();
2012-11-26 18:58:24 +00:00
decompressThread->sgf = this;
// DG: change threadname from "SGF_DecompressThread" to "SGF_Decompress", because Linux
// has a 15 (+ \0) char limit for threadnames. also, "thread" in a threadname is redundant
decompressThread->StartWorkerThread( "SGF_Decompress", CORE_2B, THREAD_NORMAL );
// DG end
2012-11-26 18:58:24 +00:00
}
if( nativeFile != NULL && sgf_threads.GetInteger() >= 2 )
{
readThread = new( TAG_IDFILE ) idSGFreadThread();
2012-11-26 18:58:24 +00:00
readThread->sgf = this;
readThread->StartWorkerThread( "SGF_ReadThread", CORE_2A, THREAD_NORMAL );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::OpenForReading
============================
*/
bool idFile_SaveGamePipelined::OpenForReading( idFile* file )
{
2012-11-26 18:58:24 +00:00
assert( mode == CLOSED );
if( file == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
name = file->GetName();
osPath = file->GetFullPath();
mode = READ;
nativeFile = file;
numChecksums = 0;
2012-11-26 18:58:24 +00:00
// init zlib for raw inflate with a 32k dictionary
//mem.PushHeap();
int status = inflateInit2( &zStream, sgf_windowBits.GetInteger() );
//mem.PopHeap();
if( status != Z_OK )
{
2012-11-26 18:58:24 +00:00
idLib::FatalError( "idFile_SaveGamePipelined::OpenForReading: inflateInit2() error %i", status );
}
2012-11-26 18:58:24 +00:00
// spawn threads
if( sgf_threads.GetInteger() >= 1 )
{
decompressThread = new( TAG_IDFILE ) idSGFdecompressThread();
2012-11-26 18:58:24 +00:00
decompressThread->sgf = this;
// DG: change threadname from "SGF_DecompressThread" to "SGF_Decompress", because Linux
// has a 15 (+ \0) char limit for threadnames. also, "thread" in a threadname is redundant
decompressThread->StartWorkerThread( "SGF_Decompress", CORE_1B, THREAD_NORMAL );
// DG end
2012-11-26 18:58:24 +00:00
}
if( nativeFile != NULL && sgf_threads.GetInteger() >= 2 )
{
readThread = new( TAG_IDFILE ) idSGFreadThread();
2012-11-26 18:58:24 +00:00
readThread->sgf = this;
readThread->StartWorkerThread( "SGF_ReadThread", CORE_1A, THREAD_NORMAL );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::NextReadBlock
Reads the next data block from the filesystem into the memory buffer.
Modifies:
compressed
compressedProducedBytes
nativeFileEndHit
============================
*/
bool idFile_SaveGamePipelined::NextReadBlock( blockForIO_t* block, size_t lastReadBytes )
{
2012-11-26 18:58:24 +00:00
assert( mode == READ );
2012-11-26 18:58:24 +00:00
assert( ( lastReadBytes & ( COMPRESSED_BLOCK_SIZE - 1 ) ) == 0 || block == NULL );
compressedProducedBytes += lastReadBytes;
2012-11-26 18:58:24 +00:00
blockAvailable.Raise(); // a new block is available for the pipeline to consume
if( nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
blockRequested.Wait(); // wait for the last block to be consumed by the pipeline
if( finished || block == NULL )
{
2012-11-26 18:58:24 +00:00
nativeFileEndHit = true;
blockAvailable.Raise();
blockFinished.Raise();
return false;
}
2012-11-26 18:58:24 +00:00
assert( 0 == ( compressedProducedBytes & ( COMPRESSED_BLOCK_SIZE - 1 ) ) );
block->data = & compressed[compressedProducedBytes & ( COMPRESSED_BUFFER_SIZE - 1 )];
block->bytes = COMPRESSED_BLOCK_SIZE;
2012-11-26 18:58:24 +00:00
return true;
}
/*
============================
idFile_SaveGamePipelined::ReadBlock
Reads the next data block from the filesystem into the memory buffer.
Modifies:
compressed
compressedProducedBytes
nativeFile
nativeFileEndHit
============================
*/
void idFile_SaveGamePipelined::ReadBlock()
{
2012-11-26 18:58:24 +00:00
assert( nativeFile != NULL );
// normally run in a separate thread
if( nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
return;
}
// when we are reading the last block of the file, we may not fill the entire block
assert( 0 == ( compressedProducedBytes & ( COMPRESSED_BLOCK_SIZE - 1 ) ) );
byte* dest = &compressed[ compressedProducedBytes & ( COMPRESSED_BUFFER_SIZE - 1 ) ];
2012-11-26 18:58:24 +00:00
size_t ioBytes = nativeFile->Read( dest, COMPRESSED_BLOCK_SIZE );
compressedProducedBytes += ioBytes;
if( ioBytes != COMPRESSED_BLOCK_SIZE )
{
2012-11-26 18:58:24 +00:00
nativeFileEndHit = true;
}
}
/*
============================
idFile_SaveGamePipelined::PumpCompressedBlock
Reads:
compressed
compressedProducedBytes
Modifies:
dataIO
byteIO
compressedConsumedBytes
============================
*/
void idFile_SaveGamePipelined::PumpCompressedBlock()
{
2012-11-26 18:58:24 +00:00
// block until the background thread is done with the last block
if( readThread != NULL )
{
2012-11-26 18:58:24 +00:00
readThread->WaitForThread();
}
else if( nativeFile == NULL )
{
if( !nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
blockAvailable.Wait();
}
}
2012-11-26 18:58:24 +00:00
// fetch the next block read in
dataIO = &compressed[ compressedConsumedBytes & ( COMPRESSED_BUFFER_SIZE - 1 ) ];
bytesIO = compressedProducedBytes - compressedConsumedBytes;
compressedConsumedBytes = compressedProducedBytes;
if( readThread != NULL )
{
2012-11-26 18:58:24 +00:00
// signal read thread to read another block
readThread->SignalWork();
}
else if( nativeFile != NULL )
{
2012-11-26 18:58:24 +00:00
// run syncronously
ReadBlock();
}
else
{
2012-11-26 18:58:24 +00:00
// request a new block
blockRequested.Raise();
}
}
/*
============================
idFile_SaveGamePipelined::DecompressBlock
Decompresses the next data block from the memory buffer
Normally this runs in a separate thread when signalled, but
can be called in the main thread for debugging.
This will not exit until a complete block has been decompressed,
unless end-of-file is reached.
This may require additional compressed blocks to be read.
Reads:
nativeFileEndHit
Modifies:
dataIO
bytesIO
uncompressed
uncompressedProducedBytes
zStreamEndHit
zStream
============================
*/
void idFile_SaveGamePipelined::DecompressBlock()
{
if( zStreamEndHit )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
assert( ( uncompressedProducedBytes & ( UNCOMPRESSED_BLOCK_SIZE - 1 ) ) == 0 );
zStream.next_out = ( Bytef* )&uncompressed[ uncompressedProducedBytes & ( UNCOMPRESSED_BUFFER_SIZE - 1 ) ];
2012-11-26 18:58:24 +00:00
zStream.avail_out = UNCOMPRESSED_BLOCK_SIZE;
while( zStream.avail_out > 0 )
{
if( zStream.avail_in == 0 )
{
do
{
2012-11-26 18:58:24 +00:00
PumpCompressedBlock();
if( bytesIO == 0 && nativeFileEndHit )
{
2012-11-26 18:58:24 +00:00
// don't try to decompress any more if there is no more data
zStreamEndHit = true;
return;
}
}
while( bytesIO == 0 );
zStream.next_in = ( Bytef* ) dataIO;
zStream.avail_in = ( uInt ) bytesIO;
2012-11-26 18:58:24 +00:00
dataIO = NULL;
bytesIO = 0;
if( sgf_checksums.GetBool() )
{
if( sgf_testCorruption.GetInteger() == numChecksums )
{
2012-11-26 18:58:24 +00:00
zStream.next_in[0] ^= 0xFF;
}
zStream.avail_in -= sizeof( uint32 );
uint32 checksum = MD5_BlockChecksum( zStream.next_in, zStream.avail_in );
if( !verify( zStream.next_in[zStream.avail_in + 0] == ( ( checksum >> 0 ) & 0xFF ) ) ||
2012-11-26 18:58:24 +00:00
!verify( zStream.next_in[zStream.avail_in + 1] == ( ( checksum >> 8 ) & 0xFF ) ) ||
!verify( zStream.next_in[zStream.avail_in + 2] == ( ( checksum >> 16 ) & 0xFF ) ) ||
!verify( zStream.next_in[zStream.avail_in + 3] == ( ( checksum >> 24 ) & 0xFF ) ) )
{
2012-11-26 18:58:24 +00:00
// don't try to decompress any more if the checksum is wrong
zStreamEndHit = true;
return;
}
numChecksums++;
}
}
2012-11-26 18:58:24 +00:00
const int zstat = inflate( &zStream, Z_SYNC_FLUSH );
2012-11-26 18:58:24 +00:00
uncompressedProducedBytes = zStream.total_out;
if( zstat == Z_STREAM_END )
{
2012-11-26 18:58:24 +00:00
// don't try to decompress any more
zStreamEndHit = true;
return;
}
if( zstat != Z_OK )
{
2012-11-26 18:58:24 +00:00
idLib::Warning( "idFile_SaveGamePipelined::DecompressBlock: inflate() returned %i", zstat );
zStreamEndHit = true;
return;
}
}
2012-11-26 18:58:24 +00:00
assert( ( uncompressedProducedBytes & ( UNCOMPRESSED_BLOCK_SIZE - 1 ) ) == 0 );
}
/*
============================
idFile_SaveGamePipelined::PumpUncompressedBlock
Called when an uncompressed block is drained.
Reads:
uncompressed
uncompressedProducedBytes
Modifies:
dataZlib
bytesZlib
uncompressedConsumedBytes
============================
*/
void idFile_SaveGamePipelined::PumpUncompressedBlock()
{
if( decompressThread != NULL )
{
2012-11-26 18:58:24 +00:00
// make sure thread has completed the last work
decompressThread->WaitForThread();
}
2012-11-26 18:58:24 +00:00
// fetch the next block produced by Zlib
dataZlib = &uncompressed[ uncompressedConsumedBytes & ( UNCOMPRESSED_BUFFER_SIZE - 1 ) ];
bytesZlib = uncompressedProducedBytes - uncompressedConsumedBytes;
uncompressedConsumedBytes = uncompressedProducedBytes;
if( decompressThread != NULL )
{
2012-11-26 18:58:24 +00:00
// signal thread for more work
decompressThread->SignalWork();
}
else
{
2012-11-26 18:58:24 +00:00
// run syncronously
DecompressBlock();
}
}
/*
============================
idFile_SaveGamePipelined::Read
Modifies:
dataZlib
bytesZlib
============================
*/
int idFile_SaveGamePipelined::Read( void* buffer, int length )
{
if( buffer == NULL || length <= 0 )
{
2012-11-26 18:58:24 +00:00
return 0;
}
2012-11-26 18:58:24 +00:00
assert( mode == READ );
2012-11-26 18:58:24 +00:00
size_t ioCount = 0;
size_t lengthRemaining = length;
byte* buffer_p = ( byte* )buffer;
while( lengthRemaining > 0 )
{
while( bytesZlib == 0 )
{
2012-11-26 18:58:24 +00:00
PumpUncompressedBlock();
if( bytesZlib == 0 && zStreamEndHit )
{
2012-11-26 18:58:24 +00:00
return ioCount;
}
}
2012-11-26 18:58:24 +00:00
const size_t copyFromBlock = ( lengthRemaining < bytesZlib ) ? lengthRemaining : bytesZlib;
2012-11-26 18:58:24 +00:00
memcpy( buffer_p, dataZlib, copyFromBlock );
dataZlib += copyFromBlock;
bytesZlib -= copyFromBlock;
2012-11-26 18:58:24 +00:00
buffer_p += copyFromBlock;
ioCount += copyFromBlock;
lengthRemaining -= copyFromBlock;
}
return ioCount;
}
/*
===================================================================================
TEST CODE
===================================================================================
*/
/*
============================
TestProcessFile
============================
*/
static void TestProcessFile( const char* const filename )
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "Processing %s:\n", filename );
// load some test data
void* testData;
2012-11-26 18:58:24 +00:00
const int testDataLength = fileSystem->ReadFile( filename, &testData, NULL );
const char* const outFileName = "junk/savegameTest.bin";
idFile_SaveGamePipelined* saveFile = new( TAG_IDFILE ) idFile_SaveGamePipelined;
2012-11-26 18:58:24 +00:00
saveFile->OpenForWriting( outFileName, true );
2012-11-26 18:58:24 +00:00
const uint64 startWriteMicroseconds = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
saveFile->Write( testData, testDataLength );
delete saveFile; // final flush
const int readDataLength = fileSystem->GetFileLength( outFileName );
2012-11-26 18:58:24 +00:00
const uint64 endWriteMicroseconds = Sys_Microseconds();
const uint64 writeMicroseconds = endWriteMicroseconds - startWriteMicroseconds;
idLib::Printf( "%lld microseconds to compress %i bytes to %i written bytes = %4.1f MB/s\n",
writeMicroseconds, testDataLength, readDataLength, ( float )readDataLength / writeMicroseconds );
void* readData = ( void* )Mem_Alloc( testDataLength, TAG_SAVEGAMES );
2012-11-26 18:58:24 +00:00
const uint64 startReadMicroseconds = Sys_Microseconds();
idFile_SaveGamePipelined* loadFile = new( TAG_IDFILE ) idFile_SaveGamePipelined;
2012-11-26 18:58:24 +00:00
loadFile->OpenForReading( outFileName, true );
loadFile->Read( readData, testDataLength );
delete loadFile;
2012-11-26 18:58:24 +00:00
const uint64 endReadMicroseconds = Sys_Microseconds();
const uint64 readMicroseconds = endReadMicroseconds - startReadMicroseconds;
idLib::Printf( "%lld microseconds to decompress = %4.1f MB/s\n", readMicroseconds, ( float )testDataLength / readMicroseconds );
2012-11-26 18:58:24 +00:00
int comparePoint;
for( comparePoint = 0; comparePoint < testDataLength; comparePoint++ )
{
if( ( ( byte* )readData )[comparePoint] != ( ( byte* )testData )[comparePoint] )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( comparePoint != testDataLength )
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "Compare failed at %i.\n", comparePoint );
assert( 0 );
}
else
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "Compare succeeded.\n" );
}
Mem_Free( readData );
Mem_Free( testData );
}
/*
============================
TestSaveGameFile
============================
*/
CONSOLE_COMMAND( TestSaveGameFile, "Exercises the pipelined savegame code", 0 )
{
2012-11-26 18:58:24 +00:00
#if 1
TestProcessFile( "maps/game/wasteland1/wasteland1.map" );
#else
// test every file in base (found a fencepost error >100 files in originally!)
idFileList* fileList = fileSystem->ListFiles( "", "" );
for( int i = 0; i < fileList->GetNumFiles(); i++ )
{
2012-11-26 18:58:24 +00:00
TestProcessFile( fileList->GetFile( i ) );
common->UpdateConsoleDisplay();
}
delete fileList;
#endif
}
/*
============================
TestCompressionSpeeds
============================
*/
CONSOLE_COMMAND( TestCompressionSpeeds, "Compares zlib and our code", 0 )
{
const char* const filename = "-colorMap.tga";
2012-11-26 18:58:24 +00:00
idLib::Printf( "Processing %s:\n", filename );
// load some test data
void* testData;
2012-11-26 18:58:24 +00:00
const int testDataLength = fileSystem->ReadFile( filename, &testData, NULL );
2012-11-26 18:58:24 +00:00
const int startWriteMicroseconds = Sys_Microseconds();
idCompressor* compressor = idCompressor::AllocLZW();
2012-11-26 18:58:24 +00:00
// idFile *f = fileSystem->OpenFileWrite( "junk/lzwTest.bin" );
idFile_Memory* f = new( TAG_IDFILE ) idFile_Memory( "junk/lzwTest.bin" );
2012-11-26 18:58:24 +00:00
compressor->Init( f, true, 8 );
2012-11-26 18:58:24 +00:00
compressor->Write( testData, testDataLength );
2012-11-26 18:58:24 +00:00
const int readDataLength = f->Tell();
2012-11-26 18:58:24 +00:00
delete compressor;
delete f;
2012-11-26 18:58:24 +00:00
const int endWriteMicroseconds = Sys_Microseconds();
const int writeMicroseconds = endWriteMicroseconds - startWriteMicroseconds;
idLib::Printf( "%i microseconds to compress %i bytes to %i written bytes = %4.1f MB/s\n",
writeMicroseconds, testDataLength, readDataLength, ( float )readDataLength / writeMicroseconds );
2012-11-26 18:58:24 +00:00
}