2017-02-16 15:58:40 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-02-16 15:58:40 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2017-11-26 17:24:01 +00:00
|
|
|
#include <optional>
|
2017-02-16 15:58:40 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-10-30 16:22:37 +00:00
|
|
|
#include "Common/Config/ConfigInfo.h"
|
2017-02-16 15:58:40 +00:00
|
|
|
#include "Common/Config/Enums.h"
|
|
|
|
#include "Common/Config/Layer.h"
|
|
|
|
|
|
|
|
namespace Config
|
|
|
|
{
|
2023-08-16 19:37:12 +00:00
|
|
|
struct ConfigChangedCallbackID
|
|
|
|
{
|
|
|
|
size_t id = -1;
|
|
|
|
|
|
|
|
bool operator==(const ConfigChangedCallbackID&) const = default;
|
|
|
|
bool operator!=(const ConfigChangedCallbackID&) const = default;
|
|
|
|
};
|
|
|
|
|
2017-02-16 15:58:40 +00:00
|
|
|
using ConfigChangedCallback = std::function<void()>;
|
|
|
|
|
|
|
|
// Layer management
|
|
|
|
void AddLayer(std::unique_ptr<ConfigLayerLoader> loader);
|
2019-07-30 14:40:52 +00:00
|
|
|
std::shared_ptr<Layer> GetLayer(LayerType layer);
|
2017-02-16 15:58:40 +00:00
|
|
|
void RemoveLayer(LayerType layer);
|
|
|
|
|
2023-08-16 19:16:41 +00:00
|
|
|
// Returns an ID that can be passed to RemoveConfigChangedCallback().
|
|
|
|
// The callback may be called from any thread.
|
2023-08-16 19:37:12 +00:00
|
|
|
ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func);
|
|
|
|
void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id);
|
2020-12-05 17:24:41 +00:00
|
|
|
void OnConfigChanged();
|
|
|
|
|
|
|
|
// Returns the number of times the config has changed in the current execution of the program
|
|
|
|
u64 GetConfigVersion();
|
2017-02-16 15:58:40 +00:00
|
|
|
|
|
|
|
// Explicit load and save of layers
|
|
|
|
void Load();
|
|
|
|
void Save();
|
|
|
|
|
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
2017-05-13 22:34:49 +00:00
|
|
|
void ClearCurrentRunLayer();
|
2017-02-16 15:58:40 +00:00
|
|
|
|
|
|
|
const std::string& GetSystemName(System system);
|
2017-11-26 17:24:01 +00:00
|
|
|
std::optional<System> GetSystemFromName(const std::string& system);
|
2017-02-16 15:58:40 +00:00
|
|
|
const std::string& GetLayerName(LayerType layer);
|
2020-05-02 12:39:40 +00:00
|
|
|
LayerType GetActiveLayerForConfig(const Location&);
|
2017-07-09 23:17:36 +00:00
|
|
|
|
2020-11-25 15:26:13 +00:00
|
|
|
std::optional<std::string> GetAsString(const Location&);
|
|
|
|
|
2017-07-09 23:17:36 +00:00
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
T Get(LayerType layer, const Info<T>& info)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
2017-10-29 19:17:58 +00:00
|
|
|
if (layer == LayerType::Meta)
|
|
|
|
return Get(info);
|
2017-08-02 17:20:52 +00:00
|
|
|
return GetLayer(layer)->Get(info);
|
2017-07-09 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
T Get(const Info<T>& info)
|
2020-12-05 17:24:41 +00:00
|
|
|
{
|
|
|
|
CachedValue<T> cached = info.GetCachedValue();
|
|
|
|
const u64 config_version = GetConfigVersion();
|
|
|
|
|
|
|
|
if (cached.config_version < config_version)
|
|
|
|
{
|
|
|
|
cached.value = GetUncached(info);
|
|
|
|
cached.config_version = config_version;
|
|
|
|
|
|
|
|
info.SetCachedValue(cached);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cached.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T GetUncached(const Info<T>& info)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
2020-09-20 11:58:17 +00:00
|
|
|
const std::optional<std::string> str = GetAsString(info.GetLocation());
|
2020-11-25 15:26:13 +00:00
|
|
|
if (!str)
|
2020-09-20 11:58:17 +00:00
|
|
|
return info.GetDefaultValue();
|
2020-11-25 15:26:13 +00:00
|
|
|
|
2020-09-20 11:58:17 +00:00
|
|
|
return detail::TryParse<T>(*str).value_or(info.GetDefaultValue());
|
2017-07-09 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
T GetBase(const Info<T>& info)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
|
|
|
return Get(LayerType::Base, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
LayerType GetActiveLayerForConfig(const Info<T>& info)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
2020-09-20 11:58:17 +00:00
|
|
|
return GetActiveLayerForConfig(info.GetLocation());
|
2017-07-09 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
void Set(LayerType layer, const Info<T>& info, const std::common_type_t<T>& value)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
2021-02-25 23:14:00 +00:00
|
|
|
if (GetLayer(layer)->Set(info, value))
|
|
|
|
OnConfigChanged();
|
2017-07-09 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
void SetBase(const Info<T>& info, const std::common_type_t<T>& value)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
|
|
|
Set<T>(LayerType::Base, info, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
void SetCurrent(const Info<T>& info, const std::common_type_t<T>& value)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
|
|
|
Set<T>(LayerType::CurrentRun, info, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-05-02 12:39:40 +00:00
|
|
|
void SetBaseOrCurrent(const Info<T>& info, const std::common_type_t<T>& value)
|
2017-07-09 23:17:36 +00:00
|
|
|
{
|
|
|
|
if (GetActiveLayerForConfig(info) == LayerType::Base)
|
|
|
|
Set<T>(LayerType::Base, info, value);
|
|
|
|
else
|
|
|
|
Set<T>(LayerType::CurrentRun, info, value);
|
|
|
|
}
|
2019-03-03 16:58:37 +00:00
|
|
|
|
2022-09-24 11:03:45 +00:00
|
|
|
template <typename T>
|
|
|
|
void DeleteKey(LayerType layer, const Info<T>& info)
|
|
|
|
{
|
|
|
|
if (GetLayer(layer)->DeleteKey(info.GetLocation()))
|
|
|
|
OnConfigChanged();
|
|
|
|
}
|
|
|
|
|
2020-12-05 17:24:41 +00:00
|
|
|
// Used to defer OnConfigChanged until after the completion of many config changes.
|
2019-03-03 16:58:37 +00:00
|
|
|
class ConfigChangeCallbackGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConfigChangeCallbackGuard();
|
|
|
|
~ConfigChangeCallbackGuard();
|
|
|
|
|
|
|
|
ConfigChangeCallbackGuard(const ConfigChangeCallbackGuard&) = delete;
|
|
|
|
ConfigChangeCallbackGuard& operator=(const ConfigChangeCallbackGuard&) = delete;
|
|
|
|
};
|
|
|
|
} // namespace Config
|