You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.8KB

  1. #if defined ARCH_MAC
  2. #include <CoreFoundation/CoreFoundation.h>
  3. #include <CoreServices/CoreServices.h>
  4. #include <pwd.h>
  5. #endif
  6. #if defined ARCH_WIN
  7. #include <Windows.h>
  8. #include <Shlobj.h>
  9. #include <Shlwapi.h>
  10. #endif
  11. #if defined ARCH_LIN
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15. #endif
  16. #include <asset.hpp>
  17. #include <system.hpp>
  18. #include <settings.hpp>
  19. #include <string.hpp>
  20. #include <plugin/Plugin.hpp>
  21. #include <app/common.hpp>
  22. namespace rack {
  23. namespace asset {
  24. static void initSystemDir() {
  25. if (systemDir != "")
  26. return;
  27. if (settings::devMode) {
  28. systemDir = ".";
  29. return;
  30. }
  31. #if defined ARCH_MAC
  32. CFBundleRef bundle = CFBundleGetMainBundle();
  33. assert(bundle);
  34. // Check if we're running as a command-line program or an app bundle.
  35. CFURLRef bundleUrl = CFBundleCopyBundleURL(bundle);
  36. // Thanks Ken Thomases! https://stackoverflow.com/a/58369256/272642
  37. CFStringRef uti;
  38. if (CFURLCopyResourcePropertyForKey(bundleUrl, kCFURLTypeIdentifierKey, &uti, NULL) && uti && UTTypeConformsTo(uti, kUTTypeApplicationBundle)) {
  39. char bundleBuf[PATH_MAX];
  40. Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf));
  41. assert(success);
  42. bundlePath = bundleBuf;
  43. }
  44. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  45. char resourcesBuf[PATH_MAX];
  46. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
  47. assert(success);
  48. CFRelease(resourcesUrl);
  49. systemDir = resourcesBuf;
  50. #endif
  51. #if defined ARCH_WIN
  52. // Get path to executable
  53. wchar_t moduleBufW[MAX_PATH];
  54. DWORD length = GetModuleFileNameW(NULL, moduleBufW, LENGTHOF(moduleBufW));
  55. assert(length > 0);
  56. // Get folder of executable
  57. PathRemoveFileSpecW(moduleBufW);
  58. systemDir = string::fromWstring(moduleBufW);
  59. #endif
  60. #if defined ARCH_LIN
  61. // Users should launch Rack from their terminal in the system directory
  62. systemDir = ".";
  63. #endif
  64. }
  65. static void initUserDir() {
  66. if (userDir != "")
  67. return;
  68. if (settings::devMode) {
  69. userDir = ".";
  70. return;
  71. }
  72. #if defined ARCH_WIN
  73. // Get "My Documents" folder
  74. wchar_t documentsBufW[MAX_PATH] = L".";
  75. HRESULT result = SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBufW);
  76. assert(result == S_OK);
  77. userDir = string::fromWstring(documentsBufW);
  78. userDir += "/Rack";
  79. #endif
  80. #if defined ARCH_MAC
  81. // Get home directory
  82. struct passwd* pw = getpwuid(getuid());
  83. assert(pw);
  84. userDir = pw->pw_dir;
  85. userDir += "/Documents/Rack";
  86. #endif
  87. #if defined ARCH_LIN
  88. // Get home directory
  89. const char* homeBuf = getenv("HOME");
  90. if (!homeBuf) {
  91. struct passwd* pw = getpwuid(getuid());
  92. assert(pw);
  93. homeBuf = pw->pw_dir;
  94. }
  95. userDir = homeBuf;
  96. userDir += "/.Rack";
  97. #endif
  98. }
  99. void init() {
  100. initSystemDir();
  101. initUserDir();
  102. system::createDirectory(systemDir);
  103. system::createDirectory(userDir);
  104. // Set paths
  105. if (settings::devMode) {
  106. pluginsPath = userDir + "/plugins";
  107. settingsPath = userDir + "/settings.json";
  108. autosavePath = userDir + "/autosave.vcv";
  109. templatePath = userDir + "/template.vcv";
  110. }
  111. else {
  112. logPath = userDir + "/log.txt";
  113. pluginsPath = userDir + "/plugins-v" + ABI_VERSION;
  114. settingsPath = userDir + "/settings-v" + ABI_VERSION + ".json";
  115. autosavePath = userDir + "/autosave-v" + ABI_VERSION + ".vcv";
  116. templatePath = userDir + "/template-v" + ABI_VERSION + ".vcv";
  117. }
  118. }
  119. std::string system(std::string filename) {
  120. return systemDir + "/" + filename;
  121. }
  122. std::string user(std::string filename) {
  123. return userDir + "/" + filename;
  124. }
  125. std::string plugin(plugin::Plugin* plugin, std::string filename) {
  126. assert(plugin);
  127. return plugin->path + "/" + filename;
  128. }
  129. std::string systemDir;
  130. std::string userDir;
  131. std::string logPath;
  132. std::string pluginsPath;
  133. std::string settingsPath;
  134. std::string autosavePath;
  135. std::string templatePath;
  136. std::string bundlePath;
  137. } // namespace asset
  138. } // namespace rack