From 924a759641bd382e80414db9e4b17beef2bba6a8 Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Fri, 17 Jan 2025 15:10:28 +0100 Subject: [PATCH] Add ResourceLoader::resource_exists --- liboscar/Platform/FilesystemResourceLoader.cpp | 6 ++++++ liboscar/Platform/FilesystemResourceLoader.h | 1 + liboscar/Platform/IResourceLoader.h | 2 ++ liboscar/Platform/ResourceLoader.h | 5 +++++ liboscar/Platform/ResourceLoader.tests.cpp | 15 +++++++++++++++ 5 files changed, 29 insertions(+) diff --git a/liboscar/Platform/FilesystemResourceLoader.cpp b/liboscar/Platform/FilesystemResourceLoader.cpp index 396558968b..6d24cb7e33 100644 --- a/liboscar/Platform/FilesystemResourceLoader.cpp +++ b/liboscar/Platform/FilesystemResourceLoader.cpp @@ -19,6 +19,12 @@ namespace } } +bool osc::FilesystemResourceLoader::impl_resource_exists(const ResourcePath& resource_path) +{ + const std::filesystem::path full_path = calc_full_path(root_directory_, resource_path); + return std::filesystem::exists(full_path); +} + ResourceStream osc::FilesystemResourceLoader::impl_open(const ResourcePath& resource_path) { if (log_level() <= LogLevel::debug) { diff --git a/liboscar/Platform/FilesystemResourceLoader.h b/liboscar/Platform/FilesystemResourceLoader.h index 859f0e2edf..80d60f7508 100644 --- a/liboscar/Platform/FilesystemResourceLoader.h +++ b/liboscar/Platform/FilesystemResourceLoader.h @@ -18,6 +18,7 @@ namespace osc {} private: + bool impl_resource_exists(const ResourcePath&) final; ResourceStream impl_open(const ResourcePath&) final; std::function()> impl_iterate_directory(const ResourcePath&) final; diff --git a/liboscar/Platform/IResourceLoader.h b/liboscar/Platform/IResourceLoader.h index 122c779c77..b84d93aac1 100644 --- a/liboscar/Platform/IResourceLoader.h +++ b/liboscar/Platform/IResourceLoader.h @@ -21,6 +21,7 @@ namespace osc public: virtual ~IResourceLoader() noexcept = default; + bool resource_exists(const ResourcePath& resource_path) { return impl_resource_exists(resource_path); } ResourceStream open(const ResourcePath& resource_path) { return impl_open(resource_path); } std::string slurp(const ResourcePath&); @@ -30,6 +31,7 @@ namespace osc } private: + virtual bool impl_resource_exists(const ResourcePath&) = 0; virtual ResourceStream impl_open(const ResourcePath&) = 0; virtual std::function()> impl_iterate_directory(const ResourcePath&) { diff --git a/liboscar/Platform/ResourceLoader.h b/liboscar/Platform/ResourceLoader.h index 39a140ca29..db46109e89 100644 --- a/liboscar/Platform/ResourceLoader.h +++ b/liboscar/Platform/ResourceLoader.h @@ -19,6 +19,11 @@ namespace osc operator IResourceLoader& () { return *impl_; } operator const IResourceLoader& () const { return *impl_; } + bool resource_exists(const ResourcePath& resource_path) + { + return impl_->resource_exists(resource_path); + } + ResourceStream open(const ResourcePath& resource_path) { return impl_->open(prefix_ / resource_path); diff --git a/liboscar/Platform/ResourceLoader.tests.cpp b/liboscar/Platform/ResourceLoader.tests.cpp index 0ef6d5349d..e09d1b61f0 100644 --- a/liboscar/Platform/ResourceLoader.tests.cpp +++ b/liboscar/Platform/ResourceLoader.tests.cpp @@ -18,6 +18,7 @@ namespace { struct MockState final { std::optional last_open_call_path; + std::optional last_existence_check_path; }; class MockResourceLoader : public IResourceLoader { @@ -26,6 +27,12 @@ namespace state_{std::move(state_)} {} private: + bool impl_resource_exists(const ResourcePath& resource_path) final + { + state_->last_existence_check_path = resource_path; + return true; + } + ResourceStream impl_open(const ResourcePath& resource_path) override { state_->last_open_call_path = resource_path; @@ -64,3 +71,11 @@ TEST(ResourceLoader, WithPrefixCausesIResourceLoaderToBeCalledWithPrefixedPath) prefixed_loader.open(ResourcePath{"path"}); ASSERT_EQ(mock_state->last_open_call_path, ResourcePath{"prefix/path"}) << "with_prefix should return a loader the prefixes each open call"; } + +TEST(ResourceLoader, resource_exists_calls_underlying_impl_resource_exists) +{ + const auto mock_state = std::make_shared(); + ResourceLoader resource_loader = make_resource_loader(mock_state); + ASSERT_TRUE(resource_loader.resource_exists("should/exist")); + ASSERT_EQ(mock_state->last_existence_check_path, ResourcePath{"should/exist"}); +}