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.

asset.cpp 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "asset.hpp"
  2. #include "util.hpp"
  3. #include <assert.h>
  4. #include <sys/stat.h> // for mkdir
  5. #include "../ext/osdialog/osdialog.h"
  6. #if ARCH_MAC
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <pwd.h>
  9. #endif
  10. #if ARCH_WIN
  11. #include <Windows.h>
  12. #include <Shlobj.h>
  13. #endif
  14. #if ARCH_LIN
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <pwd.h>
  18. #endif
  19. namespace rack {
  20. std::string assetGlobal(std::string filename) {
  21. std::string dir;
  22. #if defined(RELEASE)
  23. #if ARCH_MAC
  24. CFBundleRef bundle = CFBundleGetMainBundle();
  25. assert(bundle);
  26. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  27. char buf[PATH_MAX];
  28. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8 *)buf, sizeof(buf));
  29. assert(success);
  30. CFRelease(resourcesUrl);
  31. dir = buf;
  32. #endif
  33. #if ARCH_WIN
  34. // Must launch Rack with the "Start In" directory as the global directory
  35. dir = ".";
  36. #endif
  37. #if ARCH_LIN
  38. // TODO For now, users should launch Rack from their terminal in the global directory
  39. dir = ".";
  40. #endif
  41. #else // RELEASE
  42. dir = ".";
  43. #endif // RELEASE
  44. return dir + "/" + filename;
  45. }
  46. std::string assetLocal(std::string filename) {
  47. std::string dir;
  48. #if defined(RELEASE)
  49. #if ARCH_MAC
  50. // Get home directory
  51. struct passwd *pw = getpwuid(getuid());
  52. assert(pw);
  53. dir = pw->pw_dir;
  54. dir += "/Documents/Rack";
  55. mkdir(dir.c_str(), 0755);
  56. #endif
  57. #if ARCH_WIN
  58. // Get "My Documents" folder
  59. char buf[MAX_PATH];
  60. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, buf);
  61. assert(result == S_OK);
  62. dir = buf;
  63. dir += "/Rack";
  64. CreateDirectory(dir.c_str(), NULL);
  65. #endif
  66. #if ARCH_LIN
  67. const char *home = getenv("HOME");
  68. if (!home) {
  69. struct passwd *pw = getpwuid(getuid());
  70. assert(pw);
  71. home = pw->pw_dir;
  72. }
  73. dir = home;
  74. dir += "/.Rack";
  75. mkdir(dir.c_str(), 0755);
  76. #endif
  77. #else // RELEASE
  78. dir = ".";
  79. #endif // RELEASE
  80. return dir + "/" + filename;
  81. }
  82. std::string assetPlugin(Plugin *plugin, std::string filename) {
  83. assert(plugin);
  84. return plugin->path + "/" + filename;
  85. }
  86. } // namespace rack