Game Settings: Add an option to create game config using default settings

Fix a potential crash on creating global config.
This commit is contained in:
Eladash 2023-12-19 21:04:55 +02:00 committed by Elad Ashkenazi
parent dbd1f27862
commit 33d46172a6
5 changed files with 60 additions and 31 deletions

View File

@ -91,7 +91,7 @@ bool emu_settings::Init()
return true; 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; m_title_id = title_id;
@ -113,21 +113,25 @@ void emu_settings::LoadSettings(const std::string& title_id)
.arg(QString::fromStdString(default_error)), QMessageBox::Ok); .arg(QString::fromStdString(default_error)), QMessageBox::Ok);
} }
// Add global config if (create_config_from_global)
const std::string global_config_path = fs::get_config_dir() + "config.yml"; {
fs::file config(global_config_path, fs::read + fs::write + fs::create); // Add global config
auto [global_config, global_error] = yaml_load(config.to_string()); const std::string global_config_path = fs::get_config_dir() + "config.yml";
config.close(); 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 (global_error.empty()) if (config && global_error.empty())
{ {
m_current_settings += global_config; m_current_settings += global_config;
} }
else else
{ {
cfg_log.fatal("Failed to load global config %s:\n%s", global_config_path, global_error); config.close();
QMessageBox::critical(nullptr, tr("Config Error"), tr("Failed to load global config:\nFile: %0\nError: %1") cfg_log.fatal("Failed to load global config %s:\n%s (%s)", global_config_path, global_error, fs::g_tls_error);
.arg(QString::fromStdString(global_config_path)).arg(QString::fromStdString(global_error)), QMessageBox::Ok); 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 // Add game config
@ -146,7 +150,7 @@ void emu_settings::LoadSettings(const std::string& title_id)
if (!custom_config_path.empty()) 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()); auto [custom_config, custom_error] = yaml_load(config.to_string());
config.close(); config.close();
@ -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); .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);
}
} }
} }
} }

View File

@ -85,7 +85,7 @@ public:
midi_creator m_midi_creator; midi_creator m_midi_creator;
/** Loads the settings from path.*/ /** 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.*/ /** Fixes all registered invalid settings after asking the user for permission.*/
void OpenCorrectionDialog(QWidget* parent = Q_NULLPTR); void OpenCorrectionDialog(QWidget* parent = Q_NULLPTR);

View File

@ -1116,7 +1116,9 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
QAction* configure = menu.addAction(gameinfo->hasCustomConfig QAction* configure = menu.addAction(gameinfo->hasCustomConfig
? tr("&Change Custom Configuration") ? 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 QAction* pad_configure = menu.addAction(gameinfo->hasCustomPadConfig
? tr("&Change Custom Gamepad Configuration") ? tr("&Change Custom Gamepad Configuration")
: tr("&Create Custom Gamepad Configuration")); : tr("&Create Custom Gamepad Configuration"));
@ -1422,9 +1424,11 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
sys_log.notice("Booting from gamelist per context menu..."); sys_log.notice("Booting from gamelist per context menu...");
Q_EMIT RequestBoot(gameinfo, cfg_mode::global); 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]() connect(&dlg, &settings_dialog::EmuSettingsApplied, [this, gameinfo]()
{ {
if (!gameinfo->hasCustomConfig) if (!gameinfo->hasCustomConfig)
@ -1434,8 +1438,20 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
} }
Q_EMIT NotifyEmuSettingsChange(); Q_EMIT NotifyEmuSettingsChange();
}); });
dlg.exec(); 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]() connect(pad_configure, &QAction::triggered, this, [this, current_game, gameinfo]()
{ {
pad_settings_dialog dlg(m_gui_settings, this, &current_game); pad_settings_dialog dlg(m_gui_settings, this, &current_game);

View File

@ -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; 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) : QDialog(parent)
, m_tab_index(tab_index) , m_tab_index(tab_index)
, ui(new Ui::settings_dialog) , ui(new Ui::settings_dialog)
@ -138,7 +138,7 @@ settings_dialog::settings_dialog(std::shared_ptr<gui_settings> gui_settings, std
if (game) 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))); setWindowTitle(tr("Settings: [%0] %1", "Settings dialog").arg(qstr(game->serial)).arg(qstr(game->name)));
} }
else else

View File

@ -21,7 +21,7 @@ class settings_dialog : public QDialog
Q_OBJECT Q_OBJECT
public: 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(); ~settings_dialog();
int exec() override; int exec() override;
Q_SIGNALS: Q_SIGNALS:
@ -41,21 +41,24 @@ private:
void AddStylesheets(); void AddStylesheets();
void ApplyStylesheet(bool reset); void ApplyStylesheet(bool reset);
QString m_current_stylesheet; QString m_current_stylesheet;
// Gpu tab // Gpu tab
QString m_old_renderer; QString m_old_renderer;
// Audio tab
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;
int m_tab_index; // Audio tab
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{};
int m_tab_index = 0;
std::unique_ptr<Ui::settings_dialog> ui; std::unique_ptr<Ui::settings_dialog> ui;
std::shared_ptr<gui_settings> m_gui_settings; std::shared_ptr<gui_settings> m_gui_settings;
std::shared_ptr<emu_settings> m_emu_settings; std::shared_ptr<emu_settings> m_emu_settings;
// Discord // Discord
bool m_use_discord; bool m_use_discord = false;
QString m_discord_state; QString m_discord_state;
// Descriptions // Descriptions