Qt: Clean and remove empty game settings

This commit is contained in:
Stenzek
2024-04-25 14:02:16 +10:00
parent d6ffdb0242
commit 1cdfca155d
23 changed files with 247 additions and 47 deletions

View File

@ -336,7 +336,7 @@ void ControllerBindingWidget::doDeviceAutomaticBinding(const QString& device)
else
{
result = InputManager::MapController(*m_dialog->getProfileSettingsInterface(), m_port_number, mapping);
m_dialog->getProfileSettingsInterface()->Save();
QtHost::SaveGameSettings(m_dialog->getProfileSettingsInterface(), false);
g_emu_thread->reloadInputBindings();
}

View File

@ -278,8 +278,7 @@ void ControllerSettingsWindow::setBoolValue(const char* section, const char* key
if (m_profile_interface)
{
m_profile_interface->SetBoolValue(section, key, value);
m_profile_interface->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -294,8 +293,7 @@ void ControllerSettingsWindow::setIntValue(const char* section, const char* key,
if (m_profile_interface)
{
m_profile_interface->SetIntValue(section, key, value);
m_profile_interface->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -310,8 +308,7 @@ void ControllerSettingsWindow::setStringValue(const char* section, const char* k
if (m_profile_interface)
{
m_profile_interface->SetStringValue(section, key, value);
m_profile_interface->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -321,6 +318,13 @@ void ControllerSettingsWindow::setStringValue(const char* section, const char* k
}
}
void ControllerSettingsWindow::saveAndReloadGameSettings()
{
DebugAssert(m_profile_interface);
QtHost::SaveGameSettings(m_profile_interface.get(), false);
g_emu_thread->reloadGameSettings(false);
}
void ControllerSettingsWindow::clearSettingValue(const char* section, const char* key)
{
if (m_profile_interface)

View File

@ -64,6 +64,7 @@ public:
void setIntValue(const char* section, const char* key, s32 value);
void setStringValue(const char* section, const char* key, const char* value);
void clearSettingValue(const char* section, const char* key);
void saveAndReloadGameSettings();
Q_SIGNALS:
void inputProfileSwitched();

View File

@ -37,7 +37,7 @@ static void BindWidgetToInputProfileBool(SettingsInterface* sif, WidgetType* wid
Accessor::connectValueChanged(widget, [sif, widget, section = std::move(section), key = std::move(key)]() {
const bool new_value = Accessor::getBoolValue(widget);
sif->SetBoolValue(section.c_str(), key.c_str(), new_value);
sif->Save();
QtHost::SaveGameSettings(sif, false);
g_emu_thread->reloadGameSettings();
});
}
@ -70,7 +70,7 @@ static void BindWidgetToInputProfileFloat(SettingsInterface* sif, WidgetType* wi
Accessor::connectValueChanged(widget, [sif, widget, section = std::move(section), key = std::move(key)]() {
const float new_value = Accessor::getFloatValue(widget);
sif->SetFloatValue(section.c_str(), key.c_str(), new_value);
sif->Save();
QtHost::SaveGameSettings(sif, false);
g_emu_thread->reloadGameSettings();
});
}
@ -103,7 +103,7 @@ static void BindWidgetToInputProfileNormalized(SettingsInterface* sif, WidgetTyp
Accessor::connectValueChanged(widget, [sif, widget, section = std::move(section), key = std::move(key), range]() {
const int new_value = Accessor::getIntValue(widget);
sif->SetFloatValue(section.c_str(), key.c_str(), static_cast<float>(new_value) / range);
sif->Save();
QtHost::SaveGameSettings(sif, false);
g_emu_thread->reloadGameSettings();
});
}
@ -142,7 +142,7 @@ static void BindWidgetToInputProfileString(SettingsInterface* sif, WidgetType* w
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, false);
g_emu_thread->reloadGameSettings();
});
}

View File

@ -234,7 +234,7 @@ void InputBindingDialog::saveListToSettings()
m_sif->SetStringList(m_section_name.c_str(), m_key_name.c_str(), m_bindings);
else
m_sif->DeleteValue(m_section_name.c_str(), m_key_name.c_str());
m_sif->Save();
QtHost::SaveGameSettings(m_sif, false);
g_emu_thread->reloadGameSettings();
}
else

View File

@ -218,7 +218,7 @@ void InputBindingWidget::setNewBinding()
if (m_sif)
{
m_sif->SetStringValue(m_section_name.c_str(), m_key_name.c_str(), new_binding.c_str());
m_sif->Save();
QtHost::SaveGameSettings(m_sif, false);
g_emu_thread->reloadGameSettings();
}
else
@ -239,7 +239,7 @@ void InputBindingWidget::clearBinding()
if (m_sif)
{
m_sif->DeleteValue(m_section_name.c_str(), m_key_name.c_str());
m_sif->Save();
QtHost::SaveGameSettings(m_sif, false);
g_emu_thread->reloadGameSettings();
}
else

View File

@ -41,15 +41,13 @@ void PostProcessingSettingsWidget::commitSettingsUpdate()
{
if (m_dialog->isPerGameSettings())
{
m_dialog->getSettingsInterface()->Save();
g_emu_thread->reloadGameSettings(false);
m_dialog->saveAndReloadGameSettings();
}
else
{
Host::CommitBaseSettingChanges();
g_emu_thread->updatePostProcessingSettings();
}
g_emu_thread->updatePostProcessingSettings();
}
void PostProcessingSettingsWidget::connectUi()

View File

@ -171,6 +171,42 @@ INISettingsInterface* QtHost::GetBaseSettingsInterface()
return s_base_settings_interface.get();
}
bool QtHost::SaveGameSettings(SettingsInterface* sif, bool delete_if_empty)
{
INISettingsInterface* ini = static_cast<INISettingsInterface*>(sif);
Error error;
// if there's no keys, just toss the whole thing out
if (delete_if_empty && ini->IsEmpty())
{
Log_InfoFmt("Removing empty gamesettings ini {}", Path::GetFileName(ini->GetFileName()));
if (FileSystem::FileExists(ini->GetFileName().c_str()) &&
!FileSystem::DeleteFile(ini->GetFileName().c_str(), &error))
{
Host::ReportErrorAsync(
TRANSLATE_SV("QtHost", "Error"),
fmt::format(TRANSLATE_FS("QtHost", "An error occurred while deleting empty game settings:\n{}"),
error.GetDescription()));
return false;
}
return true;
}
// clean unused sections, stops the file being bloated
sif->RemoveEmptySections();
if (!sif->Save(&error))
{
Host::ReportErrorAsync(
TRANSLATE_SV("QtHost", "Error"),
fmt::format(TRANSLATE_FS("QtHost", "An error occurred while saving game settings:\n{}"), error.GetDescription()));
return false;
}
return true;
}
QIcon QtHost::GetAppIcon()
{
return QIcon(QStringLiteral(":/icons/duck.png"));

View File

@ -280,6 +280,9 @@ QString GetResourcesBasePath();
/// Returns the base settings interface. Should lock before manipulating.
INISettingsInterface* GetBaseSettingsInterface();
/// Saves a game settings interface.
bool SaveGameSettings(SettingsInterface* sif, bool delete_if_empty);
/// Downloads the specified URL to the provided path.
bool DownloadFile(QWidget* parent, const QString& title, std::string url, const char* path);

View File

@ -289,8 +289,7 @@ void SettingsWindow::onCopyGlobalSettingsClicked()
temp.Load(*Host::Internal::GetBaseSettingsLayer());
temp.Save(*m_sif.get(), true);
}
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
reloadPages();
@ -311,8 +310,7 @@ void SettingsWindow::onClearSettingsClicked()
}
Settings::Clear(*m_sif.get());
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
reloadPages();
@ -524,8 +522,7 @@ void SettingsWindow::setBoolSettingValue(const char* section, const char* key, s
if (m_sif)
{
value.has_value() ? m_sif->SetBoolValue(section, key, value.value()) : m_sif->DeleteValue(section, key);
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -541,8 +538,7 @@ void SettingsWindow::setIntSettingValue(const char* section, const char* key, st
if (m_sif)
{
value.has_value() ? m_sif->SetIntValue(section, key, value.value()) : m_sif->DeleteValue(section, key);
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -558,8 +554,7 @@ void SettingsWindow::setFloatSettingValue(const char* section, const char* key,
if (m_sif)
{
value.has_value() ? m_sif->SetFloatValue(section, key, value.value()) : m_sif->DeleteValue(section, key);
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -575,8 +570,7 @@ void SettingsWindow::setStringSettingValue(const char* section, const char* key,
if (m_sif)
{
value.has_value() ? m_sif->SetStringValue(section, key, value.value()) : m_sif->DeleteValue(section, key);
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -600,8 +594,7 @@ void SettingsWindow::removeSettingValue(const char* section, const char* key)
if (m_sif)
{
m_sif->DeleteValue(section, key);
m_sif->Save();
g_emu_thread->reloadGameSettings();
saveAndReloadGameSettings();
}
else
{
@ -611,6 +604,13 @@ void SettingsWindow::removeSettingValue(const char* section, const char* key)
}
}
void SettingsWindow::saveAndReloadGameSettings()
{
DebugAssert(m_sif);
QtHost::SaveGameSettings(m_sif.get(), true);
g_emu_thread->reloadGameSettings(false);
}
void SettingsWindow::openGamePropertiesDialog(const std::string& path, const std::string& serial, DiscRegion region)
{
const GameDatabase::Entry* dentry = nullptr;

View File

@ -88,6 +88,7 @@ public:
void setStringSettingValue(const char* section, const char* key, std::optional<const char*> value);
bool containsSettingValue(const char* section, const char* key) const;
void removeSettingValue(const char* section, const char* key);
void saveAndReloadGameSettings();
Q_SIGNALS:
void settingsResetToDefaults();

View File

@ -710,7 +710,7 @@ static void BindWidgetToBoolSetting(SettingsInterface* sif, WidgetType* widget,
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -753,7 +753,7 @@ static void BindWidgetToIntSetting(SettingsInterface* sif, WidgetType* widget, s
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -828,7 +828,7 @@ static inline void BindWidgetAndLabelToIntSetting(SettingsInterface* sif, Widget
}
}
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -876,7 +876,7 @@ static void BindWidgetToFloatSetting(SettingsInterface* sif, WidgetType* widget,
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -917,7 +917,7 @@ static void BindWidgetToNormalizedSetting(SettingsInterface* sif, WidgetType* wi
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -959,7 +959,7 @@ static void BindWidgetToStringSetting(SettingsInterface* sif, WidgetType* widget
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -1024,7 +1024,7 @@ static void BindWidgetToEnumSetting(SettingsInterface* sif, WidgetType* widget,
sif->DeleteValue(section.c_str(), key.c_str());
}
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -1092,7 +1092,7 @@ static void BindWidgetToEnumSetting(SettingsInterface* sif, WidgetType* widget,
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}
@ -1160,7 +1160,7 @@ static void BindWidgetToEnumSetting(SettingsInterface* sif, WidgetType* widget,
else
sif->DeleteValue(section.c_str(), key.c_str());
sif->Save();
QtHost::SaveGameSettings(sif, true);
g_emu_thread->reloadGameSettings();
});
}