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. std::string assetGlobalDir;
  20. std::string assetLocalDir;
  21. void assetInit(bool devMode) {
  22. if (assetGlobalDir.empty()) {
  23. if (devMode) {
  24. assetGlobalDir = ".";
  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. assetGlobalDir = 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. assetGlobalDir = moduleBuf;
  43. #endif
  44. #if ARCH_LIN
  45. // TODO For now, users should launch Rack from their terminal in the global directory
  46. assetGlobalDir = ".";
  47. #endif
  48. }
  49. }
  50. if (assetLocalDir.empty()) {
  51. if (devMode) {
  52. assetLocalDir = ".";
  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. assetLocalDir = documentsBuf;
  61. assetLocalDir += "/Rack";
  62. #endif
  63. #if ARCH_MAC
  64. // Get home directory
  65. struct passwd *pw = getpwuid(getuid());
  66. assert(pw);
  67. assetLocalDir = pw->pw_dir;
  68. assetLocalDir += "/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. assetLocalDir = homeBuf;
  79. assetLocalDir += "/.Rack";
  80. #endif
  81. }
  82. }
  83. systemCreateDirectory(assetGlobalDir);
  84. systemCreateDirectory(assetLocalDir);
  85. }
  86. std::string assetGlobal(std::string filename) {
  87. return assetGlobalDir + "/" + filename;
  88. }
  89. std::string assetLocal(std::string filename) {
  90. return assetLocalDir + "/" + filename;
  91. }
  92. std::string assetPlugin(Plugin *plugin, std::string filename) {
  93. assert(plugin);
  94. return plugin->path + "/" + filename;
  95. }
  96. } // namespace rack