|
|
@@ -28,91 +28,99 @@ namespace rack { |
|
|
|
namespace asset { |
|
|
|
|
|
|
|
|
|
|
|
void init() { |
|
|
|
// Get system dir |
|
|
|
if (systemDir.empty()) { |
|
|
|
if (settings::devMode) { |
|
|
|
systemDir = "."; |
|
|
|
} |
|
|
|
else { |
|
|
|
static void initSystemDir() { |
|
|
|
if (systemDir != "") |
|
|
|
return; |
|
|
|
|
|
|
|
if (settings::devMode) { |
|
|
|
systemDir = "."; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
#if defined ARCH_MAC |
|
|
|
CFBundleRef bundle = CFBundleGetMainBundle(); |
|
|
|
assert(bundle); |
|
|
|
|
|
|
|
// Check if we're running as a command-line program or an app bundle. |
|
|
|
CFURLRef bundleUrl = CFBundleCopyBundleURL(bundle); |
|
|
|
// Thanks Ken Thomases! https://stackoverflow.com/a/58369256/272642 |
|
|
|
CFStringRef uti; |
|
|
|
if (CFURLCopyResourcePropertyForKey(bundleUrl, kCFURLTypeIdentifierKey, &uti, NULL) && uti && UTTypeConformsTo(uti, kUTTypeApplicationBundle)) { |
|
|
|
char bundleBuf[PATH_MAX]; |
|
|
|
Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf)); |
|
|
|
assert(success); |
|
|
|
bundlePath = bundleBuf; |
|
|
|
} |
|
|
|
|
|
|
|
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); |
|
|
|
char resourcesBuf[PATH_MAX]; |
|
|
|
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); |
|
|
|
assert(success); |
|
|
|
CFRelease(resourcesUrl); |
|
|
|
systemDir = resourcesBuf; |
|
|
|
CFBundleRef bundle = CFBundleGetMainBundle(); |
|
|
|
assert(bundle); |
|
|
|
|
|
|
|
// Check if we're running as a command-line program or an app bundle. |
|
|
|
CFURLRef bundleUrl = CFBundleCopyBundleURL(bundle); |
|
|
|
// Thanks Ken Thomases! https://stackoverflow.com/a/58369256/272642 |
|
|
|
CFStringRef uti; |
|
|
|
if (CFURLCopyResourcePropertyForKey(bundleUrl, kCFURLTypeIdentifierKey, &uti, NULL) && uti && UTTypeConformsTo(uti, kUTTypeApplicationBundle)) { |
|
|
|
char bundleBuf[PATH_MAX]; |
|
|
|
Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf)); |
|
|
|
assert(success); |
|
|
|
bundlePath = bundleBuf; |
|
|
|
} |
|
|
|
|
|
|
|
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); |
|
|
|
char resourcesBuf[PATH_MAX]; |
|
|
|
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); |
|
|
|
assert(success); |
|
|
|
CFRelease(resourcesUrl); |
|
|
|
systemDir = resourcesBuf; |
|
|
|
#endif |
|
|
|
#if defined ARCH_WIN |
|
|
|
// Get path to executable |
|
|
|
wchar_t moduleBufW[MAX_PATH]; |
|
|
|
DWORD length = GetModuleFileNameW(NULL, moduleBufW, LENGTHOF(moduleBufW)); |
|
|
|
assert(length > 0); |
|
|
|
// Get folder of executable |
|
|
|
PathRemoveFileSpecW(moduleBufW); |
|
|
|
// Convert to short path to avoid Unicode |
|
|
|
wchar_t moduleBufShortW[MAX_PATH]; |
|
|
|
GetShortPathNameW(moduleBufW, moduleBufShortW, LENGTHOF(moduleBufShortW)); |
|
|
|
systemDir = string::fromWstring(moduleBufShortW); |
|
|
|
// Get path to executable |
|
|
|
wchar_t moduleBufW[MAX_PATH]; |
|
|
|
DWORD length = GetModuleFileNameW(NULL, moduleBufW, LENGTHOF(moduleBufW)); |
|
|
|
assert(length > 0); |
|
|
|
// Get folder of executable |
|
|
|
PathRemoveFileSpecW(moduleBufW); |
|
|
|
// Convert to short path to avoid Unicode |
|
|
|
wchar_t moduleBufShortW[MAX_PATH]; |
|
|
|
GetShortPathNameW(moduleBufW, moduleBufShortW, LENGTHOF(moduleBufShortW)); |
|
|
|
systemDir = string::fromWstring(moduleBufShortW); |
|
|
|
#endif |
|
|
|
#if defined ARCH_LIN |
|
|
|
// Users should launch Rack from their terminal in the system directory |
|
|
|
systemDir = "."; |
|
|
|
// Users should launch Rack from their terminal in the system directory |
|
|
|
systemDir = "."; |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void initUserDir() { |
|
|
|
if (userDir != "") |
|
|
|
return; |
|
|
|
|
|
|
|
if (settings::devMode) { |
|
|
|
userDir = "."; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Get user dir |
|
|
|
if (userDir.empty()) { |
|
|
|
if (settings::devMode) { |
|
|
|
userDir = "."; |
|
|
|
} |
|
|
|
else { |
|
|
|
#if defined ARCH_WIN |
|
|
|
// Get "My Documents" folder |
|
|
|
wchar_t documentsBufW[MAX_PATH] = L"."; |
|
|
|
HRESULT result = SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBufW); |
|
|
|
assert(result == S_OK); |
|
|
|
// Convert to short path to avoid Unicode |
|
|
|
wchar_t documentsBufShortW[MAX_PATH]; |
|
|
|
GetShortPathNameW(documentsBufW, documentsBufShortW, LENGTHOF(documentsBufShortW)); |
|
|
|
userDir = string::fromWstring(documentsBufShortW); |
|
|
|
userDir += "/Rack"; |
|
|
|
// Get "My Documents" folder |
|
|
|
wchar_t documentsBufW[MAX_PATH] = L"."; |
|
|
|
HRESULT result = SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBufW); |
|
|
|
assert(result == S_OK); |
|
|
|
// Convert to short path to avoid Unicode |
|
|
|
wchar_t documentsBufShortW[MAX_PATH]; |
|
|
|
GetShortPathNameW(documentsBufW, documentsBufShortW, LENGTHOF(documentsBufShortW)); |
|
|
|
userDir = string::fromWstring(documentsBufShortW); |
|
|
|
userDir += "/Rack"; |
|
|
|
#endif |
|
|
|
#if defined ARCH_MAC |
|
|
|
// Get home directory |
|
|
|
struct passwd* pw = getpwuid(getuid()); |
|
|
|
assert(pw); |
|
|
|
userDir = pw->pw_dir; |
|
|
|
userDir += "/Documents/Rack"; |
|
|
|
// Get home directory |
|
|
|
struct passwd* pw = getpwuid(getuid()); |
|
|
|
assert(pw); |
|
|
|
userDir = pw->pw_dir; |
|
|
|
userDir += "/Documents/Rack"; |
|
|
|
#endif |
|
|
|
#if defined ARCH_LIN |
|
|
|
// Get home directory |
|
|
|
const char* homeBuf = getenv("HOME"); |
|
|
|
if (!homeBuf) { |
|
|
|
struct passwd* pw = getpwuid(getuid()); |
|
|
|
assert(pw); |
|
|
|
homeBuf = pw->pw_dir; |
|
|
|
} |
|
|
|
userDir = homeBuf; |
|
|
|
userDir += "/.Rack"; |
|
|
|
#endif |
|
|
|
} |
|
|
|
// Get home directory |
|
|
|
const char* homeBuf = getenv("HOME"); |
|
|
|
if (!homeBuf) { |
|
|
|
struct passwd* pw = getpwuid(getuid()); |
|
|
|
assert(pw); |
|
|
|
homeBuf = pw->pw_dir; |
|
|
|
} |
|
|
|
userDir = homeBuf; |
|
|
|
userDir += "/.Rack"; |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
void init() { |
|
|
|
initSystemDir(); |
|
|
|
initUserDir(); |
|
|
|
|
|
|
|
system::createDirectory(systemDir); |
|
|
|
system::createDirectory(userDir); |
|
|
|