hecl/FindBlender: Fixes and cleanup

- GetEnv for Windows returns null-terminator in string
- GetEnv for POSIX could crash with a null pointer to std::string conversion
- FindBlender logic path simplified
This commit is contained in:
Luke Street 2021-11-21 16:39:57 -05:00
parent 8bc63e0385
commit 5c98ff8b13
2 changed files with 23 additions and 29 deletions

View File

@ -165,9 +165,13 @@ inline std::optional<std::string> GetEnv(const char* name) {
} }
auto wbuf = std::make_unique<wchar_t[]>(sz); auto wbuf = std::make_unique<wchar_t[]>(sz);
_wgetenv_s(&sz, wbuf.get(), sz, wname.get()); _wgetenv_s(&sz, wbuf.get(), sz, wname.get());
return nowide::narrow(wbuf.get(), sz); return nowide::narrow(wbuf.get(), sz - 1); // null-terminated
#else #else
return getenv(name); const auto* env = getenv(name);
if (env != nullptr) {
return env;
}
return {};
#endif #endif
#endif #endif
} }

View File

@ -41,30 +41,24 @@ std::optional<std::string> FindBlender(int& major, int& minor) {
minor = 0; minor = 0;
/* User-specified blender path */ /* User-specified blender path */
#if _WIN32
auto blenderBin = GetEnv("BLENDER_BIN"); auto blenderBin = GetEnv("BLENDER_BIN");
#else if (blenderBin && !RegFileExists(blenderBin->c_str())) {
const char* cblenderBin = getenv("BLENDER_BIN"); blenderBin.reset();
std::optional<std::string> blenderBin{};
if (cblenderBin != nullptr) {
blenderBin = cblenderBin;
} }
#endif
/* Steam blender */
std::string steamBlender;
/* Child process of blender */ /* Child process of blender */
#if _WIN32 #if _WIN32
if (!blenderBin || !RegFileExists(blenderBin->c_str())) { if (!blenderBin) {
/* Environment not set; try steam */ /* Environment not set; try steam */
steamBlender = hecl::FindCommonSteamApp("Blender"); std::string steamBlender = hecl::FindCommonSteamApp("Blender");
if (steamBlender.size()) { if (!steamBlender.empty()) {
steamBlender += "\\blender.exe"; steamBlender += "\\blender.exe";
blenderBin = steamBlender.c_str(); if (RegFileExists(steamBlender.c_str())) {
blenderBin = std::move(steamBlender);
}
} }
if (!RegFileExists(blenderBin->c_str())) { if (!blenderBin) {
/* No steam; try default */ /* No steam; try default */
wchar_t wProgFiles[256]; wchar_t wProgFiles[256];
if (GetEnvironmentVariableW(L"ProgramFiles", wProgFiles, 256)) { if (GetEnvironmentVariableW(L"ProgramFiles", wProgFiles, 256)) {
@ -90,33 +84,29 @@ std::optional<std::string> FindBlender(int& major, int& minor) {
} }
#else #else
if (!RegFileExists(blenderBin->c_str())) { if (!blenderBin) {
/* Try steam */ /* Try steam */
steamBlender = hecl::FindCommonSteamApp("Blender"); std::string steamBlender = hecl::FindCommonSteamApp("Blender");
if (steamBlender.size()) { if (steamBlender.size()) {
#ifdef __APPLE__ #ifdef __APPLE__
steamBlender += "/blender.app/Contents/MacOS/blender"; steamBlender += "/blender.app/Contents/MacOS/blender";
#else #else
steamBlender += "/blender"; steamBlender += "/blender";
#endif #endif
blenderBin = steamBlender; if (RegFileExists(steamBlender->c_str())) {
if (!RegFileExists(blenderBin->c_str())) { blenderBin = std::move(steamBlender);
} else if (RegFileExists(DEFAULT_BLENDER_BIN)) {
blenderBin = DEFAULT_BLENDER_BIN; blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin->c_str())) {
blenderBin.reset();
}
} }
} else { } else if (RegFileExists(DEFAULT_BLENDER_BIN)) {
blenderBin = DEFAULT_BLENDER_BIN; blenderBin = DEFAULT_BLENDER_BIN;
if (!RegFileExists(blenderBin->c_str())) {
blenderBin.reset();
}
} }
} }
#endif #endif
if (!blenderBin) if (!blenderBin) {
return {}; return {};
}
#if _WIN32 #if _WIN32
const nowide::wstackstring wblenderBin(blenderBin.value()); const nowide::wstackstring wblenderBin(blenderBin.value());