| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Config/Config.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <atomic> |
| 8 | #include <map> |
| 9 | #include <mutex> |
| 10 | #include <shared_mutex> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "Common/Projection.h" |
| 15 | |
| 16 | namespace Config |
| 17 | { |
| 18 | using Layers = std::map<LayerType, std::shared_ptr<Layer>>; |
| 19 | |
| 20 | static Layers s_layers; |
| 21 | static std::vector<std::pair<ConfigChangedCallbackID, ConfigChangedCallback>> s_callbacks; |
| 22 | static size_t s_next_callback_id = 0; |
| 23 | static std::atomic<u32> s_callback_guards = 0; |
| 24 | static std::atomic<u64> s_config_version = 0; |
| 25 | |
| 26 | static std::shared_mutex s_layers_rw_lock; |
| 27 | static std::recursive_mutex s_callbacks_lock; |
| 28 | |
| 29 | using ReadLock = std::shared_lock<std::shared_mutex>; |
| 30 | using WriteLock = std::unique_lock<std::shared_mutex>; |
| 31 | |
| 32 | static void AddLayerInternal(std::shared_ptr<Layer> layer) |
| 33 | { |
| 34 | { |
| 35 | WriteLock lock(s_layers_rw_lock); |
| 36 | |
| 37 | const Config::LayerType layer_type = layer->GetLayer(); |
| 38 | s_layers.insert_or_assign(layer_type, std::move(layer)); |
| 39 | } |
| 40 | OnConfigChanged(); |
| 41 | } |
| 42 | |
| 43 | void AddLayer(std::unique_ptr<ConfigLayerLoader> loader) |
| 44 | { |
| 45 | AddLayerInternal(std::make_shared<Layer>(std::move(loader))); |
| 46 | } |
| 47 | |
| 48 | std::shared_ptr<Layer> GetLayer(LayerType layer) |
| 49 | { |
| 50 | ReadLock lock(s_layers_rw_lock); |
| 51 | |
| 52 | std::shared_ptr<Layer> result; |
| 53 | const auto it = s_layers.find(layer); |
| 54 | if (it != s_layers.end()) |
| 55 | { |
| 56 | result = it->second; |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | void RemoveLayer(LayerType layer) |
| 62 | { |
| 63 | { |
| 64 | WriteLock lock(s_layers_rw_lock); |
| 65 | |
| 66 | s_layers.erase(layer); |
| 67 | } |
| 68 | OnConfigChanged(); |
| 69 | } |
| 70 | |
| 71 | ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func) |
| 72 | { |
| 73 | std::lock_guard lock(s_callbacks_lock); |
| 74 | const ConfigChangedCallbackID callback_id{s_next_callback_id}; |
| 75 | ++s_next_callback_id; |
| 76 | s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func))); |
| 77 | return callback_id; |
| 78 | } |
| 79 | |
| 80 | void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id) |
| 81 | { |
| 82 | std::lock_guard lock(s_callbacks_lock); |
| 83 | for (auto it = s_callbacks.begin(); it != s_callbacks.end(); ++it) |
| 84 | { |
| 85 | if (it->first == callback_id) |
| 86 | { |
| 87 | s_callbacks.erase(it); |
| 88 | return; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void OnConfigChanged() |
| 94 | { |
| 95 | // Increment the config version to invalidate caches. |
| 96 | // To ensure that getters do not return stale data, this should always be done |
| 97 | // even when callbacks are suppressed. |
| 98 | s_config_version.fetch_add(1, std::memory_order_relaxed); |
| 99 | |
| 100 | if (s_callback_guards) |
| 101 | return; |
| 102 | |
| 103 | std::lock_guard lock(s_callbacks_lock); |
| 104 | |
| 105 | for (const auto& callback : s_callbacks) |
| 106 | callback.second(); |
| 107 | } |
| 108 | |
| 109 | u64 GetConfigVersion() |
| 110 | { |
| 111 | return s_config_version.load(std::memory_order_relaxed); |
| 112 | } |
| 113 | |
| 114 | // Explicit load and save of layers |
| 115 | void Load() |
| 116 | { |
| 117 | { |
| 118 | ReadLock lock(s_layers_rw_lock); |
| 119 | |
| 120 | for (auto& layer : s_layers) |
| 121 | layer.second->Load(); |
| 122 | } |
| 123 | OnConfigChanged(); |
| 124 | } |
| 125 | |
| 126 | void Save() |
| 127 | { |
| 128 | { |
| 129 | ReadLock lock(s_layers_rw_lock); |
| 130 | |
| 131 | for (auto& layer : s_layers) |
| 132 | layer.second->Save(); |
| 133 | } |
| 134 | OnConfigChanged(); |
| 135 | } |
| 136 | |
| 137 | void Init() |
| 138 | { |
| 139 | // These layers contain temporary values |
| 140 | ClearCurrentRunLayer(); |
| 141 | } |
| 142 | |
| 143 | void Shutdown() |
| 144 | { |
| 145 | WriteLock lock(s_layers_rw_lock); |
| 146 | |
| 147 | s_layers.clear(); |
| 148 | } |
| 149 | |
| 150 | void ClearCurrentRunLayer() |
| 151 | { |
| 152 | WriteLock lock(s_layers_rw_lock); |
| 153 | |
| 154 | s_layers.insert_or_assign(LayerType::CurrentRun, std::make_shared<Layer>(LayerType::CurrentRun)); |
| 155 | } |
| 156 | |
| 157 | static const std::map<System, std::string> system_to_name = { |
| 158 | {System::Main, "Dolphin"}, |
| 159 | {System::GCPad, "GCPad"}, |
| 160 | {System::WiiPad, "Wiimote"}, |
| 161 | {System::GCKeyboard, "GCKeyboard"}, |
| 162 | {System::GFX, "Graphics"}, |
| 163 | {System::Logger, "Logger"}, |
| 164 | {System::SYSCONF, "SYSCONF"}, |
| 165 | {System::DualShockUDPClient, "DualShockUDPClient"}, |
| 166 | {System::FreeLook, "FreeLook"}, |
| 167 | {System::Session, "Session"}, |
| 168 | {System::GameSettingsOnly, "GameSettingsOnly"}, |
| 169 | {System::Achievements, "Achievements"}}; |
| 170 | |
| 171 | const std::string& GetSystemName(System system) |
| 172 | { |
| 173 | return system_to_name.at(system); |
| 174 | } |
| 175 | |
| 176 | std::optional<System> GetSystemFromName(const std::string& name) |
| 177 | { |
| 178 | const auto system = std::ranges::find(system_to_name, name, Common::Projection::Value{}); |
| 179 | if (system != system_to_name.end()) |
| 180 | return system->first; |
| 181 | |
| 182 | return {}; |
| 183 | } |
| 184 | |
| 185 | const std::string& GetLayerName(LayerType layer) |
| 186 | { |
| 187 | static const std::map<LayerType, std::string> layer_to_name = { |
| 188 | {LayerType::Base, "Base"}, |
| 189 | {LayerType::GlobalGame, "Global GameINI"}, |
| 190 | {LayerType::LocalGame, "Local GameINI"}, |
| 191 | {LayerType::Netplay, "Netplay"}, |
| 192 | {LayerType::Movie, "Movie"}, |
| 193 | {LayerType::CommandLine, "Command Line"}, |
| 194 | {LayerType::CurrentRun, "Current Run"}, |
| 195 | }; |
| 196 | return layer_to_name.at(layer); |
| 197 | } |
| 198 | |
| 199 | LayerType GetActiveLayerForConfig(const Location& config) |
| 200 | { |
| 201 | ReadLock lock(s_layers_rw_lock); |
| 202 | |
| 203 | for (auto layer : SEARCH_ORDER) |
| 204 | { |
| 205 | const auto it = s_layers.find(layer); |
| 206 | if (it != s_layers.end()) |
| 207 | { |
| 208 | if (it->second->Exists(config)) |
| 209 | return layer; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // If config is not present in any layer, base layer is considered active. |
| 214 | return LayerType::Base; |
| 215 | } |
| 216 | |
| 217 | std::optional<std::string> GetAsString(const Location& config) |
| 218 | { |
| 219 | std::optional<std::string> result; |
| 220 | ReadLock lock(s_layers_rw_lock); |
| 221 | |
| 222 | for (auto layer : SEARCH_ORDER) |
| 223 | { |
| 224 | const auto it = s_layers.find(layer); |
| 225 | if (it != s_layers.end()) |
| 226 | { |
| 227 | result = it->second->Get<std::string>(config); |
| 228 | if (result.has_value()) |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | ConfigChangeCallbackGuard::ConfigChangeCallbackGuard() |
| 237 | { |
| 238 | ++s_callback_guards; |
| 239 | } |
| 240 | |
| 241 | ConfigChangeCallbackGuard::~ConfigChangeCallbackGuard() |
| 242 | { |
| 243 | if (--s_callback_guards) |
| 244 | return; |
| 245 | |
| 246 | OnConfigChanged(); |
| 247 | } |
| 248 | |
| 249 | } // namespace Config |