diff --git a/include/asset.hpp b/include/asset.hpp index ab4d860b..428f0977 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -7,7 +7,7 @@ namespace rack { -void assetInit(bool devMode); +void assetInit(bool devMode, std::string customGlobalDir = std::string(), std::string customLocalDir = std::string()); /** Returns the path of a global resource. Should only read files from this location. */ std::string assetGlobal(std::string filename); /** Returns the path of a local resource. Can read and write files to this location. */ diff --git a/src/asset.cpp b/src/asset.cpp index f73882b1..51f30f0b 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -27,7 +27,7 @@ static std::string globalDir; static std::string localDir; -void assetInit(bool devMode) { +void assetInit(bool devMode, std::string customGlobalDir, std::string customLocalDir) { if (devMode) { // Use current working directory if running in development mode globalDir = "."; @@ -35,50 +35,65 @@ void assetInit(bool devMode) { return; } + if (customGlobalDir.empty()) { + #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); + globalDir = 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); + globalDir = 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 + globalDir = "."; +#endif + } + else { + globalDir = customGlobalDir; } - localDir = homeBuf; - localDir += "/.Rack"; + + if (customGlobalDir.empty()) { +#if ARCH_MAC + // Get home directory + struct passwd *pw = getpwuid(getuid()); + assert(pw); + localDir = pw->pw_dir; + localDir += "/Documents/Rack"; #endif +#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); + localDir = documentsBuf; + localDir += "/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; + } + localDir = homeBuf; + localDir += "/.Rack"; +#endif + } + else { + localDir = customLocalDir; + } } diff --git a/src/main.cpp b/src/main.cpp index ba3a1ea1..5945b462 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,11 +24,15 @@ using namespace rack; int main(int argc, char* argv[]) { bool devMode = false; std::string patchFile; + std::string customLocalDir; + std::string customGlobalDir; // Parse command line arguments argagg::parser argparser {{ { "help", {"-h", "--help"}, "shows this help message", 0}, - { "devmod", {"-d", "--devmod"}, "enable dev mode", 0}, + { "devmod", {"-d", "--devmod"}, "enables dev mode (supersedes local/global folders)", 0}, + { "global", {"-g", "--globaldir"}, "set golbalDir", 1}, + { "local", {"-l", "--localdir"}, "set localDir", 1}, }}; argagg::parser_results args; @@ -50,6 +54,14 @@ int main(int argc, char* argv[]) { devMode = true; } + if (args["global"]) { + customGlobalDir = args["global"].as(); + } + + if (args["local"]) { + customLocalDir = args["local"].as(); + } + // Filename as first positional argument if (args.pos.size() > 0) { patchFile = args.as(0); @@ -57,7 +69,7 @@ int main(int argc, char* argv[]) { // Initialize environment randomInit(); - assetInit(devMode); + assetInit(devMode, customGlobalDir, customLocalDir); loggerInit(devMode); // Log environment