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.

130 lines
2.9KB

  1. #include "asset.hpp"
  2. #include "system.hpp"
  3. #include "settings.hpp"
  4. #include "string.hpp"
  5. #include "plugin/Plugin.hpp"
  6. #if defined ARCH_MAC
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <pwd.h>
  9. #endif
  10. #if defined ARCH_WIN
  11. #include <Windows.h>
  12. #include <Shlobj.h>
  13. #include <Shlwapi.h>
  14. #endif
  15. #if defined ARCH_LIN
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <pwd.h>
  19. #endif
  20. namespace rack {
  21. namespace asset {
  22. void init() {
  23. // Get system dir
  24. if (systemDir.empty()) {
  25. if (settings::devMode) {
  26. systemDir = ".";
  27. }
  28. else {
  29. #if defined ARCH_MAC
  30. CFBundleRef bundle = CFBundleGetMainBundle();
  31. assert(bundle);
  32. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  33. char resourcesBuf[PATH_MAX];
  34. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
  35. assert(success);
  36. CFRelease(resourcesUrl);
  37. systemDir = resourcesBuf;
  38. #endif
  39. #if defined ARCH_WIN
  40. // Get path to executable
  41. wchar_t moduleBufW[MAX_PATH];
  42. DWORD length = GetModuleFileNameW(NULL, moduleBufW, LENGTHOF(moduleBufW));
  43. assert(length > 0);
  44. // Get folder of executable
  45. PathRemoveFileSpecW(moduleBufW);
  46. // Convert to short path to avoid Unicode
  47. wchar_t moduleBufShortW[MAX_PATH];
  48. GetShortPathNameW(moduleBufW, moduleBufShortW, LENGTHOF(moduleBufShortW));
  49. systemDir = string::fromWstring(moduleBufShortW);
  50. #endif
  51. #if defined ARCH_LIN
  52. // Users should launch Rack from their terminal in the system directory
  53. systemDir = ".";
  54. #endif
  55. }
  56. }
  57. // Get user dir
  58. if (userDir.empty()) {
  59. if (settings::devMode) {
  60. userDir = ".";
  61. }
  62. else {
  63. #if defined ARCH_WIN
  64. // Get "My Documents" folder
  65. wchar_t documentsBufW[MAX_PATH] = L".";
  66. HRESULT result = SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBufW);
  67. assert(result == S_OK);
  68. // Convert to short path to avoid Unicode
  69. wchar_t documentsBufShortW[MAX_PATH];
  70. GetShortPathNameW(documentsBufW, documentsBufShortW, LENGTHOF(documentsBufShortW));
  71. userDir = string::fromWstring(documentsBufShortW);
  72. userDir += "/Rack";
  73. #endif
  74. #if defined ARCH_MAC
  75. // Get home directory
  76. struct passwd *pw = getpwuid(getuid());
  77. assert(pw);
  78. userDir = pw->pw_dir;
  79. userDir += "/Documents/Rack";
  80. #endif
  81. #if defined ARCH_LIN
  82. // Get home directory
  83. const char *homeBuf = getenv("HOME");
  84. if (!homeBuf) {
  85. struct passwd *pw = getpwuid(getuid());
  86. assert(pw);
  87. homeBuf = pw->pw_dir;
  88. }
  89. userDir = homeBuf;
  90. userDir += "/.Rack";
  91. #endif
  92. }
  93. }
  94. system::createDirectory(systemDir);
  95. system::createDirectory(userDir);
  96. }
  97. std::string system(std::string filename) {
  98. return systemDir + "/" + filename;
  99. }
  100. std::string user(std::string filename) {
  101. return userDir + "/" + filename;
  102. }
  103. std::string plugin(plugin::Plugin *plugin, std::string filename) {
  104. assert(plugin);
  105. return plugin->path + "/" + filename;
  106. }
  107. std::string systemDir;
  108. std::string userDir;
  109. } // namespace asset
  110. } // namespace rack