SettingsInterface: Add ContainsValue() and copy helpers

This commit is contained in:
Connor McLaughlin 2022-06-08 02:25:01 +10:00 committed by refractionpcsx2
parent 7db9627ff6
commit d1a235272e
9 changed files with 122 additions and 5 deletions

View File

@ -42,11 +42,12 @@ public:
virtual void SetBoolValue(const char* section, const char* key, bool value) = 0; virtual void SetBoolValue(const char* section, const char* key, bool value) = 0;
virtual void SetStringValue(const char* section, const char* key, const char* value) = 0; virtual void SetStringValue(const char* section, const char* key, const char* value) = 0;
virtual std::vector<std::string> GetStringList(const char* section, const char* key) = 0; virtual std::vector<std::string> GetStringList(const char* section, const char* key) const = 0;
virtual void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) = 0; virtual void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) = 0;
virtual bool RemoveFromStringList(const char* section, const char* key, const char* item) = 0; virtual bool RemoveFromStringList(const char* section, const char* key, const char* item) = 0;
virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0; virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0;
virtual bool ContainsValue(const char* section, const char* key) const = 0;
virtual void DeleteValue(const char* section, const char* key) = 0; virtual void DeleteValue(const char* section, const char* key) = 0;
virtual void ClearSection(const char* section) = 0; virtual void ClearSection(const char* section) = 0;
@ -153,4 +154,67 @@ public:
{ {
value.has_value() ? SetStringValue(section, key, value.value()) : DeleteValue(section, key); value.has_value() ? SetStringValue(section, key, value.value()) : DeleteValue(section, key);
} }
__fi void CopyBoolValue(const SettingsInterface& si, const char* section, const char* key)
{
bool value;
if (si.GetBoolValue(section, key, &value))
SetBoolValue(section, key, value);
else
DeleteValue(section, key);
}
__fi void CopyIntValue(const SettingsInterface& si, const char* section, const char* key)
{
int value;
if (si.GetIntValue(section, key, &value))
SetIntValue(section, key, value);
else
DeleteValue(section, key);
}
__fi void CopyUIntValue(const SettingsInterface& si, const char* section, const char* key)
{
uint value;
if (si.GetUIntValue(section, key, &value))
SetUIntValue(section, key, value);
else
DeleteValue(section, key);
}
__fi void CopyFloatValue(const SettingsInterface& si, const char* section, const char* key)
{
float value;
if (si.GetFloatValue(section, key, &value))
SetFloatValue(section, key, value);
else
DeleteValue(section, key);
}
__fi void CopyDoubleValue(const SettingsInterface& si, const char* section, const char* key)
{
double value;
if (si.GetDoubleValue(section, key, &value))
SetDoubleValue(section, key, value);
else
DeleteValue(section, key);
}
__fi void CopyStringValue(const SettingsInterface& si, const char* section, const char* key)
{
std::string value;
if (si.GetStringValue(section, key, &value))
SetStringValue(section, key, value.c_str());
else
DeleteValue(section, key);
}
__fi void CopyStringListValue(const SettingsInterface& si, const char* section, const char* key)
{
std::vector<std::string> value(si.GetStringList(section, key));
if (!value.empty())
SetStringList(section, key, value);
else
DeleteValue(section, key);
}
}; };

View File

@ -185,6 +185,11 @@ void INISettingsInterface::SetStringValue(const char* section, const char* key,
m_ini.SetValue(section, key, value, nullptr, true); m_ini.SetValue(section, key, value, nullptr, true);
} }
bool INISettingsInterface::ContainsValue(const char* section, const char* key) const
{
return (m_ini.GetValue(section, key, nullptr) != nullptr);
}
void INISettingsInterface::DeleteValue(const char* section, const char* key) void INISettingsInterface::DeleteValue(const char* section, const char* key)
{ {
m_dirty = true; m_dirty = true;

View File

@ -48,6 +48,7 @@ public:
void SetDoubleValue(const char* section, const char* key, double value) override; void SetDoubleValue(const char* section, const char* key, double value) override;
void SetBoolValue(const char* section, const char* key, bool value) override; void SetBoolValue(const char* section, const char* key, bool value) override;
void SetStringValue(const char* section, const char* key, const char* value) override; void SetStringValue(const char* section, const char* key, const char* value) override;
bool ContainsValue(const char* section, const char* key) const override;
void DeleteValue(const char* section, const char* key) override; void DeleteValue(const char* section, const char* key) override;
void ClearSection(const char* section) override; void ClearSection(const char* section) override;

View File

@ -147,6 +147,19 @@ void LayeredSettingsInterface::SetStringValue(const char* section, const char* k
pxFailRel("Attempt to call SetStringValue() on layered settings interface"); pxFailRel("Attempt to call SetStringValue() on layered settings interface");
} }
bool LayeredSettingsInterface::ContainsValue(const char* section, const char* key) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->ContainsValue(key, section))
return true;
}
}
return false;
}
void LayeredSettingsInterface::DeleteValue(const char* section, const char* key) void LayeredSettingsInterface::DeleteValue(const char* section, const char* key)
{ {
pxFailRel("Attempt to call DeleteValue() on layered settings interface"); pxFailRel("Attempt to call DeleteValue() on layered settings interface");
@ -157,7 +170,7 @@ void LayeredSettingsInterface::ClearSection(const char* section)
pxFailRel("Attempt to call ClearSection() on layered settings interface"); pxFailRel("Attempt to call ClearSection() on layered settings interface");
} }
std::vector<std::string> LayeredSettingsInterface::GetStringList(const char* section, const char* key) std::vector<std::string> LayeredSettingsInterface::GetStringList(const char* section, const char* key) const
{ {
std::vector<std::string> ret; std::vector<std::string> ret;

View File

@ -52,10 +52,11 @@ public:
void SetDoubleValue(const char* section, const char* key, double value) override; void SetDoubleValue(const char* section, const char* key, double value) override;
void SetBoolValue(const char* section, const char* key, bool value) override; void SetBoolValue(const char* section, const char* key, bool value) override;
void SetStringValue(const char* section, const char* key, const char* value) override; void SetStringValue(const char* section, const char* key, const char* value) override;
bool ContainsValue(const char* section, const char* key) const override;
void DeleteValue(const char* section, const char* key) override; void DeleteValue(const char* section, const char* key) override;
void ClearSection(const char* section) override; void ClearSection(const char* section) override;
std::vector<std::string> GetStringList(const char* section, const char* key) override; std::vector<std::string> GetStringList(const char* section, const char* key) const override;
void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) override; void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) override;
bool RemoveFromStringList(const char* section, const char* key, const char* item) override; bool RemoveFromStringList(const char* section, const char* key, const char* item) override;
bool AddToStringList(const char* section, const char* key, const char* item) override; bool AddToStringList(const char* section, const char* key, const char* item) override;

View File

@ -32,6 +32,12 @@ SettingsInterface* Host::GetSettingsInterface()
return &s_layered_settings_interface; return &s_layered_settings_interface;
} }
SettingsInterface* Host::GetSettingsInterfaceForBindings()
{
SettingsInterface* input_layer = s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_INPUT);
return input_layer ? input_layer : &s_layered_settings_interface;
}
std::string Host::GetBaseStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) std::string Host::GetBaseStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
{ {
std::unique_lock lock(s_settings_mutex); std::unique_lock lock(s_settings_mutex);
@ -169,6 +175,16 @@ SettingsInterface* Host::Internal::GetBaseSettingsLayer()
return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE); return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE);
} }
SettingsInterface* Host::Internal::GetGameSettingsLayer()
{
return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_GAME);
}
SettingsInterface* Host::Internal::GetInputSettingsLayer()
{
return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_INPUT);
}
void Host::Internal::SetBaseSettingsLayer(SettingsInterface* sif) void Host::Internal::SetBaseSettingsLayer(SettingsInterface* sif)
{ {
pxAssertRel(s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE) == nullptr, "Base layer has not been set"); pxAssertRel(s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE) == nullptr, "Base layer has not been set");

View File

@ -56,11 +56,21 @@ namespace Host
std::unique_lock<std::mutex> GetSettingsLock(); std::unique_lock<std::mutex> GetSettingsLock();
SettingsInterface* GetSettingsInterface(); SettingsInterface* GetSettingsInterface();
/// Returns the settings interface that controller bindings should be loaded from.
/// If an input profile is being used, this will be the input layer, otherwise the layered interface.
SettingsInterface* GetSettingsInterfaceForBindings();
namespace Internal namespace Internal
{ {
/// Retrieves the base settings layer. Must call with lock held. /// Retrieves the base settings layer. Must call with lock held.
SettingsInterface* GetBaseSettingsLayer(); SettingsInterface* GetBaseSettingsLayer();
/// Retrieves the game settings layer, if present. Must call with lock held.
SettingsInterface* GetGameSettingsLayer();
/// Retrieves the input settings layer, if present. Must call with lock held.
SettingsInterface* GetInputSettingsLayer();
/// Sets the base settings layer. Should be called by the host at initialization time. /// Sets the base settings layer. Should be called by the host at initialization time.
void SetBaseSettingsLayer(SettingsInterface* sif); void SetBaseSettingsLayer(SettingsInterface* sif);

View File

@ -141,7 +141,7 @@ void wxSettingsInterface::SetStringValue(const char* section, const char* key, c
m_config->Write(key, wxString::FromUTF8(value)); m_config->Write(key, wxString::FromUTF8(value));
} }
std::vector<std::string> wxSettingsInterface::GetStringList(const char* section, const char* key) std::vector<std::string> wxSettingsInterface::GetStringList(const char* section, const char* key) const
{ {
pxFailRel("Not implemented"); pxFailRel("Not implemented");
return {}; return {};
@ -163,6 +163,12 @@ bool wxSettingsInterface::AddToStringList(const char* section, const char* key,
return false; return false;
} }
bool wxSettingsInterface::ContainsValue(const char* section, const char* key) const
{
CheckPath(section);
return m_config->Exists(key);
}
void wxSettingsInterface::DeleteValue(const char* section, const char* key) void wxSettingsInterface::DeleteValue(const char* section, const char* key)
{ {
CheckPath(section); CheckPath(section);

View File

@ -44,11 +44,12 @@ public:
void SetStringValue(const char* section, const char* key, const char* value) override; void SetStringValue(const char* section, const char* key, const char* value) override;
std::vector<std::string> GetStringList(const char* section, const char* key) override; std::vector<std::string> GetStringList(const char* section, const char* key) const override;
void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) override; void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) override;
bool RemoveFromStringList(const char* section, const char* key, const char* item) override; bool RemoveFromStringList(const char* section, const char* key, const char* item) override;
bool AddToStringList(const char* section, const char* key, const char* item) override; bool AddToStringList(const char* section, const char* key, const char* item) override;
bool ContainsValue(const char* section, const char* key) const override;
void DeleteValue(const char* section, const char* key) override; void DeleteValue(const char* section, const char* key) override;
void ClearSection(const char* section) override; void ClearSection(const char* section) override;