Skip to content

Commit a5765ed

Browse files
author
Gin
committed
Add missing resources msg box
1 parent 81ea3de commit a5765ed

File tree

4 files changed

+107
-70
lines changed

4 files changed

+107
-70
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,6 @@ file(GLOB MAIN_SOURCES
22422242
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}../../ FILES ${MAIN_SOURCES})
22432243

22442244
if (APPLE)
2245-
# TODO: set up the correct application icon
22462245
set(SerialPrograms_ICON ${CMAKE_CURRENT_SOURCE_DIR}/../IconResource/icon.icns)
22472246
# set_source_files_properties(SerialPrograms_ICON PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
22482247

@@ -2262,6 +2261,8 @@ if (APPLE)
22622261
RESOURCE ${SerialPrograms_ICON}
22632262
)
22642263

2264+
# make sure Packages repo, https://github.com/PokemonAutomation/Packages is placed in the same folder as this Arduino-Source repo
2265+
# so the post-build command can copy the resources folder Packages/SerialPrograms/Resources/ into the built app bundle
22652266
add_custom_command(
22662267
TARGET SerialPrograms
22672268
POST_BUILD

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ int main(int argc, char *argv[]){
5252
//#endif
5353
QApplication application(argc, argv);
5454

55-
global_logger_tagged().log("================================================================================");
56-
global_logger_tagged().log("Starting Program...");
55+
Logger& logger = global_logger_tagged();
56+
57+
logger.log("================================================================================");
58+
logger.log("Starting Program...");
5759

5860
qRegisterMetaType<size_t>("size_t");
5961
qRegisterMetaType<uint8_t>("uint8_t");
@@ -66,22 +68,28 @@ int main(int argc, char *argv[]){
6668
QDir().mkpath(QString::fromStdString(SETTINGS_PATH()));
6769
QDir().mkpath(QString::fromStdString(SCREENSHOTS_PATH()));
6870

71+
// Several novice developers struggled to build and run the program due to missing Resources folder.
72+
// Add this check to pop a message box when Resources folder is missing.
73+
if (!check_resource_folder(logger)){
74+
return 1;
75+
}
76+
6977
// Read program settings from json file: SerialPrograms-Settings.json.
7078
try{
71-
if (!migrate_settings(global_logger_tagged(), application.applicationName().toStdString() + "-Settings.json")){
79+
if (!migrate_settings(logger, application.applicationName().toStdString() + "-Settings.json")){
7280
return 1;
7381
}
7482

7583
std::cout << "Loading from program setting JSON: " << PROGRAM_SETTING_JSON_PATH() << std::endl;
7684
PERSISTENT_SETTINGS().read();
7785

78-
if (!migrate_stats(global_logger_tagged())){
86+
if (!migrate_stats(logger)){
7987
return 1;
8088
}
8189
}catch (const FileException& error){
82-
global_logger_tagged().log(error.message(), COLOR_RED);
90+
logger.log(error.message(), COLOR_RED);
8391
}catch (const ParseException& error){
84-
global_logger_tagged().log(error.message(), COLOR_RED);
92+
logger.log(error.message(), COLOR_RED);
8593
}
8694

8795
if (GlobalSettings::instance().COMMAND_LINE_TEST_MODE){
@@ -93,7 +101,7 @@ int main(int argc, char *argv[]){
93101
return 1;
94102
}
95103

96-
check_new_version(global_logger_tagged());
104+
check_new_version(logger);
97105

98106
Integration::DiscordIntegrationSettingsOption& discord_settings = GlobalSettings::instance().DISCORD->integration;
99107
if (discord_settings.run_on_start){
@@ -113,7 +121,7 @@ int main(int argc, char *argv[]){
113121
set_working_directory();
114122

115123
// Run this asynchronously to we don't block startup.
116-
std::unique_ptr<AsyncTask> task = send_all_unsent_reports(global_logger_tagged(), true);
124+
std::unique_ptr<AsyncTask> task = send_all_unsent_reports(logger, true);
117125

118126
int ret = 0;
119127
{

SerialPrograms/Source/CommonFramework/Startup/SetupSettings.cpp

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <QFile>
8+
#include <QDir>
89
#include <QMessageBox>
910
#include <QApplication>
1011
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
@@ -23,27 +24,27 @@ using std::endl;
2324

2425
namespace PokemonAutomation{
2526

26-
bool migrate_settings(Logger& logger, std::string file_name){
27-
QFile root_file(QString::fromStdString(file_name));
27+
bool migrate_settings(Logger& logger, const std::string& file_name){
28+
QFile cur_dir_file(QString::fromStdString(file_name));
2829
QFile folder_file(QString::fromStdString(SETTINGS_PATH() + file_name));
2930

30-
logger.log("Checking settings configuration...");
31+
logger.log("Checking if user setting JSON file need to be migrated...");
3132

32-
if (!root_file.exists() && !folder_file.exists()){
33-
logger.log("Clean install, nothing to do.");
33+
if (!cur_dir_file.exists() && !folder_file.exists()){
34+
logger.log("Clean install, nothing to migrate.");
3435
return true;
3536
}
3637

37-
if (!root_file.exists() && folder_file.exists()){
38+
if (!cur_dir_file.exists() && folder_file.exists()){
3839
logger.log("Migrated, nothing to do.");
3940
return true;
4041
}
4142

42-
if (root_file.exists() && !folder_file.exists()){
43-
logger.log("Migrating root file to the folder...");
44-
root_file.copy(folder_file.fileName());
45-
logger.log("Renaming root file as backup...");
46-
root_file.rename(root_file.fileName() + ".bak");
43+
if (cur_dir_file.exists() && !folder_file.exists()){
44+
logger.log("Migrating old setting file to the new folder...");
45+
cur_dir_file.copy(folder_file.fileName());
46+
logger.log("Renaming old setting file as backup...");
47+
cur_dir_file.rename(cur_dir_file.fileName() + ".bak");
4748
logger.log("Migrated.");
4849
QMessageBox box;
4950
box.setTextFormat(Qt::RichText);
@@ -58,29 +59,26 @@ bool migrate_settings(Logger& logger, std::string file_name){
5859
return true;
5960
}
6061

61-
if (root_file.exists() && folder_file.exists()){
62-
logger.log("Two configs detected, showing prompt...");
63-
QMessageBox box;
64-
box.setTextFormat(Qt::RichText);
65-
box.critical(
66-
nullptr,
67-
"Two settings files detected!",
68-
QString::fromStdString(
69-
"Detected two settings files at these locations:<br><br>" +
70-
root_file.fileName().toStdString() + " (versions 0.29 and older)<br>" +
71-
folder_file.fileName().toStdString() + " (version 0.30)" +
72-
"<br><br>This probably happened because you used an early beta of v0.30.x which created a new settings file at the new location. "
73-
"Please either delete or rename the file you do not want to use."
74-
)
75-
);
76-
return false;
77-
}
78-
79-
return true;
62+
// last case: cur_dir_file.exists() && folder_file.exists()
63+
logger.log("Two configs detected, showing prompt...");
64+
QMessageBox box;
65+
box.setTextFormat(Qt::RichText);
66+
box.critical(
67+
nullptr,
68+
"Two settings files detected!",
69+
QString::fromStdString(
70+
"Detected two settings files at these locations:<br><br>" +
71+
cur_dir_file.fileName().toStdString() + " (versions 0.29 and older)<br>" +
72+
folder_file.fileName().toStdString() + " (version 0.30)" +
73+
"<br><br>This probably happened because you used an early beta of v0.30.x which created a new "
74+
"settings file at the new location. Please either delete or rename the file you do not want to use."
75+
)
76+
);
77+
return false;
8078
}
8179

8280
bool migrate_stats(Logger& logger){
83-
logger.log("Checking if stats should be migrated...");
81+
logger.log("Checking if stats file should be migrated...");
8482

8583
std::string path = GlobalSettings::instance().STATS_FILE;
8684
if (path != "PA-Stats.txt"){
@@ -90,25 +88,27 @@ bool migrate_stats(Logger& logger){
9088

9189
const std::string new_path = GlobalSettings::instance().STATS_FILE.default_value();
9290

93-
QFile root_file(QString::fromStdString(path));
91+
// old location: current working directory
92+
QFile cur_dir_file(QString::fromStdString(path));
93+
// new location:
9494
QFile folder_file(QString::fromStdString(new_path));
9595

96-
if (!root_file.exists() && !folder_file.exists()){
97-
logger.log("Clean install, nothing to do.");
96+
if (!cur_dir_file.exists() && !folder_file.exists()){
97+
logger.log("Clean install, nothing to migrate.");
9898
return true;
9999
}
100100

101-
if (!root_file.exists() && folder_file.exists()){
101+
if (!cur_dir_file.exists() && folder_file.exists()){
102102
logger.log("File migrated. Stats path in settings not updated. Updating...");
103103
GlobalSettings::instance().STATS_FILE.restore_defaults();
104104
return true;
105105
}
106106

107-
if (root_file.exists() && !folder_file.exists()){
108-
logger.log("Migrating root file to the folder...");
109-
root_file.copy(folder_file.fileName());
110-
logger.log("Renaming root file as backup...");
111-
root_file.rename(root_file.fileName() + ".bak");
107+
if (cur_dir_file.exists() && !folder_file.exists()){
108+
logger.log("Migrating old stats file to the folder...");
109+
cur_dir_file.copy(folder_file.fileName());
110+
logger.log("Renaming old stats file as backup...");
111+
cur_dir_file.rename(cur_dir_file.fileName() + ".bak");
112112
GlobalSettings::instance().STATS_FILE.restore_defaults();
113113
logger.log("Migrated.");
114114
QMessageBox box;
@@ -124,25 +124,22 @@ bool migrate_stats(Logger& logger){
124124
return true;
125125
}
126126

127-
if (root_file.exists() && folder_file.exists()){
128-
logger.log("Two stats files detected, showing prompt...");
129-
QMessageBox box;
130-
box.setTextFormat(Qt::RichText);
131-
box.critical(
132-
nullptr,
133-
"Two settings files detected!",
134-
QString::fromStdString(
135-
"Detected two stats files at these locations:<br><br>" +
136-
root_file.fileName().toStdString() + " (versions 0.29 and older)<br>" +
137-
folder_file.fileName().toStdString() + " (version 0.30)" +
138-
"<br><br>This probably happened because you used an early beta of v0.30.x which created a new stats file at the new location. "
139-
"Please either delete or rename the file you do not want to use."
140-
)
141-
);
142-
return false;
143-
}
144-
145-
return true;
127+
// last case: cur_dir_file.exists() && folder_file.exists()
128+
logger.log("Two stats files detected, showing prompt...");
129+
QMessageBox box;
130+
box.setTextFormat(Qt::RichText);
131+
box.critical(
132+
nullptr,
133+
"Two settings files detected!",
134+
QString::fromStdString(
135+
"Detected two stats files at these locations:<br><br>" +
136+
cur_dir_file.fileName().toStdString() + " (versions 0.29 and older)<br>" +
137+
folder_file.fileName().toStdString() + " (version 0.30)" +
138+
"<br><br>This probably happened because you used an early beta of v0.30.x which created a new stats file at the new location. "
139+
"Please either delete or rename the file you do not want to use."
140+
)
141+
);
142+
return false;
146143
}
147144

148145

@@ -198,6 +195,28 @@ void set_permissions(QObject& object){
198195
}
199196

200197

198+
bool check_resource_folder(Logger& logger){
199+
QDir resources_folder(QString::fromStdString(RESOURCE_PATH()));
200+
if (resources_folder.exists()){
201+
logger.log("Found Resource folder at " + RESOURCE_PATH());
202+
return true;
203+
}
204+
logger.log("No Resources/ folder at " + RESOURCE_PATH());
205+
QMessageBox box;
206+
box.setTextFormat(Qt::RichText);
207+
box.critical(
208+
nullptr,
209+
"No Resources Folder!",
210+
QString::fromStdString(
211+
"No Resources Folder:<br><br>"
212+
"Make sure you download Resources folder from latest release at<br>"
213+
"https://github.com/PokemonAutomation/ComputerControl/releases/<br><br>"
214+
"The Resources folder is part of PA-SerialPrograms-[VERSION]-[DATE].zip<br><br>"
215+
"Extract the Resources folder from the zip file and place it at following path:<br><br>" + RESOURCE_PATH()
216+
)
217+
);
218+
return false;
219+
}
201220

202221

203222

SerialPrograms/Source/CommonFramework/Startup/SetupSettings.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ namespace PokemonAutomation{
1515

1616
class Logger;
1717

18-
19-
bool migrate_settings(Logger& logger, std::string file_name);
18+
// Move user setting JSON file from old location to the new one
19+
// return true if there is no need for migration or the migration is successful
20+
// return false if migration fails. In this case it will also pop a critical error message box
21+
bool migrate_settings(Logger& logger, const std::string& file_name);
22+
23+
// Move stats file from old location to the new one
24+
// return true if there is no need for migration or the migration is successful
25+
// return false if migration fails. In this case it will also pop a critical error message box
2026
bool migrate_stats(Logger& logger);
2127

2228
// Use Qt to setup permissions of cameras and microphones on macOS.
2329
// See https://www.qt.io/blog/permission-apis-in-qt-6.5
2430
void set_permissions(QObject& object);
2531

32+
// Return true if there is the required Resources/ folder.
33+
// When the folder is missing, it will also pop a critical error message box
34+
bool check_resource_folder(Logger& logger);
2635

2736
}
2837
#endif

0 commit comments

Comments
 (0)