SDL: Only save settings when changes have been made

This commit is contained in:
Connor McLaughlin 2020-02-16 00:14:42 +09:00
parent 37d765e8d7
commit b0c846388e
2 changed files with 15 additions and 3 deletions

View File

@ -11,11 +11,14 @@ SDLSettingsInterface::SDLSettingsInterface(const char* filename) : m_filename(fi
}
SDLSettingsInterface::~SDLSettingsInterface()
{
if (m_dirty)
{
SI_Error err = m_ini.SaveFile(m_filename.c_str(), false);
if (err != SI_OK)
Log_WarningPrintf("Failed to save settings to '%s'.", m_filename.c_str());
}
}
int SDLSettingsInterface::GetIntValue(const char* section, const char* key, int default_value /*= 0*/)
{
@ -40,26 +43,31 @@ std::string SDLSettingsInterface::GetStringValue(const char* section, const char
void SDLSettingsInterface::SetIntValue(const char* section, const char* key, int value)
{
m_dirty = true;
m_ini.SetLongValue(section, key, static_cast<long>(value), nullptr, false, true);
}
void SDLSettingsInterface::SetFloatValue(const char* section, const char* key, float value)
{
m_dirty = true;
m_ini.SetDoubleValue(section, key, static_cast<double>(value), nullptr, true);
}
void SDLSettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
{
m_dirty = true;
m_ini.SetBoolValue(section, key, value, nullptr, true);
}
void SDLSettingsInterface::SetStringValue(const char* section, const char* key, const char* value)
{
m_dirty = true;
m_ini.SetValue(section, key, value, nullptr, true);
}
void SDLSettingsInterface::DeleteValue(const char* section, const char* key)
{
m_dirty = true;
m_ini.Delete(section, key);
}
@ -79,6 +87,7 @@ std::vector<std::string> SDLSettingsInterface::GetStringList(const char* section
void SDLSettingsInterface::SetStringList(const char* section, const char* key,
const std::vector<std::string_view>& items)
{
m_dirty = true;
m_ini.Delete(section, key);
for (const std::string_view& sv : items)
@ -87,6 +96,7 @@ void SDLSettingsInterface::SetStringList(const char* section, const char* key,
bool SDLSettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item)
{
m_dirty = true;
return m_ini.DeleteValue(section, key, item, true);
}
@ -100,6 +110,7 @@ bool SDLSettingsInterface::AddToStringList(const char* section, const char* key,
return false;
}
m_dirty = true;
m_ini.SetValue(section, key, item, nullptr, false);
return true;
}

View File

@ -28,4 +28,5 @@ public:
private:
std::string m_filename;
CSimpleIniA m_ini;
bool m_dirty = false;
};