Skip to content

Commit

Permalink
Organize output file paths for easier modification in future
Browse files Browse the repository at this point in the history
  • Loading branch information
Gin committed Nov 25, 2023
1 parent 272cc27 commit 49e753a
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 26 deletions.
2 changes: 2 additions & 0 deletions SerialPrograms/Source/CommonFramework/CrashDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
40 changes: 38 additions & 2 deletions SerialPrograms/Source/CommonFramework/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions SerialPrograms/Source/CommonFramework/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand Down
4 changes: 2 additions & 2 deletions SerialPrograms/Source/CommonFramework/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions SerialPrograms/Source/CommonFramework/PersistentSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
6 changes: 3 additions & 3 deletions SerialPrograms/Source/CommonFramework/SetupSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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...");

Expand All @@ -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.<br>"
"It has been automatically moved into the \"" + SETTINGS_PATH + "\" folder."
"It has been automatically moved into the \"" + SETTINGS_PATH() + "\" folder."
)
);
return true;
Expand Down Expand Up @@ -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.<br>"
"It has been automatically moved into the \"" + SETTINGS_PATH + "\" folder."
"It has been automatically moved into the \"" + SETTINGS_PATH() + "\" folder."
)
);
return true;
Expand Down
12 changes: 4 additions & 8 deletions SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
#include <QDir>
#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(
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QDir>
#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"
Expand All @@ -29,9 +30,8 @@ std::string dump_image_alone(
static std::mutex lock;
std::lock_guard<std::mutex> 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 49e753a

Please sign in to comment.