1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <list>
#include <map>
#if __APPLE__
#include <mutex>
#else
#include <shared_mutex>
#endif
#include "Common/Config/Config.h"
namespace Config
{
using Layers = std::map<LayerType, std::shared_ptr<Layer>>;
static Layers s_layers;
static std::list<ConfigChangedCallback> s_callbacks;
static u32 s_callback_guards = 0;
// Mac supports shared_mutex since 10.12 and we're targeting 10.10,
// so only use unique locks there...
#if __APPLE__
static std::mutex s_layers_rw_lock;
using ReadLock = std::unique_lock<std::mutex>;
using WriteLock = std::unique_lock<std::mutex>;
#else
static std::shared_mutex s_layers_rw_lock;
using ReadLock = std::shared_lock<std::shared_mutex>;
using WriteLock = std::unique_lock<std::shared_mutex>;
#endif
static void AddLayerInternal(std::shared_ptr<Layer> layer)
{
{
WriteLock lock(s_layers_rw_lock);
const Config::LayerType layer_type = layer->GetLayer();
s_layers.insert_or_assign(layer_type, std::move(layer));
}
InvokeConfigChangedCallbacks();
}
void AddLayer(std::unique_ptr<ConfigLayerLoader> loader)
{
AddLayerInternal(std::make_shared<Layer>(std::move(loader)));
}
std::shared_ptr<Layer> GetLayer(LayerType layer)
{
ReadLock lock(s_layers_rw_lock);
std::shared_ptr<Layer> result;
const auto it = s_layers.find(layer);
if (it != s_layers.end())
{
result = it->second;
}
return result;
}
void RemoveLayer(LayerType layer)
{
{
WriteLock lock(s_layers_rw_lock);
s_layers.erase(layer);
}
InvokeConfigChangedCallbacks();
}
void AddConfigChangedCallback(ConfigChangedCallback func)
{
s_callbacks.emplace_back(std::move(func));
}
void InvokeConfigChangedCallbacks()
{
if (s_callback_guards)
return;
for (const auto& callback : s_callbacks)
callback();
}
// Explicit load and save of layers
void Load()
{
{
ReadLock lock(s_layers_rw_lock);
for (auto& layer : s_layers)
layer.second->Load();
}
InvokeConfigChangedCallbacks();
}
void Save()
{
{
ReadLock lock(s_layers_rw_lock);
for (auto& layer : s_layers)
layer.second->Save();
}
InvokeConfigChangedCallbacks();
}
void Init()
{
// These layers contain temporary values
ClearCurrentRunLayer();
}
void Shutdown()
{
WriteLock lock(s_layers_rw_lock);
s_layers.clear();
s_callbacks.clear();
}
void ClearCurrentRunLayer()
{
WriteLock lock(s_layers_rw_lock);
s_layers.insert_or_assign(LayerType::CurrentRun, std::make_shared<Layer>(LayerType::CurrentRun));
}
static const std::map<System, std::string> system_to_name = {
{System::Main, "Dolphin"},
{System::GCPad, "GCPad"},
{System::WiiPad, "Wiimote"},
{System::GCKeyboard, "GCKeyboard"},
{System::GFX, "Graphics"},
{System::Logger, "Logger"},
{System::Debugger, "Debugger"},
{System::SYSCONF, "SYSCONF"},
{System::DualShockUDPClient, "DualShockUDPClient"}};
const std::string& GetSystemName(System system)
{
return system_to_name.at(system);
}
std::optional<System> GetSystemFromName(const std::string& name)
{
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(),
[&name](const auto& entry) { return entry.second == name; });
if (system != system_to_name.end())
return system->first;
return {};
}
const std::string& GetLayerName(LayerType layer)
{
static const std::map<LayerType, std::string> layer_to_name = {
{LayerType::Base, "Base"},
{LayerType::GlobalGame, "Global GameINI"},
{LayerType::LocalGame, "Local GameINI"},
{LayerType::Netplay, "Netplay"},
{LayerType::Movie, "Movie"},
{LayerType::CommandLine, "Command Line"},
{LayerType::CurrentRun, "Current Run"},
};
return layer_to_name.at(layer);
}
LayerType GetActiveLayerForConfig(const ConfigLocation& config)
{
ReadLock lock(s_layers_rw_lock);
for (auto layer : SEARCH_ORDER)
{
const auto it = s_layers.find(layer);
if (it != s_layers.end())
{
if (it->second->Exists(config))
return layer;
}
}
// If config is not present in any layer, base layer is considered active.
return LayerType::Base;
}
ConfigChangeCallbackGuard::ConfigChangeCallbackGuard()
{
++s_callback_guards;
}
ConfigChangeCallbackGuard::~ConfigChangeCallbackGuard()
{
if (--s_callback_guards)
return;
InvokeConfigChangedCallbacks();
}
} // namespace Config
|