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.

120 lines
2.4KB

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