Improve error handling during config loading

This commit is contained in:
Megamouse 2020-07-28 19:01:07 +02:00
parent 21a1072117
commit ef3e8d26ce
2 changed files with 81 additions and 18 deletions

View File

@ -127,7 +127,11 @@ void Emulator::Init()
if (const fs::file cfg_file{cfg_path, fs::read + fs::create}) if (const fs::file cfg_file{cfg_path, fs::read + fs::create})
{ {
g_cfg.from_string(cfg_file.to_string()); if (!g_cfg.from_string(cfg_file.to_string()))
{
sys_log.fatal("Failed to apply global config: %s", cfg_path);
}
g_cfg.name = cfg_path; g_cfg.name = cfg_path;
} }
else else
@ -873,22 +877,37 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
if (fs::file cfg_file{ config_path_old }) if (fs::file cfg_file{ config_path_old })
{ {
sys_log.notice("Applying custom config: %s", config_path_old); sys_log.notice("Applying custom config: %s", config_path_old);
g_cfg.from_string(cfg_file.to_string());
if (!g_cfg.from_string(cfg_file.to_string()))
{
sys_log.fatal("Failed to apply custom config: %s", config_path_old);
}
} }
// Load custom config-2 // Load custom config-2
if (fs::file cfg_file{ config_path_new }) if (fs::file cfg_file{ config_path_new })
{ {
sys_log.notice("Applying custom config: %s", config_path_new); sys_log.notice("Applying custom config: %s", config_path_new);
g_cfg.from_string(cfg_file.to_string());
if (g_cfg.from_string(cfg_file.to_string()))
{
g_cfg.name = config_path_new; g_cfg.name = config_path_new;
} }
else
{
sys_log.fatal("Failed to apply custom config: %s", config_path_new);
}
}
// Load custom config-3 // Load custom config-3
if (fs::file cfg_file{ m_path + ".yml" }) if (fs::file cfg_file{ m_path + ".yml" })
{ {
sys_log.notice("Applying custom config: %s.yml", m_path); sys_log.notice("Applying custom config: %s.yml", m_path);
g_cfg.from_string(cfg_file.to_string());
if (!g_cfg.from_string(cfg_file.to_string()))
{
sys_log.fatal("Failed to apply custom config: %s.yml", m_path);
}
} }
} }

View File

@ -7,6 +7,8 @@
#include "Emu/System.h" #include "Emu/System.h"
#include "Emu/system_config.h" #include "Emu/system_config.h"
#include "util/yaml.hpp"
LOG_CHANNEL(cfg_log, "CFG"); LOG_CHANNEL(cfg_log, "CFG");
extern std::string g_cfg_defaults; //! Default settings grabbed from Utilities/Config.h extern std::string g_cfg_defaults; //! Default settings grabbed from Utilities/Config.h
@ -73,31 +75,71 @@ void emu_settings::LoadSettings(const std::string& title_id)
fs::create_path(title_id.empty() ? fs::get_config_dir() : Emulator::GetCustomConfigDir()); fs::create_path(title_id.empty() ? fs::get_config_dir() : Emulator::GetCustomConfigDir());
// Load default config // Load default config
m_defaultSettings = YAML::Load(g_cfg_defaults); auto [default_config, default_error] = yaml_load(g_cfg_defaults);
m_currentSettings = YAML::Load(g_cfg_defaults);
if (default_error.empty())
{
m_defaultSettings = default_config;
m_currentSettings = YAML::Clone(default_config);
}
else
{
cfg_log.fatal("Failed to load default config:\n%s", default_error);
QMessageBox::critical(nullptr, tr("Config Error"), tr("Failed to load default config:\n%0")
.arg(QString::fromStdString(default_error)), QMessageBox::Ok);
}
// Add global config // Add global config
fs::file config(fs::get_config_dir() + "/config.yml", fs::read + fs::write + fs::create); const std::string global_config_path = fs::get_config_dir() + "config.yml";
m_currentSettings += YAML::Load(config.to_string()); fs::file config(global_config_path, fs::read + fs::write + fs::create);
auto [global_config, global_error] = yaml_load(config.to_string());
config.close(); config.close();
if (global_error.empty())
{
m_currentSettings += global_config;
}
else
{
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 game config // Add game config
if (!title_id.empty()) if (!title_id.empty())
{ {
const std::string config_path_new = Emulator::GetCustomConfigPath(m_title_id); const std::string config_path_new = Emulator::GetCustomConfigPath(m_title_id);
const std::string config_path_old = Emulator::GetCustomConfigPath(m_title_id, true); const std::string config_path_old = Emulator::GetCustomConfigPath(m_title_id, true);
std::string custom_config_path;
if (fs::is_file(config_path_new)) if (fs::is_file(config_path_new))
{ {
config = fs::file(config_path_new, fs::read + fs::write); custom_config_path = config_path_new;
m_currentSettings += YAML::Load(config.to_string());
config.close();
} }
else if (fs::is_file(config_path_old)) else if (fs::is_file(config_path_old))
{ {
config = fs::file(config_path_old, fs::read + fs::write); custom_config_path = config_path_old;
m_currentSettings += YAML::Load(config.to_string()); }
if (!custom_config_path.empty())
{
if (config = fs::file(custom_config_path, fs::read + fs::write))
{
auto [custom_config, custom_error] = yaml_load(config.to_string());
config.close(); config.close();
if (custom_error.empty())
{
m_currentSettings += custom_config;
}
else
{
cfg_log.fatal("Failed to load custom config %s:\n%s", custom_config_path, custom_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(custom_error)), QMessageBox::Ok);
}
}
} }
} }
} }
@ -126,9 +168,11 @@ void emu_settings::SaveSettings()
if (config_name == g_cfg.name || m_title_id == Emu.GetTitleID()) if (config_name == g_cfg.name || m_title_id == Emu.GetTitleID())
{ {
// Update current config // Update current config
g_cfg.from_string({out.c_str(), out.size()}, !Emu.IsStopped()); if (!g_cfg.from_string({out.c_str(), out.size()}, !Emu.IsStopped()))
{
if (!Emu.IsStopped()) // Don't spam the log while emulation is stopped. The config will be logged on boot anyway. cfg_log.fatal("Failed to update configuration");
}
else if (!Emu.IsStopped()) // Don't spam the log while emulation is stopped. The config will be logged on boot anyway.
{ {
cfg_log.notice("Updated configuration:\n%s\n", g_cfg.to_string()); cfg_log.notice("Updated configuration:\n%s\n", g_cfg.to_string());
} }