-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathConfig.cpp
More file actions
316 lines (275 loc) · 7.36 KB
/
Copy pathConfig.cpp
File metadata and controls
316 lines (275 loc) · 7.36 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "Config.h"
#include "ModLoader.h"
#include <fstream>
Config::Config(IMod* mod) : m_mod(mod), m_modname(mod->GetID()) {
Load();
}
Config::~Config() {
for (Category& cate : m_data) {
for (Property* prop : cate.props)
delete prop;
}
}
void Config::Load() {
std::ifstream fin("../ModLoader/Config/" + m_modname + ".cfg");
if (fin.fail()) return;
std::string token, comment, category;
bool inCate = false;
while (fin >> token) {
if (token == "#") {
std::getline(fin, comment);
trim(comment);
}
else if (token == "{")
inCate = true;
else if (token == "}")
inCate = false;
else if (inCate) {
std::string propname;
fin >> propname;
Property* prop = new Property(nullptr, category, propname);
switch (token[0]) {
case 'S': {
std::string value;
std::getline(fin, value);
trim(value);
prop->SetDefaultString(value.c_str());
break;
}
case 'B': {
bool value;
fin >> value;
prop->SetDefaultBoolean(value);
break;
}
case 'K': {
int value;
fin >> value;
prop->SetDefaultKey(CKKEYBOARD(value));
break;
}
case 'I': {
int value;
fin >> value;
prop->SetDefaultInteger(value);
break;
}
case 'F': {
float value;
fin >> value;
prop->SetDefaultFloat(value);
break;
}
}
prop->SetComment(comment.c_str());
GetCategory(category.c_str()).props.push_back(prop);
}
else {
category = token;
GetCategory(category.c_str()).comment = comment;
comment.clear();
}
}
}
void Config::Save() {
std::ofstream fout("../ModLoader/Config/" + m_modname + ".cfg");
if (fout.fail()) return;
for (auto& category : m_data) {
for (auto iter = category.props.begin(); iter != category.props.end(); ) {
if (!(*iter)->m_config) {
delete (*iter);
iter = category.props.erase(iter);
} else iter++;
}
}
for (auto iter = m_data.begin(); iter != m_data.end(); ) {
if (iter->props.empty())
iter = m_data.erase(iter);
else iter++;
}
fout << "# Configuration File for Mod: " << m_mod->GetName() << " - " << m_mod->GetVersion() << std::endl << std::endl;
for (auto& category : m_data) {
fout << "# " << category.comment << std::endl;
fout << category.name << " {" << std::endl << std::endl;
for (auto property : category.props) {
fout << "\t# " << property->GetComment() << std::endl;
fout << "\t";
switch (property->GetType()) {
case IProperty::STRING: fout << "S "; break;
case IProperty::BOOLEAN: fout << "B "; break;
case IProperty::FLOAT: fout << "F "; break;
case IProperty::KEY: fout << "K "; break;
case IProperty::INTEGER:
default: fout << "I "; break;
}
fout << property->m_key << " ";
switch (property->GetType()) {
case IProperty::STRING: fout << property->GetString(); break;
case IProperty::BOOLEAN: fout << property->GetBoolean(); break;
case IProperty::FLOAT: fout << property->GetFloat(); break;
case IProperty::KEY: fout << (int)property->GetKey(); break;
case IProperty::INTEGER:
default: fout << property->GetInteger(); break;
}
fout << std::endl << std::endl;
}
fout << "}" << std::endl << std::endl;
}
}
bool Config::HasCategory(CKSTRING category) {
for (Category& cate : m_data)
if (cate.name == category)
return true;
return false;
}
bool Config::HasKey(CKSTRING category, CKSTRING key) {
if (HasCategory(category))
for (Property* prop : GetCategory(category).props)
if (prop->m_key == key)
return true;
return false;
}
IProperty* Config::GetProperty(CKSTRING category, CKSTRING key) {
bool exist = HasKey(category, key);
Property* prop = GetCategory(category).GetProperty(key);
prop->m_config = this;
if (!exist) {
prop->m_type = IProperty::NONE;
prop->m_value.m_int = 0;
prop->m_category = category;
prop->m_key = key;
}
return prop;
}
Config::Category& Config::GetCategory(CKSTRING name) {
for (Category& cate : m_data)
if (cate.name == name)
return cate;
Config::Category cate;
cate.name = name;
cate.config = this;
m_data.push_back(cate);
return m_data.back();
}
Property* Config::Category::GetProperty(CKSTRING name) {
for (Property* prop : props)
if (prop->m_key == name)
return prop;
Property* prop = new Property(config, this->name, name);
props.push_back(prop);
return props.back();
}
CKSTRING Config::GetCategoryComment(CKSTRING category) {
return GetCategory(category).comment.c_str();
}
void Config::SetCategoryComment(CKSTRING category, CKSTRING comment) {
GetCategory(category).comment = comment;
}
Property::Property(Config* config, std::string category, std::string key) {
m_type = NONE;
m_value.m_int = 0;
m_config = config;
m_category = category;
m_key = key;
}
CKSTRING Property::GetString() {
return m_type == STRING ? m_string.c_str() : "";
}
bool Property::GetBoolean() {
return m_type == BOOLEAN ? m_value.m_bool : false;
}
int Property::GetInteger() {
return m_type == INTEGER ? m_value.m_int : 0;
}
float Property::GetFloat() {
return m_type == FLOAT ? m_value.m_float : 0.0f;
}
CKKEYBOARD Property::GetKey() {
return m_type == KEY ? m_value.m_key : (CKKEYBOARD)0;
}
void Property::SetString(CKSTRING value) {
if (m_type != STRING || m_string != value) {
m_string = value;
m_type = STRING;
if (m_config)
ModLoader::m_instance->OnModifyConfig(m_config->m_mod, m_category.c_str(), m_key.c_str(), this);
}
}
void Property::SetBoolean(bool value) {
if (m_type != BOOLEAN || m_value.m_bool != value) {
m_value.m_bool = value;
m_type = BOOLEAN;
if (m_config)
ModLoader::m_instance->OnModifyConfig(m_config->m_mod, m_category.c_str(), m_key.c_str(), this);
}
}
void Property::SetInteger(int value) {
if (m_type != INTEGER || m_value.m_int != value) {
m_value.m_int = value;
m_type = INTEGER;
if (m_config)
ModLoader::m_instance->OnModifyConfig(m_config->m_mod, m_category.c_str(), m_key.c_str(), this);
}
}
void Property::SetFloat(float value) {
if (m_type != FLOAT || m_value.m_float != value) {
m_value.m_float = value;
m_type = FLOAT;
if (m_config)
ModLoader::m_instance->OnModifyConfig(m_config->m_mod, m_category.c_str(), m_key.c_str(), this);
}
}
void Property::SetKey(CKKEYBOARD value) {
if (m_type != KEY || m_value.m_key != value) {
m_value.m_key = value;
m_type = KEY;
if (m_config)
ModLoader::m_instance->OnModifyConfig(m_config->m_mod, m_category.c_str(), m_key.c_str(), this);
}
}
CKSTRING Property::GetComment() {
return m_comment.c_str();
}
void Property::SetComment(CKSTRING comment) {
m_comment = comment;
}
void Property::SetDefaultString(CKSTRING value) {
if (m_type != STRING) {
m_type = STRING;
m_string = value;
}
}
void Property::SetDefaultBoolean(bool value) {
if (m_type != BOOLEAN) {
m_type = BOOLEAN;
m_value.m_bool = value;
}
}
void Property::SetDefaultInteger(int value) {
if (m_type != INTEGER) {
m_type = INTEGER;
m_value.m_int = value;
}
}
void Property::SetDefaultFloat(float value) {
if (m_type != FLOAT) {
m_type = FLOAT;
m_value.m_float = value;
}
}
void Property::SetDefaultKey(CKKEYBOARD value) {
if (m_type != KEY) {
m_type = KEY;
m_value.m_key = value;
}
}
void Property::CopyValue(Property* o) {
m_type = o->GetType();
switch (m_type) {
case INTEGER: SetInteger(o->GetInteger()); break;
case FLOAT: SetFloat(o->GetFloat()); break;
case BOOLEAN: SetBoolean(o->GetBoolean()); break;
case KEY: SetKey(o->GetKey()); break;
case STRING: SetString(o->GetString()); break;
}
}