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.

117 lines
2.3KB

  1. #if ARCH_MAC
  2. #include <CoreFoundation/CoreFoundation.h>
  3. #include <pwd.h>
  4. #endif
  5. #if ARCH_WIN
  6. #include <Windows.h>
  7. #include <Shlobj.h>
  8. #include <Shlwapi.h>
  9. #endif
  10. #if ARCH_LIN
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <pwd.h>
  14. #endif
  15. #include "asset.hpp"
  16. #include "system.hpp"
  17. namespace rack {
  18. namespace asset {
  19. std::string globalDir;
  20. std::string localDir;
  21. void init(bool devMode) {
  22. if (globalDir.empty()) {
  23. if (devMode) {
  24. globalDir = ".";
  25. }
  26. else {
  27. #if ARCH_MAC
  28. CFBundleRef bundle = CFBundleGetMainBundle();
  29. assert(bundle);
  30. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  31. char resourcesBuf[PATH_MAX];
  32. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
  33. assert(success);
  34. CFRelease(resourcesUrl);
  35. globalDir = resourcesBuf;
  36. #endif
  37. #if ARCH_WIN
  38. char moduleBuf[MAX_PATH];
  39. DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
  40. assert(length > 0);
  41. PathRemoveFileSpec(moduleBuf);
  42. globalDir = moduleBuf;
  43. #endif
  44. #if ARCH_LIN
  45. // TODO For now, users should launch Rack from their terminal in the global directory
  46. globalDir = ".";
  47. #endif
  48. }
  49. }
  50. if (localDir.empty()) {
  51. if (devMode) {
  52. localDir = ".";
  53. }
  54. else {
  55. #if ARCH_WIN
  56. // Get "My Documents" folder
  57. char documentsBuf[MAX_PATH];
  58. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
  59. assert(result == S_OK);
  60. localDir = documentsBuf;
  61. localDir += "/Rack";
  62. #endif
  63. #if ARCH_MAC
  64. // Get home directory
  65. struct passwd *pw = getpwuid(getuid());
  66. assert(pw);
  67. localDir = pw->pw_dir;
  68. localDir += "/Documents/Rack";
  69. #endif
  70. #if ARCH_LIN
  71. // Get home directory
  72. const char *homeBuf = getenv("HOME");
  73. if (!homeBuf) {
  74. struct passwd *pw = getpwuid(getuid());
  75. assert(pw);
  76. homeBuf = pw->pw_dir;
  77. }
  78. localDir = homeBuf;
  79. localDir += "/.Rack";
  80. #endif
  81. }
  82. }
  83. system::createDirectory(globalDir);
  84. system::createDirectory(localDir);
  85. }
  86. std::string global(std::string filename) {
  87. return globalDir + "/" + filename;
  88. }
  89. std::string local(std::string filename) {
  90. return localDir + "/" + filename;
  91. }
  92. std::string plugin(Plugin *plugin, std::string filename) {
  93. assert(plugin);
  94. return plugin->path + "/" + filename;
  95. }
  96. } // namespace asset
  97. } // namespace rack