Skip to content

Commit

Permalink
For Qt GUI, store archive file index so that a hard reload knows how …
Browse files Browse the repository at this point in the history
…to refind the ROM inside the archive. For netplay, don't send entire zip archives. Instead just send the single extracted ROM of interest.
  • Loading branch information
thor2016 committed May 5, 2024
1 parent 4b36de6 commit 0ea2670
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 40 deletions.
48 changes: 29 additions & 19 deletions src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,41 +343,49 @@ 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)
{
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);

Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions src/drivers/Qt/NetPlayMsgDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,27 @@ 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));
}

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);
}
};

Expand Down
46 changes: 29 additions & 17 deletions src/drivers/Qt/fceuWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -600,19 +592,21 @@ 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 )
{
CloseGame();
//printf("Loading: '%s'\n", romPath );
LoadGame ( romPath );
}
fceuWrapperClearArchiveFileLoadIndex();
}
return 0;
}
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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) ||
Expand All @@ -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<size_t>(archiveFileLoadIndex) < asr.files.size()))
{
searchFile.clear();
}
else if ( consoleWindow != NULL )
{
int sel = consoleWindow->showListSelectDialog( "Select ROM From Archive", fileList );

Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/drivers/Qt/fceuWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/fceu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down

0 comments on commit 0ea2670

Please sign in to comment.