diff --git a/src/drivers/Qt/NetPlay.cpp b/src/drivers/Qt/NetPlay.cpp index 46259836..83add75d 100644 --- a/src/drivers/Qt/NetPlay.cpp +++ b/src/drivers/Qt/NetPlay.cpp @@ -343,23 +343,34 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client ) char buf[BufferSize]; size_t bytesRead; long fileSize = 0; + int userCancel = 0; + int archiveIndex = -1; netPlayLoadRomReq msg; + const char* romextensions[] = { "nes", "fds", "nsf", 0 }; const char *filepath = nullptr; - if ( GameInfo ) + if ( GameInfo == nullptr) { - printf("filename: '%s' \n", GameInfo->filename ); - printf("archiveFilename: '%s' \n", GameInfo->archiveFilename ); + return -1; + } - if (GameInfo->archiveFilename) - { - filepath = GameInfo->archiveFilename; - } - else - { - filepath = GameInfo->filename; - } + //printf("filename: '%s' \n", GameInfo->filename ); + //printf("archiveFilename: '%s' \n", GameInfo->archiveFilename ); + + if (GameInfo->archiveFilename) + { + filepath = GameInfo->archiveFilename; + } + else + { + filepath = GameInfo->filename; + } + archiveIndex = GameInfo->archiveIndex; + + if (archiveIndex >= 0) + { + msg.archiveIndex = archiveIndex; } if (filepath == nullptr) @@ -367,17 +378,14 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client ) return -1; } printf("Prep ROM Load Request: %s \n", filepath ); - FILE *fp = ::fopen( filepath, "rb"); + + FCEUFILE* fp = FCEU_fopen( filepath, nullptr, "rb", 0, archiveIndex, romextensions, &userCancel); if (fp == nullptr) { return -1; } - fseek( fp, 0, SEEK_END); - - fileSize = ftell(fp); - - rewind(fp); + fileSize = fp->size; QFileInfo fileInfo(GameInfo->filename); @@ -389,13 +397,13 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client ) sendMsg( client, &msg, sizeof(netPlayLoadRomReq), [&msg]{ msg.toNetworkByteOrder(); } ); - while ( (bytesRead = fread( buf, 1, sizeof(buf), fp )) > 0 ) + while ( (bytesRead = FCEU_fread( buf, 1, sizeof(buf), fp )) > 0 ) { sendMsg( client, buf, bytesRead ); } client->flushData(); - ::fclose(fp); + FCEU_fclose(fp); return 0; } @@ -1977,7 +1985,9 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize ) tmpFile.close(); FCEU_WRAPPER_LOCK(); + fceuWrapperSetArchiveFileLoadIndex( msg->archiveIndex ); LoadGame( filepath.toLocal8Bit().constData(), true, true ); + fceuWrapperClearArchiveFileLoadIndex(); opsCrc32 = 0; netPlayFrameData.reset(); diff --git a/src/drivers/Qt/NetPlayMsgDef.h b/src/drivers/Qt/NetPlayMsgDef.h index 091e2bc4..f5868e98 100644 --- a/src/drivers/Qt/NetPlayMsgDef.h +++ b/src/drivers/Qt/NetPlayMsgDef.h @@ -236,10 +236,11 @@ struct netPlayLoadRomReq netPlayMsgHdr hdr; uint32_t fileSize; + uint32_t archiveIndex; char fileName[256]; netPlayLoadRomReq(void) - : hdr(NETPLAY_LOAD_ROM_REQ, sizeof(netPlayLoadRomReq)), fileSize(0) + : hdr(NETPLAY_LOAD_ROM_REQ, sizeof(netPlayLoadRomReq)), fileSize(0), archiveIndex(0) { memset(fileName, 0, sizeof(fileName)); } @@ -247,13 +248,15 @@ struct netPlayLoadRomReq void toHostByteOrder() { hdr.toHostByteOrder(); - fileSize = netPlayByteSwap(fileSize); + fileSize = netPlayByteSwap(fileSize); + archiveIndex = netPlayByteSwap(archiveIndex); } void toNetworkByteOrder() { hdr.toNetworkByteOrder(); - fileSize = netPlayByteSwap(fileSize); + fileSize = netPlayByteSwap(fileSize); + archiveIndex = netPlayByteSwap(archiveIndex); } }; diff --git a/src/drivers/Qt/fceuWrapper.cpp b/src/drivers/Qt/fceuWrapper.cpp index 6a92d86a..060b97e5 100644 --- a/src/drivers/Qt/fceuWrapper.cpp +++ b/src/drivers/Qt/fceuWrapper.cpp @@ -65,6 +65,7 @@ #include "common/os_utils.h" #include "common/configSys.h" #include "utils/timeStamp.h" +#include "utils/StringUtils.h" #include "../../oldmovie.h" #include "../../types.h" @@ -109,6 +110,7 @@ static int mutexLocks = 0; static int mutexPending = 0; static bool emulatorHasMutex = 0; unsigned int emulatorCycleCount = 0; +static int archiveFileLoadIndex = -1; extern double g_fpsScale; @@ -341,16 +343,6 @@ int LoadGame(const char *path, bool silent, bool netPlayRequested) { fullpath.assign( path ); } -//#if defined(__linux__) || defined(__APPLE__) || defined(__unix__) -// -// // Resolve absolute path to file -// if ( realpath( path, fullpath ) == NULL ) -// { -// strcpy( fullpath, path ); -// } -//#else -// strcpy( fullpath, path ); -//#endif //printf("Fullpath: %zi '%s'\n", sizeof(fullpath), fullpath ); @@ -600,12 +592,13 @@ int fceuWrapperHardReset(void) if ( GameInfo->archiveFilename ) { - strcpy( romPath, GameInfo->archiveFilename ); + Strlcpy( romPath, GameInfo->archiveFilename, sizeof(romPath) ); } else if ( GameInfo->filename ) { - strcpy( romPath, GameInfo->filename ); + Strlcpy( romPath, GameInfo->filename, sizeof(romPath) ); } + fceuWrapperSetArchiveFileLoadIndex( GameInfo->archiveIndex ); if ( romPath[0] != 0 ) { @@ -613,6 +606,7 @@ int fceuWrapperHardReset(void) //printf("Loading: '%s'\n", romPath ); LoadGame ( romPath ); } + fceuWrapperClearArchiveFileLoadIndex(); } return 0; } @@ -1739,7 +1733,7 @@ static FCEUFILE* minizip_OpenArchive(ArchiveScanRecord& asr, std::string &fname, //printf("Filename: %u '%s' \n", fi.uncompressed_size, filename ); - if (searchFile) + if ( (searchFile != nullptr) && !searchFile->empty()) { if ( strcmp( searchFile->c_str(), filename ) == 0 ) { @@ -1858,7 +1852,8 @@ static FCEUFILE* libarchive_OpenArchive( ArchiveScanRecord& asr, std::string& fn filename = archive_entry_pathname(entry); fileSize = archive_entry_size(entry); - if (searchFile) + printf("ArchiveFile:%i %s\n", idx, filename); + if ( (searchFile != nullptr) && !searchFile->empty()) { if (strcmp( filename, searchFile->c_str() ) == 0) { @@ -1923,6 +1918,16 @@ static FCEUFILE* libarchive_OpenArchive( ArchiveScanRecord& asr, std::string& fn #endif +void fceuWrapperSetArchiveFileLoadIndex(int idx) +{ + archiveFileLoadIndex = idx; +} + +void fceuWrapperClearArchiveFileLoadIndex() +{ + archiveFileLoadIndex = -1; +} + FCEUFILE* FCEUD_OpenArchive(ArchiveScanRecord& asr, std::string& fname, std::string* innerFilename, int* userCancel) { FCEUFILE* fp = nullptr; @@ -1940,6 +1945,7 @@ FCEUFILE* FCEUD_OpenArchive(ArchiveScanRecord& asr, std::string& fname, std::str { char base[512], suffix[128]; + //printf("File:%zi %s\n", i, asr.files[i].name.c_str() ); getFileBaseName( asr.files[i].name.c_str(), base, suffix ); if ( (strcasecmp( suffix, ".nes" ) == 0) || @@ -1954,7 +1960,11 @@ FCEUFILE* FCEUD_OpenArchive(ArchiveScanRecord& asr, std::string& fname, std::str if ( fileList.size() > 1 ) { - if ( consoleWindow != NULL ) + if ( (archiveFileLoadIndex >= 0) && (static_cast(archiveFileLoadIndex) < asr.files.size())) + { + searchFile.clear(); + } + else if ( consoleWindow != NULL ) { int sel = consoleWindow->showListSelectDialog( "Select ROM From Archive", fileList ); @@ -1973,16 +1983,18 @@ FCEUFILE* FCEUD_OpenArchive(ArchiveScanRecord& asr, std::string& fname, std::str { searchFile = fileList[0]; } + //printf("Archive Search File: %s\n", searchFile.c_str()); } #ifdef _USE_LIBARCHIVE - fp = libarchive_OpenArchive(asr, fname, &searchFile, -1 ); + fp = libarchive_OpenArchive(asr, fname, &searchFile, archiveFileLoadIndex ); #endif if (fp == nullptr) { - fp = minizip_OpenArchive(asr, fname, &searchFile, -1 ); + fp = minizip_OpenArchive(asr, fname, &searchFile, archiveFileLoadIndex ); } + //printf("Archive File Index: %i\n", fp->archiveIndex); return fp; } diff --git a/src/drivers/Qt/fceuWrapper.h b/src/drivers/Qt/fceuWrapper.h index def80edc..3df7166f 100644 --- a/src/drivers/Qt/fceuWrapper.h +++ b/src/drivers/Qt/fceuWrapper.h @@ -48,6 +48,8 @@ int fceuWrapperHardReset(void); int fceuWrapperTogglePause(void); bool fceuWrapperGameLoaded(void); void fceuWrapperRequestAppExit(void); +void fceuWrapperClearArchiveFileLoadIndex(void); +void fceuWrapperSetArchiveFileLoadIndex(int idx); class fceuCriticalSection { diff --git a/src/fceu.cpp b/src/fceu.cpp index cca8fd26..7395db8e 100644 --- a/src/fceu.cpp +++ b/src/fceu.cpp @@ -483,6 +483,7 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen if (fp->archiveFilename != "") GameInfo->archiveFilename = strdup(fp->archiveFilename.c_str()); GameInfo->archiveCount = fp->archiveCount; + GameInfo->archiveIndex = fp->archiveIndex; GameInfo->soundchan = 0; GameInfo->soundrate = 0; diff --git a/src/git.h b/src/git.h index b02878cb..c245eb60 100644 --- a/src/git.h +++ b/src/git.h @@ -173,7 +173,8 @@ struct FCEUGI char* filename = nullptr; char* archiveFilename = nullptr; - int archiveCount = 0; + int archiveCount = 0; // the number of files that were in the archive + int archiveIndex = -1; // the index of the file within the archive bool loadedFromTmpFile = false; // Was loaded from temporary file, file most likely no longer exists };