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.

110 lines
2.4KB

  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. namespace rack {
  15. #if ARCH_MAC
  16. /** Is it actually difficult to determine whether we are running in a Mac bundle or not.
  17. This heuristically guesses based on the existence of a Resources directory
  18. */
  19. static bool isBundle() {
  20. CFBundleRef bundle = CFBundleGetMainBundle();
  21. if (bundle) {
  22. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  23. char buf[PATH_MAX];
  24. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8 *)buf, sizeof(buf));
  25. assert(success);
  26. CFRelease(resourcesUrl);
  27. if (extractFilename(buf) == "Resources")
  28. return true;
  29. }
  30. return false;
  31. }
  32. #endif
  33. std::string assetGlobal(std::string filename) {
  34. std::string path;
  35. #if ARCH_MAC
  36. CFBundleRef bundle = CFBundleGetMainBundle();
  37. if (bundle && isBundle()) {
  38. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  39. char buf[PATH_MAX];
  40. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8 *)buf, sizeof(buf));
  41. assert(success);
  42. CFRelease(resourcesUrl);
  43. path = buf;
  44. }
  45. else {
  46. path = ".";
  47. }
  48. path += "/" + filename;
  49. #endif
  50. #if ARCH_WIN
  51. path = "./" + filename;
  52. #endif
  53. #if ARCH_LIN
  54. path = "./" + filename;
  55. #endif
  56. return path;
  57. }
  58. std::string assetLocal(std::string filename) {
  59. std::string path;
  60. #if ARCH_MAC
  61. if (isBundle()) {
  62. // Get home directory
  63. struct passwd *pw = getpwuid(getuid());
  64. assert(pw);
  65. path = pw->pw_dir;
  66. path += "/Documents/Rack";
  67. mkdir(path.c_str(), 0755);
  68. }
  69. else {
  70. path = ".";
  71. }
  72. path += "/" + filename;
  73. #endif
  74. #if ARCH_WIN
  75. // Get My Documents folder
  76. char buf[MAX_PATH];
  77. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, buf);
  78. assert(result == S_OK);
  79. path = buf;
  80. path += "/Rack";
  81. CreateDirectory(path.c_str(), NULL);
  82. path += "/" + filename;
  83. #endif
  84. #if ARCH_LIN
  85. // TODO
  86. // If Rack is "installed" (however that may be defined), look in ~/.Rack or something instead
  87. path = "./" + filename;
  88. #endif
  89. return path;
  90. }
  91. std::string assetPlugin(Plugin *plugin, std::string filename) {
  92. assert(plugin);
  93. std::string path;
  94. path = plugin->path + "/" + filename;
  95. return path;
  96. }
  97. } // namespace rack