From c366dc1f2b16dda058bd5137280a90ef675765f2 Mon Sep 17 00:00:00 2001 From: zeromus Date: Tue, 3 Jun 2008 05:50:58 +0000 Subject: [PATCH] repair and speedup tasedit --- src/movie.cpp | 15 ++++++++---- src/state.cpp | 51 +++++++++++++++++++++++----------------- src/utils/memorystream.h | 35 +++++++++++++++++---------- 3 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/movie.cpp b/src/movie.cpp index 08689bfb..510ebe5a 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -87,7 +87,7 @@ void MovieData::TryDumpIncremental() { if(currFrameCounter < (int)currMovieData.records.size()) { - MovieData::dumpSavestateTo(&currMovieData.records[currFrameCounter].savestate,Z_NO_COMPRESSION); + MovieData::dumpSavestateTo(&currMovieData.records[currFrameCounter].savestate,Z_DEFAULT_COMPRESSION); currMovieData.greenZoneCount++; } } @@ -437,6 +437,7 @@ void MovieData::dumpSavestateTo(std::vector* buf, int compressionLevel) { memorystream ms(buf); FCEUSS_SaveMS(&ms,compressionLevel); + ms.trim(); } //begin playing an existing movie @@ -716,9 +717,12 @@ bool FCEUMOV_ReadState(FILE* st, uint32 size) fread(&buf[0],1,size,st); FILE* tmp = tmpfile(); fwrite(&buf[0],1,size,tmp); - FILE* wtf = fopen("d:\\wtf.txt","wb"); - fwrite(&buf[0],1,size,wtf); - fclose(wtf); + //--------- + //(debug) + //FILE* wtf = fopen("d:\\wtf.txt","wb"); + //fwrite(&buf[0],1,size,wtf); + //fclose(wtf); + //--------- fseek(tmp,0,SEEK_SET); MovieData tempMovieData = MovieData(); LoadFM2(tempMovieData, tmp); @@ -797,7 +801,8 @@ int FCEUMOV_PostLoad(void) if(!FCEUI_IsMovieActive()) return 1; else - return load_successful; + //mbg tasedit hack!!!!!!!!! + return load_successful || moviePleaseLogSavestates; } int FCEUI_IsMovieActive(void) diff --git a/src/state.cpp b/src/state.cpp index c668b077..830ce434 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -389,24 +389,25 @@ bool FCEUSS_SaveMS(std::ostream* outstream, int compressionLevel) FCEUPPU_SaveState(); FCEUSND_SaveState(); totalsize=WriteStateChunk(os,1,SFCPU); - ms.sync(); totalsize+=WriteStateChunk(os,2,SFCPUC); - ms.sync(); totalsize+=WriteStateChunk(os,3,FCEUPPU_STATEINFO); - ms.sync(); totalsize+=WriteStateChunk(os,4,FCEUCTRL_STATEINFO); - ms.sync(); totalsize+=WriteStateChunk(os,5,FCEUSND_STATEINFO); - ms.sync(); if(FCEUI_IsMovieActive()) { totalsize+=WriteStateChunk(os,6,FCEUMOV_STATEINFO); - ms.sync(); - uint32 size = FCEUMOV_WriteState((std::ostream*)0); - os->put(7); - write32le(size, os); - FCEUMOV_WriteState(os); - totalsize += 5 + size; + + //MBG tasedit HACK HACK HACK! + //do not save the movie state if we are in tasedit! that is a huge waste of time and space! + extern bool moviePleaseLogSavestates; + if(!moviePleaseLogSavestates) + { + uint32 size = FCEUMOV_WriteState((std::ostream*)0); + os->put(7); + write32le(size, os); + FCEUMOV_WriteState(os); + totalsize += 5 + size; + } } // save back buffer { @@ -416,7 +417,6 @@ bool FCEUSS_SaveMS(std::ostream* outstream, int compressionLevel) write32le(size, os); os->write((char*)XBackBuf,size); totalsize += 5 + size; - ms.sync(); } if(SPreSave) SPreSave(); @@ -453,7 +453,7 @@ bool FCEUSS_SaveMS(std::ostream* outstream, int compressionLevel) //dump it to the destination file outstream->write((char*)header,16); - outstream->write((char*)cbuf,comprlen); + outstream->write((char*)cbuf,comprlen==-1?totalsize:comprlen); if(cbuf != (uint8*)ms.buf()) delete[] cbuf; return error == Z_OK; @@ -657,16 +657,25 @@ bool FCEUSS_LoadFP(FILE *st, ENUM_SSLOADPARAMS params) int stateversion = FCEU_de32lsb(header + 8); int comprlen = FCEU_de32lsb(header + 12); - //load the compressed chunk and decompress - std::vector cbuf(comprlen); + //load the compressed chunk and decompress if necessary std::vector buf(totalsize); - if(fread(&cbuf[0],1,comprlen,st) != comprlen) - return false; + if(comprlen == -1) + { + int ret = fread(&buf[0],1,totalsize,st); + if(ret != totalsize) + return false; + } + else + { + std::vector cbuf(comprlen); + if(fread(&cbuf[0],1,comprlen,st) != comprlen) + return false; - uLongf uncomprlen = totalsize; - int error = uncompress(&buf[0],&uncomprlen,&cbuf[0],comprlen); - if(error != Z_OK || uncomprlen != totalsize) - return false; + uLongf uncomprlen = totalsize; + int error = uncompress(&buf[0],&uncomprlen,&cbuf[0],comprlen); + if(error != Z_OK || uncomprlen != totalsize) + return false; + } //dump it back to a tempfile FILE* tmp = tmpfile(); diff --git a/src/utils/memorystream.h b/src/utils/memorystream.h index fcc76620..28238c46 100644 --- a/src/utils/memorystream.h +++ b/src/utils/memorystream.h @@ -30,13 +30,6 @@ private: public: - //the logical length of the buffer - size_t size() - { - sync(); - return length; - } - memory_streambuf() : length(0) , myBuf(true) @@ -73,12 +66,18 @@ public: sync(); } - ~memory_streambuf() { //only cleanup if we own the seq if(myBuf) delete[] buf; } + + //the logical length of the buffer + size_t size() + { + sync(); + return length; + } //to avoid copying, rebuilds the provided vector and copies the streambuf contents into it void toVector(std::vector& out) @@ -93,6 +92,14 @@ public: return std::vector(buf,buf+length); } + //if the memorystream wraps a vector, the vector will be trimmed to the correct size,. + //you probably need to use this if you are using the vector wrapper + void trim() + { + if(!usevec) return; + usevec->resize(size()); + } + //tells the current read or write position std::streampos tell(std::ios::openmode which) { @@ -234,13 +241,13 @@ public: {} memorystream(char* usebuf, int buflength) - : streambuf(usebuf, buflength) - , std::basic_iostream >(&streambuf) + : std::basic_iostream >(&streambuf) + , streambuf(usebuf, buflength) {} memorystream(std::vector* usevec) - : streambuf(usevec) - , std::basic_iostream >(&streambuf) + : std::basic_iostream >(&streambuf) + , streambuf(usevec) {} //the underlying memory_streambuf @@ -255,4 +262,8 @@ public: void sync() { streambuf.sync(); } //rewinds the cursors to offset 0 void rewind() { streambuf.seekpos(0,std::ios::in | std::ios::out); } + + //if the memorystream wraps a vector, the vector will be trimmed to the correct size,. + //you probably need to use this if you are using the vector wrapper + void trim() { streambuf.trim(); } };