Skip to content

Commit 2498feb

Browse files
committed
separate common from main window, add namespaces for common/config
1 parent 4bd9bd3 commit 2498feb

17 files changed

+349
-309
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ set(PROJECT_SOURCES
5656
main.cpp
5757
modules/bblauncher.cpp
5858
modules/bblauncher.h
59-
modules/bblauncher.ui
59+
modules/bblauncher.ui
60+
modules/Common.cpp
61+
modules/Common.h
6062
modules/ModManager.h
6163
modules/ModManager.cpp
6264
modules/ModManager.ui

modules/Common.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: Copyright 2024 BBLauncher Project
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#include "Common.h"
5+
6+
namespace Common {
7+
8+
std::string game_serial;
9+
std::filesystem::path installPath;
10+
std::filesystem::path SaveDir;
11+
std::filesystem::path shadPs4Executable;
12+
char VERSION[] = "Release4.5";
13+
14+
std::filesystem::path GetShadUserDir() {
15+
auto user_dir = std::filesystem::current_path() / "user";
16+
if (!std::filesystem::exists(user_dir)) {
17+
#ifdef __APPLE__
18+
user_dir =
19+
std::filesystem::path(getenv("HOME")) / "Library" / "Application Support" / "shadPS4";
20+
#elif defined(__linux__)
21+
const char* xdg_data_home = getenv("XDG_DATA_HOME");
22+
if (xdg_data_home != nullptr && strlen(xdg_data_home) > 0) {
23+
user_dir = std::filesystem::path(xdg_data_home) / "shadPS4";
24+
} else {
25+
user_dir = std::filesystem::path(getenv("HOME")) / ".local" / "share" / "shadPS4";
26+
}
27+
#endif
28+
}
29+
return user_dir;
30+
}
31+
32+
void PathToQString(QString& result, const std::filesystem::path& path) {
33+
#ifdef _WIN32
34+
result = QString::fromStdWString(path.wstring());
35+
#else
36+
result = QString::fromStdString(path.string());
37+
#endif
38+
}
39+
40+
std::filesystem::path PathFromQString(const QString& path) {
41+
#ifdef _WIN32
42+
return std::filesystem::path(path.toStdWString());
43+
#else
44+
return std::filesystem::path(path.toStdString());
45+
#endif
46+
}
47+
48+
std::string PathToU8(const std::filesystem::path& path) {
49+
const auto u8_string = path.u8string();
50+
return std::string{u8_string.begin(), u8_string.end()};
51+
}
52+
53+
} // namespace Common

modules/Common.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: Copyright 2024 BBLauncher Project
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#pragma once
5+
6+
#include <filesystem>
7+
#include <QString>
8+
9+
namespace Common {
10+
11+
void PathToQString(QString& result, const std::filesystem::path& path);
12+
std::filesystem::path PathFromQString(const QString& path);
13+
std::string PathToU8(const std::filesystem::path& path);
14+
std::filesystem::path GetShadUserDir();
15+
16+
extern std::string game_serial;
17+
extern std::filesystem::path installPath;
18+
extern std::filesystem::path SaveDir;
19+
extern std::filesystem::path shadPs4Executable;
20+
extern char VERSION[];
21+
22+
const std::filesystem::path BBLFilesPath = std::filesystem::current_path() / "BBLauncher";
23+
const std::filesystem::path ModPath = BBLFilesPath / "Mods";
24+
25+
} // namespace Common

modules/ModManager.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <QMessageBox>
66
#include <QProgressBar>
77
#include "ModManager.h"
8-
#include "bblauncher.h"
98
#include "modules/ui_ModManager.h"
109

1110
std::filesystem::path ModInstallPath;
@@ -23,8 +22,8 @@ ModManager::ModManager(QWidget* parent) : QDialog(parent), ui(new Ui::ModManager
2322
ui->RecModsLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
2423
ui->RecModsLabel->setOpenExternalLinks(true);
2524

26-
if (!std::filesystem::exists(ModPath)) {
27-
std::filesystem::create_directories(ModPath);
25+
if (!std::filesystem::exists(Common::ModPath)) {
26+
std::filesystem::create_directories(Common::ModPath);
2827
}
2928

3029
if (!std::filesystem::exists(ModBackupPath)) {
@@ -42,10 +41,11 @@ ModManager::ModManager(QWidget* parent) : QDialog(parent), ui(new Ui::ModManager
4241
&ModManager::DeactivateButton_isPressed);
4342
connect(this, &ModManager::progressChanged, ui->progressBar, &QProgressBar::setValue);
4443

45-
if (std::filesystem::exists(installPath.parent_path() / (game_serial + "-UPDATE"))) {
46-
ModInstallPath = installPath.parent_path() / (game_serial + "-UPDATE");
44+
if (std::filesystem::exists(Common::installPath.parent_path() /
45+
(Common::game_serial + "-UPDATE"))) {
46+
ModInstallPath = Common::installPath.parent_path() / (Common::game_serial + "-UPDATE");
4747
} else {
48-
ModInstallPath = installPath;
48+
ModInstallPath = Common::installPath;
4949
}
5050
}
5151

@@ -59,7 +59,7 @@ void ModManager::ActivateButton_isPressed() {
5959

6060
bool hasconflict = false;
6161
const std::string ModName = ui->InactiveModList->currentItem()->text().toStdString();
62-
const std::string ModFolderString = (ModPath / ModName).string();
62+
const std::string ModFolderString = (Common::ModPath / ModName).string();
6363
const std::filesystem::path ModFolderPath = std::filesystem::u8path(ModFolderString);
6464
const std::string ModBackupString = (ModBackupPath / ModName).string();
6565
const std::filesystem::path ModBackupFolderPath = std::filesystem::u8path(ModBackupString);
@@ -82,7 +82,7 @@ void ModManager::ActivateButton_isPressed() {
8282
for (const auto& entry : std::filesystem::directory_iterator(ModSourcePath)) {
8383
if (entry.is_directory()) {
8484
auto relative_path = std::filesystem::relative(entry, ModSourcePath);
85-
std::string relative_path_string = PathToU8(relative_path);
85+
std::string relative_path_string = Common::PathToU8(relative_path);
8686
if (std::find(BBFolders.begin(), BBFolders.end(), relative_path_string) ==
8787
BBFolders.end()) {
8888
QMessageBox::warning(this, "Invalid Mod",
@@ -94,7 +94,7 @@ void ModManager::ActivateButton_isPressed() {
9494
}
9595
}
9696

97-
std::ifstream ModInfoFile(ModPath / "ModifiedFiles.txt", std::ios::binary);
97+
std::ifstream ModInfoFile(Common::ModPath / "ModifiedFiles.txt", std::ios::binary);
9898
while (std::getline(ModInfoFile, line)) {
9999
lineCount++;
100100
FileList.push_back(line);
@@ -104,7 +104,7 @@ void ModManager::ActivateButton_isPressed() {
104104
for (const auto& entry : std::filesystem::recursive_directory_iterator(ModSourcePath)) {
105105
if (!entry.is_directory()) {
106106
auto relative_path = std::filesystem::relative(entry, ModSourcePath);
107-
std::string relative_path_string = PathToU8(relative_path);
107+
std::string relative_path_string = Common::PathToU8(relative_path);
108108
for (int i = 0; i < FileList.size(); i++) {
109109
if (FileList[i].contains(relative_path_string)) {
110110
hasconflict = true;
@@ -130,13 +130,13 @@ void ModManager::ActivateButton_isPressed() {
130130
for (const auto& entry : std::filesystem::recursive_directory_iterator(ModSourcePath)) {
131131
if (!entry.is_directory()) {
132132
auto relative_path = std::filesystem::relative(entry, ModSourcePath);
133-
const auto u8_string = PathToU8(relative_path) + ", " + ModName;
133+
const auto u8_string = Common::PathToU8(relative_path) + ", " + ModName;
134134
std::string relative_path_string{u8_string.begin(), u8_string.end()};
135135
FileList.push_back(relative_path_string);
136136
}
137137
}
138138

139-
std::ofstream ModInfoFileSave(ModPath / "ModifiedFiles.txt", std::ios::binary);
139+
std::ofstream ModInfoFileSave(Common::ModPath / "ModifiedFiles.txt", std::ios::binary);
140140
for (const auto& i : FileList)
141141
ModInfoFileSave << i << "\n";
142142
ModInfoFileSave.close();
@@ -200,7 +200,7 @@ void ModManager::ActivateButton_isPressed() {
200200

201201
ui->FileTransferLabel->setText("Modified File List is being written");
202202
ui->progressBar->setValue(0);
203-
std::ifstream ActiveFile(ModPath / "ActiveMods.txt", std::ios::binary);
203+
std::ifstream ActiveFile(Common::ModPath / "ActiveMods.txt", std::ios::binary);
204204
lineCount = 0;
205205
while (std::getline(ActiveFile, line)) {
206206
lineCount++;
@@ -211,7 +211,7 @@ void ModManager::ActivateButton_isPressed() {
211211
ActiveFile.close();
212212
ActiveModList.push_back(ModName);
213213

214-
std::ofstream ActiveFileSave(ModPath / "ActiveMods.txt", std::ios::binary);
214+
std::ofstream ActiveFileSave(Common::ModPath / "ActiveMods.txt", std::ios::binary);
215215
for (const auto& l : ActiveModList)
216216
ActiveFileSave << l << "\n";
217217
ActiveFileSave.close();
@@ -255,7 +255,7 @@ void ModManager::DeactivateButton_isPressed() {
255255
return;
256256
}
257257

258-
std::ifstream ConflictFile(ModPath / "ConflictMods.txt", std::ios::binary);
258+
std::ifstream ConflictFile(Common::ModPath / "ConflictMods.txt", std::ios::binary);
259259
while (std::getline(ConflictFile, line)) {
260260
lineCount++;
261261
ConflictMods.push_back(line);
@@ -374,7 +374,7 @@ void ModManager::RefreshLists() {
374374
std::vector<std::string> ActiveModList;
375375
std::string line;
376376
int lineCount = 0;
377-
std::ifstream ActiveFile(ModPath / "ActiveMods.txt", std::ios::binary);
377+
std::ifstream ActiveFile(Common::ModPath / "ActiveMods.txt", std::ios::binary);
378378

379379
ui->ActiveModList->clear();
380380
ui->InactiveModList->clear();
@@ -391,9 +391,9 @@ void ModManager::RefreshLists() {
391391
ui->ActiveModList->addItems(ActiveModStringList);
392392

393393
std::vector<std::string> InactiveModFolders;
394-
for (auto& FolderEntry : std::filesystem::directory_iterator(ModPath)) {
394+
for (auto& FolderEntry : std::filesystem::directory_iterator(Common::ModPath)) {
395395
if (FolderEntry.is_directory()) {
396-
std::string Foldername = PathToU8(FolderEntry.path().filename());
396+
std::string Foldername = Common::PathToU8(FolderEntry.path().filename());
397397
if (std::find(ActiveModList.begin(), ActiveModList.end(), Foldername) ==
398398
ActiveModList.end()) {
399399
InactiveModFolders.push_back(Foldername);
@@ -424,7 +424,7 @@ void ModManager::ConflictAdd(std::string ModName) {
424424
int lineCount = 0;
425425
std::vector<std::string> ConflictMods;
426426

427-
std::ifstream ConflictFile(ModPath / "ConflictMods.txt", std::ios::binary);
427+
std::ifstream ConflictFile(Common::ModPath / "ConflictMods.txt", std::ios::binary);
428428
while (std::getline(ConflictFile, line)) {
429429
lineCount++;
430430
ConflictMods.push_back(line);
@@ -433,7 +433,7 @@ void ModManager::ConflictAdd(std::string ModName) {
433433

434434
ConflictMods.push_back(ModName);
435435

436-
std::ofstream ConflictFileSave(ModPath / "ConflictMods.txt", std::ios::binary);
436+
std::ofstream ConflictFileSave(Common::ModPath / "ConflictMods.txt", std::ios::binary);
437437
for (const auto& i : ConflictMods)
438438
ConflictFileSave << i << "\n";
439439
ConflictFileSave.close();
@@ -444,7 +444,7 @@ void ModManager::ConflictRemove(std::string ModName) {
444444
int lineCount = 0;
445445
std::vector<std::string> ConflictMods;
446446

447-
std::ifstream ConflictFile(ModPath / "ConflictMods.txt", std::ios::binary);
447+
std::ifstream ConflictFile(Common::ModPath / "ConflictMods.txt", std::ios::binary);
448448
while (std::getline(ConflictFile, line)) {
449449
lineCount++;
450450
ConflictMods.push_back(line);
@@ -454,7 +454,7 @@ void ModManager::ConflictRemove(std::string ModName) {
454454
auto itr = std::find(ConflictMods.begin(), ConflictMods.end(), ModName);
455455
ConflictMods.erase(itr);
456456

457-
std::ofstream ConflictFileSave(ModPath / "ConflictMods.txt", std::ios::binary);
457+
std::ofstream ConflictFileSave(Common::ModPath / "ConflictMods.txt", std::ios::binary);
458458
for (const auto& i : ConflictMods)
459459
ConflictFileSave << i << "\n";
460460
ConflictFileSave.close();
@@ -466,7 +466,7 @@ void ModManager::ActiveModRemove(std::string ModName) {
466466
std::string line;
467467
int lineCount = 0;
468468

469-
std::ifstream ActiveFile(ModPath / "ActiveMods.txt", std::ios::binary);
469+
std::ifstream ActiveFile(Common::ModPath / "ActiveMods.txt", std::ios::binary);
470470
while (std::getline(ActiveFile, line)) {
471471
lineCount++;
472472
ActiveModList.push_back(line);
@@ -477,16 +477,16 @@ void ModManager::ActiveModRemove(std::string ModName) {
477477
if (itr != ActiveModList.end())
478478
ActiveModList.erase(itr);
479479

480-
std::ofstream ActiveFileSave(ModPath / "ActiveMods.txt", std::ios::binary);
480+
std::ofstream ActiveFileSave(Common::ModPath / "ActiveMods.txt", std::ios::binary);
481481
for (const auto& l : ActiveModList)
482482
ActiveFileSave << l << "\n";
483483
ActiveFileSave.close();
484484

485-
std::filesystem::remove(ModPath / "ModifiedFiles.txt");
485+
std::filesystem::remove(Common::ModPath / "ModifiedFiles.txt");
486486

487487
for (auto& FolderEntry : std::filesystem::directory_iterator(ModBackupPath)) {
488488
if (FolderEntry.is_directory()) {
489-
std::string Foldername = PathToU8(FolderEntry.path().filename());
489+
std::string Foldername = Common::PathToU8(FolderEntry.path().filename());
490490
const std::string BackupModPathString = (ModBackupPath / Foldername).string();
491491
const std::filesystem::path BackupModPath =
492492
std::filesystem::u8path(BackupModPathString);
@@ -499,7 +499,7 @@ void ModManager::ActiveModRemove(std::string ModName) {
499499
std::filesystem::recursive_directory_iterator(BackupModPath)) {
500500
if (!entry.is_directory()) {
501501
auto relative_path = std::filesystem::relative(entry, BackupModPath);
502-
const auto u8_string = PathToU8(relative_path) + ", " + Foldername;
502+
const auto u8_string = Common::PathToU8(relative_path) + ", " + Foldername;
503503
std::string relative_path_string{u8_string.begin(), u8_string.end()};
504504
FileList.push_back(relative_path_string);
505505
}
@@ -513,7 +513,7 @@ void ModManager::ActiveModRemove(std::string ModName) {
513513
ui->progressBar->setValue(0);
514514
for (auto& FolderEntry : std::filesystem::directory_iterator(ModUniquePath)) {
515515
if (FolderEntry.is_directory()) {
516-
std::string Foldername = PathToU8(FolderEntry.path().filename());
516+
std::string Foldername = Common::PathToU8(FolderEntry.path().filename());
517517
const std::string UniqueModPathString = (ModUniquePath / Foldername).string();
518518
const std::filesystem::path UniqueModPath =
519519
std::filesystem::u8path(UniqueModPathString);
@@ -524,7 +524,7 @@ void ModManager::ActiveModRemove(std::string ModName) {
524524
for (const auto& entry : std::filesystem::recursive_directory_iterator(UniqueModPath)) {
525525
if (!entry.is_directory()) {
526526
auto relative_path = std::filesystem::relative(entry, UniqueModPath);
527-
const auto u8_string = PathToU8(relative_path) + ", " + Foldername;
527+
const auto u8_string = Common::PathToU8(relative_path) + ", " + Foldername;
528528
std::string relative_path_string{u8_string.begin(), u8_string.end()};
529529
FileList.push_back(relative_path_string);
530530
}
@@ -535,7 +535,7 @@ void ModManager::ActiveModRemove(std::string ModName) {
535535
}
536536

537537
ui->progressBar->setValue(0);
538-
std::ofstream ModInfoFileSave(ModPath / "ModifiedFiles.txt", std::ios::binary);
538+
std::ofstream ModInfoFileSave(Common::ModPath / "ModifiedFiles.txt", std::ios::binary);
539539
for (const auto& i : FileList)
540540
ModInfoFileSave << i << "\n";
541541
ModInfoFileSave.close();

modules/ModManager.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
#include <filesystem>
55
#include <QDialog>
6+
#include "Common.h"
67

78
namespace Ui {
89
class ModManager;
910
}
1011

11-
const std::filesystem::path ModPath = std::filesystem::current_path() / "BBLauncher" / "Mods";
12-
1312
class ModManager : public QDialog {
1413
Q_OBJECT
1514

@@ -33,10 +32,8 @@ private slots:
3332
void ConflictAdd(std::string ModName);
3433
void ConflictRemove(std::string ModName);
3534

36-
const std::filesystem::path ModBackupPath =
37-
std::filesystem::current_path() / "BBLauncher" / "Mods-BACKUP";
38-
const std::filesystem::path ModUniquePath =
39-
std::filesystem::current_path() / "BBLauncher" / "Mods-BACKUP" / "Mods-UNIQUEFILES";
35+
const std::filesystem::path ModBackupPath = Common::BBLFilesPath / "Mods-BACKUP";
36+
const std::filesystem::path ModUniquePath = ModBackupPath / "Mods-UNIQUEFILES";
4037
const std::vector<std::string> BBFolders = {
4138
"dvdroot_ps4", "action", "chr", "event", "facegen", "map", "menu",
4239
"movie", "msg", "mtd", "obj", "other", "param", "paramdef",

modules/SaveManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
#include <QMessageBox>
66

77
#include "SaveManager.h"
8-
#include "modules/bblauncher.h"
98
#include "modules/ui_SaveManager.h"
109

1110
SaveManager::SaveManager(QWidget* parent) : QDialog(parent), ui(new Ui::SaveManager) {
1211
ui->setupUi(this);
1312
ui->SelectSaveComboBox->addItem("Current Save");
14-
ExactSaveDir = SaveDir / "1" / game_serial / "SPRJ0005";
13+
ExactSaveDir = Common::SaveDir / "1" / Common::game_serial / "SPRJ0005";
1514

1615
if (!std::filesystem::exists(BackupsDir)) {
1716
std::filesystem::create_directories(BackupsDir);
@@ -20,7 +19,7 @@ SaveManager::SaveManager(QWidget* parent) : QDialog(parent), ui(new Ui::SaveMana
2019
for (auto& FolderEntry : std::filesystem::directory_iterator(BackupsDir)) {
2120
if (FolderEntry.is_directory()) {
2221
if (!std::filesystem::is_empty(FolderEntry)) {
23-
std::string Foldername = PathToU8(FolderEntry.path().filename());
22+
std::string Foldername = Common::PathToU8(FolderEntry.path().filename());
2423
ui->SelectSaveComboBox->addItem(QString::fromStdString(Foldername));
2524
}
2625
}

modules/SaveManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

44
#include <QDialog>
5-
#include "modules/bblauncher.h"
5+
#include "Common.h"
66

77
namespace Ui {
88
class SaveManager;

0 commit comments

Comments
 (0)