Use fs::path::native whenever possible, avoid unnecessary fs->string conversions in GUI code (#1064)

* Use filesystem::path whenever possible, remove fs::path::string

* My hatred for Windows grows with every passing day

* More Qt stuff

* custom u8string formatter for fmt library

* Use u8string for imgui

* Fix toml errors hopefully

* Fix not printing issue

* Oh and on SDL

* I hate Windows even more today

* fix toml reading utf-8 paths

also small fix for fmt::UTF

* Formatting

* Fix QT path to run games

* Fix path logging in save data

* Fix trophy path handling

* Update game_list_frame.cpp

fixed snd0path

* Update main_window.cpp

fix snd0path

* Update main_window.cpp

* paths finally fixed

* git info in WIP versions title

---------

Co-authored-by: Vinicius Rangel <me@viniciusrangel.dev>
Co-authored-by: georgemoralis <giorgosmrls@gmail.com>
This commit is contained in:
Paris Oplopoios 2024-09-26 11:41:59 +03:00 committed by GitHub
parent 54e2179337
commit 6295d6c416
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 271 additions and 125 deletions

View file

@ -50,8 +50,9 @@ void CheatsPatches::setupUI() {
defaultTextEdit = tr("defaultTextEdit_MSG");
defaultTextEdit.replace("\\n", "\n");
QString CHEATS_DIR_QString =
QString::fromStdString(Common::FS::GetUserPath(Common::FS::PathType::CheatsDir).string());
QString CHEATS_DIR_QString;
Common::FS::PathToQString(CHEATS_DIR_QString,
Common::FS::GetUserPath(Common::FS::PathType::CheatsDir));
QString NameCheatJson = m_gameSerial + "_" + m_gameVersion + ".json";
m_cheatFilePath = CHEATS_DIR_QString + "/" + NameCheatJson;
@ -275,9 +276,9 @@ void CheatsPatches::onSaveButtonClicked() {
int separatorIndex = selectedPatchName.indexOf(" | ");
selectedPatchName = selectedPatchName.mid(separatorIndex + 3);
QString patchDir =
QString::fromStdString(Common::FS::GetUserPath(Common::FS::PathType::PatchesDir).string()) +
"/" + selectedPatchName;
QString patchDir;
Common::FS::PathToQString(patchDir, Common::FS::GetUserPath(Common::FS::PathType::PatchesDir));
patchDir += "/" + selectedPatchName;
QString filesJsonPath = patchDir + "/files.json";
QFile jsonFile(filesJsonPath);
@ -555,10 +556,10 @@ void CheatsPatches::downloadCheats(const QString& source, const QString& gameSer
if (dotIndex != -1) {
baseFileName.insert(dotIndex, "_wolf2022");
}
QString filePath =
QString::fromStdString(
Common::FS::GetUserPath(Common::FS::PathType::CheatsDir).string()) +
"/" + baseFileName;
QString filePath;
Common::FS::PathToQString(filePath,
Common::FS::GetUserPath(Common::FS::PathType::CheatsDir));
filePath += "/" + baseFileName;
if (QFile::exists(filePath) && showMessageBox) {
QMessageBox::StandardButton reply2;
reply2 =
@ -612,8 +613,9 @@ void CheatsPatches::populateFileListPatches() {
}
m_patchInfos.clear();
QString patchesDir =
QString::fromStdString(Common::FS::GetUserPath(Common::FS::PathType::PatchesDir).string());
QString patchesDir;
Common::FS::PathToQString(patchesDir,
Common::FS::GetUserPath(Common::FS::PathType::PatchesDir));
QDir dir(patchesDir);
QStringList folders = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
@ -906,8 +908,8 @@ void CheatsPatches::addCheatsToLayout(const QJsonArray& modsArray, const QJsonAr
}
void CheatsPatches::populateFileListCheats() {
QString cheatsDir =
QString::fromStdString(Common::FS::GetUserPath(Common::FS::PathType::CheatsDir).string());
QString cheatsDir;
Common::FS::PathToQString(cheatsDir, Common::FS::GetUserPath(Common::FS::PathType::CheatsDir));
QString pattern = m_gameSerial + "_" + m_gameVersion + "*.json";
QDir dir(cheatsDir);
@ -932,8 +934,9 @@ void CheatsPatches::populateFileListCheats() {
if (!selectedIndexes.isEmpty()) {
QString selectedFileName = selectedIndexes.first().data().toString();
QString cheatsDir = QString::fromStdString(
Common::FS::GetUserPath(Common::FS::PathType::CheatsDir).string());
QString cheatsDir;
Common::FS::PathToQString(
cheatsDir, Common::FS::GetUserPath(Common::FS::PathType::CheatsDir));
QFile file(cheatsDir + "/" + selectedFileName);
if (file.open(QIODevice::ReadOnly)) {

View file

@ -44,7 +44,8 @@ void GameGridFrame::PlayBackgroundMusic(QTableWidgetItem* item) {
BackgroundMusicPlayer::getInstance().stopMusic();
return;
}
const auto snd0path = QString::fromStdString(m_game_info->m_games[item->row()].snd0_path);
QString snd0path;
Common::FS::PathToQString(snd0path, m_game_info->m_games[item->row()].snd0_path);
BackgroundMusicPlayer::getInstance().playMusic(snd0path);
}
@ -122,14 +123,12 @@ void GameGridFrame::SetGridBackgroundImage(int row, int column) {
int itemID = (row * this->columnCount()) + column;
QWidget* item = this->cellWidget(row, column);
if (item) {
QString pic1Path = QString::fromStdString((*m_games_shared)[itemID].pic_path);
QString pic1Path;
Common::FS::PathToQString(pic1Path, (*m_games_shared)[itemID].pic_path);
const auto blurredPic1Path = Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) /
(*m_games_shared)[itemID].serial / "pic1.png";
#ifdef _WIN32
const auto blurredPic1PathQt = QString::fromStdWString(blurredPic1Path.wstring());
#else
const auto blurredPic1PathQt = QString::fromStdString(blurredPic1Path.string());
#endif
QString blurredPic1PathQt;
Common::FS::PathToQString(blurredPic1PathQt, blurredPic1Path);
backgroundImage = QImage(blurredPic1PathQt);
if (backgroundImage.isNull()) {

View file

@ -3,13 +3,15 @@
#include <QProgressDialog>
#include "common/path_util.h"
#include "game_info.h"
GameInfoClass::GameInfoClass() = default;
GameInfoClass::~GameInfoClass() = default;
void GameInfoClass::GetGameInfo(QWidget* parent) {
QString installDir = QString::fromStdString(Config::getGameInstallDir());
QString installDir;
Common::FS::PathToQString(installDir, Config::getGameInstallDir());
QStringList filePaths;
QDir parentFolder(installDir);
QFileInfoList fileList = parentFolder.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
@ -19,7 +21,7 @@ void GameInfoClass::GetGameInfo(QWidget* parent) {
}
}
m_games = QtConcurrent::mapped(filePaths, [&](const QString& path) {
return readGameInfo(path.toStdString());
return readGameInfo(Common::FS::PathFromQString(path));
}).results();
// Progress bar, please be patient :)

View file

@ -22,17 +22,19 @@ public:
return a.name < b.name;
}
static GameInfo readGameInfo(const std::string& filePath) {
static GameInfo readGameInfo(const std::filesystem::path& filePath) {
GameInfo game;
game.path = filePath;
PSF psf;
if (psf.Open(std::filesystem::path(game.path) / "sce_sys" / "param.sfo")) {
game.icon_path = game.path + "/sce_sys/icon0.png";
QString iconpath = QString::fromStdString(game.icon_path);
if (psf.Open(game.path / "sce_sys" / "param.sfo")) {
game.icon_path = game.path / "sce_sys" / "icon0.png";
QString iconpath;
Common::FS::PathToQString(iconpath, game.icon_path);
game.icon = QImage(iconpath);
game.pic_path = game.path + "/sce_sys/pic1.png";
game.snd0_path = game.path + "/sce_sys/snd0.at9";
game.pic_path = game.path / "sce_sys" / "pic1.png";
game.snd0_path = game.path / "sce_sys" / "snd0.at9";
if (const auto title = psf.GetString("TITLE"); title.has_value()) {
game.name = *title;
}

View file

@ -41,7 +41,9 @@ QWidget* GameInstallDialog::SetupGamesDirectory() {
// Input.
m_gamesDirectory = new QLineEdit();
m_gamesDirectory->setText(QString::fromStdString(Config::getGameInstallDir()));
QString install_dir;
Common::FS::PathToQString(install_dir, Config::getGameInstallDir());
m_gamesDirectory->setText(install_dir);
m_gamesDirectory->setMinimumWidth(400);
layout->addWidget(m_gamesDirectory);
@ -76,7 +78,7 @@ void GameInstallDialog::Save() {
return;
}
Config::setGameInstallDir(gamesDirectory.toStdString());
Config::setGameInstallDir(Common::FS::PathFromQString(gamesDirectory));
const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
Config::save(config_dir / "config.toml");
accept();

View file

@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/path_util.h"
#include "common/string_util.h"
#include "game_list_frame.h"
GameListFrame::GameListFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidget* parent)
@ -73,7 +74,8 @@ void GameListFrame::PlayBackgroundMusic(QTableWidgetItem* item) {
BackgroundMusicPlayer::getInstance().stopMusic();
return;
}
const auto snd0path = QString::fromStdString(m_game_info->m_games[item->row()].snd0_path);
QString snd0path;
Common::FS::PathToQString(snd0path, m_game_info->m_games[item->row()].snd0_path);
BackgroundMusicPlayer::getInstance().playMusic(snd0path);
}
@ -88,7 +90,9 @@ void GameListFrame::PopulateGameList() {
SetTableItem(i, 4, QString::fromStdString(m_game_info->m_games[i].fw));
SetTableItem(i, 5, QString::fromStdString(m_game_info->m_games[i].size));
SetTableItem(i, 6, QString::fromStdString(m_game_info->m_games[i].version));
SetTableItem(i, 7, QString::fromStdString(m_game_info->m_games[i].path));
QString path;
Common::FS::PathToQString(path, m_game_info->m_games[i].path);
SetTableItem(i, 7, path);
}
}
@ -98,14 +102,12 @@ void GameListFrame::SetListBackgroundImage(QTableWidgetItem* item) {
return;
}
QString pic1Path = QString::fromStdString(m_game_info->m_games[item->row()].pic_path);
QString pic1Path;
Common::FS::PathToQString(pic1Path, m_game_info->m_games[item->row()].pic_path);
const auto blurredPic1Path = Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) /
m_game_info->m_games[item->row()].serial / "pic1.png";
#ifdef _WIN32
const auto blurredPic1PathQt = QString::fromStdWString(blurredPic1Path.wstring());
#else
const auto blurredPic1PathQt = QString::fromStdString(blurredPic1Path.string());
#endif
QString blurredPic1PathQt;
Common::FS::PathToQString(blurredPic1PathQt, blurredPic1Path);
backgroundImage = QImage(blurredPic1PathQt);
if (backgroundImage.isNull()) {

View file

@ -3,11 +3,14 @@
#pragma once
#include "common/path_util.h"
struct GameInfo {
std::string path; // root path of game directory (normally directory that contains eboot.bin)
std::string icon_path; // path of icon0.png
std::string pic_path; // path of pic1.png
std::string snd0_path; // path of snd0.at9
std::filesystem::path path; // root path of game directory
// (normally directory that contains eboot.bin)
std::filesystem::path icon_path; // path of icon0.png
std::filesystem::path pic_path; // path of pic1.png
std::filesystem::path snd0_path; // path of snd0.at9
QImage icon;
std::string size;
// variables extracted from param.sfo
@ -44,7 +47,9 @@ public:
}
static void GetFolderSize(GameInfo& game) {
QDir dir(QString::fromStdString(game.path));
QString dirPath;
Common::FS::PathToQString(dirPath, game.path);
QDir dir(dirPath);
QDirIterator it(dir.absolutePath(), QDirIterator::Subdirectories);
qint64 total = 0;
while (it.hasNext()) {

View file

@ -75,7 +75,8 @@ public:
}
if (selected == &openFolder) {
QString folderPath = QString::fromStdString(m_games[itemID].path);
QString folderPath;
Common::FS::PathToQString(folderPath, m_games[itemID].path);
QDesktopServices::openUrl(QUrl::fromLocalFile(folderPath));
}
@ -158,7 +159,9 @@ public:
QString gameSerial = QString::fromStdString(m_games[itemID].serial);
QString gameVersion = QString::fromStdString(m_games[itemID].version);
QString gameSize = QString::fromStdString(m_games[itemID].size);
QPixmap gameImage(QString::fromStdString(m_games[itemID].icon_path));
QString iconPath;
Common::FS::PathToQString(iconPath, m_games[itemID].icon_path);
QPixmap gameImage(iconPath);
CheatsPatches* cheatsPatches =
new CheatsPatches(gameName, gameSerial, gameVersion, gameSize, gameImage);
cheatsPatches->show();
@ -167,8 +170,9 @@ public:
}
if (selected == &openTrophyViewer) {
QString trophyPath = QString::fromStdString(m_games[itemID].serial);
QString gameTrpPath = QString::fromStdString(m_games[itemID].path);
QString trophyPath, gameTrpPath;
Common::FS::PathToQString(trophyPath, m_games[itemID].serial);
Common::FS::PathToQString(gameTrpPath, m_games[itemID].path);
TrophyViewer* trophyViewer = new TrophyViewer(trophyPath, gameTrpPath);
trophyViewer->show();
connect(widget->parent(), &QWidget::destroyed, trophyViewer,
@ -176,11 +180,13 @@ public:
}
if (selected == &createShortcut) {
QString targetPath = QString::fromStdString(m_games[itemID].path);
QString targetPath;
Common::FS::PathToQString(targetPath, m_games[itemID].path);
QString ebootPath = targetPath + "/eboot.bin";
// Get the full path to the icon
QString iconPath = QString::fromStdString(m_games[itemID].icon_path);
QString iconPath;
Common::FS::PathToQString(iconPath, m_games[itemID].icon_path);
QFileInfo iconFileInfo(iconPath);
QString icoPath = iconFileInfo.absolutePath() + "/" + iconFileInfo.baseName() + ".ico";

View file

@ -8,10 +8,18 @@
#include "game_install_dialog.h"
#include "main_window.h"
#ifdef _WIN32
#include <windows.h>
#endif
// Custom message handler to ignore Qt logs
void customMessageHandler(QtMsgType, const QMessageLogContext&, const QString&) {}
int main(int argc, char* argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
QApplication a(argc, argv);
// Load configurations and initialize Qt application
@ -22,7 +30,7 @@ int main(int argc, char* argv[]) {
bool has_command_line_argument = argc > 1;
// Check if the game install directory is set
if (Config::getGameInstallDir() == "" && !has_command_line_argument) {
if (Config::getGameInstallDir().empty() && !has_command_line_argument) {
GameInstallDialog dlg;
dlg.exec();
}

View file

@ -4,10 +4,12 @@
#include <QDockWidget>
#include <QProgressDialog>
#include <common/scm_rev.h>
#include "about_dialog.h"
#include "cheats_patches.h"
#include "check_update.h"
#include "common/io_file.h"
#include "common/path_util.h"
#include "common/string_util.h"
#include "common/version.h"
#include "core/file_format/pkg.h"
@ -43,7 +45,14 @@ bool MainWindow::Init() {
GetPhysicalDevices();
// show ui
setMinimumSize(350, minimumSizeHint().height());
setWindowTitle(QString::fromStdString("shadPS4 v" + std::string(Common::VERSION)));
std::string window_title = "";
if (Common::isRelease) {
window_title = fmt::format("shadPS4 v{}", Common::VERSION);
} else {
window_title = fmt::format("shadPS4 v{} {} {}", Common::VERSION, Common::g_scm_branch,
Common::g_scm_desc);
}
setWindowTitle(QString::fromStdString(window_title));
this->show();
// load game list
LoadGameLists();
@ -433,12 +442,14 @@ void MainWindow::CreateConnects() {
.arg(" APP VERSION", -11)
.arg(" Path");
for (const GameInfo& game : m_game_info->m_games) {
QString game_path;
Common::FS::PathToQString(game_path, game.path);
out << QString("%1 %2 %3 %4 %5\n")
.arg(QString::fromStdString(game.name), -50)
.arg(QString::fromStdString(game.serial), -10)
.arg(QString::fromStdString(game.fw), -4)
.arg(QString::fromStdString(game.version), -11)
.arg(QString::fromStdString(game.path));
.arg(game_path);
}
});
@ -517,7 +528,8 @@ void MainWindow::PlayBackgroundMusic() {
: m_game_grid_frame->crtRow * m_game_grid_frame->columnCnt +
m_game_grid_frame->crtColumn;
const auto snd0path = QString::fromStdString(m_game_info->m_games[itemID].snd0_path);
QString snd0path;
Common::FS::PathToQString(snd0path, m_game_info->m_games[itemID].snd0_path);
BackgroundMusicPlayer::getInstance().playMusic(snd0path);
}
@ -529,28 +541,29 @@ void MainWindow::StartGame() {
if (table_mode == 0) {
if (m_game_list_frame->currentItem()) {
int itemID = m_game_list_frame->currentItem()->row();
gamePath = QString::fromStdString(m_game_info->m_games[itemID].path + "/eboot.bin");
Common::FS::PathToQString(gamePath, m_game_info->m_games[itemID].path / "eboot.bin");
}
} else if (table_mode == 1) {
if (m_game_grid_frame->cellClicked) {
int itemID = (m_game_grid_frame->crtRow * m_game_grid_frame->columnCnt) +
m_game_grid_frame->crtColumn;
gamePath = QString::fromStdString(m_game_info->m_games[itemID].path + "/eboot.bin");
Common::FS::PathToQString(gamePath, m_game_info->m_games[itemID].path / "eboot.bin");
}
} else {
if (m_elf_viewer->currentItem()) {
int itemID = m_elf_viewer->currentItem()->row();
gamePath = QString::fromStdString(m_elf_viewer->m_elf_list[itemID].toStdString());
gamePath = m_elf_viewer->m_elf_list[itemID];
}
}
if (gamePath != "") {
AddRecentFiles(gamePath);
Core::Emulator emulator;
if (!std::filesystem::exists(gamePath.toUtf8().constData())) {
const auto path = Common::FS::PathFromQString(gamePath);
if (!std::filesystem::exists(path)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return;
}
emulator.Run(gamePath.toUtf8().constData());
emulator.Run(path);
}
}
@ -666,9 +679,11 @@ void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int
pkg = PKG();
pkg.Open(file);
std::string failreason;
auto extract_path = std::filesystem::path(Config::getGameInstallDir()) / pkg.GetTitleID();
auto extract_path = Config::getGameInstallDir() / pkg.GetTitleID();
QString pkgType = QString::fromStdString(pkg.GetPkgFlags());
QDir game_dir(QString::fromStdString(extract_path.string()));
QString gameDirPath;
Common::FS::PathToQString(gameDirPath, extract_path);
QDir game_dir(gameDirPath);
if (game_dir.exists()) {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("PKG Extraction"));
@ -690,7 +705,9 @@ void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int
auto addon_extract_path = Common::FS::GetUserPath(Common::FS::PathType::AddonsDir) /
pkg.GetTitleID() / entitlement_label;
QDir addon_dir(QString::fromStdString(addon_extract_path.string()));
QString addonDirPath;
Common::FS::PathToQString(addonDirPath, addon_extract_path);
QDir addon_dir(addonDirPath);
auto category = psf.GetString("CATEGORY");
if (pkgType.contains("PATCH")) {
@ -755,8 +772,7 @@ void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int
return;
}
} else {
msgBox.setText(QString(tr("DLC already installed:") + "\n" +
QString::fromStdString(addon_extract_path.string()) +
msgBox.setText(QString(tr("DLC already installed:") + "\n" + addonDirPath +
"\n\n" + tr("Would you like to overwrite?")));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
@ -768,8 +784,7 @@ void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int
}
}
} else {
msgBox.setText(QString(tr("Game already installed") + "\n" +
QString::fromStdString(extract_path.string()) + "\n" +
msgBox.setText(QString(tr("Game already installed") + "\n" + addonDirPath + "\n" +
tr("Would you like to overwrite?")));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
@ -812,7 +827,8 @@ void MainWindow::InstallDragDropPkg(std::filesystem::path file, int pkgNum, int
QFutureWatcher<void> futureWatcher;
connect(&futureWatcher, &QFutureWatcher<void>::finished, this, [=, this]() {
if (pkgNum == nPkg) {
QString path = QString::fromStdString(Config::getGameInstallDir());
QString path;
Common::FS::PathToQString(path, Config::getGameInstallDir());
QMessageBox extractMsgBox(this);
extractMsgBox.setWindowTitle(tr("Extraction Finished"));
extractMsgBox.setText(
@ -984,14 +1000,14 @@ void MainWindow::CreateRecentGameActions() {
}
connect(m_recent_files_group, &QActionGroup::triggered, this, [this](QAction* action) {
QString gamePath = action->text();
AddRecentFiles(gamePath); // Update the list.
auto gamePath = Common::FS::PathFromQString(action->text());
AddRecentFiles(action->text()); // Update the list.
Core::Emulator emulator;
if (!std::filesystem::exists(gamePath.toUtf8().constData())) {
if (!std::filesystem::exists(gamePath)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return;
}
emulator.Run(gamePath.toUtf8().constData());
emulator.Run(gamePath);
});
}
@ -1025,4 +1041,4 @@ void MainWindow::OnLanguageChanged(const std::string& locale) {
Config::setEmulatorLanguage(locale);
LoadTranslation();
}
}

View file

@ -21,15 +21,10 @@ TrophyViewer::TrophyViewer(QString trophyPath, QString gameTrpPath) : QMainWindo
}
void TrophyViewer::PopulateTrophyWidget(QString title) {
#ifdef _WIN32
const auto trophyDir = Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) /
title.toStdWString() / "TrophyFiles";
const auto trophyDirQt = QString::fromStdWString(trophyDir.wstring());
#else
const auto trophyDir = Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) /
title.toStdString() / "TrophyFiles";
const auto trophyDirQt = QString::fromStdString(trophyDir.string());
#endif
Common::FS::PathFromQString(title) / "TrophyFiles";
QString trophyDirQt;
Common::FS::PathToQString(trophyDirQt, trophyDir);
QDir dir(trophyDirQt);
if (!dir.exists()) {