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.cpp
Views: 1401
1
#include "Common/Data/Format/IniFile.h"
2
#include "Common/Net/URL.h"
3
#include "Common/Log.h"
4
5
#include "Core/ConfigSettings.h"
6
#include "Core/ConfigValues.h"
7
#include "Core/Config.h"
8
9
std::unordered_map<void*, ConfigSetting*>& ConfigSetting::getPtrLUT() {
10
static std::unordered_map<void*, ConfigSetting*> lut;
11
return lut;
12
}
13
14
bool ConfigSetting::perGame(void *ptr) {
15
return g_Config.bGameSpecific && getPtrLUT().count(ptr) > 0 && getPtrLUT()[ptr]->PerGame();
16
}
17
18
bool ConfigSetting::Get(const Section *section) const {
19
switch (type_) {
20
case TYPE_BOOL:
21
return section->Get(iniKey_, ptr_.b, cb_.b ? cb_.b() : default_.b);
22
23
case TYPE_INT:
24
if (translateFrom_) {
25
std::string value;
26
if (section->Get(iniKey_, &value, nullptr)) {
27
*ptr_.i = translateFrom_(value);
28
return true;
29
}
30
}
31
return section->Get(iniKey_, ptr_.i, cb_.i ? cb_.i() : default_.i);
32
case TYPE_UINT32:
33
return section->Get(iniKey_, ptr_.u, cb_.u ? cb_.u() : default_.u);
34
case TYPE_UINT64:
35
return section->Get(iniKey_, ptr_.lu, cb_.lu ? cb_.lu() : default_.lu);
36
case TYPE_FLOAT:
37
return section->Get(iniKey_, ptr_.f, cb_.f ? cb_.f() : default_.f);
38
case TYPE_STRING:
39
return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s() : default_.s);
40
case TYPE_TOUCH_POS:
41
{
42
ConfigTouchPos defaultTouchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;
43
section->Get(iniKey_, &ptr_.touchPos->x, defaultTouchPos.x);
44
section->Get(ini2_, &ptr_.touchPos->y, defaultTouchPos.y);
45
section->Get(ini3_, &ptr_.touchPos->scale, defaultTouchPos.scale);
46
if (ini4_) {
47
section->Get(ini4_, &ptr_.touchPos->show, defaultTouchPos.show);
48
} else {
49
ptr_.touchPos->show = defaultTouchPos.show;
50
}
51
return true;
52
}
53
case TYPE_PATH:
54
{
55
std::string tmp;
56
bool result = section->Get(iniKey_, &tmp, cb_.p ? cb_.p() : default_.p);
57
if (result) {
58
*ptr_.p = Path(tmp);
59
}
60
return result;
61
}
62
case TYPE_CUSTOM_BUTTON:
63
{
64
ConfigCustomButton defaultCustomButton = cb_.customButton ? cb_.customButton() : default_.customButton;
65
section->Get(iniKey_, &ptr_.customButton->key, defaultCustomButton.key);
66
section->Get(ini2_, &ptr_.customButton->image, defaultCustomButton.image);
67
section->Get(ini3_, &ptr_.customButton->shape, defaultCustomButton.shape);
68
section->Get(ini4_, &ptr_.customButton->toggle, defaultCustomButton.toggle);
69
section->Get(ini5_, &ptr_.customButton->repeat, defaultCustomButton.repeat);
70
return true;
71
}
72
default:
73
_dbg_assert_msg_(false, "Get(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);
74
return false;
75
}
76
}
77
78
void ConfigSetting::Set(Section *section) const {
79
if (!SaveSetting()) {
80
return;
81
}
82
83
switch (type_) {
84
case TYPE_BOOL:
85
return section->Set(iniKey_, *ptr_.b);
86
case TYPE_INT:
87
if (translateTo_) {
88
std::string value = translateTo_(*ptr_.i);
89
return section->Set(iniKey_, value);
90
}
91
return section->Set(iniKey_, *ptr_.i);
92
case TYPE_UINT32:
93
return section->Set(iniKey_, *ptr_.u);
94
case TYPE_UINT64:
95
return section->Set(iniKey_, *ptr_.lu);
96
case TYPE_FLOAT:
97
return section->Set(iniKey_, *ptr_.f);
98
case TYPE_STRING:
99
return section->Set(iniKey_, *ptr_.s);
100
case TYPE_PATH:
101
return section->Set(iniKey_, ptr_.p->ToString());
102
case TYPE_TOUCH_POS:
103
section->Set(iniKey_, ptr_.touchPos->x);
104
section->Set(ini2_, ptr_.touchPos->y);
105
section->Set(ini3_, ptr_.touchPos->scale);
106
if (ini4_) {
107
section->Set(ini4_, ptr_.touchPos->show);
108
}
109
return;
110
case TYPE_CUSTOM_BUTTON:
111
section->Set(iniKey_, ptr_.customButton->key);
112
section->Set(ini2_, ptr_.customButton->image);
113
section->Set(ini3_, ptr_.customButton->shape);
114
section->Set(ini4_, ptr_.customButton->toggle);
115
section->Set(ini5_, ptr_.customButton->repeat);
116
return;
117
default:
118
_dbg_assert_msg_(false, "Set(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);
119
return;
120
}
121
}
122
123
void ConfigSetting::RestoreToDefault() const {
124
switch (type_) {
125
case TYPE_BOOL: *ptr_.b = cb_.b ? cb_.b() : default_.b; break;
126
case TYPE_INT: *ptr_.i = cb_.i ? cb_.i() : default_.i; break;
127
case TYPE_UINT32: *ptr_.u = cb_.u ? cb_.u() : default_.u; break;
128
case TYPE_UINT64: *ptr_.lu = cb_.lu ? cb_.lu() : default_.lu; break;
129
case TYPE_FLOAT: *ptr_.f = cb_.f ? cb_.f() : default_.f; break;
130
case TYPE_STRING: *ptr_.s = cb_.s ? cb_.s() : default_.s; break;
131
case TYPE_TOUCH_POS: *ptr_.touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos; break;
132
case TYPE_PATH: *ptr_.p = Path(cb_.p ? cb_.p() : default_.p); break;
133
case TYPE_CUSTOM_BUTTON: *ptr_.customButton = cb_.customButton ? cb_.customButton() : default_.customButton; break;
134
default:
135
_dbg_assert_msg_(false, "RestoreToDefault(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);
136
}
137
}
138
139
void ConfigSetting::ReportSetting(UrlEncoder &data, const std::string &prefix) const {
140
if (!Report())
141
return;
142
143
switch (type_) {
144
case TYPE_BOOL: return data.Add(prefix + iniKey_, *ptr_.b);
145
case TYPE_INT: return data.Add(prefix + iniKey_, *ptr_.i);
146
case TYPE_UINT32: return data.Add(prefix + iniKey_, *ptr_.u);
147
case TYPE_UINT64: return data.Add(prefix + iniKey_, *ptr_.lu);
148
case TYPE_FLOAT: return data.Add(prefix + iniKey_, *ptr_.f);
149
case TYPE_STRING: return data.Add(prefix + iniKey_, *ptr_.s);
150
case TYPE_PATH: return data.Add(prefix + iniKey_, ptr_.p->ToString());
151
case TYPE_TOUCH_POS: return; // Doesn't report.
152
case TYPE_CUSTOM_BUTTON: return; // Doesn't report.
153
default:
154
_dbg_assert_msg_(false, "Report(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);
155
return;
156
}
157
}
158
159