CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/ConfigSettings.h
Views: 1401
1
#pragma once
2
3
#include <cstdint>
4
#include <unordered_map>
5
#include "Core/ConfigValues.h"
6
7
class Path;
8
class Section; // ini file section
9
struct UrlEncoder;
10
11
enum class CfgFlag : u8 {
12
DEFAULT = 0,
13
DONT_SAVE = 1, // normally don't like negative flags, but these are really not many.
14
PER_GAME = 2,
15
REPORT = 4,
16
};
17
ENUM_CLASS_BITOPS(CfgFlag);
18
19
struct ConfigSetting {
20
enum Type {
21
TYPE_TERMINATOR,
22
TYPE_BOOL,
23
TYPE_INT,
24
TYPE_UINT32,
25
TYPE_UINT64,
26
TYPE_FLOAT,
27
TYPE_STRING,
28
TYPE_TOUCH_POS,
29
TYPE_PATH,
30
TYPE_CUSTOM_BUTTON
31
};
32
union DefaultValue {
33
bool b;
34
int i;
35
uint32_t u;
36
uint64_t lu;
37
float f;
38
const char *s;
39
const char *p; // not sure how much point..
40
ConfigTouchPos touchPos;
41
ConfigCustomButton customButton;
42
};
43
union SettingPtr {
44
bool *b;
45
int *i;
46
uint32_t *u;
47
uint64_t *lu;
48
float *f;
49
std::string *s;
50
Path *p;
51
ConfigTouchPos *touchPos;
52
ConfigCustomButton *customButton;
53
};
54
55
typedef bool (*BoolDefaultCallback)();
56
typedef int (*IntDefaultCallback)();
57
typedef uint32_t(*Uint32DefaultCallback)();
58
typedef uint64_t(*Uint64DefaultCallback)();
59
typedef float (*FloatDefaultCallback)();
60
typedef const char *(*StringDefaultCallback)();
61
typedef ConfigTouchPos(*TouchPosDefaultCallback)();
62
typedef const char *(*PathDefaultCallback)();
63
typedef ConfigCustomButton(*CustomButtonDefaultCallback)();
64
65
union DefaultCallback {
66
BoolDefaultCallback b;
67
IntDefaultCallback i;
68
Uint32DefaultCallback u;
69
Uint64DefaultCallback lu;
70
FloatDefaultCallback f;
71
StringDefaultCallback s;
72
PathDefaultCallback p;
73
TouchPosDefaultCallback touchPos;
74
CustomButtonDefaultCallback customButton;
75
};
76
77
ConfigSetting(const char *ini, bool *v, bool def, CfgFlag flags = CfgFlag::DEFAULT)
78
: iniKey_(ini), type_(TYPE_BOOL), flags_(flags) {
79
ptr_.b = v;
80
cb_.b = nullptr;
81
default_.b = def;
82
getPtrLUT()[v] = this;
83
}
84
85
ConfigSetting(const char *ini, int *v, int def, CfgFlag flags = CfgFlag::DEFAULT)
86
: iniKey_(ini), type_(TYPE_INT), flags_(flags) {
87
ptr_.i = v;
88
cb_.i = nullptr;
89
default_.i = def;
90
getPtrLUT()[v] = this;
91
}
92
93
ConfigSetting(const char *ini, int *v, int def, std::string(*transTo)(int), int (*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT)
94
: iniKey_(ini), type_(TYPE_INT), flags_(flags), translateTo_(transTo), translateFrom_(transFrom) {
95
ptr_.i = v;
96
cb_.i = nullptr;
97
default_.i = def;
98
getPtrLUT()[v] = this;
99
}
100
101
ConfigSetting(const char *ini, uint32_t *v, uint32_t def, CfgFlag flags = CfgFlag::DEFAULT)
102
: iniKey_(ini), type_(TYPE_UINT32), flags_(flags) {
103
ptr_.u = v;
104
cb_.u = nullptr;
105
default_.u = def;
106
getPtrLUT()[v] = this;
107
}
108
109
ConfigSetting(const char *ini, uint64_t *v, uint64_t def, CfgFlag flags = CfgFlag::DEFAULT)
110
: iniKey_(ini), type_(TYPE_UINT64), flags_(flags) {
111
ptr_.lu = v;
112
cb_.lu = nullptr;
113
default_.lu = def;
114
getPtrLUT()[v] = this;
115
}
116
117
ConfigSetting(const char *ini, float *v, float def, CfgFlag flags = CfgFlag::DEFAULT)
118
: iniKey_(ini), type_(TYPE_FLOAT), flags_(flags) {
119
ptr_.f = v;
120
cb_.f = nullptr;
121
default_.f = def;
122
getPtrLUT()[v] = this;
123
}
124
125
ConfigSetting(const char *ini, std::string *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT)
126
: iniKey_(ini), type_(TYPE_STRING), flags_(flags) {
127
ptr_.s = v;
128
cb_.s = nullptr;
129
default_.s = def;
130
getPtrLUT()[v] = this;
131
}
132
133
ConfigSetting(const char *ini, Path *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT)
134
: iniKey_(ini), type_(TYPE_PATH), flags_(flags) {
135
ptr_.p = v;
136
cb_.p = nullptr;
137
default_.p = def;
138
getPtrLUT()[v] = this;
139
}
140
141
ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, ConfigTouchPos def, CfgFlag flags = CfgFlag::DEFAULT)
142
: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), flags_(flags) {
143
ptr_.touchPos = v;
144
cb_.touchPos = nullptr;
145
default_.touchPos = def;
146
getPtrLUT()[v] = this;
147
}
148
149
ConfigSetting(const char *iniKey, const char *iniImage, const char *iniShape, const char *iniToggle, const char *iniRepeat, ConfigCustomButton *v, ConfigCustomButton def, CfgFlag flags = CfgFlag::DEFAULT)
150
: iniKey_(iniKey), ini2_(iniImage), ini3_(iniShape), ini4_(iniToggle), ini5_(iniRepeat), type_(TYPE_CUSTOM_BUTTON), flags_(flags) {
151
ptr_.customButton = v;
152
cb_.customButton = nullptr;
153
default_.customButton = def;
154
getPtrLUT()[v] = this;
155
}
156
157
ConfigSetting(const char *ini, bool *v, BoolDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
158
: iniKey_(ini), type_(TYPE_BOOL), flags_(flags) {
159
ptr_.b = v;
160
cb_.b = def;
161
getPtrLUT()[v] = this;
162
}
163
164
ConfigSetting(const char *ini, int *v, IntDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
165
: iniKey_(ini), type_(TYPE_INT), flags_(flags) {
166
ptr_.i = v;
167
cb_.i = def;
168
getPtrLUT()[v] = this;
169
}
170
171
ConfigSetting(const char *ini, int *v, IntDefaultCallback def, std::string(*transTo)(int), int(*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT)
172
: iniKey_(ini), type_(TYPE_INT), flags_(flags), translateTo_(transTo), translateFrom_(transFrom) {
173
ptr_.i = v;
174
cb_.i = def;
175
getPtrLUT()[v] = this;
176
}
177
178
ConfigSetting(const char *ini, uint32_t *v, Uint32DefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
179
: iniKey_(ini), type_(TYPE_UINT32), flags_(flags) {
180
ptr_.u = v;
181
cb_.u = def;
182
getPtrLUT()[v] = this;
183
}
184
185
ConfigSetting(const char *ini, float *v, FloatDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
186
: iniKey_(ini), type_(TYPE_FLOAT), flags_(flags) {
187
ptr_.f = v;
188
cb_.f = def;
189
getPtrLUT()[v] = this;
190
}
191
192
ConfigSetting(const char *ini, std::string *v, StringDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
193
: iniKey_(ini), type_(TYPE_STRING), flags_(flags) {
194
ptr_.s = v;
195
cb_.s = def;
196
getPtrLUT()[v] = this;
197
}
198
199
ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, TouchPosDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)
200
: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), flags_(flags) {
201
ptr_.touchPos = v;
202
cb_.touchPos = def;
203
getPtrLUT()[v] = this;
204
}
205
206
// Should actually be called ReadFromIni or something.
207
bool Get(const Section *section) const;
208
209
// Yes, this can be const because what's modified is not the ConfigSetting struct, but the value which is stored elsewhere.
210
// Should actually be called WriteToIni or something.
211
void Set(Section *section) const;
212
213
void RestoreToDefault() const;
214
215
void ReportSetting(UrlEncoder &data, const std::string &prefix) const;
216
217
// Easy flag accessors.
218
bool PerGame() const { return flags_ & CfgFlag::PER_GAME; }
219
bool SaveSetting() const { return !(flags_ & CfgFlag::DONT_SAVE); }
220
bool Report() const { return flags_ & CfgFlag::REPORT; }
221
222
const char *iniKey_ = nullptr;
223
const char *ini2_ = nullptr;
224
const char *ini3_ = nullptr;
225
const char *ini4_ = nullptr;
226
const char *ini5_ = nullptr;
227
228
const Type type_;
229
230
// Returns false if per-game settings are not currently used
231
static bool perGame(void *ptr);
232
233
private:
234
CfgFlag flags_;
235
SettingPtr ptr_;
236
DefaultValue default_{};
237
DefaultCallback cb_;
238
239
// We only support transform for ints.
240
std::string (*translateTo_)(int) = nullptr;
241
int (*translateFrom_)(const std::string &) = nullptr;
242
243
static std::unordered_map<void*, ConfigSetting*>& getPtrLUT();
244
};
245
246
struct ConfigSectionSettings {
247
const char *section;
248
const ConfigSetting *settings;
249
size_t settingsCount;
250
};
251
252