diff --git a/desmume/src/NDSSystem.cpp b/desmume/src/NDSSystem.cpp index 5601d506d..989976aef 100644 --- a/desmume/src/NDSSystem.cpp +++ b/desmume/src/NDSSystem.cpp @@ -559,7 +559,6 @@ bool GameInfo::loadROM(std::string fname, u32 type) } } reader->Seek(fROM, headerOffset, SEEK_SET); - lastReadPos = 0; return true; } @@ -580,17 +579,16 @@ void GameInfo::closeROM() reader = NULL; romdataForReader = NULL; romsize = 0; - lastReadPos = 0xFFFFFFFF; } u32 GameInfo::readROM(u32 pos) { u32 num; u32 data; - if (lastReadPos != pos) - reader->Seek(fROM, pos + headerOffset, SEEK_SET); + + //reader must try to be efficient and not do unneeded seeks + reader->Seek(fROM, pos, SEEK_SET); num = reader->Read(fROM, &data, 4); - lastReadPos = (pos + num); //in case we didn't read enough data, pad the remainder with 0xFF u32 pad = 0; diff --git a/desmume/src/NDSSystem.h b/desmume/src/NDSSystem.h index 7b5edda84..a40bbf365 100644 --- a/desmume/src/NDSSystem.h +++ b/desmume/src/NDSSystem.h @@ -323,7 +323,6 @@ struct GameInfo u32 mask; u32 crc; u32 chipID; - u32 lastReadPos; u32 romType; u32 headerOffset; char ROMserial[20]; @@ -342,7 +341,6 @@ struct GameInfo romsize(0), cardSize(0), mask(0), - lastReadPos(0xFFFFFFFF), romType(ROM_NDS), headerOffset(0), _isDSiEnhanced(false) diff --git a/desmume/src/ROMReader.cpp b/desmume/src/ROMReader.cpp index 5d01bd3ef..8fd0bb911 100644 --- a/desmume/src/ROMReader.cpp +++ b/desmume/src/ROMReader.cpp @@ -69,7 +69,13 @@ ROMReader_struct STDROMReader = STDROMReaderWrite }; -void * STDROMReaderInit(const char * filename) +struct STDROMReaderData +{ + FILE* file; + long pos; +}; + +void* STDROMReaderInit(const char* filename) { #ifndef _MSC_VER struct stat sb; @@ -80,13 +86,21 @@ void * STDROMReaderInit(const char * filename) return 0; #endif - return (void *) fopen(filename, "rb"); + FILE* inf = fopen(filename, "rb"); + if(!inf) return NULL; + + STDROMReaderData* ret = new STDROMReaderData(); + ret->file = inf; + ret->pos = 0; + return (void*)ret; } + void STDROMReaderDeInit(void * file) { if (!file) return ; - fclose((FILE*)file); + fclose(((STDROMReaderData*)file)->file); + delete ((STDROMReaderData*)file); } u32 STDROMReaderSize(void * file) @@ -95,23 +109,32 @@ u32 STDROMReaderSize(void * file) if (!file) return 0; - fseek((FILE*)file, 0, SEEK_END); - size = ftell((FILE*)file); - fseek((FILE*)file, 0, SEEK_SET); + FILE* inf = ((STDROMReaderData*)file)->file; + + fseek(inf, 0, SEEK_END); + size = ftell(inf); + fseek(inf, ((STDROMReaderData*)file)->pos, SEEK_SET); return size; } int STDROMReaderSeek(void * file, int offset, int whence) { + //not normal fseek return value meanings. awesome. if (!file) return 0; - return fseek((FILE*)file, offset, whence); + if(whence == SEEK_SET && offset == ((STDROMReaderData*)file)->pos) + return 1; + fseek(((STDROMReaderData*)file)->file, offset, whence); + ((STDROMReaderData*)file)->pos = ftell(((STDROMReaderData*)file)->file); + return 1; } int STDROMReaderRead(void * file, void * buffer, u32 size) { if (!file) return 0; - return fread(buffer, 1, size, (FILE*)file); + int read = fread(buffer, 1, size, ((STDROMReaderData*)file)->file); + ((STDROMReaderData*)file)->pos += read; + return read; } int STDROMReaderWrite(void *, void *, u32) @@ -300,7 +323,9 @@ int MemROMReaderRead(void * file, void * buffer, u32 size) if(remain