Add ResourceLoader
This commit is contained in:
76
openVulkanoCpp/Host/ResourceLoader.cpp
Normal file
76
openVulkanoCpp/Host/ResourceLoader.cpp
Normal file
@@ -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 <vector>
|
||||||
|
|
||||||
|
namespace OpenVulkano
|
||||||
|
{
|
||||||
|
class AggregatingResourceLoader final : public ResourceLoader
|
||||||
|
{
|
||||||
|
std::vector<std::unique_ptr<ResourceLoader>> m_loaders;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Array<char> 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<ResourceLoader> 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<ResourceLoader> loader)
|
||||||
|
{
|
||||||
|
AggregatingResourceLoader* aggregator = static_cast<AggregatingResourceLoader*>(&GetInstance());
|
||||||
|
return aggregator->Register(std::move(loader));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ResourceLoader::UnregisterResourceLoader(void* loaderHandle)
|
||||||
|
{
|
||||||
|
AggregatingResourceLoader* aggregator = static_cast<AggregatingResourceLoader*>(&GetInstance());
|
||||||
|
return aggregator->Unregister(loaderHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
openVulkanoCpp/Host/ResourceLoader.hpp
Normal file
25
openVulkanoCpp/Host/ResourceLoader.hpp
Normal file
@@ -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 <memory>
|
||||||
|
|
||||||
|
namespace OpenVulkano
|
||||||
|
{
|
||||||
|
class ResourceLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~ResourceLoader() = default;
|
||||||
|
|
||||||
|
virtual Array<char> GetResource(const std::string& resourceName) = 0;
|
||||||
|
|
||||||
|
static ResourceLoader& GetInstance();
|
||||||
|
|
||||||
|
static void* RegisterResourceLoader(std::unique_ptr<ResourceLoader> loader);
|
||||||
|
|
||||||
|
static bool UnregisterResourceLoader(void* loaderHandle);
|
||||||
|
};
|
||||||
|
}
|
||||||
19
openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h
Normal file
19
openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.h
Normal file
@@ -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 <Foundation/Foundation.h>
|
||||||
|
#include "Host/ResourceLoader.hpp"
|
||||||
|
|
||||||
|
namespace OpenVulkano
|
||||||
|
{
|
||||||
|
class BundledResourceLoaderIOS : public ResourceLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Array<char> GetResource(const std::string& resourceName) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
30
openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm
Normal file
30
openVulkanoCpp/Host/iOS/BundledResoureLoaderIos.mm
Normal file
@@ -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<ResourceLoader>(new BundledResourceLoaderIOS())));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Array<char> 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};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user