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"
|
2018-01-11 16:28:44 +00:00
|
|
|
#elif __linux__
|
|
|
|
#define DEFAULT_BLENDER_BIN "/usr/bin/blender"
|
2018-01-10 06:19:48 +00:00
|
|
|
#else
|
|
|
|
#define DEFAULT_BLENDER_BIN "blender"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const std::regex regBlenderVersion(R"(Blender ([0-9]+).([0-9]+) )",
|
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)) {
|
|
|
|
_snwprintf(BLENDER_BIN_BUF, 2048, L"%s\\Blender Foundation\\Blender\\blender.exe", progFiles);
|
|
|
|
blenderBin = BLENDER_BIN_BUF;
|
2018-01-10 06:19:48 +00:00
|
|
|
if (!RegFileExists(blenderBin))
|
2018-12-08 05:19:40 +00:00
|
|
|
blenderBin = nullptr;
|
|
|
|
} else
|
|
|
|
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
|
|
|
#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-01-11 16:28:44 +00:00
|
|
|
}
|
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
|
|
|
|
2018-12-08 05:19:40 +00:00
|
|
|
hecl::SystemString command = hecl::SystemString(_SYS_STR("\"")) + blenderBin + _SYS_STR("\" --version");
|
2018-01-10 06:19:48 +00:00
|
|
|
#if _WIN32
|
2018-12-08 05:19:40 +00:00
|
|
|
FILE* fp = _wpopen(command.c_str(), _SYS_STR("r"));
|
2018-01-10 06:19:48 +00:00
|
|
|
#else
|
2018-12-08 05:19:40 +00:00
|
|
|
FILE* fp = popen(command.c_str(), "r");
|
2018-01-10 06:19:48 +00:00
|
|
|
#endif
|
2018-12-08 05:19:40 +00:00
|
|
|
char versionBuf[256];
|
|
|
|
size_t rdSize = fread(versionBuf, 1, 255, fp);
|
|
|
|
versionBuf[rdSize] = '\0';
|
2018-01-10 06:19:48 +00:00
|
|
|
#if _WIN32
|
2018-12-08 05:19:40 +00:00
|
|
|
_pclose(fp);
|
2018-01-10 06:19:48 +00:00
|
|
|
#else
|
2018-12-08 05:19:40 +00:00
|
|
|
pclose(fp);
|
2018-01-10 06:19:48 +00:00
|
|
|
#endif
|
|
|
|
|
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());
|
2018-01-10 06:19:48 +00:00
|
|
|
return blenderBin;
|
2018-12-08 05:19:40 +00:00
|
|
|
}
|
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
|