metaforce/metaforce-gui/FindBlender.cpp

141 lines
4.0 KiB
C++
Raw Permalink Normal View History

2018-01-10 06:19:48 +00:00
#include "FindBlender.hpp"
#include "hecl/SteamFinder.hpp"
#include "hecl/hecl.hpp"
2018-12-08 05:19:40 +00:00
namespace hecl::blender {
2018-01-10 06:19:48 +00:00
#ifdef __APPLE__
#define DEFAULT_BLENDER_BIN "/Applications/Blender.app/Contents/MacOS/blender"
#elif __linux__
#define DEFAULT_BLENDER_BIN "/usr/bin/blender"
2018-01-10 06:19:48 +00:00
#else
#define DEFAULT_BLENDER_BIN "blender"
#endif
2020-06-24 15:44:00 +00:00
static const std::regex regBlenderVersion(R"(Blender (\d+)\.(\d+)(?:\.(\d+))?)",
2018-12-08 05:19:40 +00:00
std::regex::ECMAScript | std::regex::optimize);
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
static bool RegFileExists(const hecl::SystemChar* path) {
if (!path)
return false;
hecl::Sstat theStat;
return !hecl::Stat(path, &theStat) && S_ISREG(theStat.st_mode);
2018-01-10 06:19:48 +00:00
}
2018-12-08 05:19:40 +00:00
hecl::SystemString FindBlender(int& major, int& minor) {
major = 0;
minor = 0;
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
/* User-specified blender path */
2018-01-10 06:19:48 +00:00
#if _WIN32
2018-12-08 05:19:40 +00:00
wchar_t BLENDER_BIN_BUF[2048];
const wchar_t* blenderBin = _wgetenv(L"BLENDER_BIN");
2018-01-10 06:19:48 +00:00
#else
2018-12-08 05:19:40 +00:00
const char* blenderBin = getenv("BLENDER_BIN");
2018-01-10 06:19:48 +00:00
#endif
2018-12-08 05:19:40 +00:00
/* Steam blender */
hecl::SystemString steamBlender;
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
/* Child process of blender */
2018-01-10 06:19:48 +00:00
#if _WIN32
2018-12-08 05:19:40 +00:00
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();
}
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
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;
}
}
}
2018-01-10 06:19:48 +00:00
}
2018-12-08 05:19:40 +00:00
}
2018-01-10 06:19:48 +00:00
#else
2018-12-08 05:19:40 +00:00
if (!RegFileExists(blenderBin)) {
/* Try steam */
steamBlender = hecl::FindCommonSteamApp(_SYS_STR("Blender"));
if (steamBlender.size()) {
2018-01-10 06:19:48 +00:00
#ifdef __APPLE__
2018-12-08 05:19:40 +00:00
steamBlender += "/blender.app/Contents/MacOS/blender";
2018-01-10 06:19:48 +00:00
#else
2018-12-08 05:19:40 +00:00
steamBlender += "/blender";
2018-01-10 06:19:48 +00:00
#endif
2018-12-08 05:19:40 +00:00
blenderBin = steamBlender.c_str();
if (!RegFileExists(blenderBin)) {
blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin)) {
blenderBin = nullptr;
}
2018-12-08 05:19:40 +00:00
}
} else {
blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin)) {
blenderBin = nullptr;
}
2018-01-10 06:19:48 +00:00
}
2018-12-08 05:19:40 +00:00
}
2018-01-10 06:19:48 +00:00
#endif
2018-12-08 05:19:40 +00:00
if (!blenderBin)
return {};
2018-01-10 06:19:48 +00:00
#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;
}
2018-01-10 06:19:48 +00:00
#else
hecl::SystemString command = hecl::SystemString(_SYS_STR("\"")) + blenderBin + _SYS_STR("\" --version");
2018-12-08 05:19:40 +00:00
FILE* fp = popen(command.c_str(), "r");
char versionBuf[256];
size_t rdSize = fread(versionBuf, 1, 255, fp);
versionBuf[rdSize] = '\0';
pclose(fp);
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
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
2018-01-10 06:19:48 +00:00
2018-12-08 05:19:40 +00:00
return blenderBin;
2018-01-10 06:19:48 +00:00
}
2018-12-08 05:19:40 +00:00
} // namespace hecl::blender