Skip to content

Commit

Permalink
Game Settings: Add an option to create game config using default sett…
Browse files Browse the repository at this point in the history
…ings

Fix a potential crash on creating global config.
  • Loading branch information
elad335 committed Dec 20, 2023
1 parent dbd1f27 commit f550d3d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
42 changes: 26 additions & 16 deletions rpcs3/rpcs3qt/emu_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool emu_settings::Init()
return true;
}

void emu_settings::LoadSettings(const std::string& title_id)
void emu_settings::LoadSettings(const std::string& title_id, bool create_config_from_global)
{
m_title_id = title_id;

Expand All @@ -113,21 +113,25 @@ void emu_settings::LoadSettings(const std::string& title_id)
.arg(QString::fromStdString(default_error)), QMessageBox::Ok);
}

// Add global config
const std::string global_config_path = fs::get_config_dir() + "config.yml";
fs::file config(global_config_path, fs::read + fs::write + fs::create);
auto [global_config, global_error] = yaml_load(config.to_string());
config.close();

if (global_error.empty())
{
m_current_settings += global_config;
}
else
if (create_config_from_global)
{
cfg_log.fatal("Failed to load global config %s:\n%s", global_config_path, global_error);
QMessageBox::critical(nullptr, tr("Config Error"), tr("Failed to load global config:\nFile: %0\nError: %1")
.arg(QString::fromStdString(global_config_path)).arg(QString::fromStdString(global_error)), QMessageBox::Ok);
// Add global config
const std::string global_config_path = fs::get_config_dir() + "config.yml";
fs::g_tls_error = fs::error::ok;
fs::file config(global_config_path, fs::read + fs::create);
auto [global_config, global_error] = yaml_load(config ? config.to_string() : "");

if (config && global_error.empty())
{
m_current_settings += global_config;
}
else
{
config.close();
cfg_log.fatal("Failed to load global config %s:\n%s (%s)", global_config_path, global_error, fs::g_tls_error);
QMessageBox::critical(nullptr, tr("Config Error"), tr("Failed to load global config:\nFile: %0\nError: %1")
.arg(QString::fromStdString(global_config_path)).arg(QString::fromStdString(global_error)), QMessageBox::Ok);
}
}

// Add game config
Expand All @@ -146,7 +150,7 @@ void emu_settings::LoadSettings(const std::string& title_id)

if (!custom_config_path.empty())
{
if ((config = fs::file(custom_config_path, fs::read + fs::write)))
if (fs::file config{custom_config_path})
{
auto [custom_config, custom_error] = yaml_load(config.to_string());
config.close();
Expand All @@ -162,6 +166,12 @@ void emu_settings::LoadSettings(const std::string& title_id)
.arg(QString::fromStdString(custom_config_path)).arg(QString::fromStdString(custom_error)), QMessageBox::Ok);
}
}
else if (fs::g_tls_error != fs::error::noent)
{
cfg_log.fatal("Failed to load custom config %s (file error: %s)", custom_config_path, fs::g_tls_error);
QMessageBox::critical(nullptr, tr("Config Error"), tr("Failed to load custom config:\nFile: %0\nError: %1")
.arg(QString::fromStdString(custom_config_path)).arg(QString::fromStdString(fmt::format("%s", fs::g_tls_error))), QMessageBox::Ok);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/emu_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class emu_settings : public QObject
midi_creator m_midi_creator;

/** Loads the settings from path.*/
void LoadSettings(const std::string& title_id = "");
void LoadSettings(const std::string& title_id = "", bool create_config_from_global = true);

/** Fixes all registered invalid settings after asking the user for permission.*/
void OpenCorrectionDialog(QWidget* parent = Q_NULLPTR);
Expand Down
24 changes: 20 additions & 4 deletions rpcs3/rpcs3qt/game_list_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,9 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)

QAction* configure = menu.addAction(gameinfo->hasCustomConfig
? tr("&Change Custom Configuration")
: tr("&Create Custom Configuration"));
: tr("&Create Custom Configuration From Global Settings"));
QAction* create_game_default_config = gameinfo->hasCustomConfig ? nullptr
: menu.addAction(tr("&Create Custom Configuration From Default Settings"));
QAction* pad_configure = menu.addAction(gameinfo->hasCustomPadConfig
? tr("&Change Custom Gamepad Configuration")
: tr("&Create Custom Gamepad Configuration"));
Expand Down Expand Up @@ -1422,9 +1424,11 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
sys_log.notice("Booting from gamelist per context menu...");
Q_EMIT RequestBoot(gameinfo, cfg_mode::global);
});
connect(configure, &QAction::triggered, this, [this, current_game, gameinfo]()

auto configure_l = [this, current_game, gameinfo](bool create_cfg_from_global_cfg)
{
settings_dialog dlg(m_gui_settings, m_emu_settings, 0, this, &current_game);
settings_dialog dlg(m_gui_settings, m_emu_settings, 0, this, &current_game, create_cfg_from_global_cfg);

connect(&dlg, &settings_dialog::EmuSettingsApplied, [this, gameinfo]()
{
if (!gameinfo->hasCustomConfig)
Expand All @@ -1434,8 +1438,20 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
}
Q_EMIT NotifyEmuSettingsChange();
});

dlg.exec();
});
};

if (create_game_default_config)
{
connect(configure, &QAction::triggered, this, [configure_l]() { configure_l(true); });
connect(create_game_default_config, &QAction::triggered, this, [configure_l = std::move(configure_l)]() { configure_l(false); });
}
else
{
connect(configure, &QAction::triggered, this, [configure_l = std::move(configure_l)]() { configure_l(true); });
}

connect(pad_configure, &QAction::triggered, this, [this, current_game, gameinfo]()
{
pad_settings_dialog dlg(m_gui_settings, this, &current_game);
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/rpcs3qt/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void remove_item(QComboBox* box, int data_value, int def_value)

extern const std::map<std::string_view, int> g_prx_list;

settings_dialog::settings_dialog(std::shared_ptr<gui_settings> gui_settings, std::shared_ptr<emu_settings> emu_settings, const int& tab_index, QWidget* parent, const GameInfo* game)
settings_dialog::settings_dialog(std::shared_ptr<gui_settings> gui_settings, std::shared_ptr<emu_settings> emu_settings, const int& tab_index, QWidget* parent, const GameInfo* game, bool create_cfg_from_global_cfg)
: QDialog(parent)
, m_tab_index(tab_index)
, ui(new Ui::settings_dialog)
Expand Down Expand Up @@ -138,7 +138,7 @@ settings_dialog::settings_dialog(std::shared_ptr<gui_settings> gui_settings, std

if (game)
{
m_emu_settings->LoadSettings(game->serial);
m_emu_settings->LoadSettings(game->serial, create_cfg_from_global_cfg);
setWindowTitle(tr("Settings: [%0] %1", "Settings dialog").arg(qstr(game->serial)).arg(qstr(game->name)));
}
else
Expand Down
15 changes: 9 additions & 6 deletions rpcs3/rpcs3qt/settings_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class settings_dialog : public QDialog
Q_OBJECT

public:
explicit settings_dialog(std::shared_ptr<gui_settings> gui_settings, std::shared_ptr<emu_settings> emu_settings, const int& tab_index = 0, QWidget* parent = nullptr, const GameInfo* game = nullptr);
explicit settings_dialog(std::shared_ptr<gui_settings> gui_settings, std::shared_ptr<emu_settings> emu_settings, const int& tab_index = 0, QWidget* parent = nullptr, const GameInfo* game = nullptr, bool create_cfg_from_global_cfg = true);
~settings_dialog();
int exec() override;
Q_SIGNALS:
Expand All @@ -41,21 +41,24 @@ class settings_dialog : public QDialog
void AddStylesheets();
void ApplyStylesheet(bool reset);
QString m_current_stylesheet;

// Gpu tab
QString m_old_renderer;

// Audio tab
std::array<QComboBox*, 4> m_mics_combo;
std::array<QComboBox*, 4> m_mics_combo{};

// IO tab
std::array<QComboBox*, 3> m_midi_type_combo;
std::array<QComboBox*, 3> m_midi_device_combo;
std::array<QComboBox*, 3> m_midi_type_combo{};
std::array<QComboBox*, 3> m_midi_device_combo{};

int m_tab_index;
int m_tab_index = 0;
std::unique_ptr<Ui::settings_dialog> ui;
std::shared_ptr<gui_settings> m_gui_settings;
std::shared_ptr<emu_settings> m_emu_settings;

// Discord
bool m_use_discord;
bool m_use_discord = false;
QString m_discord_state;

// Descriptions
Expand Down

0 comments on commit f550d3d

Please sign in to comment.