Backport SettingsInterface

This commit is contained in:
Connor McLaughlin
2022-07-09 00:14:48 +10:00
parent 0c0bd4c995
commit 6a6ab6529a
13 changed files with 749 additions and 113 deletions

View File

@ -2866,10 +2866,9 @@ void CommonHostInterface::ClearAllControllerBindings()
bool CommonHostInterface::ApplyInputProfile(const char* profile_path)
{
if (!FileSystem::FileExists(profile_path))
return false;
INISettingsInterface profile(profile_path);
if (!profile.Load())
return false;
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
@ -2966,7 +2965,6 @@ bool CommonHostInterface::SaveInputProfile(const char* profile_path)
Log_WarningPrintf("Input profile at '%s' does not exist, new input profile will be created", profile_path);
INISettingsInterface profile(profile_path);
profile.Clear();
for (u32 controller_index = 1; controller_index <= NUM_CONTROLLER_AND_CARD_PORTS; controller_index++)
{
@ -3309,6 +3307,10 @@ void CommonHostInterface::LoadSettings()
#ifndef __ANDROID__
// we don't check the settings version on android, because it's not using the ini yet..
// we can re-enable this once we move it over.. eventually.
const bool loaded = static_cast<INISettingsInterface*>(m_settings_interface.get())->Load();
if (!loaded)
SetDefaultSettings(*m_settings_interface);
const int settings_version = m_settings_interface->GetIntValue("Main", "SettingsVersion", -1);
if (settings_version != SETTINGS_VERSION)
{
@ -3876,6 +3878,13 @@ bool CommonHostInterface::UpdateControllerInputMapFromGameSettings()
}
INISettingsInterface si(std::move(path));
if (!si.Load())
{
AddFormattedOSDMessage(10.0f, TranslateString("OSDMessage", "Input profile '%s' cannot be loaded."),
gs->input_profile_name.c_str());
return false;
}
UpdateControllerInputMap(si);
return true;
}

View File

@ -1,24 +1,28 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "ini_settings_interface.h"
#include "common/file_system.h"
#include "common/log.h"
#include "common/string_util.h"
#include <algorithm>
#include <iterator>
Log_SetChannel(INISettingsInterface);
INISettingsInterface::INISettingsInterface(std::string filename) : m_filename(std::move(filename)), m_ini(true, true)
{
SI_Error err = SI_FAIL;
std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "rb");
if (fp)
{
err = m_ini.LoadFile(fp);
std::fclose(fp);
}
if (err != SI_OK)
Log_WarningPrintf("Settings could not be loaded from '%s', defaults will be used.", m_filename.c_str());
}
INISettingsInterface::INISettingsInterface(std::string filename) : m_filename(std::move(filename)), m_ini(true, true) {}
INISettingsInterface::~INISettingsInterface()
{
@ -26,8 +30,24 @@ INISettingsInterface::~INISettingsInterface()
Save();
}
bool INISettingsInterface::Load()
{
if (m_filename.empty())
return false;
SI_Error err = SI_FAIL;
auto fp = FileSystem::OpenManagedCFile(m_filename.c_str(), "rb");
if (fp)
err = m_ini.LoadFile(fp.get());
return (err == SI_OK);
}
bool INISettingsInterface::Save()
{
if (m_filename.empty())
return false;
SI_Error err = SI_FAIL;
std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "wb");
if (fp)
@ -51,25 +71,84 @@ void INISettingsInterface::Clear()
m_ini.Reset();
}
int INISettingsInterface::GetIntValue(const char* section, const char* key, int default_value /*= 0*/)
bool INISettingsInterface::GetIntValue(const char* section, const char* key, s32* value) const
{
return static_cast<int>(m_ini.GetLongValue(section, key, default_value));
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
std::optional<s32> parsed_value = StringUtil::FromChars<s32>(str_value, 10);
if (!parsed_value.has_value())
return false;
*value = parsed_value.value();
return true;
}
float INISettingsInterface::GetFloatValue(const char* section, const char* key, float default_value /*= 0.0f*/)
bool INISettingsInterface::GetUIntValue(const char* section, const char* key, u32* value) const
{
return static_cast<float>(m_ini.GetDoubleValue(section, key, default_value));
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
std::optional<u32> parsed_value = StringUtil::FromChars<u32>(str_value, 10);
if (!parsed_value.has_value())
return false;
*value = parsed_value.value();
return true;
}
bool INISettingsInterface::GetBoolValue(const char* section, const char* key, bool default_value /*= false*/)
bool INISettingsInterface::GetFloatValue(const char* section, const char* key, float* value) const
{
return m_ini.GetBoolValue(section, key, default_value);
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
std::optional<float> parsed_value = StringUtil::FromChars<float>(str_value);
if (!parsed_value.has_value())
return false;
*value = parsed_value.value();
return true;
}
std::string INISettingsInterface::GetStringValue(const char* section, const char* key,
const char* default_value /*= ""*/)
bool INISettingsInterface::GetDoubleValue(const char* section, const char* key, double* value) const
{
return m_ini.GetValue(section, key, default_value);
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
std::optional<double> parsed_value = StringUtil::FromChars<double>(str_value);
if (!parsed_value.has_value())
return false;
*value = parsed_value.value();
return true;
}
bool INISettingsInterface::GetBoolValue(const char* section, const char* key, bool* value) const
{
const char* str_value = m_ini.GetValue(section, key);
if (!str_value)
return false;
std::optional<bool> parsed_value = StringUtil::FromChars<bool>(str_value);
if (!parsed_value.has_value())
return false;
*value = parsed_value.value();
return true;
}
bool INISettingsInterface::GetStringValue(const char* section, const char* key, std::string* 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)
@ -78,12 +157,24 @@ void INISettingsInterface::SetIntValue(const char* section, const char* key, int
m_ini.SetLongValue(section, key, static_cast<long>(value), nullptr, false, true);
}
void INISettingsInterface::SetUIntValue(const char* section, const char* key, u32 value)
{
m_dirty = true;
m_ini.SetLongValue(section, key, static_cast<long>(value), nullptr, false, true);
}
void INISettingsInterface::SetFloatValue(const char* section, const char* key, float value)
{
m_dirty = true;
m_ini.SetDoubleValue(section, key, static_cast<double>(value), nullptr, true);
}
void INISettingsInterface::SetDoubleValue(const char* section, const char* key, double value)
{
m_dirty = true;
m_ini.SetDoubleValue(section, key, value, nullptr, true);
}
void INISettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
{
m_dirty = true;
@ -96,6 +187,11 @@ void INISettingsInterface::SetStringValue(const char* section, const char* key,
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)
{
m_dirty = true;
@ -109,7 +205,7 @@ void INISettingsInterface::ClearSection(const char* section)
m_ini.SetValue(section, nullptr, nullptr);
}
std::vector<std::string> INISettingsInterface::GetStringList(const char* section, const char* key)
std::vector<std::string> INISettingsInterface::GetStringList(const char* section, const char* key) const
{
std::list<CSimpleIniA::Entry> entries;
if (!m_ini.GetAllValues(section, key, entries))
@ -117,8 +213,8 @@ std::vector<std::string> INISettingsInterface::GetStringList(const char* section
std::vector<std::string> ret;
ret.reserve(entries.size());
std::transform(entries.begin(), entries.end(), std::back_inserter(ret),
[](const CSimpleIniA::Entry& it) { return std::string(it.pItem); });
for (const CSimpleIniA::Entry& entry : entries)
ret.emplace_back(entry.pItem);
return ret;
}

View File

@ -1,5 +1,20 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "core/settings.h"
#include "common/settings_interface.h"
// being a pain here...
#ifdef _WIN32
@ -13,27 +28,43 @@ public:
INISettingsInterface(std::string filename);
~INISettingsInterface() override;
const std::string& GetFileName() const { return m_filename; }
bool Load();
bool Save() override;
void Clear() override;
int GetIntValue(const char* section, const char* key, int default_value = 0) override;
float GetFloatValue(const char* section, const char* key, float default_value = 0.0f) override;
bool GetBoolValue(const char* section, const char* key, bool default_value = false) override;
std::string GetStringValue(const char* section, const char* key, const char* default_value = "") override;
bool GetIntValue(const char* section, const char* key, s32* value) const override;
bool GetUIntValue(const char* section, const char* key, u32* value) const override;
bool GetFloatValue(const char* section, const char* key, float* value) const override;
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;
void SetIntValue(const char* section, const char* key, int value) override;
void SetIntValue(const char* section, const char* key, s32 value) override;
void SetUIntValue(const char* section, const char* key, u32 value) override;
void SetFloatValue(const char* section, const char* key, float 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 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 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;
bool RemoveFromStringList(const char* section, const char* key, const char* item) override;
bool AddToStringList(const char* section, const char* key, const char* item) override;
// default parameter overloads
using SettingsInterface::GetBoolValue;
using SettingsInterface::GetDoubleValue;
using SettingsInterface::GetFloatValue;
using SettingsInterface::GetIntValue;
using SettingsInterface::GetStringValue;
using SettingsInterface::GetUIntValue;
private:
std::string m_filename;
CSimpleIniA m_ini;