diff --git a/SerialPrograms/Source/CommonFramework/CrashDump.cpp b/SerialPrograms/Source/CommonFramework/CrashDump.cpp index 745692bdd..1a2c3081b 100644 --- a/SerialPrograms/Source/CommonFramework/CrashDump.cpp +++ b/SerialPrograms/Source/CommonFramework/CrashDump.cpp @@ -16,6 +16,8 @@ using std::cout; using std::endl; +// TODO: let this file use ERROR_PATH() declared in commonFrames/Globals.h. + #if _WIN32 && _MSC_VER #pragma comment (lib, "Dbghelp.lib") diff --git a/SerialPrograms/Source/CommonFramework/Globals.cpp b/SerialPrograms/Source/CommonFramework/Globals.cpp index 468cff474..d0b20095b 100644 --- a/SerialPrograms/Source/CommonFramework/Globals.cpp +++ b/SerialPrograms/Source/CommonFramework/Globals.cpp @@ -51,6 +51,8 @@ const std::string PROJECT_GITHUB_URL = "https://github.com/PokemonAutomation/"; const std::string PROJECT_SOURCE_URL = "https://github.com/PokemonAutomation/Arduino-Source/"; +namespace { + std::string get_resource_path(){ // Find the resource directory. QString path = QCoreApplication::applicationDirPath(); @@ -79,10 +81,44 @@ std::string get_training_path(){ } return (QCoreApplication::applicationDirPath() + "/../TrainingData/").toStdString(); } +std::string get_setting_path(){ + return "UserSettings/"; +} +std::string get_screenshot_path(){ + return "Screenshots/"; +} +std::string get_debug_path(){ + return "DebugDumps/"; +} +std::string get_error_path(){ + return "ErrorDumps/"; +} +std::string get_user_file_path(){ + return "./"; +} +} // anonymous namespace -const std::string SETTINGS_PATH = "UserSettings/"; -const std::string SCREENSHOTS_PATH = "Screenshots/"; +const std::string& SETTINGS_PATH(){ + static std::string path = get_setting_path(); + return path; +} +const std::string& SCREENSHOTS_PATH(){ + static std::string path = get_screenshot_path(); + return path; +} +const std::string& DEBUG_PATH(){ + static std::string path = get_debug_path(); + return path; +} +const std::string& ERROR_PATH(){ + static std::string path = get_error_path(); + return path; +} +const std::string& USER_FILE_PATH(){ + static std::string path = get_user_file_path(); + return path; +} const std::string& RESOURCE_PATH(){ static std::string path = get_resource_path(); return path; diff --git a/SerialPrograms/Source/CommonFramework/Globals.h b/SerialPrograms/Source/CommonFramework/Globals.h index 3d207eab8..03bfcc65c 100644 --- a/SerialPrograms/Source/CommonFramework/Globals.h +++ b/SerialPrograms/Source/CommonFramework/Globals.h @@ -30,9 +30,26 @@ extern const std::string PROJECT_SOURCE_URL; const auto SERIAL_REFRESH_RATE = std::chrono::milliseconds(1000); -extern const std::string SETTINGS_PATH; -extern const std::string SCREENSHOTS_PATH; +// Folder path (end with "/") to hold program setting files. +const std::string& SETTINGS_PATH(); + +// Folder path (end with "/") to hold screenshots from the program "Screenshot" button. +const std::string& SCREENSHOTS_PATH(); + +// Folder path (end with "/") to hold debugging images and other debugging files +const std::string& DEBUG_PATH(); + +// Folder path (end with "/") to hold error images and other related files here. Useful for debugging the errors. +const std::string& ERROR_PATH(); + +// Folder path (end with "/") that holds various user genereated files. +// e.g. for a program that records and dumps screenshots, the saved images can go to USER_FILE_PATH()/ScreenshotDumper. +const std::string& USER_FILE_PATH(); + +// Resource folder path. Resources include JSON files, images, sound files and others required by +// various automation programs. const std::string& RESOURCE_PATH(); +// Hold ML trainign data. const std::string& TRAINING_PATH(); diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 6696364cf..3b84ed572 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -52,8 +52,8 @@ int main(int argc, char *argv[]){ OutputRedirector redirect_stdout(std::cout, "stdout", Color()); OutputRedirector redirect_stderr(std::cerr, "stderr", COLOR_RED); - QDir().mkpath(QString::fromStdString(SETTINGS_PATH)); - QDir().mkpath(QString::fromStdString(SCREENSHOTS_PATH)); + QDir().mkpath(QString::fromStdString(SETTINGS_PATH())); + QDir().mkpath(QString::fromStdString(SCREENSHOTS_PATH())); // Read program settings from json file: SerialPrograms-Settings.json. try{ diff --git a/SerialPrograms/Source/CommonFramework/Notifications/MessageAttachment.cpp b/SerialPrograms/Source/CommonFramework/Notifications/MessageAttachment.cpp index 1a23f8135..a3c85849a 100644 --- a/SerialPrograms/Source/CommonFramework/Notifications/MessageAttachment.cpp +++ b/SerialPrograms/Source/CommonFramework/Notifications/MessageAttachment.cpp @@ -86,7 +86,7 @@ PendingFileSend::PendingFileSend(Logger& logger, const ImageAttachment& image) m_filename = now_to_filestring() + format; if (image.keep_file){ - m_filepath = SCREENSHOTS_PATH + m_filename; + m_filepath = SCREENSHOTS_PATH() + m_filename; }else{ QDir().mkdir("TempFiles"); // m_filename = "temp-" + m_filename; diff --git a/SerialPrograms/Source/CommonFramework/PersistentSettings.cpp b/SerialPrograms/Source/CommonFramework/PersistentSettings.cpp index 8059f8b3f..e58b65091 100644 --- a/SerialPrograms/Source/CommonFramework/PersistentSettings.cpp +++ b/SerialPrograms/Source/CommonFramework/PersistentSettings.cpp @@ -41,14 +41,14 @@ void PersistentSettings::write() const{ root["99-Panels"] = panels.clone(); try{ - std::string settings_path = SETTINGS_PATH + QCoreApplication::applicationName().toStdString() + "-Settings.json"; + std::string settings_path = SETTINGS_PATH() + QCoreApplication::applicationName().toStdString() + "-Settings.json"; root.dump(settings_path); }catch (FileException&){} } void PersistentSettings::read(){ - std::string settings_path = SETTINGS_PATH + QCoreApplication::applicationName().toStdString() + "-Settings.json"; + std::string settings_path = SETTINGS_PATH() + QCoreApplication::applicationName().toStdString() + "-Settings.json"; JsonValue json = load_json_file(settings_path); JsonObject* obj = json.get_object(); if (obj == nullptr){ diff --git a/SerialPrograms/Source/CommonFramework/SetupSettings.cpp b/SerialPrograms/Source/CommonFramework/SetupSettings.cpp index 9d14d4341..93851f856 100644 --- a/SerialPrograms/Source/CommonFramework/SetupSettings.cpp +++ b/SerialPrograms/Source/CommonFramework/SetupSettings.cpp @@ -14,7 +14,7 @@ namespace PokemonAutomation{ bool migrate_settings(Logger& logger, std::string file_name){ QFile root_file(QString::fromStdString(file_name)); - QFile folder_file(QString::fromStdString(SETTINGS_PATH + file_name)); + QFile folder_file(QString::fromStdString(SETTINGS_PATH() + file_name)); logger.log("Checking settings configuration..."); @@ -41,7 +41,7 @@ bool migrate_settings(Logger& logger, std::string file_name){ "Settings Migrated!", QString::fromStdString( "Detected a settings file at the old location used by version 0.29 and earlier.
" - "It has been automatically moved into the \"" + SETTINGS_PATH + "\" folder." + "It has been automatically moved into the \"" + SETTINGS_PATH() + "\" folder." ) ); return true; @@ -107,7 +107,7 @@ bool migrate_stats(Logger& logger){ "Settings Migrated!", QString::fromStdString( "Detected a stats file at the old location used by version 0.29 and earlier.
" - "It has been automatically moved into the \"" + SETTINGS_PATH + "\" folder." + "It has been automatically moved into the \"" + SETTINGS_PATH() + "\" folder." ) ); return true; diff --git a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp index c7ede4be4..ee5336737 100644 --- a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp +++ b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp @@ -7,20 +7,17 @@ #include #include "DebugDumper.h" #include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Globals.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/Logging/Logger.h" namespace PokemonAutomation{ -namespace { -const char* debug_dump_folder_name = "DebugDumps"; -} - // create a folder with path ./DebugDumps/`folder_path`. // The function will create all parent folders necessary to create the folder. void create_debug_folder(const std::string& folder_path){ - QDir().mkdir(debug_dump_folder_name); - QDir(debug_dump_folder_name).mkpath(folder_path.c_str()); + QDir().mkdir(DEBUG_PATH().c_str()); + QDir(DEBUG_PATH().c_str()).mkpath(folder_path.c_str()); } std::string dump_debug_image( @@ -30,8 +27,7 @@ std::string dump_debug_image( const ImageViewRGB32& image ){ create_debug_folder(path); - std::string full_path = debug_dump_folder_name; - full_path += "/" + path + "/" + now_to_filestring() + "-" + label + ".png"; + std::string full_path = DEBUG_PATH() + path + "/" + now_to_filestring() + "-" + label + ".png"; logger.log("Saving debug image to: " + full_path, COLOR_YELLOW); image.save(full_path); return full_path; diff --git a/SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp b/SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp index 85e5c0db2..d440ce715 100644 --- a/SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp +++ b/SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp @@ -8,6 +8,7 @@ #include #include "Common/Cpp/PrettyPrint.h" #include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Globals.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/Notifications/EventNotificationOption.h" #include "CommonFramework/Notifications/ProgramInfo.h" @@ -29,9 +30,8 @@ std::string dump_image_alone( static std::mutex lock; std::lock_guard lg(lock); - QDir().mkdir("ErrorDumps"); - std::string name = "ErrorDumps/"; - name += now_to_filestring(); + QDir().mkdir(ERROR_PATH().c_str()); + std::string name = ERROR_PATH() + now_to_filestring(); name += "-"; name += label; name += ".png"; diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.cpp b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.cpp index d707be7b1..567709794 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.cpp @@ -145,7 +145,7 @@ SwitchSystemWidget::SwitchSystemWidget( if (!image){ return; } - std::string filename = SCREENSHOTS_PATH + "screenshot-" + now_to_filestring() + ".png"; + std::string filename = SCREENSHOTS_PATH() + "screenshot-" + now_to_filestring() + ".png"; m_session.logger().log("Saving screenshot to: " + filename, COLOR_PURPLE); image->save(filename); }); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp index 9ac736130..ec6ddd66c 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp @@ -8,6 +8,7 @@ #include "Common/Cpp/Time.h" #include "Common/Cpp/PrettyPrint.h" #include "ClientSource/Connection/BotBase.h" +#include "CommonFramework/Globals.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch_SnapshotDumper.h" @@ -50,10 +51,11 @@ SnapshotDumper::SnapshotDumper() } void SnapshotDumper::program(SingleSwitchProgramEnvironment& env, BotBaseContext& context){ - QDir().mkpath("ScreenshotDumper/"); + std::string folder_path = USER_FILE_PATH() + "ScreenshotDumper/"; + QDir().mkpath(folder_path.c_str()); while (true){ VideoSnapshot last = env.console.video().snapshot(); - std::string filename = "ScreenshotDumper/" + now_to_filestring(); + std::string filename = folder_path + now_to_filestring(); switch (FORMAT){ case Format::PNG: filename += ".png";