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.

116 lines
2.4KB

  1. #include "asset.hpp"
  2. #include "util/common.hpp"
  3. #include "osdialog.h"
  4. #if ARCH_MAC
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <pwd.h>
  7. #endif
  8. #if ARCH_WIN
  9. #include <Windows.h>
  10. #include <Shlobj.h>
  11. #include <Shlwapi.h>
  12. #endif
  13. #if ARCH_LIN
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <pwd.h>
  17. #endif
  18. namespace rack {
  19. static std::string globalDir;
  20. static std::string localDir;
  21. void assetInit(bool devMode, std::string customGlobalDir, std::string customLocalDir) {
  22. if (devMode) {
  23. // Use current working directory if running in development mode
  24. globalDir = ".";
  25. localDir = ".";
  26. return;
  27. }
  28. if (customGlobalDir.empty()) {
  29. #if 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. globalDir = resourcesBuf;
  38. #endif
  39. #if ARCH_WIN
  40. char moduleBuf[MAX_PATH];
  41. DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
  42. assert(length > 0);
  43. PathRemoveFileSpec(moduleBuf);
  44. globalDir = moduleBuf;
  45. #endif
  46. #if ARCH_LIN
  47. // TODO For now, users should launch Rack from their terminal in the global directory
  48. globalDir = ".";
  49. #endif
  50. }
  51. else {
  52. globalDir = customGlobalDir;
  53. }
  54. if (customLocalDir.empty()) {
  55. #if ARCH_MAC
  56. // Get home directory
  57. struct passwd *pw = getpwuid(getuid());
  58. assert(pw);
  59. localDir = pw->pw_dir;
  60. localDir += "/Documents/Rack";
  61. #endif
  62. #if ARCH_WIN
  63. // Get "My Documents" folder
  64. char documentsBuf[MAX_PATH];
  65. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
  66. assert(result == S_OK);
  67. localDir = documentsBuf;
  68. localDir += "/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. else {
  83. localDir = customLocalDir;
  84. }
  85. }
  86. std::string assetGlobal(std::string filename) {
  87. return globalDir + "/" + filename;
  88. }
  89. std::string assetLocal(std::string filename) {
  90. return localDir + "/" + filename;
  91. }
  92. std::string assetPlugin(Plugin *plugin, std::string filename) {
  93. assert(plugin);
  94. return plugin->path + "/" + filename;
  95. }
  96. } // namespace rack