diff --git a/include/asset.hpp b/include/asset.hpp index ab4d860b..d0864420 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -16,4 +16,8 @@ std::string assetLocal(std::string filename); std::string assetPlugin(Plugin *plugin, std::string filename); +extern std::string assetGlobalDir; +extern std::string assetLocalDir; + + } // namespace rack diff --git a/src/asset.cpp b/src/asset.cpp index 2194107f..3b2f2a8a 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -23,73 +23,86 @@ namespace rack { -static std::string globalDir; -static std::string localDir; +std::string assetGlobalDir; +std::string assetLocalDir; void assetInit(bool devMode) { - if (devMode) { - // Use current working directory if running in development mode - globalDir = "."; - localDir = "."; - return; - } - + if (assetGlobalDir.empty()) { + if (devMode) { + assetGlobalDir = "."; + } + else { #if ARCH_MAC - CFBundleRef bundle = CFBundleGetMainBundle(); - assert(bundle); - CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); - char resourcesBuf[PATH_MAX]; - Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); - assert(success); - CFRelease(resourcesUrl); - globalDir = resourcesBuf; - - // Get home directory - struct passwd *pw = getpwuid(getuid()); - assert(pw); - localDir = pw->pw_dir; - localDir += "/Documents/Rack"; + CFBundleRef bundle = CFBundleGetMainBundle(); + assert(bundle); + CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); + char resourcesBuf[PATH_MAX]; + Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); + assert(success); + CFRelease(resourcesUrl); + assetGlobalDir = resourcesBuf; #endif #if ARCH_WIN - char moduleBuf[MAX_PATH]; - DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf)); - assert(length > 0); - PathRemoveFileSpec(moduleBuf); - globalDir = moduleBuf; - - // Get "My Documents" folder - char documentsBuf[MAX_PATH]; - HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf); - assert(result == S_OK); - localDir = documentsBuf; - localDir += "/Rack"; + char moduleBuf[MAX_PATH]; + DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf)); + assert(length > 0); + PathRemoveFileSpec(moduleBuf); + assetGlobalDir = moduleBuf; #endif #if ARCH_LIN - // TODO For now, users should launch Rack from their terminal in the global directory - globalDir = "."; - - // Get home directory - const char *homeBuf = getenv("HOME"); - if (!homeBuf) { - struct passwd *pw = getpwuid(getuid()); - assert(pw); - homeBuf = pw->pw_dir; + // TODO For now, users should launch Rack from their terminal in the global directory + assetGlobalDir = "."; +#endif + } } - localDir = homeBuf; - localDir += "/.Rack"; + + if (assetLocalDir.empty()) { + if (devMode) { + assetLocalDir = "."; + } + else { +#if ARCH_WIN + // Get "My Documents" folder + char documentsBuf[MAX_PATH]; + HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf); + assert(result == S_OK); + assetLocalDir = documentsBuf; + assetLocalDir += "/Rack"; #endif - systemCreateDirectory(localDir); +#if ARCH_MAC + // Get home directory + struct passwd *pw = getpwuid(getuid()); + assert(pw); + assetLocalDir = pw->pw_dir; + assetLocalDir += "/Documents/Rack"; +#endif +#if ARCH_LIN + // Get home directory + const char *homeBuf = getenv("HOME"); + if (!homeBuf) { + struct passwd *pw = getpwuid(getuid()); + assert(pw); + homeBuf = pw->pw_dir; + } + assetLocalDir = homeBuf; + assetLocalDir += "/.Rack"; +#endif + } + } + + systemCreateDirectory(assetGlobalDir); + systemCreateDirectory(assetLocalDir); } std::string assetGlobal(std::string filename) { - return globalDir + "/" + filename; + return assetGlobalDir + "/" + filename; } std::string assetLocal(std::string filename) { - return localDir + "/" + filename; + return assetLocalDir + "/" + filename; } diff --git a/src/main.cpp b/src/main.cpp index 964525af..a5562176 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,11 +26,17 @@ int main(int argc, char* argv[]) { // Parse command line arguments int c; opterr = 0; - while ((c = getopt(argc, argv, "d")) != -1) { + while ((c = getopt(argc, argv, "dg:l:")) != -1) { switch (c) { case 'd': { devMode = true; } break; + case 'g': { + assetGlobalDir = optarg; + } break; + case 'l': { + assetLocalDir = optarg; + } break; default: break; } }