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