Skip to content

Commit afa8d82

Browse files
authored
Migrate more Mac stuff in GlobalAutoPaths away from Qt. (#1371)
* Migrate more Mac stuff in GlobalAutoPaths away from Qt. * fix * fix * fix
1 parent 9d23404 commit afa8d82

7 files changed

Lines changed: 71 additions & 26 deletions

File tree

Common/Cpp/Filesystem/Filesystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void rename(const Path& old_path, const Path& new_path, std::error_code& ec);
6161

6262
Path current_path();
6363

64-
Path application_path();
64+
Path application_binary_path();
65+
Path application_working_path();
6566

6667

6768

Common/Cpp/Filesystem/Filesystem_Linux.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ namespace PokemonAutomation{
1212
namespace Filesystem{
1313

1414

15-
Path application_path(){
15+
Path application_binary_path(){
1616
char buffer[PATH_MAX];
1717
ssize_t count = readlink("/proc/self/exe", buffer, PATH_MAX);
1818
if (count != -1) {
1919
return Path(std::string(buffer, count)).parent_path();
2020
}
2121
return Path();
2222
}
23+
Path application_working_path(){
24+
return application_binary_path();
25+
}
2326

2427

2528

Common/Cpp/Filesystem/Filesystem_Mac.h

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
*
55
*/
66

7-
#include <mach-o/dyld.h>
87
#include <limits.h>
8+
#include <vector>
9+
#include <mach-o/dyld.h>
10+
#include <CoreFoundation/CoreFoundation.h>
11+
#include <CoreFoundation/CFBundle.h>
912
#include "FilePath.h"
1013

1114
namespace PokemonAutomation{
1215
namespace Filesystem{
1316

1417

15-
Path application_path(){
18+
Path application_binary_path(){
1619
char buffer[PATH_MAX];
1720
uint32_t size = sizeof(buffer);
1821
if (_NSGetExecutablePath(buffer, &size) == 0) {
@@ -23,5 +26,54 @@ Path application_path(){
2326

2427

2528

29+
std::filesystem::path CFString_to_path(CFStringRef cfString) {
30+
if (!cfString) {
31+
return {};
32+
}
33+
34+
// 1. Get the exact buffer size needed for the file system representation
35+
CFIndex maxLength = CFStringGetMaximumSizeOfFileSystemRepresentation(cfString);
36+
37+
// 2. Allocate a buffer to store the raw file system bytes
38+
std::vector<char> buffer(maxLength);
39+
40+
// 3. Extract the file system representation directly into the buffer
41+
if (CFStringGetFileSystemRepresentation(cfString, buffer.data(), buffer.size())) {
42+
return std::filesystem::path(buffer.data());
43+
}
44+
45+
// Fallback: If file-system specific translation failed, try standard UTF-8 text extraction
46+
CFIndex length = CFStringGetLength(cfString);
47+
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
48+
std::vector<char> fallbackBuffer(maxSize);
49+
50+
if (CFStringGetCString(cfString, fallbackBuffer.data(), fallbackBuffer.size(), kCFStringEncodingUTF8)) {
51+
return std::filesystem::path(fallbackBuffer.data());
52+
}
53+
54+
return {};
55+
}
56+
Path application_working_path(){
57+
// Use CFBundle to find the .app bundle path.
58+
// Change working directory to the folder that hosts the .app bundle.
59+
CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
60+
if (bundleURL){
61+
CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
62+
CFRelease(bundleURL);
63+
if (cfPath){
64+
std::filesystem::path bundlePath = CFString_to_path(cfPath);
65+
CFRelease(cfPath);
66+
if (bundlePath.extension() == ".app"){
67+
return std::filesystem::absolute(bundlePath).parent_path().lexically_normal();
68+
}
69+
}
70+
}
71+
72+
// Fallback
73+
return application_binary_path();
74+
}
75+
76+
77+
2678
}
2779
}

Common/Cpp/Filesystem/Filesystem_Qt.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ namespace PokemonAutomation{
1111
namespace Filesystem{
1212

1313

14-
Path application_path(){
14+
Path application_binary_path(){
1515
QString application_dir_path = qApp->applicationDirPath();
1616
return Path(application_dir_path.toStdString());
1717
}
18+
Path application_working_path(){
19+
return application_binary_path();
20+
}
1821

1922

2023

Common/Cpp/Filesystem/Filesystem_Windows.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ namespace PokemonAutomation{
1111
namespace Filesystem{
1212

1313

14-
Path application_path(){
14+
Path application_binary_path(){
1515
wchar_t buffer[MAX_PATH];
1616
GetModuleFileNameW(nullptr, buffer, MAX_PATH);
1717
return std::filesystem::path(buffer).parent_path();
1818
}
19+
Path application_working_path(){
20+
return application_binary_path();
21+
}
1922

2023

2124

SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include <QFile>
1212
#include <QFileInfo>
1313
#include <QDir>
14-
#if defined(__APPLE__)
15-
#include <CoreFoundation/CFBundle.h>
16-
#endif
1714
#include "Common/Cpp/Filesystem/Filesystem.h"
1815
#include "GlobalAutoPaths.h"
1916

@@ -58,21 +55,7 @@ namespace{
5855

5956

6057
Filesystem::Path get_application_base_dir_path(){
61-
#if defined(__APPLE__)
62-
// Use CFBundle to find the .app bundle path. Change working directory to the folder that hosts the .app bundle.
63-
CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
64-
if (bundleURL){
65-
CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
66-
CFRelease(bundleURL);
67-
if (cfPath){
68-
QString bundlePath = QDir::cleanPath(QString::fromCFString(cfPath));
69-
CFRelease(cfPath);
70-
if (bundlePath.endsWith(".app")){
71-
return QFileInfo(bundlePath).dir().absolutePath().toStdString();
72-
}
73-
}
74-
}
75-
#elif defined(__linux__)
58+
#if defined(__linux__)
7659
// Check for AppImage environment variables to find the root directory, if running as an AppImage.
7760
// PA_APPIMAGE_DIR is set by Azure via a patched AppRun script.
7861
QByteArray dir = qgetenv("PA_APPIMAGE_DIR");
@@ -108,7 +91,7 @@ Filesystem::Path get_application_base_dir_path(){
10891
}
10992
}
11093
#endif
111-
return Filesystem::application_path();
94+
return Filesystem::application_working_path();
11295
}
11396
std::string get_resource_path(){
11497
// Find the resource directory.

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int run_program(int argc, char *argv[]){
109109
logger.log("================================================================================");
110110
logger.log("Starting Program...");
111111
logger.log("Current path: " + Filesystem::current_path().string_slash_normalized());
112-
logger.log("Executable path: " + Filesystem::application_path().string_slash_normalized());
112+
logger.log("Executable path: " + Filesystem::application_binary_path().string_slash_normalized());
113113
logger.log("Program setting folder: " + SETTINGS_PATH());
114114
logger.log("Program resources folder: " + RESOURCE_PATH());
115115

0 commit comments

Comments
 (0)