Skip to content

Commit

Permalink
For Qt GUI, create a temp directory per every boot of application tha…
Browse files Browse the repository at this point in the history
…t can be used to store temporary files for things such as netplay roms. This folder is deleted at application exit.
  • Loading branch information
thor2016 committed May 5, 2024
1 parent 151c951 commit 4e57ca4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/drivers/Qt/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <QActionGroup>
#include <QShortcut>
#include <QUrl>
#include <QDir>

#include "../../fceu.h"
#include "../../fds.h"
Expand Down Expand Up @@ -125,6 +126,12 @@ consoleWin_t::consoleWin_t(QWidget *parent)
//QString libpath = QLibraryInfo::location(QLibraryInfo::PluginsPath);
//printf("LibPath: '%s'\n", libpath.toLocal8Bit().constData() );

tempDir = new QTemporaryDir();
if (tempDir->isValid())
{
printf("Temp Folder: %s\n", tempDir->path().toLocal8Bit().constData());
}

#ifdef __APPLE__
qt_set_sequence_auto_mnemonic(true);
#endif
Expand Down Expand Up @@ -385,6 +392,11 @@ consoleWin_t::~consoleWin_t(void)
consoleWindow = NULL;
}

if (tempDir != nullptr)
{
delete tempDir;
tempDir = nullptr;
}
}

int consoleWin_t::videoInit(void)
Expand Down Expand Up @@ -4729,6 +4741,20 @@ void consoleWin_t::loadMostRecentROM(void)
FCEU_WRAPPER_UNLOCK();
}

QString consoleWin_t::getTempDir()
{
QString path;
if (tempDir->isValid())
{
path = tempDir->path();
}
else
{
path = QDir::tempPath();
}
return path;
}

int consoleWin_t::getPeriodicInterval(void)
{
return gameTimer->interval();
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QCursor>
#include <QMutex>
#include <QColor>
#include <QTemporaryDir>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QRecursiveMutex>
#endif
Expand Down Expand Up @@ -184,6 +185,8 @@ class consoleWin_t : public QMainWindow

QColor *getVideoBgColorPtr(void){ return &videoBgColor; }

QString getTempDir();

protected:
consoleMenuBar *menubar;

Expand Down Expand Up @@ -271,6 +274,7 @@ class consoleWin_t : public QMainWindow
QTimer *gameTimer;
QColor videoBgColor;
ColorMenuItem *bgColorMenuItem;
QTemporaryDir *tempDir;

std::string errorMsg;
bool errorMsgValid;
Expand Down
14 changes: 9 additions & 5 deletions src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,11 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client )

rewind(fp);

QFileInfo fileInfo(GameInfo->filename);

msg.hdr.msgSize += fileSize;
msg.fileSize = fileSize;
Strlcpy( msg.fileName, GameInfo->filename, sizeof(msg.fileName) );
Strlcpy( msg.fileName, fileInfo.fileName().toLocal8Bit().constData(), sizeof(msg.fileName) );

printf("Sending ROM Load Request: %s %lu\n", filepath, fileSize );

Expand Down Expand Up @@ -1958,15 +1960,17 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )
break;
case NETPLAY_LOAD_ROM_REQ:
{
QTemporaryFile tmpFile;
netPlayLoadRomReq *msg = static_cast<netPlayLoadRomReq*>(msgBuf);
msg->toHostByteOrder();
const char *romData = &static_cast<const char*>(msgBuf)[ sizeof(netPlayLoadRomReq) ];

FCEU_printf("Load ROM Request Received: %s\n", msg->fileName);
QFileInfo fileInfo = QFileInfo(msg->fileName);
QString tempPath = consoleWindow->getTempDir() + QString("/") + fileInfo.fileName();
FCEU_printf("Load ROM Request Received: %s\n", fileInfo.fileName().toLocal8Bit().constData());

tmpFile.setFileTemplate(QDir::tempPath() + QString("/tmpRom_XXXXXX.nes"));
tmpFile.open();
QFile tmpFile( tempPath );
//tmpFile.setFileTemplate(QDir::tempPath() + QString("/tmpRom_XXXXXX.nes"));
tmpFile.open(QIODeviceBase::ReadWrite);
QString filepath = tmpFile.fileName();
printf("Dumping Temp Rom to: %s\n", tmpFile.fileName().toLocal8Bit().constData());
tmpFile.write( romData, msgSize );
Expand Down

0 comments on commit 4e57ca4

Please sign in to comment.