From d0baabd3da733de91b35d05d0820ef7833676c05 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Fri, 17 Nov 2023 21:33:44 +0100 Subject: [PATCH] Add ResourceLoader --- openVulkanoCpp/Host/ResourceLoader.cpp | 76 +++++++++++++++++++ openVulkanoCpp/Host/ResourceLoader.hpp | 25 ++++++ .../Host/iOS/BundledResoureLoaderIos.h | 19 +++++ .../Host/iOS/BundledResoureLoaderIos.mm | 30 ++++++++ 4 files changed, 150 insertions(+) create mode 100644 openVulkanoCpp/Host/ResourceLoader.cpp create mode 100644 openVulkanoCpp/Host/ResourceLoader.hpp create mode 100644 openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h create mode 100644 openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm diff --git a/openVulkanoCpp/Host/ResourceLoader.cpp b/openVulkanoCpp/Host/ResourceLoader.cpp new file mode 100644 index 0000000..9eaaf5e --- /dev/null +++ b/openVulkanoCpp/Host/ResourceLoader.cpp @@ -0,0 +1,76 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "ResourceLoader.hpp" +#include "Base/Logger.hpp" +#include + +namespace OpenVulkano +{ + class AggregatingResourceLoader final : public ResourceLoader + { + std::vector> m_loaders; + + public: + Array GetResource(const std::string& resourceName) override + { + for(auto& loader : m_loaders) + { + try + { + auto res = loader->GetResource(resourceName); + if (!res.Empty()) return res; + } + catch (const std::exception& e) + { + Logger::FILESYS->error("Error trying to get resource for '{}'! Error: {}", resourceName, e.what()); + } + catch (...) + { + Logger::FILESYS->error("Unknown error trying to get resource for '{}'!", resourceName); + } + } + return { 0 }; + } + + void* Register(std::unique_ptr loader) + { + m_loaders.push_back(std::move(loader)); + return m_loaders.back().get(); + } + + bool Unregister(void* loaderHandle) + { + for(auto iter = m_loaders.begin(); iter != m_loaders.end(); iter++) + { + if (iter->get() == loaderHandle) + { + m_loaders.erase(iter); + return true; + } + } + return false; + } + }; + + ResourceLoader& ResourceLoader::GetInstance() + { + static AggregatingResourceLoader resourceLoader; + return resourceLoader; + } + + void* ResourceLoader::RegisterResourceLoader(std::unique_ptr loader) + { + AggregatingResourceLoader* aggregator = static_cast(&GetInstance()); + return aggregator->Register(std::move(loader)); + } + + bool ResourceLoader::UnregisterResourceLoader(void* loaderHandle) + { + AggregatingResourceLoader* aggregator = static_cast(&GetInstance()); + return aggregator->Unregister(loaderHandle); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/ResourceLoader.hpp b/openVulkanoCpp/Host/ResourceLoader.hpp new file mode 100644 index 0000000..50622fe --- /dev/null +++ b/openVulkanoCpp/Host/ResourceLoader.hpp @@ -0,0 +1,25 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "Data/Containers/Array.hpp" +#include + +namespace OpenVulkano +{ + class ResourceLoader + { + public: + virtual ~ResourceLoader() = default; + + virtual Array GetResource(const std::string& resourceName) = 0; + + static ResourceLoader& GetInstance(); + + static void* RegisterResourceLoader(std::unique_ptr loader); + + static bool UnregisterResourceLoader(void* loaderHandle); + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h b/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h new file mode 100644 index 0000000..a817c82 --- /dev/null +++ b/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h @@ -0,0 +1,19 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#import +#include "Host/ResourceLoader.hpp" + +namespace OpenVulkano +{ + class BundledResourceLoaderIOS : public ResourceLoader + { + public: + Array GetResource(const std::string& resourceName) override; + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm b/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm new file mode 100644 index 0000000..a6e04ec --- /dev/null +++ b/openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm @@ -0,0 +1,30 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "BundledResoureLoaderIos.h" +#include "Base/Logger.hpp" +#include "Base/Utils.hpp" + +namespace OpenVulkano +{ + namespace + { + void* LOADER_HANDLE = ResourceLoader::RegisterResourceLoader(std::move(std::unique_ptr(new BundledResourceLoaderIOS()))); + } + + + Array BundledResourceLoaderIOS::GetResource(const std::string& resourceName) + { + @autoreleasepool + { + auto [fileName, fileType] = Utils::SplitAtLastOccurrence(resourceName, '.'); + NSString* filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:fileName.c_str()] ofType:[NSString stringWithUTF8String:fileType.c_str()]]; + return Utils::ReadFile(std::string([filePath UTF8String])); + } + Logger::FILESYS->warn("Failed to find resource: '{}' in bundle", resourceName); + return {0}; + } +}