-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathGlobalSettingsPanel.cpp
More file actions
455 lines (398 loc) · 16.2 KB
/
GlobalSettingsPanel.cpp
File metadata and controls
455 lines (398 loc) · 16.2 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
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
/* Global Settings Panel
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <iostream>
#include <set>
#include <QStandardPaths>
#include <QCryptographicHash>
#include <QDesktopServices>
#include <QUrl>
#include "Common/Cpp/Containers/Pimpl.tpp"
#include "Common/Cpp/LifetimeSanitizer.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Options/KeyboardLayoutOption.h"
#include "CommonFramework/Globals.h"
#include "CommonFramework/Options/CheckForUpdatesOption.h"
#include "CommonFramework/Options/ResolutionOption.h"
#include "CommonFramework/Options/Environment/SleepSuppressOption.h"
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
#include "CommonFramework/Recording/StreamHistoryOption.h"
#include "CommonFramework/AudioPipeline/AudioPipelineOptions.h"
#include "CommonFramework/VideoPipeline/VideoPipelineOptions.h"
#include "CommonFramework/ErrorReports/ErrorReports.h"
#include "Integrations/DiscordSettingsOption.h"
//#include "CommonFramework/Environment/Environment.h"
#include "GlobalSettingsPanel.h"
// #include <iostream>
// using std::cout;
// using std::endl;
namespace PokemonAutomation{
const std::set<std::string> TOKENS{
// "f6538243092d8a3b9959bca988f054e1670f57c7246df2cbba25c4df3fe7a4e7",
"2d04af67f6520e3550842d7eeb292868c6d0d4809b607f5a454712023d8815e1",
"475d0a0a305a02cbf8b602bd47c3b275dccd5ac19fbe480729804a8e4e360b71",
"6643d9fe87b3e54dc75dfac8ac22f0cc8bd17f6a8a786debf5fc4c517ee65469",
"8e48e38e49bffc8462ada9d2d9d850d5b3b5c9529d20978c09bc548bc9a614a4",
"7694adee4419d62c6a923c4efc9e7b41def7b96bb84ea882701b0bf2e8c13bee",
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", // jw's token.
"e8d168bc482e96553ea9f9ecaea5a817474dbccc2a6a228a6bde67f2b2aa2889", // James' token.
"7555b7c63481cad42306718c67e7f9def5bfd1da8f6cd299ccd3d7dc95f307ae", // Kuro's token.
"3d475b46d121fc24559d100de2426feaa53cd6578aac2817c4857a610ccde2dd", // kichi's token.
};
PreloadSettings::PreloadSettings(){}
PreloadSettings& PreloadSettings::instance(){
static PreloadSettings settings;
return settings;
}
DebugSettings& PreloadSettings::debug(){
return PreloadSettings::instance().DEBUG;
}
void PreloadSettings::load(const JsonValue& json){
const JsonObject* obj = json.to_object();
if (obj == nullptr){
return;
}
// Naughty mode.
obj->read_boolean(NAUGHTY_MODE, "NAUGHTY_MODE");
// Developer mode stuff.
const std::string* dev_token = obj->get_string("DEVELOPER_TOKEN");
if (dev_token){
QCryptographicHash hash(QCryptographicHash::Algorithm::Sha256);
#if QT_VERSION < 0x060700
hash.addData(dev_token->c_str(), (int)dev_token->size());
#else
QByteArrayView dataView(dev_token->data(), dev_token->size());
hash.addData(dataView);
#endif
DEVELOPER_MODE = TOKENS.find(hash.result().toHex().toStdString()) != TOKENS.end();
}
const JsonObject* debug_obj = obj->get_object("DEBUG");
if (debug_obj){
debug_obj->read_boolean(DEBUG.COLOR_CHECK, "COLOR_CHECK");
debug_obj->read_boolean(DEBUG.IMAGE_TEMPLATE_MATCHING, "IMAGE_TEMPLATE_MATCHING");
debug_obj->read_boolean(DEBUG.IMAGE_DICTIONARY_MATCHING, "IMAGE_DICTIONARY_MATCHING");
debug_obj->read_integer(DEBUG.BOX_SYSTEM_CELL_ROW, "BOX_SYSTEM_CELL_ROW");
debug_obj->read_integer(DEBUG.BOX_SYSTEM_CELL_COL, "BOX_SYSTEM_CELL_COL");
debug_obj->read_boolean(DEBUG.GENERATE_TEST_GOLDEN_FILES, "GENERATE_TEST_GOLDEN_FILES");
}
}
GlobalSettings& GlobalSettings::instance(){
static GlobalSettings settings;
return settings;
}
GlobalSettings::~GlobalSettings(){
ENABLE_LIFETIME_SANITIZER0.remove_listener(*this);
OPEN_BASE_FOLDER_BUTTON.remove_listener(static_cast<ButtonListener&>(*this));
}
GlobalSettings::GlobalSettings()
: BatchOption(LockMode::LOCK_WHILE_RUNNING)
, CHECK_FOR_UPDATES(CONSTRUCT_TOKEN)
, OPEN_BASE_FOLDER_BUTTON(
"<b>Runtime Base Folder:</b>",
"Open Folder"
)
, STATS_FILE(
false,
"<b>Stats File:</b><br>Use the stats file here. Multiple instances of the program can use the same file.",
LockMode::LOCK_WHILE_RUNNING,
#if defined(__APPLE__)
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/UserSettings/PA-Stats.txt",
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/UserSettings/PA-Stats.txt"
#else
"UserSettings/PA-Stats.txt",
"UserSettings/PA-Stats.txt"
#endif
)
, TEMP_FOLDER(
false,
"<b>Temp Folder:</b><br>Place temporary files in this directory.",
LockMode::LOCK_WHILE_RUNNING,
#if defined(__APPLE__)
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/TempFiles/",
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/TempFiles/"
#else
"TempFiles/",
"TempFiles/"
#endif
)
, THEME(CONSTRUCT_TOKEN)
, USE_PADDLE_OCR(
"<b>Enable PaddleOCR:</b><br>"
"Use PaddleOCR instead of Tesseract for OCR.",
LockMode::UNLOCK_WHILE_RUNNING,
false
)
, WINDOW_SIZE(
CONSTRUCT_TOKEN,
"Window Size/Position:",
"Set the size/position of the window. Takes effect immediately.<br>"
"Use this to easily set the window to a specific resolution for streaming alignment.",
1280, 1000,
0, 0
)
, LOG_WINDOW_SIZE(
CONSTRUCT_TOKEN,
"Output Window Size/Position:",
"Set the size/position of the output window. Takes effect immediately.<br>",
600, 1200,
0, 0
)
, LOG_WINDOW_STARTUP(
"<b>Open Output Window at startup:</b>",
LockMode::UNLOCK_WHILE_RUNNING,
false
)
, KEYBOARD_CONTROLS_LAYOUT(
CONSTRUCT_TOKEN,
"<b>System Keyboard Layout:</b><br>"
"This is the keyboard layout of your system. "
"This is needed for the HID keyboard controller to work correctly.",
LockMode::UNLOCK_WHILE_RUNNING,
KeyboardLayout::QWERTY
)
, STREAM_HISTORY(CONSTRUCT_TOKEN)
, SLEEP_SUPPRESS(CONSTRUCT_TOKEN)
, m_discord_settings(
"<font size=4><b>Discord Settings:</b> Integrate with Discord. (" +
make_text_url(
ONLINE_DOC_URL_BASE + "DiscordIntegration/index.html",
"online documentation"
) + ")</font>"
)
, RICH_PRESENCE(
"<b>Enable Rich Presence:</b><br>"
"Display program activity and status under your Discord user.<br>"
"Restart the program for the change to take effect.",
LockMode::UNLOCK_WHILE_RUNNING,
true
)
, ALL_STATS(
"<b>All Stats:</b><br>Include all-time stats for notifications.",
LockMode::UNLOCK_WHILE_RUNNING,
true
)
, DISCORD(CONSTRUCT_TOKEN)
, m_advanced_options(
"<font size=4><b>Advanced Options:</b> You should not need to touch anything below here.</font>"
)
, LOG_EVERYTHING(
"<b>Log Everything:</b><br>Log everything to the output window and output log. Will be very spammy.",
LockMode::UNLOCK_WHILE_RUNNING,
false
)
, SAVE_DEBUG_IMAGES(
"<b>Save Debug Images:</b><br>"
"If the program fails to read something when it should succeed, save the image for debugging purposes.",
LockMode::LOCK_WHILE_RUNNING,
true
)
, SAVE_DEBUG_VIDEOS_ON_SWITCH(
"<b>Save Debug Videos on Switch:</b><br>"
"If the program fails, save a video on Switch for debugging purposes.",
LockMode::LOCK_WHILE_RUNNING,
false
)
// , NAUGHTY_MODE_OPTION("<b>Naughty Mode:</b>", false)
, HIDE_NOTIF_DISCORD_LINK(
"<b>Hide Discord Link in Notifications:</b><br>"
"Many Discord servers have rules forbidding links to other Discord servers. "
"Checking this box will hide the support link that appears in the footer of every Discord notification.",
LockMode::UNLOCK_WHILE_RUNNING,
false
)
, PERFORMANCE(CONSTRUCT_TOKEN)
, AUDIO_PIPELINE(CONSTRUCT_TOKEN)
, VIDEO_PIPELINE(CONSTRUCT_TOKEN)
, ENABLE_LIFETIME_SANITIZER0(
"<b>Enable Lifetime Sanitizer: (for debugging)</b><br>"
"Check for C++ object lifetime violations. Terminate program with stack dump if violations are found. "
"If enabling, you must restart the program for it to take effect.",
LockMode::UNLOCK_WHILE_RUNNING,
true
// IS_BETA_VERSION
)
, ERROR_REPORTS(CONSTRUCT_TOKEN)
, DEVELOPER_TOKEN(
true,
"<b>Developer Token:</b><br>Restart application to take full effect after changing this.",
LockMode::LOCK_WHILE_RUNNING,
"", ""
)
{
PA_ADD_OPTION(OPEN_BASE_FOLDER_BUTTON);
PA_ADD_OPTION(CHECK_FOR_UPDATES);
PA_ADD_OPTION(STATS_FILE);
PA_ADD_OPTION(TEMP_FOLDER);
PA_ADD_OPTION(THEME);
PA_ADD_OPTION(USE_PADDLE_OCR);
PA_ADD_OPTION(RESOURCE_DOWNLOAD_TABLE);
PA_ADD_OPTION(WINDOW_SIZE);
PA_ADD_OPTION(LOG_WINDOW_SIZE);
PA_ADD_OPTION(LOG_WINDOW_STARTUP);
PA_ADD_OPTION(KEYBOARD_CONTROLS_LAYOUT);
PA_ADD_OPTION(STREAM_HISTORY);
#ifdef PA_ENABLE_SLEEP_SUPPRESS
PA_ADD_OPTION(SLEEP_SUPPRESS);
#endif
PA_ADD_STATIC(m_discord_settings);
#ifdef PA_SOCIAL_SDK
PA_ADD_OPTION(RICH_PRESENCE);
#endif
PA_ADD_OPTION(ALL_STATS);
PA_ADD_OPTION(DISCORD);
PA_ADD_STATIC(m_advanced_options);
PA_ADD_OPTION(LOG_EVERYTHING);
PA_ADD_OPTION(SAVE_DEBUG_IMAGES);
PA_ADD_OPTION(SAVE_DEBUG_VIDEOS_ON_SWITCH);
// PA_ADD_OPTION(NAUGHTY_MODE);
// PA_ADD_OPTION(HIDE_NOTIF_DISCORD_LINK);
PA_ADD_OPTION(PERFORMANCE);
PA_ADD_OPTION(AUDIO_PIPELINE);
PA_ADD_OPTION(VIDEO_PIPELINE);
PA_ADD_OPTION(ENABLE_LIFETIME_SANITIZER0);
#ifdef PA_OFFICIAL
PA_ADD_OPTION(ERROR_REPORTS);
#endif
PA_ADD_OPTION(DEVELOPER_TOKEN);
GlobalSettings::on_config_value_changed(this);
ENABLE_LIFETIME_SANITIZER0.add_listener(*this);
OPEN_BASE_FOLDER_BUTTON.add_listener(static_cast<ButtonListener&>(*this));
}
void GlobalSettings::load_json(const JsonValue& json){
const JsonObject* obj = json.to_object();
if (obj == nullptr){
return;
}
PreloadSettings::instance().load(json);
const bool developer_mode = PreloadSettings::instance().DEVELOPER_MODE;
BatchOption::load_json(json);
USE_PADDLE_OCR.set_visibility(developer_mode ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN);
SAVE_DEBUG_VIDEOS_ON_SWITCH.set_visibility(developer_mode ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN);
// Remake this to update the color.
m_discord_settings.set_text(
"<font size=4><b>Discord Settings:</b> Integrate with Discord. (" +
make_text_url(
ONLINE_DOC_URL_BASE + "DiscordIntegration/index.html",
"online documentation"
) + ")</font>"
);
COMMAND_LINE_TEST_LIST.clear();
COMMAND_LINE_IGNORE_LIST.clear();
const JsonObject* command_line_tests_setting = obj->get_object("COMMAND_LINE_TESTS");
if (command_line_tests_setting){
command_line_tests_setting->read_boolean(COMMAND_LINE_TEST_MODE, "RUN");
if (!command_line_tests_setting->read_string(COMMAND_LINE_TEST_FOLDER, "FOLDER")){
COMMAND_LINE_TEST_FOLDER = "CommandLineTests";
}
const JsonArray* test_list = command_line_tests_setting->get_array("TEST_LIST");
if (test_list){
for (const auto& value: *test_list){
if (!value.is_string()){
continue;
}
const std::string* test_name = value.to_string();
if (test_name != nullptr && !test_name->empty()){
COMMAND_LINE_TEST_LIST.emplace_back(*test_name);
}
}
}
const JsonArray* ignore_list = command_line_tests_setting->get_array("IGNORE_LIST");
if (ignore_list){
for (const auto& value: *ignore_list){
if (!value.is_string()){
continue;
}
const std::string* test_name = value.to_string();
if (test_name != nullptr && !test_name->empty()){
COMMAND_LINE_IGNORE_LIST.emplace_back(*test_name);
}
}
}
if (COMMAND_LINE_TEST_MODE){
std::cout << "Enter command line test mode:" << std::endl;
if (COMMAND_LINE_TEST_LIST.size() > 0){
std::cout << "Run following tests: " << std::endl;
for (const auto& name : COMMAND_LINE_TEST_LIST){
std::cout << "- " << name << std::endl;
}
}
if (COMMAND_LINE_IGNORE_LIST.size() > 0){
std::cout << "Ignore following " << COMMAND_LINE_IGNORE_LIST.size() << " paths: " << std::endl;
const size_t MAX_LINES = 5;
for (size_t i = 0; i < COMMAND_LINE_IGNORE_LIST.size() && i < MAX_LINES; i++){
std::cout << "- " << COMMAND_LINE_IGNORE_LIST[i] << std::endl;
}
if (COMMAND_LINE_IGNORE_LIST.size() > MAX_LINES){
std::cout << "..." << std::endl;
}
}
}
}
}
JsonValue GlobalSettings::to_json() const{
JsonObject obj = std::move(*BatchOption::to_json().to_object());
obj["NAUGHTY_MODE"] = PreloadSettings::instance().NAUGHTY_MODE;
JsonObject command_line_test_obj;
command_line_test_obj["RUN"] = COMMAND_LINE_TEST_MODE;
command_line_test_obj["FOLDER"] = COMMAND_LINE_TEST_FOLDER;
{
JsonArray test_list;
for (const auto& name : COMMAND_LINE_TEST_LIST){
test_list.push_back(name);
}
command_line_test_obj["TEST_LIST"] = std::move(test_list);
}
{
JsonArray ignore_list;
for (const auto& name : COMMAND_LINE_IGNORE_LIST){
ignore_list.push_back(name);
}
command_line_test_obj["IGNORE_LIST"] = std::move(ignore_list);
}
obj["COMMAND_LINE_TESTS"] = std::move(command_line_test_obj);
JsonObject debug_obj;
const auto& debug_settings = PreloadSettings::instance().DEBUG;
debug_obj["COLOR_CHECK"] = debug_settings.COLOR_CHECK;
debug_obj["IMAGE_TEMPLATE_MATCHING"] = debug_settings.IMAGE_TEMPLATE_MATCHING;
debug_obj["IMAGE_DICTIONARY_MATCHING"] = debug_settings.IMAGE_DICTIONARY_MATCHING;
debug_obj["BOX_SYSTEM_CELL_ROW"] = debug_settings.BOX_SYSTEM_CELL_ROW;
debug_obj["BOX_SYSTEM_CELL_COL"] = debug_settings.BOX_SYSTEM_CELL_COL;
debug_obj["GENERATE_TEST_GOLDEN_FILES"] = debug_settings.GENERATE_TEST_GOLDEN_FILES;
obj["DEBUG"] = std::move(debug_obj);
return obj;
}
void GlobalSettings::on_config_value_changed(void* object){
bool enabled = ENABLE_LIFETIME_SANITIZER0;
if (enabled){
global_logger_tagged().log("LifeTime Sanitizer: Enabled", COLOR_BLUE);
}else{
global_logger_tagged().log("LifeTime Sanitizer: Disabled", COLOR_BLUE);
LifetimeSanitizer::disable();
}
}
void GlobalSettings::on_press(){
// Open the runtime base folder in the system file manager
QDesktopServices::openUrl(QUrl::fromLocalFile(QString::fromStdString(RUNTIME_BASE_PATH())));
}
PanelDescriptorWrapper<GlobalSettings_Descriptor, GlobalSettingsPanel> GlobalSettings_Descriptor::INSTANCE;
GlobalSettings_Descriptor::GlobalSettings_Descriptor()
: PanelDescriptor(
Color(),
"",
"Global Settings", "Global Settings",
"",
"Global Settings"
)
{}
GlobalSettingsPanel::GlobalSettingsPanel(const GlobalSettings_Descriptor& descriptor)
: SettingsPanelInstance(descriptor)
, settings(GlobalSettings::instance())
{
PA_ADD_OPTION(settings);
}
}