From d642033ae469798d8e0df8141af52c7e86aced0a Mon Sep 17 00:00:00 2001 From: Dmugetsu Date: Sun, 16 Feb 2025 12:07:16 -0600 Subject: [PATCH] Changed all QT stuff to std (still cant fetch correctly the lastgamepath) --- src/emulator.cpp | 31 +++---------- src/emulator.h | 9 ++-- src/qt_gui/main_window.cpp | 45 ++++++++---------- src/qt_gui/main_window.h | 7 ++- src/sdl_window.cpp | 93 ++++++++++++++++++++++++-------------- 5 files changed, 90 insertions(+), 95 deletions(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index 453c5a7bc..906f8c89a 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -294,12 +294,11 @@ void Core::Emulator::Run(const std::filesystem::path& file, const std::vector MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); @@ -766,12 +767,6 @@ void MainWindow::BootGame() { } } } -#ifdef ENABLE_QT_GUI - -QString MainWindow::getLastEbootPath() { - return QString(); -} -#endif void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int nPkg) { if (Loader::DetectFileType(file) == Loader::FileTypes::Pkg) { @@ -1223,6 +1218,10 @@ void MainWindow::OnLanguageChanged(const std::string& locale) { LoadTranslation(); } +static void ShowMessageBox(const std::string& title, const std::string& message) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, title.c_str(), message.c_str(), NULL); +} + bool MainWindow::eventFilter(QObject* obj, QEvent* event) { if (event->type() == QEvent::KeyPress) { QKeyEvent* keyEvent = static_cast(event); @@ -1242,10 +1241,7 @@ void MainWindow::StartEmulator(std::filesystem::path path) { QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Game is already running!"))); return; } - - lastGamePath = path; // Store the last played game isGameRunning = true; - #ifdef __APPLE__ // SDL on macOS requires main thread. Core::Emulator emulator; @@ -1259,47 +1255,44 @@ void MainWindow::StartEmulator(std::filesystem::path path) { #endif } -#ifdef ENABLE_QT_GUI - void MainWindow::StopGame() { if (!isGameRunning) { - QMessageBox::information(this, tr("Stop Game"), tr("No game is currently running.")); + ShowMessageBox("Stop Game", "No game is currently running."); return; } - // Get the emulator instance and stop it - Core::Emulator& emulator = Core::Emulator::GetInstance(); // Use the correct instance method + Core::Emulator& emulator = Core::Emulator::GetInstance(); emulator.StopEmulation(); - isGameRunning = false; - QMessageBox::information(this, tr("Stop Game"), tr("Game has been stopped successfully.")); - + if (isGameRunning == true) + ; + ShowMessageBox("Stop Game", "Game has been stopped successfully."); SDL_Event quitEvent; quitEvent.type = SDL_EVENT_QUIT + 1; SDL_PushEvent(&quitEvent); } +std::string MainWindow::getLastEbootPath() { + return std::string(); +} + void MainWindow::RestartGame() { if (!isGameRunning) { - QMessageBox::warning(this, tr("Restart Game"), tr("No game is running to restart.")); + ShowMessageBox("Restart Game", "No game is running to restart."); return; } + std::string lastGamePath = getLastEbootPath(); + if (lastGamePath.empty()) { - QMessageBox::warning(this, tr("Restart Game"), tr("No recent game found.")); - return; + ShowMessageBox("Restart Game", "No recent game found."); } - // Stop the current game properly StopGame(); - // Ensure the game has fully stopped before restarting while (isGameRunning) { - QCoreApplication::processEvents(); - QThread::msleep(100); // Small delay to allow cleanup + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } - // Restart the emulator with the last played game StartEmulator(lastGamePath); } -#endif diff --git a/src/qt_gui/main_window.h b/src/qt_gui/main_window.h index f3cbe6b6d..1f10c2ea5 100644 --- a/src/qt_gui/main_window.h +++ b/src/qt_gui/main_window.h @@ -67,10 +67,9 @@ private: void SetUiIcons(bool isWhite); void InstallPkg(); void BootGame(); -#ifdef ENABLE_QT_GUI - QString getLastEbootPath(); - std::filesystem::path lastGamePath; -#endif + std::string lastGamePath; + std::string getLastEbootPath(); + void AddRecentFiles(QString filePath); void LoadTranslation(); void PlayBackgroundMusic(); diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index b955577d7..471876a29 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -10,6 +10,11 @@ #include #include "common/memory_patcher.h" #endif +#include +#include +#include +#include +#include #include "SDL3/SDL_events.h" #include "SDL3/SDL_hints.h" #include "SDL3/SDL_init.h" @@ -32,6 +37,11 @@ #ifdef __APPLE__ #include "SDL3/SDL_metal.h" #endif +#ifdef _WIN32 +#include +#else +#include +#endif namespace Input { @@ -384,52 +394,67 @@ void WindowSDL::WaitEvent() { } void WindowSDL::RelaunchEmulator() { -#ifdef Q_OS_WIN - QString emulatorPath = QCoreApplication::applicationFilePath(); // Get current executable path - QString emulatorDir = QFileInfo(emulatorPath).absolutePath(); // Get working directory - QString scriptFileName = - QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/relaunch.ps1"; - QString scriptContent = - QStringLiteral( - "Start-Sleep -Seconds 2\n" - "Start-Process -FilePath '%1' -WorkingDirectory ([WildcardPattern]::Escape('%2'))\n") - .arg(emulatorPath, emulatorDir); +#ifdef _WIN32 + char emulatorPath[MAX_PATH]; + GetModuleFileNameA(nullptr, emulatorPath, MAX_PATH); // Get current executable path - QFile scriptFile(scriptFileName); - if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - QTextStream out(&scriptFile); - scriptFile.write("\xEF\xBB\xBF"); // UTF-8 BOM - out << scriptContent; + std::string emulatorDir = std::string(emulatorPath); + size_t pos = emulatorDir.find_last_of("\\/"); + if (pos != std::string::npos) { + emulatorDir = emulatorDir.substr(0, pos); // Extract directory + } + + std::string scriptFileName = std::getenv("TEMP") + std::string("\\relaunch.ps1"); + std::ofstream scriptFile(scriptFileName); + + if (scriptFile.is_open()) { + scriptFile << "Start-Sleep -Seconds 2\n" + << "Start-Process -FilePath '" << emulatorPath + << "' -WorkingDirectory ([WildcardPattern]::Escape('" << emulatorDir << "'))\n"; scriptFile.close(); // Execute PowerShell script - QProcess::startDetached("powershell.exe", QStringList() << "-ExecutionPolicy" - << "Bypass" - << "-File" << scriptFileName); + std::string command = + "powershell.exe -ExecutionPolicy Bypass -File \"" + scriptFileName + "\""; + system(command.c_str()); + } else { + std::cerr << "Failed to create relaunch script" << std::endl; } -#elif defined(Q_OS_LINUX) || defined(Q_OS_MAC) - QString emulatorPath = QCoreApplication::applicationFilePath(); // Get current executable path - QString emulatorDir = QFileInfo(emulatorPath).absolutePath(); // Get working directory - QString scriptFileName = "/tmp/relaunch.sh"; - QString scriptContent = QStringLiteral("#!/bin/bash\n" - "sleep 2\n" - "cd '%2'\n" - "./'%1' &\n") - .arg(QFileInfo(emulatorPath).fileName(), emulatorDir); +#elif defined(__linux__) || defined(__APPLE__) + char emulatorPath[1024]; + ssize_t count = readlink("/proc/self/exe", emulatorPath, sizeof(emulatorPath) - 1); + if (count != -1) { + emulatorPath[count] = '\0'; + } else { + std::cerr << "Failed to get executable path" << std::endl; + return; + } - QFile scriptFile(scriptFileName); - if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - QTextStream out(&scriptFile); - out << scriptContent; + std::string emulatorDir = std::string(emulatorPath); + size_t pos = emulatorDir.find_last_of("/"); + if (pos != std::string::npos) { + emulatorDir = emulatorDir.substr(0, pos); // Extract directory + } + + std::string scriptFileName = "/tmp/relaunch.sh"; + std::ofstream scriptFile(scriptFileName); + + if (scriptFile.is_open()) { + scriptFile << "#!/bin/bash\n" + << "sleep 2\n" + << "cd '" << emulatorDir << "'\n" + << "./'" << emulatorPath + pos + 1 << "' &\n"; scriptFile.close(); // Make script executable - scriptFile.setPermissions(QFileDevice::ExeOwner | QFileDevice::ReadOwner | - QFileDevice::WriteOwner); + chmod(scriptFileName.c_str(), S_IRWXU); // Execute bash script - QProcess::startDetached("bash", QStringList() << scriptFileName); + std::string command = "bash " + scriptFileName; + system(command.c_str()); + } else { + std::cerr << "Failed to create relaunch script" << std::endl; } #endif }