This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindBlender.cpp
140 lines (124 loc) · 4.01 KB
/
FindBlender.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "FindBlender.hpp"
#include "hecl/SteamFinder.hpp"
#include "hecl/hecl.hpp"
namespace hecl::blender {
#ifdef __APPLE__
#define DEFAULT_BLENDER_BIN "/Applications/Blender.app/Contents/MacOS/blender"
#elif __linux__
#define DEFAULT_BLENDER_BIN "/usr/bin/blender"
#else
#define DEFAULT_BLENDER_BIN "blender"
#endif
static const std::regex regBlenderVersion(R"(Blender (\d+)\.(\d+)(?:\.(\d+))?)",
std::regex::ECMAScript | std::regex::optimize);
static bool RegFileExists(const hecl::SystemChar* path) {
if (!path)
return false;
hecl::Sstat theStat;
return !hecl::Stat(path, &theStat) && S_ISREG(theStat.st_mode);
}
hecl::SystemString FindBlender(int& major, int& minor) {
major = 0;
minor = 0;
/* User-specified blender path */
#if _WIN32
wchar_t BLENDER_BIN_BUF[2048];
const wchar_t* blenderBin = _wgetenv(L"BLENDER_BIN");
#else
const char* blenderBin = getenv("BLENDER_BIN");
#endif
/* Steam blender */
hecl::SystemString steamBlender;
/* Child process of blender */
#if _WIN32
if (!blenderBin || !RegFileExists(blenderBin)) {
/* Environment not set; try steam */
steamBlender = hecl::FindCommonSteamApp(_SYS_STR("Blender"));
if (steamBlender.size()) {
steamBlender += _SYS_STR("\\blender.exe");
blenderBin = steamBlender.c_str();
}
if (!RegFileExists(blenderBin)) {
/* No steam; try default */
wchar_t progFiles[256];
if (GetEnvironmentVariableW(L"ProgramFiles", progFiles, 256)) {
for (size_t major = MaxBlenderMajorSearch; major >= MinBlenderMajorSearch; --major) {
bool found = false;
for (size_t minor = MaxBlenderMinorSearch; minor > MinBlenderMinorSearch; --minor) {
_snwprintf(BLENDER_BIN_BUF, 2048, L"%s\\Blender Foundation\\Blender %i.%i\\blender.exe", progFiles, major,
minor);
if (RegFileExists(BLENDER_BIN_BUF)) {
blenderBin = BLENDER_BIN_BUF;
found = true;
break;
}
}
if (found) {
break;
}
}
}
}
}
#else
if (!RegFileExists(blenderBin)) {
/* Try steam */
steamBlender = hecl::FindCommonSteamApp(_SYS_STR("Blender"));
if (steamBlender.size()) {
#ifdef __APPLE__
steamBlender += "/blender.app/Contents/MacOS/blender";
#else
steamBlender += "/blender";
#endif
blenderBin = steamBlender.c_str();
if (!RegFileExists(blenderBin)) {
blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin)) {
blenderBin = nullptr;
}
}
} else {
blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin)) {
blenderBin = nullptr;
}
}
}
#endif
if (!blenderBin)
return {};
#if _WIN32
DWORD handle = 0;
DWORD infoSize = GetFileVersionInfoSizeW(blenderBin, &handle);
if (infoSize != NULL) {
auto* infoData = new char[infoSize];
if (GetFileVersionInfoW(blenderBin, handle, infoSize, infoData)) {
UINT size = 0;
LPVOID lpBuffer = nullptr;
if (VerQueryValueW(infoData, L"\\", &lpBuffer, &size) && size != 0u) {
auto* verInfo = static_cast<VS_FIXEDFILEINFO*>(lpBuffer);
if (verInfo->dwSignature == 0xfeef04bd) {
major = static_cast<int>((verInfo->dwFileVersionMS >> 16) & 0xffff);
minor = static_cast<int>((verInfo->dwFileVersionMS >> 0 & 0xffff) * 10 +
(verInfo->dwFileVersionLS >> 16 & 0xffff));
}
}
}
delete[] infoData;
}
#else
hecl::SystemString command = hecl::SystemString(_SYS_STR("\"")) + blenderBin + _SYS_STR("\" --version");
FILE* fp = popen(command.c_str(), "r");
char versionBuf[256];
size_t rdSize = fread(versionBuf, 1, 255, fp);
versionBuf[rdSize] = '\0';
pclose(fp);
std::cmatch match;
if (std::regex_search(versionBuf, match, regBlenderVersion)) {
major = atoi(match[1].str().c_str());
minor = atoi(match[2].str().c_str());
}
#endif
return blenderBin;
}
} // namespace hecl::blender