Changed all QT stuff to std (still cant fetch correctly the lastgamepath)

This commit is contained in:
Dmugetsu 2025-02-16 12:07:16 -06:00
parent 2d33b026bb
commit d642033ae4
5 changed files with 90 additions and 95 deletions

View File

@ -294,12 +294,11 @@ void Core::Emulator::Run(const std::filesystem::path& file, const std::vector<st
std::quick_exit(0);
}
#ifdef ENABLE_QT_GUI
void Emulator::saveLastEbootPath(const QString& path) {
void Emulator::saveLastEbootPath(const std::string& path) {
lastEbootPath = path;
}
QString Emulator::getLastEbootPath() {
std::string Emulator::getLastEbootPath() const {
return lastEbootPath;
}
@ -313,7 +312,7 @@ void Emulator::StopEmulation() {
return;
is_running = false;
qDebug() << "Stopping emulator...";
LOG_INFO(Loader, "Stopping emulator...");
}
void Emulator::Restart() {
@ -328,32 +327,14 @@ void Emulator::Restart() {
StopEmulation();
// Wait a moment before restarting
QThread::sleep(2);
std::this_thread::sleep_for(std::chrono::seconds(2));
// Retrieve last known EBOOT path
QString lastEbootPath = getLastEbootPath();
if (lastEbootPath.isEmpty()) {
std::string lastEbootPath = getLastEbootPath();
if (lastEbootPath.empty()) {
LOG_ERROR(Loader, "No previous EBOOT path found! Cannot restart.");
return;
}
#endif
// Relaunch emulator with last game
#ifdef Q_OS_WIN
QString emulatorPath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/shadps4.exe";
QProcess::startDetached(emulatorPath, QStringList() << lastEbootPath);
#elif defined(Q_OS_LINUX)
QString emulatorPath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/Shadps4-qt.AppImage";
QProcess::startDetached(emulatorPath, QStringList() << lastEbootPath);
#elif defined(Q_OS_MAC)
QString emulatorPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
"/shadps4.app/Contents/MacOS/shadps4";
QProcess::startDetached(emulatorPath, QStringList() << lastEbootPath);
isRunning = true;
#endif
} // namespace Core
void Core::Emulator::LoadSystemModules(const std::string& game_serial) {

View File

@ -34,16 +34,13 @@ public:
void StopEmulation();
bool is_running = false;
void Restart();
void saveLastEbootPath(const std::string& path);
std::string getLastEbootPath() const;
private:
void LoadSystemModules(const std::string& game_serial);
Common::ElfInfo game_info;
#ifdef ENABLE_QT_GUI
QString lastEbootPath;
void saveLastEbootPath(const QString& path);
QString getLastEbootPath();
#endif
std::string lastEbootPath;
bool isRunning = false;
Core::MemoryManager* memory;
Input::GameController* controller;

View File

@ -33,6 +33,7 @@
#ifdef ENABLE_DISCORD_RPC
#include "common/discord_rpc_handler.h"
#endif
#include <SDL3/SDL_messagebox.h>
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<QKeyEvent*>(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

View File

@ -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();

View File

@ -10,6 +10,11 @@
#include <QThread>
#include "common/memory_patcher.h"
#endif
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <thread>
#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 <windows.h>
#else
#include <unistd.h>
#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
}