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.

102 lines
2.1KB

  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) {
  22. if (devMode) {
  23. // Use current working directory if running in development mode
  24. globalDir = ".";
  25. localDir = ".";
  26. return;
  27. }
  28. #if ARCH_MAC
  29. CFBundleRef bundle = CFBundleGetMainBundle();
  30. assert(bundle);
  31. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  32. char resourcesBuf[PATH_MAX];
  33. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
  34. assert(success);
  35. CFRelease(resourcesUrl);
  36. globalDir = resourcesBuf;
  37. // Get home directory
  38. struct passwd *pw = getpwuid(getuid());
  39. assert(pw);
  40. localDir = pw->pw_dir;
  41. localDir += "/Documents/Rack";
  42. #endif
  43. #if ARCH_WIN
  44. char moduleBuf[MAX_PATH];
  45. DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
  46. assert(length > 0);
  47. PathRemoveFileSpec(moduleBuf);
  48. globalDir = moduleBuf;
  49. // Get "My Documents" folder
  50. char documentsBuf[MAX_PATH];
  51. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
  52. assert(result == S_OK);
  53. localDir = documentsBuf;
  54. localDir += "/Rack";
  55. #endif
  56. #if ARCH_LIN
  57. // TODO For now, users should launch Rack from their terminal in the global directory
  58. globalDir = ".";
  59. // Get home directory
  60. const char *homeBuf = getenv("HOME");
  61. if (!homeBuf) {
  62. struct passwd *pw = getpwuid(getuid());
  63. assert(pw);
  64. homeBuf = pw->pw_dir;
  65. }
  66. localDir = homeBuf;
  67. localDir += "/.Rack";
  68. #endif
  69. }
  70. std::string assetGlobal(std::string filename) {
  71. return globalDir + "/" + filename;
  72. }
  73. std::string assetLocal(std::string filename) {
  74. return localDir + "/" + filename;
  75. }
  76. std::string assetPlugin(Plugin *plugin, std::string filename) {
  77. assert(plugin);
  78. return plugin->path + "/" + filename;
  79. }
  80. } // namespace rack