diff --git a/common/MemorySettingsInterface.cpp b/common/MemorySettingsInterface.cpp index 4ab6bc0d0f..4483adc627 100644 --- a/common/MemorySettingsInterface.cpp +++ b/common/MemorySettingsInterface.cpp @@ -124,6 +124,20 @@ bool MemorySettingsInterface::GetStringValue(const char* section, const char* ke return true; } +bool MemorySettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const +{ + const auto sit = m_sections.find(section); + if (sit == m_sections.end()) + return false; + + const auto iter = sit->second.find(key); + if (iter == sit->second.end()) + return false; + + value->assign(iter->second); + return true; +} + void MemorySettingsInterface::SetIntValue(const char* section, const char* key, s32 value) { SetValue(section, key, std::to_string(value)); diff --git a/common/MemorySettingsInterface.h b/common/MemorySettingsInterface.h index 6c926aaa4e..1fac52e597 100644 --- a/common/MemorySettingsInterface.h +++ b/common/MemorySettingsInterface.h @@ -22,6 +22,7 @@ public: bool GetDoubleValue(const char* section, const char* key, double* value) const override; bool GetBoolValue(const char* section, const char* key, bool* value) const override; bool GetStringValue(const char* section, const char* key, std::string* value) const override; + bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override; void SetIntValue(const char* section, const char* key, s32 value) override; void SetUIntValue(const char* section, const char* key, u32 value) override; diff --git a/common/SettingsInterface.h b/common/SettingsInterface.h index 460b254c2b..b90c443c97 100644 --- a/common/SettingsInterface.h +++ b/common/SettingsInterface.h @@ -4,6 +4,7 @@ #pragma once #include "Pcsx2Defs.h" +#include "SmallString.h" #include #include @@ -25,6 +26,7 @@ public: virtual bool GetDoubleValue(const char* section, const char* key, double* value) const = 0; virtual bool GetBoolValue(const char* section, const char* key, bool* value) const = 0; virtual bool GetStringValue(const char* section, const char* key, std::string* value) const = 0; + virtual bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const = 0; virtual void SetIntValue(const char* section, const char* key, int value) = 0; virtual void SetUIntValue(const char* section, const char* key, uint value) = 0; @@ -83,6 +85,22 @@ public: return value; } + __fi SmallString GetSmallStringValue(const char* section, const char* key, const char* default_value = "") const + { + SmallString value; + if (!GetStringValue(section, key, &value)) + value.assign(default_value); + return value; + } + + __fi SmallString GetTinyStringValue(const char* section, const char* key, const char* default_value = "") const + { + TinyString value; + if (!GetStringValue(section, key, &value)) + value.assign(default_value); + return value; + } + __fi std::optional GetOptionalIntValue(const char* section, const char* key, std::optional default_value = std::nullopt) const { int ret; @@ -116,7 +134,29 @@ public: __fi std::optional GetOptionalStringValue(const char* section, const char* key, std::optional default_value = std::nullopt) const { std::string ret; - return GetStringValue(section, key, &ret) ? std::optional(ret) : default_value; + return GetStringValue(section, key, &ret) ? std::optional(ret) : + (default_value.has_value() ? std::optional(default_value.value()) : + std::optional()); + } + + __fi std::optional GetOptionalSmallStringValue(const char* section, const char* key, + std::optional default_value = std::nullopt) const + { + SmallString ret; + return GetStringValue(section, key, &ret) ? + std::optional(ret) : + (default_value.has_value() ? std::optional(default_value.value()) : + std::optional()); + } + + __fi std::optional GetOptionalTinyStringValue(const char* section, const char* key, + std::optional default_value = std::nullopt) const + { + TinyString ret; + return GetStringValue(section, key, &ret) ? + std::optional(ret) : + (default_value.has_value() ? std::optional(default_value.value()) : + std::optional()); } __fi void SetOptionalIntValue(const char* section, const char* key, const std::optional& value) diff --git a/pcsx2/Host.cpp b/pcsx2/Host.cpp index 18879b4825..725977c1ae 100644 --- a/pcsx2/Host.cpp +++ b/pcsx2/Host.cpp @@ -185,6 +185,20 @@ std::string Host::GetBaseStringSettingValue(const char* section, const char* key ->GetStringValue(section, key, default_value); } +SmallString Host::GetBaseSmallStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) +{ + std::unique_lock lock(s_settings_mutex); + return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE) + ->GetSmallStringValue(section, key, default_value); +} + +TinyString Host::GetBaseTinyStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) +{ + std::unique_lock lock(s_settings_mutex); + return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE) + ->GetTinyStringValue(section, key, default_value); +} + bool Host::GetBaseBoolSettingValue(const char* section, const char* key, bool default_value /*= false*/) { std::unique_lock lock(s_settings_mutex); @@ -288,6 +302,18 @@ void Host::RemoveBaseSettingValue(const char* section, const char* key) s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->DeleteValue(section, key); } +SmallString Host::GetSmallStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) +{ + std::unique_lock lock(s_settings_mutex); + return s_layered_settings_interface.GetSmallStringValue(section, key, default_value); +} + +TinyString Host::GetTinyStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) +{ + std::unique_lock lock(s_settings_mutex); + return s_layered_settings_interface.GetTinyStringValue(section, key, default_value); +} + std::string Host::GetStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) { std::unique_lock lock(s_settings_mutex); diff --git a/pcsx2/Host.h b/pcsx2/Host.h index 2682c9a3bb..8d8bc78cc0 100644 --- a/pcsx2/Host.h +++ b/pcsx2/Host.h @@ -1,9 +1,10 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once #include "common/Pcsx2Defs.h" +#include "common/SmallString.h" #include "fmt/format.h" @@ -92,6 +93,8 @@ namespace Host /// Base setting retrieval, bypasses layers. std::string GetBaseStringSettingValue(const char* section, const char* key, const char* default_value = ""); + SmallString GetBaseSmallStringSettingValue(const char* section, const char* key, const char* default_value = ""); + TinyString GetBaseTinyStringSettingValue(const char* section, const char* key, const char* default_value = ""); bool GetBaseBoolSettingValue(const char* section, const char* key, bool default_value = false); int GetBaseIntSettingValue(const char* section, const char* key, int default_value = 0); uint GetBaseUIntSettingValue(const char* section, const char* key, uint default_value = 0); @@ -115,6 +118,8 @@ namespace Host /// Settings access, thread-safe. std::string GetStringSettingValue(const char* section, const char* key, const char* default_value = ""); + SmallString GetSmallStringSettingValue(const char* section, const char* key, const char* default_value = ""); + TinyString GetTinyStringSettingValue(const char* section, const char* key, const char* default_value = ""); bool GetBoolSettingValue(const char* section, const char* key, bool default_value = false); int GetIntSettingValue(const char* section, const char* key, int default_value = 0); uint GetUIntSettingValue(const char* section, const char* key, uint default_value = 0); diff --git a/pcsx2/INISettingsInterface.cpp b/pcsx2/INISettingsInterface.cpp index 341e79f06b..01cc1e8bd6 100644 --- a/pcsx2/INISettingsInterface.cpp +++ b/pcsx2/INISettingsInterface.cpp @@ -196,6 +196,16 @@ bool INISettingsInterface::GetStringValue(const char* section, const char* key, return true; } +bool INISettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const +{ + const char* str_value = m_ini.GetValue(section, key); + if (!str_value) + return false; + + value->assign(str_value); + return true; +} + void INISettingsInterface::SetIntValue(const char* section, const char* key, int value) { m_dirty = true; @@ -314,8 +324,7 @@ std::vector> INISettingsInterface::GetKeyVal entries.emplace_back(key.pItem, value); } } - std::sort(entries.begin(), entries.end(), [](const KVEntry& a, const KVEntry& b) - { + std::sort(entries.begin(), entries.end(), [](const KVEntry& a, const KVEntry& b) { return a.second.nOrder < b.second.nOrder; }); for (const KVEntry& entry : entries) diff --git a/pcsx2/INISettingsInterface.h b/pcsx2/INISettingsInterface.h index 0d5083738d..d00ff5655f 100644 --- a/pcsx2/INISettingsInterface.h +++ b/pcsx2/INISettingsInterface.h @@ -30,6 +30,7 @@ public: bool GetDoubleValue(const char* section, const char* key, double* value) const override; bool GetBoolValue(const char* section, const char* key, bool* value) const override; bool GetStringValue(const char* section, const char* key, std::string* value) const override; + bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override; void SetIntValue(const char* section, const char* key, int value) override; void SetUIntValue(const char* section, const char* key, uint value) override; diff --git a/pcsx2/LayeredSettingsInterface.cpp b/pcsx2/LayeredSettingsInterface.cpp index ce12962f6d..1cc4476df3 100644 --- a/pcsx2/LayeredSettingsInterface.cpp +++ b/pcsx2/LayeredSettingsInterface.cpp @@ -106,6 +106,20 @@ bool LayeredSettingsInterface::GetStringValue(const char* section, const char* k return false; } +bool LayeredSettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const +{ + for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++) + { + if (SettingsInterface* sif = m_layers[layer]; sif != nullptr) + { + if (sif->GetStringValue(section, key, value)) + return true; + } + } + + return false; +} + void LayeredSettingsInterface::SetIntValue(const char* section, const char* key, int value) { pxFailRel("Attempt to call SetIntValue() on layered settings interface"); diff --git a/pcsx2/LayeredSettingsInterface.h b/pcsx2/LayeredSettingsInterface.h index e37903480f..b5399a9be0 100644 --- a/pcsx2/LayeredSettingsInterface.h +++ b/pcsx2/LayeredSettingsInterface.h @@ -35,6 +35,7 @@ public: bool GetDoubleValue(const char* section, const char* key, double* value) const override; bool GetBoolValue(const char* section, const char* key, bool* value) const override; bool GetStringValue(const char* section, const char* key, std::string* value) const override; + bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const override; void SetIntValue(const char* section, const char* key, int value) override; void SetUIntValue(const char* section, const char* key, uint value) override;