Skip to content

Commit

Permalink
IGL: Move Windows path finding into FileLoader
Browse files Browse the repository at this point in the history
Summary: This diff moves all logic for finding the folder containing a 'resource' into FileLoader.

Reviewed By: corporateshark

Differential Revision: D49135949

fbshipit-source-id: c212b1659d8661efde5a1b8e52f2bfe11abf9f35
  • Loading branch information
Eric Griffith authored and facebook-github-bot committed Sep 15, 2023
1 parent 2bbb9d0 commit 7341893
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
35 changes: 30 additions & 5 deletions shell/shared/fileLoader/win/FileLoaderWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,38 @@ std::string FileLoaderWin::fullPath(const std::string& fileName) const {
return fileName;
}

auto absolutePath = std::filesystem::path(basePath_) / fileName;
if (std::filesystem::exists(absolutePath)) {
return absolutePath.string();
auto fullPath = std::filesystem::path(basePath_) / fileName;
if (std::filesystem::exists(fullPath)) {
return fullPath.string();
}

// passthrough, since there are no bundles on Windows
return fileName;
fullPath = std::filesystem::path("shell/resources/images") / fileName;
if (std::filesystem::exists(fullPath)) {
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();
}

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

IGL_ASSERT_NOT_REACHED();
return "";
}

} // namespace igl::shell
29 changes: 2 additions & 27 deletions shell/shared/imageLoader/win/ImageLoaderWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstring>
#include <filesystem>
#include <igl/Common.h>
#include <shell/shared/fileLoader/FileLoader.h>
#include <stb_image.h>
#include <stdint.h>

Expand All @@ -29,33 +30,7 @@ ImageLoaderWin::ImageLoaderWin(FileLoader& fileLoader) : ImageLoader(fileLoader)
}

ImageData ImageLoaderWin::loadImageData(std::string imageName) noexcept {
std::string fullName = imageName;

if (!std::filesystem::exists(fullName)) {
fullName = "shell/resources/images/" + imageName;
}

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

if (!std::filesystem::exists(fullName)) {
using namespace std::filesystem;
path dir = current_path();
const path subdir("images/");
fullName = (dir / subdir / imageName).string();
}

if (!std::filesystem::exists(fullName)) {
fullName = (std::filesystem::path(executablePath_) / imageName).string();
}
std::string fullName = fileLoader().fullPath(imageName);

// Load image from file in RGBA format
auto ret = ImageData();
Expand Down

0 comments on commit 7341893

Please sign in to comment.