forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.cpp
115 lines (96 loc) · 2.41 KB
/
asset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "asset.hpp"
#include "util/common.hpp"
#include "osdialog.h"
#if ARCH_MAC
#include <CoreFoundation/CoreFoundation.h>
#include <pwd.h>
#endif
#if ARCH_WIN
#include <Windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
#endif
#if ARCH_LIN
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif
namespace rack {
std::string assetGlobalDir;
std::string assetLocalDir;
void assetInit(bool devMode) {
if (assetGlobalDir.empty()) {
if (devMode) {
assetGlobalDir = ".";
}
else {
#if ARCH_MAC
CFBundleRef bundle = CFBundleGetMainBundle();
assert(bundle);
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
char resourcesBuf[PATH_MAX];
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
assert(success);
CFRelease(resourcesUrl);
assetGlobalDir = resourcesBuf;
#endif
#if ARCH_WIN
char moduleBuf[MAX_PATH];
DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
assert(length > 0);
PathRemoveFileSpec(moduleBuf);
assetGlobalDir = moduleBuf;
#endif
#if ARCH_LIN
// TODO For now, users should launch Rack from their terminal in the global directory
assetGlobalDir = ".";
#endif
}
}
if (assetLocalDir.empty()) {
if (devMode) {
assetLocalDir = ".";
}
else {
#if ARCH_WIN
// Get "My Documents" folder
char documentsBuf[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
assert(result == S_OK);
assetLocalDir = documentsBuf;
assetLocalDir += "/Rack";
#endif
#if ARCH_MAC
// Get home directory
struct passwd *pw = getpwuid(getuid());
assert(pw);
assetLocalDir = pw->pw_dir;
assetLocalDir += "/Documents/Rack";
#endif
#if ARCH_LIN
// Get home directory
const char *homeBuf = getenv("HOME");
if (!homeBuf) {
struct passwd *pw = getpwuid(getuid());
assert(pw);
homeBuf = pw->pw_dir;
}
assetLocalDir = homeBuf;
assetLocalDir += "/.Rack";
#endif
}
}
systemCreateDirectory(assetGlobalDir);
systemCreateDirectory(assetLocalDir);
}
std::string assetGlobal(std::string filename) {
return assetGlobalDir + "/" + filename;
}
std::string assetLocal(std::string filename) {
return assetLocalDir + "/" + filename;
}
std::string assetPlugin(Plugin *plugin, std::string filename) {
assert(plugin);
return plugin->path + "/" + filename;
}
} // namespace rack