config: Migrate config files into config/custom

Co-authored-by: lat9nq <lat9nq@virginia.edu>
This commit is contained in:
Morph 2020-09-27 14:20:22 -04:00
parent c0c4ed0d3b
commit 64e174237e
4 changed files with 61 additions and 21 deletions

View File

@ -15,27 +15,10 @@
namespace FS = Common::FS; namespace FS = Common::FS;
Config::Config(const std::string& config_file, ConfigType config_type) : type(config_type) { Config::Config(const std::string& config_name, ConfigType config_type) : type(config_type) {
global = config_type == ConfigType::GlobalConfig; global = config_type == ConfigType::GlobalConfig;
switch (config_type) { Initialize(config_name);
case ConfigType::GlobalConfig:
case ConfigType::PerGameConfig:
qt_config_loc = fmt::format("{}" DIR_SEP "{}.ini", FS::GetUserPath(FS::UserPath::ConfigDir),
config_file);
FS::CreateFullPath(qt_config_loc);
qt_config = std::make_unique<QSettings>(QString::fromStdString(qt_config_loc),
QSettings::IniFormat);
Reload();
break;
case ConfigType::InputProfile:
qt_config_loc = fmt::format("{}input" DIR_SEP "{}.ini",
FS::GetUserPath(FS::UserPath::ConfigDir), config_file);
FS::CreateFullPath(qt_config_loc);
qt_config = std::make_unique<QSettings>(QString::fromStdString(qt_config_loc),
QSettings::IniFormat);
break;
}
} }
Config::~Config() { Config::~Config() {
@ -256,6 +239,34 @@ const std::array<UISettings::Shortcut, 16> Config::default_hotkeys{{
}}; }};
// clang-format on // clang-format on
void Config::Initialize(const std::string& config_name) {
switch (type) {
case ConfigType::GlobalConfig:
qt_config_loc = fmt::format("{}" DIR_SEP "{}.ini", FS::GetUserPath(FS::UserPath::ConfigDir),
config_name);
FS::CreateFullPath(qt_config_loc);
qt_config = std::make_unique<QSettings>(QString::fromStdString(qt_config_loc),
QSettings::IniFormat);
Reload();
break;
case ConfigType::PerGameConfig:
qt_config_loc = fmt::format("{}custom" DIR_SEP "{}.ini",
FS::GetUserPath(FS::UserPath::ConfigDir), config_name);
FS::CreateFullPath(qt_config_loc);
qt_config = std::make_unique<QSettings>(QString::fromStdString(qt_config_loc),
QSettings::IniFormat);
Reload();
break;
case ConfigType::InputProfile:
qt_config_loc = fmt::format("{}input" DIR_SEP "{}.ini",
FS::GetUserPath(FS::UserPath::ConfigDir), config_name);
FS::CreateFullPath(qt_config_loc);
qt_config = std::make_unique<QSettings>(QString::fromStdString(qt_config_loc),
QSettings::IniFormat);
break;
}
}
void Config::ReadPlayerValue(std::size_t player_index) { void Config::ReadPlayerValue(std::size_t player_index) {
const QString player_prefix = [this, player_index] { const QString player_prefix = [this, player_index] {
if (type == ConfigType::InputProfile) { if (type == ConfigType::InputProfile) {

View File

@ -22,7 +22,7 @@ public:
InputProfile, InputProfile,
}; };
explicit Config(const std::string& config_loc = "qt-config", explicit Config(const std::string& config_name = "qt-config",
ConfigType config_type = ConfigType::GlobalConfig); ConfigType config_type = ConfigType::GlobalConfig);
~Config(); ~Config();
@ -45,6 +45,8 @@ public:
static const std::array<UISettings::Shortcut, 16> default_hotkeys; static const std::array<UISettings::Shortcut, 16> default_hotkeys;
private: private:
void Initialize(const std::string& config_name);
void ReadValues(); void ReadValues();
void ReadPlayerValue(std::size_t player_index); void ReadPlayerValue(std::size_t player_index);
void ReadDebugValues(); void ReadDebugValues();

View File

@ -50,6 +50,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
#include <QDesktopServices> #include <QDesktopServices>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>
@ -277,6 +278,8 @@ GMainWindow::GMainWindow()
if (args.length() >= 2) { if (args.length() >= 2) {
BootGame(args[1]); BootGame(args[1]);
} }
MigrateConfigFiles();
} }
GMainWindow::~GMainWindow() { GMainWindow::~GMainWindow() {
@ -1578,7 +1581,8 @@ void GMainWindow::RemoveCustomConfiguration(u64 program_id) {
const QString config_dir = const QString config_dir =
QString::fromStdString(Common::FS::GetUserPath(Common::FS::UserPath::ConfigDir)); QString::fromStdString(Common::FS::GetUserPath(Common::FS::UserPath::ConfigDir));
const QString custom_config_file_path = const QString custom_config_file_path =
config_dir + QString::fromStdString(fmt::format("{:016X}.ini", program_id)); config_dir + QStringLiteral("custom") + QDir::separator() +
QString::fromStdString(fmt::format("{:016X}.ini", program_id));
if (!QFile::exists(custom_config_file_path)) { if (!QFile::exists(custom_config_file_path)) {
QMessageBox::warning(this, tr("Error Removing Custom Configuration"), QMessageBox::warning(this, tr("Error Removing Custom Configuration"),
@ -2394,6 +2398,28 @@ void GMainWindow::OnCaptureScreenshot() {
OnStartGame(); OnStartGame();
} }
// TODO: Written 2020-10-01: Remove per-game config migration code when it is irrelevant
void GMainWindow::MigrateConfigFiles() {
const std::string& config_dir_str = Common::FS::GetUserPath(Common::FS::UserPath::ConfigDir);
const QDir config_dir = QDir(QString::fromStdString(config_dir_str));
const QStringList config_dir_list = config_dir.entryList(QStringList(QStringLiteral("*.ini")));
Common::FS::CreateFullPath(fmt::format("{}custom" DIR_SEP, config_dir_str));
for (QStringList::const_iterator it = config_dir_list.constBegin(); it != config_dir_list.constEnd(); ++it) {
const auto filename = it->toStdString();
if (filename.find_first_not_of("0123456789abcdefACBDEF", 0) < 16) {
continue;
}
const auto origin = fmt::format("{}{}", config_dir_str, filename);
const auto destination = fmt::format("{}custom" DIR_SEP "{}", config_dir_str, filename);
LOG_INFO(Frontend, "Migrating config file from {} to {}", origin, destination);
if (!Common::FS::Rename(origin, destination)) {
// Delete the old config file if one already exists in the new location.
Common::FS::Delete(origin);
}
}
}
void GMainWindow::UpdateWindowTitle(const std::string& title_name, void GMainWindow::UpdateWindowTitle(const std::string& title_name,
const std::string& title_version) { const std::string& title_version) {
const auto full_name = std::string(Common::g_build_fullname); const auto full_name = std::string(Common::g_build_fullname);

View File

@ -251,6 +251,7 @@ private:
std::optional<u64> SelectRomFSDumpTarget(const FileSys::ContentProvider&, u64 program_id); std::optional<u64> SelectRomFSDumpTarget(const FileSys::ContentProvider&, u64 program_id);
InstallResult InstallNSPXCI(const QString& filename); InstallResult InstallNSPXCI(const QString& filename);
InstallResult InstallNCA(const QString& filename); InstallResult InstallNCA(const QString& filename);
void MigrateConfigFiles();
void UpdateWindowTitle(const std::string& title_name = {}, void UpdateWindowTitle(const std::string& title_name = {},
const std::string& title_version = {}); const std::string& title_version = {});
void UpdateStatusBar(); void UpdateStatusBar();