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.

135 lines
3.2KB

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