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
3 changes: 2 additions & 1 deletion Common/Cpp/Filesystem/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void rename(const Path& old_path, const Path& new_path, std::error_code& ec);

Path current_path();

Path application_path();
Path application_binary_path();
Path application_working_path();



Expand Down
5 changes: 4 additions & 1 deletion Common/Cpp/Filesystem/Filesystem_Linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ namespace PokemonAutomation{
namespace Filesystem{


Path application_path(){
Path application_binary_path(){
char buffer[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", buffer, PATH_MAX);
if (count != -1) {
return Path(std::string(buffer, count)).parent_path();
}
return Path();
}
Path application_working_path(){
return application_binary_path();
}



Expand Down
56 changes: 54 additions & 2 deletions Common/Cpp/Filesystem/Filesystem_Mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
*
*/

#include <mach-o/dyld.h>
#include <limits.h>
#include <vector>
#include <mach-o/dyld.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFBundle.h>
#include "FilePath.h"

namespace PokemonAutomation{
namespace Filesystem{


Path application_path(){
Path application_binary_path(){
char buffer[PATH_MAX];
uint32_t size = sizeof(buffer);
if (_NSGetExecutablePath(buffer, &size) == 0) {
Expand All @@ -23,5 +26,54 @@ Path application_path(){



std::filesystem::path CFString_to_path(CFStringRef cfString) {
if (!cfString) {
return {};
}

// 1. Get the exact buffer size needed for the file system representation
CFIndex maxLength = CFStringGetMaximumSizeOfFileSystemRepresentation(cfString);

// 2. Allocate a buffer to store the raw file system bytes
std::vector<char> buffer(maxLength);

// 3. Extract the file system representation directly into the buffer
if (CFStringGetFileSystemRepresentation(cfString, buffer.data(), buffer.size())) {
return std::filesystem::path(buffer.data());
}

// Fallback: If file-system specific translation failed, try standard UTF-8 text extraction
CFIndex length = CFStringGetLength(cfString);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
std::vector<char> fallbackBuffer(maxSize);

if (CFStringGetCString(cfString, fallbackBuffer.data(), fallbackBuffer.size(), kCFStringEncodingUTF8)) {
return std::filesystem::path(fallbackBuffer.data());
}

return {};
}
Path application_working_path(){
// Use CFBundle to find the .app bundle path.
// Change working directory to the folder that hosts the .app bundle.
CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
if (bundleURL){
CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
CFRelease(bundleURL);
if (cfPath){
std::filesystem::path bundlePath = CFString_to_path(cfPath);
CFRelease(cfPath);
if (bundlePath.extension() == ".app"){
return std::filesystem::absolute(bundlePath).parent_path().lexically_normal();
}
}
}

// Fallback
return application_binary_path();
}



}
}
5 changes: 4 additions & 1 deletion Common/Cpp/Filesystem/Filesystem_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ namespace PokemonAutomation{
namespace Filesystem{


Path application_path(){
Path application_binary_path(){
QString application_dir_path = qApp->applicationDirPath();
return Path(application_dir_path.toStdString());
}
Path application_working_path(){
return application_binary_path();
}



Expand Down
5 changes: 4 additions & 1 deletion Common/Cpp/Filesystem/Filesystem_Windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ namespace PokemonAutomation{
namespace Filesystem{


Path application_path(){
Path application_binary_path(){
wchar_t buffer[MAX_PATH];
GetModuleFileNameW(nullptr, buffer, MAX_PATH);
return std::filesystem::path(buffer).parent_path();
}
Path application_working_path(){
return application_binary_path();
}



Expand Down
21 changes: 2 additions & 19 deletions SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include <QFile>
#include <QFileInfo>
#include <QDir>
#if defined(__APPLE__)
#include <CoreFoundation/CFBundle.h>
#endif
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "GlobalAutoPaths.h"

Expand Down Expand Up @@ -58,21 +55,7 @@ namespace{


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());
if (bundleURL){
CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
CFRelease(bundleURL);
if (cfPath){
QString bundlePath = QDir::cleanPath(QString::fromCFString(cfPath));
CFRelease(cfPath);
if (bundlePath.endsWith(".app")){
return QFileInfo(bundlePath).dir().absolutePath().toStdString();
}
}
}
#elif defined(__linux__)
#if defined(__linux__)
// Check for AppImage environment variables to find the root directory, if running as an AppImage.
// PA_APPIMAGE_DIR is set by Azure via a patched AppRun script.
QByteArray dir = qgetenv("PA_APPIMAGE_DIR");
Expand Down Expand Up @@ -108,7 +91,7 @@ Filesystem::Path get_application_base_dir_path(){
}
}
#endif
return Filesystem::application_path();
return Filesystem::application_working_path();
}
std::string get_resource_path(){
// Find the resource directory.
Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/CommonFramework/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int run_program(int argc, char *argv[]){
logger.log("================================================================================");
logger.log("Starting Program...");
logger.log("Current path: " + Filesystem::current_path().string_slash_normalized());
logger.log("Executable path: " + Filesystem::application_path().string_slash_normalized());
logger.log("Executable path: " + Filesystem::application_binary_path().string_slash_normalized());
logger.log("Program setting folder: " + SETTINGS_PATH());
logger.log("Program resources folder: " + RESOURCE_PATH());

Expand Down
Loading