| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <functional> |
| 7 |
#include <limits> |
| 8 |
#include <memory> |
| 9 |
#include <optional> |
| 10 |
#include <string> |
| 11 |
|
| 12 |
#include "Common/Config/ConfigInfo.h" |
| 13 |
#include "Common/Config/Enums.h" |
| 14 |
#include "Common/Config/Layer.h" |
| 15 |
|
| 16 |
namespace Config |
| 17 |
{ |
| 18 |
struct ConfigChangedCallbackID |
| 19 |
{ |
| 20 |
size_t id = std::numeric_limits<size_t>::max(); |
| 21 |
|
| 22 |
bool operator==(const ConfigChangedCallbackID&) const = default; |
| 23 |
}; |
| 24 |
|
| 25 |
using ConfigChangedCallback = std::function<void()>; |
| 26 |
|
| 27 |
|
| 28 |
void AddLayer(std::unique_ptr<ConfigLayerLoader> loader); |
| 29 |
std::shared_ptr<Layer> GetLayer(LayerType layer); |
| 30 |
void RemoveLayer(LayerType layer); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
[[nodiscard]] ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func); |
| 35 |
void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id); |
| 36 |
void OnConfigChanged(); |
| 37 |
|
| 38 |
|
| 39 |
u64 GetConfigVersion(); |
| 40 |
|
| 41 |
|
| 42 |
void Load(); |
| 43 |
void Save(); |
| 44 |
|
| 45 |
void Init(); |
| 46 |
void Shutdown(); |
| 47 |
void ClearCurrentRunLayer(); |
| 48 |
|
| 49 |
const std::string& GetSystemName(System system); |
| 50 |
std::optional<System> GetSystemFromName(const std::string& system); |
| 51 |
const std::string& GetLayerName(LayerType layer); |
| 52 |
LayerType GetActiveLayerForConfig(const Location&); |
| 53 |
|
| 54 |
std::optional<std::string> GetAsString(const Location&); |
| 55 |
|
| 56 |
template <typename T> |
| 57 |
T Get(LayerType layer, const Info<T>& info) |
| 58 |
{ |
| 59 |
if (layer == LayerType::Meta) |
| 60 |
return Get(info); |
| 61 |
return GetLayer(layer)->Get(info); |
| 62 |
} |
| 63 |
|
| 64 |
template <typename T> |
| 65 |
T Get(const Layer* game_layer, const Info<T>& setting) |
| 66 |
{ |
| 67 |
if (game_layer != nullptr) |
| 68 |
return game_layer->Get(setting); |
| 69 |
return Get(setting); |
| 70 |
} |
| 71 |
|
| 72 |
template <typename T> |
| 73 |
T Get(const Info<T>& info) |
| 74 |
{ |
| 75 |
CachedValue<T> cached = info.GetCachedValue(); |
| 76 |
const u64 config_version = GetConfigVersion(); |
| 77 |
|
| 78 |
if (cached.config_version < config_version) |
| 79 |
{ |
| 80 |
cached.value = GetUncached(info); |
| 81 |
cached.config_version = config_version; |
| 82 |
|
| 83 |
info.TryToSetCachedValue(cached); |
| 84 |
} |
| 85 |
|
| 86 |
return std::move(cached.value); |
| 87 |
} |
| 88 |
|
| 89 |
template <typename T> |
| 90 |
T GetUncached(const Info<T>& info) |
| 91 |
{ |
| 92 |
const std::optional<std::string> str = GetAsString(info.GetLocation()); |
| 93 |
if (!str) |
| 94 |
return info.GetDefaultValue(); |
| 95 |
|
| 96 |
return detail::TryParse<T>(*str).value_or(info.GetDefaultValue()); |
| 97 |
} |
| 98 |
|
| 99 |
template <typename T> |
| 100 |
T GetBase(const Info<T>& info) |
| 101 |
{ |
| 102 |
return Get(LayerType::Base, info); |
| 103 |
} |
| 104 |
|
| 105 |
template <typename T> |
| 106 |
LayerType GetActiveLayerForConfig(const Info<T>& info) |
| 107 |
{ |
| 108 |
return GetActiveLayerForConfig(info.GetLocation()); |
| 109 |
} |
| 110 |
|
| 111 |
template <typename InfoT, typename ValueT> |
| 112 |
void Set(LayerType layer, const InfoT& info, const ValueT& value) |
| 113 |
{ |
| 114 |
if (GetLayer(layer)->Set(info, value)) |
| 115 |
OnConfigChanged(); |
| 116 |
} |
| 117 |
|
| 118 |
template <typename InfoT, typename ValueT> |
| 119 |
void SetBase(const Info<InfoT>& info, const ValueT& value) |
| 120 |
{ |
| 121 |
Set(LayerType::Base, info, value); |
| 122 |
} |
| 123 |
|
| 124 |
template <typename InfoT, typename ValueT> |
| 125 |
void SetCurrent(const Info<InfoT>& info, const ValueT& value) |
| 126 |
{ |
| 127 |
Set(LayerType::CurrentRun, info, value); |
| 128 |
} |
| 129 |
|
| 130 |
template <typename InfoT, typename ValueT> |
| 131 |
void SetBaseOrCurrent(const Info<InfoT>& info, const ValueT& value) |
| 132 |
{ |
| 133 |
if (GetActiveLayerForConfig(info) == LayerType::Base) |
| 134 |
Set(LayerType::Base, info, value); |
| 135 |
else |
| 136 |
Set(LayerType::CurrentRun, info, value); |
| 137 |
} |
| 138 |
|
| 139 |
template <typename T> |
| 140 |
void DeleteKey(LayerType layer, const Info<T>& info) |
| 141 |
{ |
| 142 |
if (GetLayer(layer)->DeleteKey(info.GetLocation())) |
| 143 |
OnConfigChanged(); |
| 144 |
} |
| 145 |
|
| 146 |
template <typename T> |
| 147 |
bool IsDefaultValue(const Info<T>& info) |
| 148 |
{ |
| 149 |
return Get(info) == info.GetDefaultValue(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
class ConfigChangeCallbackGuard |
| 154 |
{ |
| 155 |
public: |
| 156 |
ConfigChangeCallbackGuard(); |
| 157 |
~ConfigChangeCallbackGuard(); |
| 158 |
|
| 159 |
ConfigChangeCallbackGuard(const ConfigChangeCallbackGuard&) = delete; |
| 160 |
ConfigChangeCallbackGuard& operator=(const ConfigChangeCallbackGuard&) = delete; |
| 161 |
}; |
| 162 |
} |