From 189656cbc4fa5861db7d973cd5378b784ec8cb57 Mon Sep 17 00:00:00 2001 From: Albert Liu <45282415+ggrtk@users.noreply.github.com> Date: Sat, 19 Dec 2020 19:23:33 -0800 Subject: [PATCH] CommonHostInterface: Fully clear controller section before loading profile --- .../src/cpp/android_settings_interface.cpp | 5 ++++ .../app/src/cpp/android_settings_interface.h | 1 + src/core/settings.h | 1 + .../libretro_settings_interface.cpp | 5 ++++ .../libretro_settings_interface.h | 3 ++- src/frontend-common/common_host_interface.cpp | 23 ++++--------------- .../ini_settings_interface.cpp | 7 ++++++ src/frontend-common/ini_settings_interface.h | 1 + 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/android/app/src/cpp/android_settings_interface.cpp b/android/app/src/cpp/android_settings_interface.cpp index 94ebf2d9d..9da95b295 100644 --- a/android/app/src/cpp/android_settings_interface.cpp +++ b/android/app/src/cpp/android_settings_interface.cpp @@ -204,6 +204,11 @@ void AndroidSettingsInterface::DeleteValue(const char* section, const char* key) Log_ErrorPrintf("DeleteValue(\"%s\", \"%s\") not implemented", section, key); } +void AndroidSettingsInterface::ClearSection(const char* section) +{ + Log_ErrorPrintf("ClearSection(\"%s\") not implemented", section); +} + std::vector AndroidSettingsInterface::GetStringList(const char* section, const char* key) { JNIEnv* env = AndroidHelpers::GetJNIEnv(); diff --git a/android/app/src/cpp/android_settings_interface.h b/android/app/src/cpp/android_settings_interface.h index ca42ed569..baddbf290 100644 --- a/android/app/src/cpp/android_settings_interface.h +++ b/android/app/src/cpp/android_settings_interface.h @@ -20,6 +20,7 @@ public: void SetBoolValue(const char* section, const char* key, bool value) override; void SetStringValue(const char* section, const char* key, const char* value) override; void DeleteValue(const char* section, const char* key) override; + void ClearSection(const char* section) override; std::vector GetStringList(const char* section, const char* key) override; void SetStringList(const char* section, const char* key, const std::vector& items) override; diff --git a/src/core/settings.h b/src/core/settings.h index 0dc68e7fd..c4c775126 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -29,6 +29,7 @@ public: virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0; virtual void DeleteValue(const char* section, const char* key) = 0; + virtual void ClearSection(const char* section) = 0; }; struct SettingInfo diff --git a/src/duckstation-libretro/libretro_settings_interface.cpp b/src/duckstation-libretro/libretro_settings_interface.cpp index 3ddf8cf57..ea1d7fd99 100644 --- a/src/duckstation-libretro/libretro_settings_interface.cpp +++ b/src/duckstation-libretro/libretro_settings_interface.cpp @@ -115,3 +115,8 @@ void LibretroSettingsInterface::DeleteValue(const char* section, const char* key { Log_ErrorPrintf("DeleteValue(\"%s\", \"%s\") not implemented", section, key); } + +void LibretroSettingsInterface::ClearSection(const char* section) +{ + Log_ErrorPrintf("ClearSection(\"%s\") not implemented", section); +} diff --git a/src/duckstation-libretro/libretro_settings_interface.h b/src/duckstation-libretro/libretro_settings_interface.h index 878efb7fd..6ccd13491 100644 --- a/src/duckstation-libretro/libretro_settings_interface.h +++ b/src/duckstation-libretro/libretro_settings_interface.h @@ -22,4 +22,5 @@ public: bool AddToStringList(const char* section, const char* key, const char* item) override; void DeleteValue(const char* section, const char* key) override; -}; \ No newline at end of file + void ClearSection(const char* section) override; +}; diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index aa458a4c3..dfa517bbc 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -1781,24 +1781,7 @@ std::string CommonHostInterface::GetInputProfilePath(const char* name) const void CommonHostInterface::ClearAllControllerBindings(SettingsInterface& si) { for (u32 controller_index = 1; controller_index <= NUM_CONTROLLER_AND_CARD_PORTS; controller_index++) - { - const ControllerType ctype = g_settings.controller_types[controller_index - 1]; - if (ctype == ControllerType::None) - continue; - - const auto section_name = TinyString::FromFormat("Controller%u", controller_index); - - si.DeleteValue(section_name, "Type"); - - for (const auto& button : Controller::GetButtonNames(ctype)) - si.DeleteValue(section_name, button.first.c_str()); - - for (const auto& axis : Controller::GetAxisNames(ctype)) - si.DeleteValue(section_name, std::get(axis).c_str()); - - if (Controller::GetVibrationMotorCount(ctype) > 0) - si.DeleteValue(section_name, "Rumble"); - } + si.ClearSection(TinyString::FromFormat("Controller%u", controller_index)); } void CommonHostInterface::ApplyInputProfile(const char* profile_path, SettingsInterface& si) @@ -1813,7 +1796,11 @@ void CommonHostInterface::ApplyInputProfile(const char* profile_path, SettingsIn const auto section_name = TinyString::FromFormat("Controller%u", controller_index); const std::string ctype_str = profile.GetStringValue(section_name, "Type"); if (ctype_str.empty()) + { + si.SetStringValue(section_name, "Type", Settings::GetControllerTypeName(ControllerType::None)); + g_settings.controller_types[controller_index - 1] = ControllerType::None; continue; + } std::optional ctype = Settings::ParseControllerTypeName(ctype_str.c_str()); if (!ctype) diff --git a/src/frontend-common/ini_settings_interface.cpp b/src/frontend-common/ini_settings_interface.cpp index f1b698f2f..35146f828 100644 --- a/src/frontend-common/ini_settings_interface.cpp +++ b/src/frontend-common/ini_settings_interface.cpp @@ -102,6 +102,13 @@ void INISettingsInterface::DeleteValue(const char* section, const char* key) m_ini.Delete(section, key); } +void INISettingsInterface::ClearSection(const char* section) +{ + m_dirty = true; + m_ini.Delete(section, nullptr); + m_ini.SetValue(section, nullptr, nullptr); +} + std::vector INISettingsInterface::GetStringList(const char* section, const char* key) { std::list entries; diff --git a/src/frontend-common/ini_settings_interface.h b/src/frontend-common/ini_settings_interface.h index cfc0f827f..a75c4c21c 100644 --- a/src/frontend-common/ini_settings_interface.h +++ b/src/frontend-common/ini_settings_interface.h @@ -27,6 +27,7 @@ public: void SetBoolValue(const char* section, const char* key, bool value) override; void SetStringValue(const char* section, const char* key, const char* value) override; void DeleteValue(const char* section, const char* key) override; + void ClearSection(const char* section) override; std::vector GetStringList(const char* section, const char* key) override; void SetStringList(const char* section, const char* key, const std::vector& items) override;