diff --git a/src/emufile.cpp b/src/emufile.cpp index 7068220a..d07ab65f 100644 --- a/src/emufile.cpp +++ b/src/emufile.cpp @@ -11,4 +11,18 @@ bool EMUFILE::readAllBytes(std::vector* dstbuf, const std::string& fname) dstbuf->resize(size); file.fread(&dstbuf->at(0),size); return true; +} + +EMUFILE* EMUFILE::memwrap(EMUFILE* fp) +{ + EMUFILE_FILE* file; + EMUFILE_MEMORY* mem; + file = dynamic_cast(fp); + mem = dynamic_cast(fp); + if(mem) return mem; + mem = new EMUFILE_MEMORY(file->size()); + if(file->size()==0) return mem; + file->fread(mem->buf(),file->size()); + delete file; + return mem; } \ No newline at end of file diff --git a/src/emufile.h b/src/emufile.h index 98cc8e19..77ab466b 100644 --- a/src/emufile.h +++ b/src/emufile.h @@ -52,6 +52,10 @@ public: : failbit(false) {} + + //takes control of the provided EMUFILE and returns a new EMUFILE which is guranteed to be in memory + static EMUFILE* memwrap(EMUFILE* fp); + virtual ~EMUFILE() {} static bool readAllBytes(std::vector* buf, const std::string& fname); @@ -141,9 +145,19 @@ public: virtual int fgetc() { u8 temp; - if(_fread(&temp,1) != 1) - return EOF; - else return temp; + + //need an optimized codepath + //if(_fread(&temp,1) != 1) + // return EOF; + //else return temp; + u32 remain = len-pos; + if(remain<1) { + failbit = true; + return -1; + } + temp = buf()[pos]; + pos++; + return temp; } virtual int fputc(int c) { u8 temp = (u8)c; diff --git a/src/movie.cpp b/src/movie.cpp index 0a9fcb50..f5e5be27 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -906,6 +906,7 @@ bool FCEUI_LoadMovie(const char *fname, bool _read_only, bool tasedit, int _paus strcpy(curMovieFilename, fname); FCEUFILE *fp = FCEU_fopen(fname,0,"rb",0); + fp->stream = EMUFILE::memwrap(fp->stream); if (!fp) return false; if(fp->isArchive() && !_read_only) { FCEU_PrintError("Cannot open a movie in read+write from an archive.");