Skip to content

Commit

Permalink
shell | More paths to search for files in FileLoaderWin
Browse files Browse the repository at this point in the history
Summary: Added more paths to search for demo content in `FileLoaderWin`.

Reviewed By: EricGriffith

Differential Revision: D50805951

fbshipit-source-id: ff7e4d86dfe65cd61f1b82ab47251aed5c8c5c90
  • Loading branch information
corporateshark authored and facebook-github-bot committed Oct 31, 2023
1 parent 7cfb5e4 commit 85d52e8
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions shell/shared/fileLoader/win/FileLoaderWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <shell/shared/fileLoader/win/FileLoaderWin.h>

#include <array>
#include <filesystem>
#include <fstream>
#include <igl/Common.h>
Expand All @@ -17,6 +18,33 @@
#include <windows.h>
#endif

namespace {

std::string findSubdir(const char* subdir, const std::string& fileName) {
std::filesystem::path dir = std::filesystem::current_path();
// find `subdir` somewhere above our current directory
while (dir != std::filesystem::current_path().root_path() &&
!std::filesystem::exists(dir / subdir)) {
dir = dir.parent_path();
}

std::filesystem::path fullPath = (dir / subdir / fileName);
if (std::filesystem::exists(fullPath)) {
return fullPath.string();
}

dir = std::filesystem::current_path();

fullPath = (dir / "images/" / fileName);
if (std::filesystem::exists(fullPath)) {
return fullPath.string();
}

return std::string();
}

} // namespace

namespace igl::shell {

FileLoaderWin::FileLoaderWin() {
Expand Down Expand Up @@ -59,24 +87,16 @@ std::string FileLoaderWin::fullPath(const std::string& fileName) const {
return fullPath.string();
}

std::filesystem::path dir = std::filesystem::current_path();
// find `shell/resources/images/` somewhere above our current directory
std::filesystem::path subdir("shell/resources/images/");
while (dir != std::filesystem::current_path().root_path() &&
!std::filesystem::exists(dir / subdir)) {
dir = dir.parent_path();
}

fullPath = (dir / subdir / fileName);
if (std::filesystem::exists(fullPath)) {
return fullPath.string();
}
constexpr std::array<const char*, 4> folders = {"shell/resources/images/",
"samples/resources/images/",
"samples/resources/models/",
"samples/resources/fonts/"};

dir = std::filesystem::current_path();
subdir = "images/";
fullPath = (dir / subdir / fileName);
if (std::filesystem::exists(fullPath)) {
return fullPath.string();
// find folders somewhere above our current directory
for (const char* folder : folders) {
if (std::string p = findSubdir(folder, fileName); !p.empty()) {
return p;
}
}

IGL_ASSERT_NOT_REACHED();
Expand Down

0 comments on commit 85d52e8

Please sign in to comment.