-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperJson.cpp
520 lines (417 loc) · 12.9 KB
/
helperJson.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
#include <fstream>
#include <filesystem>
#include "helperJson.h"
#include "External/nlohmann/json.hpp"
#include "constants.h"
#include "helper.h"
#ifdef _WIN32
#include <windows.h>
#include <initguid.h>
#include <KnownFolders.h>
#include <ShlObj.h>
#else
#include <csignal>
#endif
//#include <cwchar>
using json = nlohmann::json;
//const wchar_t* w_configFileName{ L"UserData.json" };
const OS_SPEC_PATH_CHAR* configFileName{OS_SPEC_PATH("UserData.json") };
const OS_SPEC_PATH_CHAR* savesPath{ OS_SPEC_PATH("Saves/") };
const OS_SPEC_PATH_CHAR* appDataFolderName{ OS_SPEC_PATH("\\LatencyMeterRefreshed\\") };
const OS_SPEC_PATH_CHAR* homeDataFolderName{ OS_SPEC_PATH("/.LatencyMeterRefreshed/") };
int LatencyIndexSort(const void* a, const void* b)
{
LatencyReading* _a = (LatencyReading*)a;
LatencyReading* _b = (LatencyReading*)b;
return (int)_a->index - (int)_b->index > 0 ? +1 : -1;
}
void to_json(json& j, const StyleData& styleData)
{
j = json
{
{"mainColor", styleData.mainColor},
{"fontColor", styleData.fontColor},
{"mainColorBrightness", styleData.mainColorBrightness},
{"fontColorBrightness", styleData.fontColorBrightness},
{"fontSize", styleData.fontSize},
{"selectedFont", styleData.selectedFont},
{"internalPlotColor", styleData.internalPlotColor},
{"externalPlotColor", styleData.externalPlotColor},
{"inputPlotColor", styleData.inputPlotColor},
};
}
void to_json(json& j, const PerformanceData& performanceData)
{
j = json
{
{"lockGuiFps", performanceData.lockGuiFps},
{"guiLockedFps", performanceData.guiLockedFps},
{"showPlots", performanceData.showPlots},
{"VSync", performanceData.VSync},
//{"maxSerialReadFrequency", performanceData.maxSerialReadFrequency},
};
}
void to_json(json& j, const MiscData& miscData)
{
j = json
{
{"localUserData", miscData.localUserData},
};
}
void to_json(json& j, const UserData& userData)
{
//json styleJson = userData.style;
j = json
{
{ "style", userData.style },
{ "performance", userData.performance },
{ "misc", userData.misc },
};
}
void to_json(json& j, LatencyReading reading)
{
j = json
{
{"timeExternal", reading.timeExternal },
{"timeInternal", reading.timeInternal },
{"timePing", reading.timePing },
{"index", reading.index },
};
}
void to_json(json& j, TabInfo data)
{
j = json
{
{"version", data.saved_ver},
{"measurements", data.latencyData.measurements },
{"note", data.latencyData.note },
{"name", data.name },
};
}
void from_json(const json& j, PerformanceData& performanceData)
{
j.at("guiLockedFps").get_to(performanceData.guiLockedFps);
j.at("lockGuiFps").get_to(performanceData.lockGuiFps);
j.at("showPlots").get_to(performanceData.showPlots);
j.at("VSync").get_to(performanceData.VSync);
//j.at("maxSerialReadFrequency").get_to(performanceData.maxSerialReadFrequency);
}
void from_json(const json& j, MiscData& miscData)
{
j.at("localUserData").get_to(miscData.localUserData);
}
void from_json(const json& j, StyleData& styleData)
{
j.at("mainColor").get_to(styleData.mainColor);
j.at("fontColor").get_to(styleData.fontColor);
j.at("mainColorBrightness").get_to(styleData.mainColorBrightness);
j.at("fontColorBrightness").get_to(styleData.fontColorBrightness);
j.at("fontSize").get_to(styleData.fontSize);
j.at("selectedFont").get_to(styleData.selectedFont);
j.at("internalPlotColor").get_to(styleData.internalPlotColor);
j.at("externalPlotColor").get_to(styleData.externalPlotColor);
j.at("inputPlotColor").get_to(styleData.inputPlotColor);
}
void from_json(const json& j, UserData& userData)
{
j.at("style").get_to(userData.style);
j.at("performance").get_to(userData.performance);
j.at("misc").get_to(userData.misc);
//auto &style = j.at("style");
//style.at("mainColor").get_to(userData.style.mainColor);
//style.at("fontColor").get_to(userData.style.fontColor);
//style.at("mainColorBrightness").get_to(userData.style.mainColorBrightness);
//style.at("fontColorBrightness").get_to(userData.style.fontColorBrightness);
//style.at("fontSize").get_to(userData.style.fontSize);
//style.at("selectedFont").get_to(userData.style.selectedFont);
}
void from_json(const json& j, LatencyReading& reading)
{
j.at("timeExternal").get_to(reading.timeExternal);
j.at("timeInternal").get_to(reading.timeInternal);
j.at("timePing").get_to(reading.timePing);
j.at("index").get_to(reading.index);
}
void from_json(const json& j, TabInfo& reading)
{
j.at("measurements").get_to(reading.latencyData.measurements);
#ifdef _WIN32
strcpy_s(reading.latencyData.note, j.value("note", "").c_str());
strcpy_s(reading.name, j.value("name", "").c_str());
#else
strcpy(reading.latencyData.note, j.value("note", "").c_str());
strcpy(reading.name, j.value("name", "").c_str());
#endif
}
void from_json(const json& j, std::vector<TabInfo> &data)
{
for (auto& elem : j)
{
data.push_back(elem.get<TabInfo>());
// Try to get the version
if(!elem.contains("version"))
data.back().saved_ver = 0;
else
elem.at("version").get_to(data.back().saved_ver);
}
}
OS_SPEC_PATH_STR HelperJson::GetAppDataUserConfigPath()
{
#ifdef _WIN32
PWSTR appDataPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath) == S_OK)
{
std::wstring filePath = std::wstring(appDataPath) + std::wstring(appDataFolderName) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
std::ifstream configFile(filePath);
configFile.close();
return filePath;
}
return L"";
#else
OS_SPEC_PATH_STR home_dir = getenv("HOME");
home_dir += homeDataFolderName;
std::filesystem::create_directories(home_dir);
auto filepath = home_dir + configFileName;
std::ifstream file(filepath);
file.close();
return filepath;
#endif
}
OS_SPEC_PATH_STR HelperJson::GetLocalUserConfigPath()
{
#ifdef _WIN32
wchar_t localPath[_MAX_PATH];
GetModuleFileNameW(NULL, localPath, _MAX_PATH);
std::wstring filePath = std::wstring(localPath) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
std::ifstream configFile(filePath);
configFile.close();
return filePath;
#else
OS_SPEC_PATH_CHAR localPath[_MAX_PATH];
getcwd(localPath, _MAX_PATH);
OS_SPEC_PATH_STR filePath = OS_SPEC_PATH_STR(localPath) + OS_SPEC_PATH_STR(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(OS_SPEC_PATH("\\/")) + 1) + OS_SPEC_PATH_STR(configFileName);
std::ifstream configFile(filePath);
configFile.close();
return filePath;
#endif
}
OS_SPEC_PATH_STR GetUserDataPath(bool islocal)
{
#ifdef _WIN32
std::ofstream configFile;
std::wstring filePath;
if (islocal)
{
wchar_t localPath[_MAX_PATH];
GetModuleFileNameW(NULL, localPath, _MAX_PATH);
filePath = std::wstring(localPath) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
configFile = std::ofstream(filePath);
}
else
{
PWSTR appDataPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath) == S_OK)
{
filePath = std::wstring(appDataPath) + std::wstring(appDataFolderName) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
std::filesystem::create_directories(std::wstring(appDataPath) + std::wstring(appDataFolderName));
configFile = std::ofstream(filePath);
}
}
if (!configFile.good())
{
configFile.close();
return L"";
}
configFile.close();
return filePath;
#else
std::ofstream configFile;
OS_SPEC_PATH_STR filePath;
if (islocal)
{
filePath = HelperJson::GetLocalUserConfigPath();
configFile = std::ofstream(filePath);
}
else
{
filePath = HelperJson::GetAppDataUserConfigPath();
configFile = std::ofstream(filePath);
}
if (!configFile.good())
{
configFile.close();
return OS_SPEC_PATH("");
}
configFile.close();
return filePath;
#endif
}
void HelperJson::SaveUserData(UserData& userData)
{
#ifdef _WIN32
std::wstring filePath = GetUserDataPath(userData.misc.localUserData);
if (filePath.empty())
return;
std::ofstream configFile(filePath);
json j = userData;
configFile << j.dump();
configFile.close();
#else
OS_SPEC_PATH_STR filePath = GetUserDataPath(userData.misc.localUserData);
if (filePath.empty())
return;
std::ofstream configFile(filePath);
json j = userData;
configFile << j.dump();
configFile.close();
#endif
}
void HelperJson::SaveUserStyle(StyleData& styleData)
{
UserData userData{};
GetUserData(userData);
userData.style = styleData;
SaveUserData(userData);
}
bool HelperJson::GetUserData(UserData& userData)
{
#ifdef _WIN32
std::wstring filePath;
wchar_t localPath[_MAX_PATH];
GetModuleFileNameW(NULL, localPath, _MAX_PATH);
filePath = std::wstring(localPath) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
std::ifstream configFile(filePath);
if (!configFile.good())
{
configFile.close();
PWSTR appDataPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath) == S_OK)
{
filePath = std::wstring(appDataPath) + std::wstring(appDataFolderName) + std::wstring(configFileName);
filePath = filePath.substr(0, filePath.find_last_of(L"\\/") + 1) + std::wstring(configFileName);
configFile = std::ifstream(filePath);
if (!configFile.good())
{
configFile.close();
return false;
}
}
else
return false;
}
json data = json::parse(configFile);
from_json(data, userData);
configFile.close();
return true;
#else
OS_SPEC_PATH_STR filePath = HelperJson::GetLocalUserConfigPath();
std::ifstream configFile(filePath);
if (!configFile.good())
{
configFile.close();
filePath = HelperJson::GetAppDataUserConfigPath();
configFile = std::ifstream(filePath);
if (!configFile.good())
{
configFile.close();
return false;
}
}
json data = json::parse(configFile);
from_json(data, userData);
configFile.close();
return true;
#endif
}
void HelperJson::SaveLatencyTests(TabInfo& tests, char path[_MAX_PATH])
{
//std::string fileName = savesPath + std::string(name) + std::string(".json");
// Sort array by index before saving
if(tests.latencyData.measurements.size() > 0)
qsort(&tests.latencyData.measurements[0], (size_t)tests.latencyData.measurements.size(), sizeof(tests.latencyData.measurements[0]), LatencyIndexSort);
std::ofstream saveFile(path);
if (!saveFile.good())
return;
json j = tests;
saveFile << j.dump();
saveFile.close();
}
size_t HelperJson::GetLatencyTests(std::vector<TabInfo> &tests, const char path[_MAX_PATH])
{
//std::string fileName = savesPath + std::string(name) + std::string(".json");
std::ifstream saveFile(path);
if (!saveFile.good())
return 0;
size_t elements = 0;
json j = json::parse(saveFile);
if (j.is_array()) {
from_json(j, tests);
elements = j.size();
}
else {
TabInfo info;
from_json(j, info);
tests.push_back(info);
elements = 1;
}
// All measurements before version 5 had external time saved as millis, beyond that version I switched to micros
for(int i = tests.size() - elements; i < tests.size(); i++) {
if(tests[i].saved_ver < 5)
for (auto &item: tests[i].latencyData.measurements)
item.timeExternal *= 1000; // * helper::External2Micros in the old version
}
saveFile.close();
return elements;
}
void HelperJson::SaveLatencyTestsPack(std::vector<TabInfo> &tabs, char path[_MAX_PATH])
{
for (auto& tab : tabs) {
if (tab.latencyData.measurements.size() > 0)
qsort(&tab.latencyData.measurements[0], (size_t)tab.latencyData.measurements.size(), sizeof(tab.latencyData.measurements[0]), LatencyIndexSort);
}
std::ofstream saveFile(path);
if (!saveFile.good())
return;
json j = tabs;
saveFile << j.dump();
saveFile.close();
}
void HelperJson::UserConfigLocationChanged(bool isNewLocal)
{
//#ifdef _WIN32
OS_SPEC_PATH_STR appDataPath = GetAppDataUserConfigPath();
OS_SPEC_PATH_STR localPath = GetLocalUserConfigPath();
bool appDataPathExists = false;
bool localPathExists = false;
if (!appDataPath.empty())
{
std::ifstream testFile(appDataPath);
appDataPathExists = testFile.good();
testFile.close();
}
if (!localPath.empty())
{
std::ifstream testFile(localPath);
localPathExists = testFile.good();
testFile.close();
}
if ((isNewLocal && !appDataPathExists) || (!isNewLocal && !localPathExists))
return;
if (isNewLocal)
{
std::filesystem::copy(appDataPath, localPath, std::filesystem::copy_options::overwrite_existing);
std::filesystem::remove(appDataPath);
}
else
{
std::filesystem::copy(localPath, appDataPath, std::filesystem::copy_options::overwrite_existing);
std::filesystem::remove(localPath);
}
//#endif
}