Skip to content

Commit

Permalink
fix: apply the default game field in the config to some places that w…
Browse files Browse the repository at this point in the history
…eren't using it
  • Loading branch information
craftablescience committed Feb 1, 2025
1 parent d268542 commit 8ccad85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
58 changes: 30 additions & 28 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,11 @@ constexpr std::string_view STR_GAME_OVERRIDE = "str_game_override";

Window::Window(QWidget* parent)
: QMainWindow(parent)
, gameDefault(PROJECT_DEFAULT_MOD.data())
, configUsingLegacyBinDir(false) {
this->setWindowTitle(PROJECT_NAME.data());
this->setMinimumHeight(400);

// Default settings (recent configs are set later on)
QSettings settings;
if (!settings.contains(STR_GAME_OVERRIDE)) {
settings.setValue(STR_GAME_OVERRIDE, QString(PROJECT_DEFAULT_MOD.data()));
}

// Icon
this->setWindowIcon(QIcon{getSDKLauncherIconPath()});

Expand All @@ -111,26 +106,22 @@ Window::Window(QWidget* parent)
// Game menu
auto* gameMenu = this->menuBar()->addMenu(tr("Game"));

this->game_resetToDefault = gameMenu->addAction(tr("Reset to Default"), [this] {
QSettings settings;
settings.setValue(STR_GAME_OVERRIDE, QString(PROJECT_DEFAULT_MOD.data()));
this->game_overrideGame->setText(tr("Override \"%1\" Folder").arg(settings.value(STR_GAME_OVERRIDE).toString()));
this->loadGameConfig(settings.value(STR_RECENT_CONFIGS).toStringList().first());
});

gameMenu->addSeparator();

this->game_overrideGame = gameMenu->addAction(tr("Override \"%1\" Folder").arg(settings.value(STR_GAME_OVERRIDE).toString()), [this] {
this->game_overrideGame = gameMenu->addAction(tr("Override Game Folder"), [this] {
const auto rootPath = ::getRootPath(this->configUsingLegacyBinDir);
if (auto path = QFileDialog::getExistingDirectory(this, tr("Override Game Folder"), rootPath); !path.isEmpty()) {
const QDir rootDir{rootPath};
QSettings settings;
settings.setValue(STR_GAME_OVERRIDE, QDir::cleanPath(rootDir.relativeFilePath(path)));
this->game_overrideGame->setText(tr("Override \"%1\" Folder").arg(settings.value(STR_GAME_OVERRIDE).toString()));
this->loadGameConfig(settings.value(STR_RECENT_CONFIGS).toStringList().first());
}
});

this->game_resetToDefault = gameMenu->addAction(tr("Reset to Default"), [this] {
QSettings settings;
settings.remove(STR_GAME_OVERRIDE);
this->loadGameConfig(settings.value(STR_RECENT_CONFIGS).toStringList().first());
});

// Utilities menu
auto* utilitiesMenu = this->menuBar()->addMenu(tr("Utilities"));

Expand All @@ -139,8 +130,16 @@ Window::Window(QWidget* parent)
});

this->utilities_createNewAddon = utilitiesMenu->addAction(this->style()->standardIcon(QStyle::SP_FileIcon), tr("Create New Addon"), [this] {
QSettings settings;
NewP2CEAddonDialog::open(::getRootPath(this->configUsingLegacyBinDir) + QDir::separator() + settings.value(STR_GAME_OVERRIDE, {PROJECT_DEFAULT_MOD.data()}).toString(), this);
QString gameRoot;
if (QSettings settings; settings.contains(STR_GAME_OVERRIDE)) {
gameRoot = settings.value(STR_GAME_OVERRIDE).toString();
} else {
gameRoot = this->gameDefault;
}
if (!QDir::isAbsolutePath(gameRoot)) {
gameRoot = ::getRootPath(this->configUsingLegacyBinDir) + QDir::separator() + gameRoot;
}
NewP2CEAddonDialog::open(gameRoot, this);
});

// Help menu
Expand Down Expand Up @@ -172,7 +171,7 @@ Window::Window(QWidget* parent)

new QVBoxLayout(this->main);

if (!settings.contains(STR_RECENT_CONFIGS)) {
if (QSettings settings; !settings.contains(STR_RECENT_CONFIGS)) {
settings.setValue(STR_RECENT_CONFIGS, QStringList{});
if (auto defaultConfigPath = QCoreApplication::applicationDirPath() + "/SDKLauncherDefault.json"; QFile::exists(defaultConfigPath)) {
this->loadGameConfig(defaultConfigPath);
Expand Down Expand Up @@ -246,9 +245,18 @@ void Window::loadGameConfig(const QString& path) {
#warning "Unknown platform! ${PLATFORM} will not be substituted!"
#endif

// Set ${STRATA_ICON}
gameConfig->setVariable("STRATA_ICON", getStrataIconPath());

// Set ${SDKLAUNCHER_ICON}
gameConfig->setVariable("SDKLAUNCHER_ICON", getSDKLauncherIconPath());

// Get default game
this->gameDefault = gameConfig->getGameDefault();

// tiny hack: get default game icon before ${GAME} substitution
QString defaultGameIconPath = gameConfig->getGameIcon();
defaultGameIconPath.replace("${GAME}", PROJECT_DEFAULT_MOD.data());
defaultGameIconPath.replace("${GAME}", this->gameDefault);
if (QIcon defaultGameIcon{defaultGameIconPath}; !defaultGameIcon.isNull() && !defaultGameIcon.availableSizes().isEmpty()) {
this->config_loadDefault->setIcon(defaultGameIcon);
this->game_resetToDefault->setIcon(defaultGameIcon);
Expand All @@ -258,7 +266,7 @@ void Window::loadGameConfig(const QString& path) {
}

// Set ${GAME}
QString gameDir = settings.contains(STR_GAME_OVERRIDE) ? settings.value(STR_GAME_OVERRIDE).toString() : gameConfig->getGameDefault();
QString gameDir = settings.contains(STR_GAME_OVERRIDE) ? settings.value(STR_GAME_OVERRIDE).toString() : this->gameDefault;
gameConfig->setVariable("GAME", gameDir);

// Set ${GAME_ICON}
Expand All @@ -270,12 +278,6 @@ void Window::loadGameConfig(const QString& path) {
gameConfig->setVariable("GAME_ICON", "");
}

// Set ${STRATA_ICON}
gameConfig->setVariable("STRATA_ICON", getStrataIconPath());

// Set ${SDKLAUNCHER_ICON}
gameConfig->setVariable("SDKLAUNCHER_ICON", getSDKLauncherIconPath());

for (int i = 0; i < gameConfig->getSections().size(); i++) {
auto& section = gameConfig->getSections()[i];

Expand Down
3 changes: 2 additions & 1 deletion src/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Window : public QMainWindow {
void regenerateRecentConfigs();

private:
QString gameDefault;
bool configUsingLegacyBinDir;
QString configModTemplateURL;

QMenu* recent;
QAction* config_loadDefault;
QAction* game_resetToDefault;
QAction* game_overrideGame;
QAction* game_resetToDefault;
QAction* utilities_createNewMod;
QAction* utilities_createNewAddon;

Expand Down

0 comments on commit 8ccad85

Please sign in to comment.