-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSettingsUtils.cpp
544 lines (512 loc) · 19.7 KB
/
SettingsUtils.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "SettingsUtils.h"
#include "ROMUtils.h"
#include <QDir>
#include <QFileInfo>
#include <QSettings>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonArray>
#include "WL4EditorWindow.h"
extern WL4EditorWindow *singleton;
namespace SettingsUtils
{
// Global variables
QString ProgramSettingFilePath;
QString ProjectSettingFilePath;
/// <summary>
/// Initialize editor's ini file path and check the ini file.
/// </summary>
void InitProgramSetupPath(QCoreApplication &application)
{
ProgramSettingFilePath = application.applicationDirPath();
ProgramSettingFilePath += QString("/WL4Editor.ini");
// Check INI file
QFileInfo fileInfo(ProgramSettingFilePath);
if (!fileInfo.isFile())
{
QSettings WL4EditorIni(ProgramSettingFilePath, QSettings::IniFormat);
for (int i = 0; i < KeyStringSet.size(); i++)
WL4EditorIni.setValue(KeyStringSet[i], "");
}
}
/// <summary>
/// Set a Key in the ini file.
/// </summary>
/// <param name="key">
/// The key whose value to set.
/// </param>
/// <param name="value">
/// The new value of the Key.
/// </param>
void SetKey(IniKeys key, QString value)
{
QSettings WL4EditorIni(ProgramSettingFilePath, QSettings::IniFormat);
WL4EditorIni.setValue(KeyStringSet[key], value);
}
/// <summary>
/// Get a Key value from the ini file.
/// </summary>
/// <param name="key">
/// The key whose value to get.
/// </param>
/// <returns>
/// The value of the key.
/// </returns>
QString GetKey(IniKeys key)
{
QSettings WL4EditorIni(ProgramSettingFilePath, QSettings::IniFormat);
return WL4EditorIni.value(KeyStringSet[key]).toString();
}
// ***************************************************project settings****************************************************
// definition of project setting global variables
namespace projectSettings
{
QColor cameraboxcolor = QColor(0xFF, 0, 0);
QColor cameraboxcolor_extended = QColor(0, 0xFF, 0);
QColor doorboxcolor = QColor(0, 0, 0xFF);
QColor doorboxcolor_filling = QColor(0, 0, 0xFF, 0x5F);
QColor doorboxcolorselected = QColor(0, 0xFF, 0xFF); // cyan
QColor doorboxcolorselected_filling = QColor(0, 0xFF, 0xFF, 0x5F);
QColor entityboxcolor = QColor(0xFF, 0xFF, 0);
QColor entityboxcolorselected = QColor(0xFF, 0x7F, 0);
QColor extraEventIDhintboxcolor = QColor(255, 153, 18, 0xFF); // chrome yellow
QVector<int> extraEventIDhinteventids = {0x0C, 0x0E, 0x20, 0x22, 0x2E, 0x5C}; // blocks with coin
QStringList extraEventIDhintChars = {"C", "C", "C", "C", "C", "C"};
QColor extraTerrainIDhintboxcolor = QColor(0xFF, 0, 0xFF, 0xFF);
QVector<int> extraTerrainIDhintTerrainids = {};
QStringList extraTerrainIDhintChars = {};
std::map<int, QString> bgmNameList;
std::map<int, QVector<unsigned short> > cusomOAMdata;
QString customHintRenderJSFilePath = "";
}
/// <summary>
/// Load and parse the current project setting file
/// create the file if it does not exist.
/// </summary>
void LoadProjectSettings()
{
// generate json for new project
QJsonObject json;
QFileInfo curROMFileInfo(ROMUtils::ROMFileMetadata->FilePath);
QString projectSettingFilePath = QFileInfo(curROMFileInfo).dir().path() +
"/" + curROMFileInfo.completeBaseName() + ".json";
QFileInfo fileInfo(projectSettingFilePath);
if (!fileInfo.isFile())
{
// create file
QFile newFile(projectSettingFilePath);
newFile.open(QIODevice::WriteOnly);
newFile.write(QJsonDocument(json).toJson());
newFile.close();
}
ProjectSettingFilePath = projectSettingFilePath;
// Read file
QJsonParseError json_error;
QJsonObject jsonObj;
QFile loadFile(projectSettingFilePath);
loadFile.open(QIODevice::ReadOnly);
QByteArray saveData = loadFile.readAll();
loadFile.close();
QJsonDocument settingJsonDoc(QJsonDocument::fromJson(saveData, &json_error));
if (json_error.error == QJsonParseError::NoError)
{
if (settingJsonDoc.isObject())
{
jsonObj = settingJsonDoc.object();
}
}
// Load settings one by one, and send them into the runtime global variables of WL4Editor
QStringList list = jsonObj.keys();
//--------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
// the logic to maintain project settings
// example to load key value pair
QString key = "test-key";
if (list.contains(key) && jsonObj[key].isBool())
{
// accept and apply the correct key - value pair
singleton->GetOutputWidgetPtr()->PrintString("project setting file loaded correctly.\n");
}
// modify the key and value to the correct default contents, and write the value we read to the new json, so it is always clean
json.insert(key, true);
// camera box render color settings
key = "camerabox_render_color";
int helper_size = 0;
bool okay = false;
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::cameraboxcolor = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::cameraboxcolor));
key = "camerabox_render_color_extended";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::cameraboxcolor_extended = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::cameraboxcolor_extended));
// door box render color settings
key = "doorbox_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::doorboxcolor = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::doorboxcolor));
key = "doorbox_filling_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::doorboxcolor_filling = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::doorboxcolor_filling));
key = "doorbox_selected_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::doorboxcolorselected = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::doorboxcolorselected));
key = "doorbox_selected_filling_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::doorboxcolorselected_filling = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::doorboxcolorselected_filling));
// entity box render color settings
key = "entitybox_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::entityboxcolor = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::entityboxcolor));
key = "entitybox_selected_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::entityboxcolorselected = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::entityboxcolorselected));
// extra layer hint box render settings
key = "extraEventIDhintbox_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::extraEventIDhintboxcolor = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::extraEventIDhintboxcolor));
key = "extraEventIDhintbox_event_indexes";
helper_size = 6;
if (list.contains(key) && jsonObj[key].isString())
{
QString tmpstr = jsonObj[key].toString();
if (auto datavec = string2intvec<int>(tmpstr) ; datavec.size())
{
helper_size = datavec.size();
projectSettings::extraEventIDhinteventids = datavec;
}
else
{
helper_size = 0;
projectSettings::extraEventIDhinteventids.clear();
}
}
json.insert(key, intvec2string(projectSettings::extraEventIDhinteventids));
key = "extraEventIDhint_optional_characters";
if (list.contains(key) && jsonObj[key].isString())
{
QString tmpstr = jsonObj[key].toString();
QStringList strlist = string2strlist(tmpstr, 1);
if (int strlistsize = strlist.size(); strlistsize && (strlistsize == helper_size))
{
projectSettings::extraEventIDhintChars = strlist;
}
else
{
strlist.clear();
if (helper_size)
{
for (int i = 0; i < helper_size; i++)
{
strlist << "";
}
}
else
{
strlist << "";
}
projectSettings::extraEventIDhintChars = strlist;
}
}
json.insert(key, strlist2string(projectSettings::extraEventIDhintChars, 1));
key = "extraTerrainIDhintbox_render_color";
if (list.contains(key) && jsonObj[key].isString())
{
if (QColor tmpcolor = string2color(jsonObj[key].toString(), okay) ; okay)
{
projectSettings::extraTerrainIDhintboxcolor = tmpcolor;
}
}
json.insert(key, color2string(projectSettings::extraTerrainIDhintboxcolor));
key = "extraTerrainIDhintbox_terrain_indexes";
helper_size = 0;
if (list.contains(key) && jsonObj[key].isString())
{
QString tmpstr = jsonObj[key].toString();
if (auto datavec = string2intvec<int>(tmpstr) ; datavec.size())
{
helper_size = datavec.size();
projectSettings::extraTerrainIDhintTerrainids = datavec;
}
else
{
projectSettings::extraTerrainIDhintTerrainids.clear();
}
}
json.insert(key, intvec2string(projectSettings::extraTerrainIDhintTerrainids));
key = "extraTerrainIDhint_optional_characters";
if (list.contains(key) && jsonObj[key].isString())
{
QString tmpstr = jsonObj[key].toString();
QStringList strlist = string2strlist(tmpstr, 1);
if (int strlistsize = strlist.size(); strlistsize && (strlistsize == helper_size))
{
projectSettings::extraTerrainIDhintChars = strlist;
}
else
{
strlist.clear();
if (helper_size)
{
for (int i = 0; i < helper_size; i++)
{
strlist << "";
}
}
else
{
strlist << "";
}
projectSettings::extraTerrainIDhintChars = strlist;
}
}
json.insert(key, strlist2string(projectSettings::extraTerrainIDhintChars, 1));
key = "customHint_renderlogic_JSfilePath";
if (list.contains(key) && jsonObj[key].isString())
{
QString filepath = jsonObj[key].toString();
QFile jsfile(filepath);
if (jsfile.exists() && filepath.endsWith(".js"))
{
projectSettings::customHintRenderJSFilePath = filepath;
}
}
json.insert(key, projectSettings::customHintRenderJSFilePath);
// array stuff
key = "new_bgm_name";
QJsonObject saving_arr;
saving_arr.insert("-1 (decimal only)", "invalid exmaple bgm name");
if (list.contains(key) && jsonObj[key].isObject())
{
QJsonObject name_list_obj = jsonObj[key].toObject();
if (int name_num = name_list_obj.count())
{
projectSettings::bgmNameList.clear();
QStringList tmpkeys = name_list_obj.keys();
for (int i = 0; i < name_num; i++)
{
if (int bgm_id = tmpkeys[i].toInt(); (bgm_id > -1) && name_list_obj[tmpkeys[i]].isString())
{
QString bgm_name = name_list_obj[tmpkeys[i]].toString();
projectSettings::bgmNameList[bgm_id] = bgm_name;
saving_arr.insert(QString::number(bgm_id), bgm_name);
}
}
}
}
json.insert(key, saving_arr);
key = "custom_entity_oam_data";
QJsonObject saving_arr_oam;
saving_arr_oam.insert("-1 (use prefix 0x if you want to use hex number for entiy's global id, decimal can works too)",
"0xE3, 0x11FD, 0x14 (three u16 per oam, you can fetch the data from Oam designer in Sprites Editor)");
if (list.contains(key) && jsonObj[key].isObject())
{
QJsonObject oam_list_obj = jsonObj[key].toObject();
if (int oam_data_num = oam_list_obj.count())
{
projectSettings::cusomOAMdata.clear();
QStringList tmpkeys = oam_list_obj.keys();
for (int i = 0; i < oam_data_num; i++)
{
int global_entity_id = tmpkeys[i].contains("0x") ? tmpkeys[i].toInt(nullptr, 16) : tmpkeys[i].toInt();
if ((global_entity_id > -1) && oam_list_obj[tmpkeys[i]].isString())
{
QString oam_string = oam_list_obj[tmpkeys[i]].toString();
QVector<unsigned short> oamdata = string2intvec<unsigned short>(oam_string);
if (int oam_data_size = oamdata.size(); oam_data_size && ((oam_data_size % 3) == 0))
{
projectSettings::cusomOAMdata[global_entity_id] = oamdata;
saving_arr_oam.insert("0x" + QString::number(global_entity_id, 16).toUpper(), oam_string);
}
}
}
}
}
json.insert(key, saving_arr_oam);
// TODO: add more project settings
//--------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
// write the json obj back to the file to modify incorrect settings
SaveProjectSettings(json);
}
void SaveProjectSettings(QJsonObject &jsonobj)
{
// test save
QFile newFile(ProjectSettingFilePath);
newFile.open(QIODevice::WriteOnly);
newFile.write(QJsonDocument(jsonobj).toJson(QJsonDocument::Indented));
newFile.close();
}
/// <summary>
/// int to QColor
/// </summary>
QColor string2color(QString data, bool &ok)
{
// use RGB888 in both QColor and int
QVector<int> intvec = string2intvec<int>(data);
if (intvec.size() < 3)
{
// return non-transparent black if the format is incorrect
ok = false;
return QColor(0, 0, 0, 0xFF);
}
unsigned int r = intvec[0];
unsigned int g = intvec[1];
unsigned int b = intvec[2];
unsigned int a = 0xFF;
if (intvec.size() > 3)
{
a = intvec[3];
}
ok = true;
return QColor(r, g, b, a);
}
// ***************************************************helper functions****************************************************
/// <summary>
/// QColor to int
/// </summary>
QString color2string(QColor color)
{
// use RGB888 in both QColor and int
return QString::number(color.red(), 16) + "," + QString::number(color.green(), 16) +
"," + QString::number(color.blue(), 16) + "," + QString::number(color.alpha(), 16);
}
/// <summary>
/// QString (should always use hex when representing values) to a QVector of int, or short, or char
/// </summary>
template<typename TypeInt> QVector<TypeInt> string2intvec(QString &data)
{
static_assert(std::is_integral<TypeInt>::value, "Incorrect type for string2intvec<T>() !");
QStringList datalist = data.split(QChar(','));
QVector<TypeInt> result;
if (!data.size())
{
return result;
}
for (auto &substr : datalist)
{
result.append((TypeInt) substr.toInt(nullptr, 16));
}
return result;
}
/// <summary>
/// A QVector of int to a QString (should always use hex when representing values)
/// </summary>
QString intvec2string(QVector<int> &intvec)
{
QString result;
if (!intvec.size())
{
result += "";
return result;
}
for (auto &substr : intvec)
{
result += QString::number(substr, 16) + ",";
}
result.chop(1); // chop the last ","
return result;
}
/// <summary>
/// Convert a QString to a string list
/// </summary>
QStringList string2strlist(QString data, int size_per_str)
{
QStringList datalist;
if (!data.size())
{
datalist << "";
return datalist;
}
datalist = data.split(QChar(','));
if (size_per_str > 0)
{
for (int i = 0; i < datalist.size(); i++)
{
if (datalist[i].size())
{
datalist[i] = datalist[i][0];
}
}
}
return datalist;
}
/// <summary>
/// Convert a QStringlist to a string
/// </summary>
QString strlist2string(QStringList datalist, int size_per_str)
{
QString result;
if (!datalist.size())
{
return result;
}
if (size_per_str > 0)
{
for (auto &substr : datalist)
{
result += substr.left(size_per_str) + ",";
}
result.chop(1); // chop the last ","
}
else
{
for (int i = 0; i < (datalist.size() - 1); i++)
{
result += ",";
}
}
return result;
}
} // namespace SettingsUtils