Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
namespace PokemonAutomation{





#if defined(__APPLE__)
static std::string g_startup_profile;

void set_startup_profile(int& argc, char* argv[]){
for (int i = 1; i + 1 < argc; i++){
if (strcmp(argv[i], "--profile") == 0){
QString profile = QString::fromUtf8(argv[i + 1]);
for (QChar& c : profile){
if (!c.isLetterOrNumber() && c != u'_' && c != u'-') c = u'_';
std::string profile = argv[i + 1];
for (char c : profile){
if (!(std::isalpha(c) || std::isdigit(c)) && c != u'_' && c != u'-') c = u'_';
}
g_startup_profile = profile.toStdString();
g_startup_profile = std::move(profile);
// Shift everything after --profile <name> down by 2.
for (int j = i; j + 2 < argc; j++){
argv[j] = argv[j + 2];
Expand All @@ -52,8 +55,9 @@ const std::string& STARTUP_PROFILE(){

namespace{

QString get_application_base_dir_path(){
QString application_dir_path = qApp->applicationDirPath();


Filesystem::Path get_application_base_dir_path(){
#if defined(__APPLE__)
// Use CFBundle to find the .app bundle path. Change working directory to the folder that hosts the .app bundle.
CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
Expand All @@ -64,7 +68,7 @@ QString get_application_base_dir_path(){
QString bundlePath = QDir::cleanPath(QString::fromCFString(cfPath));
CFRelease(cfPath);
if (bundlePath.endsWith(".app")){
return QFileInfo(bundlePath).dir().absolutePath();
return QFileInfo(bundlePath).dir().absolutePath().toStdString();
}
}
}
Expand All @@ -73,11 +77,11 @@ QString get_application_base_dir_path(){
// PA_APPIMAGE_DIR is set by Azure via a patched AppRun script.
QByteArray dir = qgetenv("PA_APPIMAGE_DIR");
if (!dir.isEmpty()){
return QString::fromUtf8(dir);
return QString::fromUtf8(dir).toStdString();
}
QByteArray path = qgetenv("APPIMAGE");
if (!path.isEmpty()){
return QDir::cleanPath(QFileInfo(QString::fromUtf8(path)).dir().absolutePath());
return QDir::cleanPath(QFileInfo(QString::fromUtf8(path)).dir().absolutePath()).toStdString();
}
QByteArray appDirBytes = qgetenv("APPDIR");
if (!appDirBytes.isEmpty()){
Expand All @@ -98,37 +102,37 @@ QString get_application_base_dir_path(){
QString mountPoint = pre[4].replace(QStringLiteral("\\040"), QStringLiteral(" "));
QString source = post[1].replace(QStringLiteral("\\040"), QStringLiteral(" "));
if (mountPoint == appDir && source.endsWith(QStringLiteral(".AppImage"))){
return QDir::cleanPath(QFileInfo(source).dir().absolutePath());
return QDir::cleanPath(QFileInfo(source).dir().absolutePath()).toStdString();
}
}
}
}
#endif
return application_dir_path;
return Filesystem::application_path();
}
std::string get_resource_path(){
// Find the resource directory.
Filesystem::Path base = get_application_base_dir_path().toStdString();
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "Resources/";
if (exists(try_path)){
return try_path.string();
return try_path.string_slash_normalized();
}
path = path / "..";
}
return (base / "Resources/").string();
return (base / "Resources/").string_slash_normalized();
}
std::string get_unittest_resource_path(){
// Find the unit test resource directory.

// Try the intended folder name first.
Filesystem::Path base = get_application_base_dir_path().toStdString();
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "UnitTestResources/";
if (exists(try_path)){
return try_path.string();
return try_path.string_slash_normalized();
}
path = path / "..";
}
Expand All @@ -138,26 +142,27 @@ std::string get_unittest_resource_path(){
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "CommandLineTests/";
if (exists(try_path)){
return try_path.string();
return try_path.string_slash_normalized();
}
path = path / "..";
}

return (base / "UnitTestResources/").string();
return (base / "UnitTestResources/").string_slash_normalized();
}

std::string get_training_path(){
// Find the training data directory.
Filesystem::Path base = get_application_base_dir_path().toStdString();
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "TrainingData/";
if (exists(try_path)){
return try_path.string();
return try_path.string_slash_normalized();
}
path = path / "..";
}
return (base / "TrainingData/").string();

return (base / "TrainingData/").string_slash_normalized();
}

std::string get_runtime_base_path(){
Expand Down
5 changes: 3 additions & 2 deletions SerialPrograms/Source/CommonFramework/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Common/Cpp/Logging/FileLogger.h"
#include "Common/Cpp/Logging/GlobalLogger.h"
#include "Common/Cpp/Logging/MultiOutputLogger.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "Common/Cpp/Concurrency/Qt6.9ThreadBugWorkaround.h"
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "Common/Cpp/Concurrency/FireForgetDispatcher.h"
Expand Down Expand Up @@ -107,8 +108,8 @@ int run_program(int argc, char *argv[]){

logger.log("================================================================================");
logger.log("Starting Program...");
logger.log("Current path: " + QDir::currentPath().toStdString());
logger.log("Executable path: " + qApp->applicationDirPath().toStdString());
logger.log("Current path: " + Filesystem::current_path().string_slash_normalized());
logger.log("Executable path: " + Filesystem::application_path().string_slash_normalized());
logger.log("Program setting folder: " + SETTINGS_PATH());
logger.log("Program resources folder: " + RESOURCE_PATH());

Expand Down
Loading