libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
ResourceLoader.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <unordered_map>
5#include "ResourceType.h"
8
9namespace Ship {
10struct File;
11class ResourceManager;
12
23 uint32_t resourceType;
26
27 bool operator==(const ResourceFactoryKey& o) const {
30 }
31};
32
35 std::size_t operator()(const ResourceFactoryKey& key) const {
36 return std::hash<int>()(key.resourceFormat) ^ std::hash<int>()(key.resourceType) ^
37 std::hash<int>()(key.resourceVersion);
38 }
39};
40
52 public:
53 explicit ResourceLoader(std::shared_ptr<ResourceManager> resourceManager = nullptr);
54 virtual ~ResourceLoader();
55
67 std::shared_ptr<IResource> LoadResource(const ResourceIdentifier& identifier, std::shared_ptr<File> fileToLoad,
68 std::shared_ptr<ResourceInitData> initData = nullptr);
69
73 std::shared_ptr<IResource> LoadResource(std::string filePath, std::shared_ptr<File> fileToLoad,
74 std::shared_ptr<ResourceInitData> initData = nullptr);
75
86 bool RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName,
87 uint32_t type, uint32_t version);
88
94 uint32_t GetResourceType(const std::string& type);
95
96 protected:
99
104 std::shared_ptr<ResourceFactory> GetFactory(uint32_t format, uint32_t type, uint32_t version);
105
110 std::shared_ptr<ResourceFactory> GetFactory(uint32_t format, std::string typeName, uint32_t version);
111
118 std::shared_ptr<ResourceInitData> ReadResourceInitData(const std::string& filePath,
119 std::shared_ptr<File> metaFileToLoad);
120
122 static std::shared_ptr<ResourceInitData> CreateDefaultResourceInitData();
123
130 std::shared_ptr<ResourceInitData> ReadResourceInitDataLegacy(const std::string& filePath,
131 std::shared_ptr<File> fileToLoad);
132
139 static std::shared_ptr<ResourceInitData> ReadResourceInitDataBinary(const std::string& filePath,
140 std::shared_ptr<BinaryReader> headerReader);
141
148 std::shared_ptr<ResourceInitData> ReadResourceInitDataXml(const std::string& filePath,
149 std::shared_ptr<tinyxml2::XMLDocument> document);
150
157 static std::shared_ptr<ResourceInitData> ReadResourceInitDataPng(const std::string& filePath,
158 std::shared_ptr<BinaryReader> headerReader);
159
166 std::shared_ptr<BinaryReader> CreateBinaryReader(std::shared_ptr<File> fileToLoad,
167 std::shared_ptr<ResourceInitData> initData);
168
175 std::shared_ptr<tinyxml2::XMLDocument> CreateXMLReader(std::shared_ptr<File> fileToLoad,
176 std::shared_ptr<ResourceInitData> initData);
177
178 private:
179 std::string DecodeASCII(uint32_t value);
180 std::unordered_map<std::string, uint32_t> mResourceTypes;
181 std::unordered_map<ResourceFactoryKey, std::shared_ptr<ResourceFactory>, ResourceFactoryKeyHash> mFactories;
182 std::shared_ptr<ResourceManager> mResourceManager;
183};
184} // namespace Ship
Uniquely identifies a cached resource by path/hash, owner, and source archive.
Definition ResourceIdentifier.h:15
Parses raw file data into IResource objects using registered ResourceFactory instances.
Definition ResourceLoader.h:51
std::shared_ptr< IResource > LoadResource(std::string filePath, std::shared_ptr< File > fileToLoad, std::shared_ptr< ResourceInitData > initData=nullptr)
Compatibility overload that loads a resource by virtual path.
bool RegisterResourceFactory(std::shared_ptr< ResourceFactory > factory, uint32_t format, std::string typeName, uint32_t type, uint32_t version)
Registers a factory so that ResourceLoader can handle a new resource type.
std::shared_ptr< ResourceInitData > ReadResourceInitData(const std::string &filePath, std::shared_ptr< File > metaFileToLoad)
Reads and parses the resource header from the meta-file to produce ResourceInitData.
static std::shared_ptr< ResourceInitData > ReadResourceInitDataBinary(const std::string &filePath, std::shared_ptr< BinaryReader > headerReader)
Reads a standard binary resource header from a BinaryReader.
uint32_t GetResourceType(const std::string &type)
Looks up the numeric type ID for a given type name string.
static std::shared_ptr< ResourceInitData > CreateDefaultResourceInitData()
Creates a ResourceInitData with default/zeroed fields.
std::shared_ptr< ResourceFactory > GetFactory(uint32_t format, std::string typeName, uint32_t version)
Returns the factory registered for the given (format, typeName, version) triple.
std::shared_ptr< IResource > LoadResource(const ResourceIdentifier &identifier, std::shared_ptr< File > fileToLoad, std::shared_ptr< ResourceInitData > initData=nullptr)
Deserializes a File into a fully constructed IResource.
std::shared_ptr< tinyxml2::XMLDocument > CreateXMLReader(std::shared_ptr< File > fileToLoad, std::shared_ptr< ResourceInitData > initData)
Parses the buffer of a loaded File as XML and returns the resulting document.
void RegisterGlobalResourceFactories()
Registers the built-in factories (Blob, JSON, Shader). Called during construction.
std::shared_ptr< ResourceInitData > ReadResourceInitDataLegacy(const std::string &filePath, std::shared_ptr< File > fileToLoad)
Reads a legacy (pre-versioned) binary resource header.
virtual ~ResourceLoader()
std::shared_ptr< ResourceInitData > ReadResourceInitDataXml(const std::string &filePath, std::shared_ptr< tinyxml2::XMLDocument > document)
Reads an XML resource header from an XMLDocument.
std::shared_ptr< BinaryReader > CreateBinaryReader(std::shared_ptr< File > fileToLoad, std::shared_ptr< ResourceInitData > initData)
Creates a BinaryReader wrapping the buffer of a loaded File.
std::shared_ptr< ResourceFactory > GetFactory(uint32_t format, uint32_t type, uint32_t version)
Returns the factory registered for the given (format, type, version) triple.
static std::shared_ptr< ResourceInitData > ReadResourceInitDataPng(const std::string &filePath, std::shared_ptr< BinaryReader > headerReader)
Reads a PNG image header to produce ResourceInitData.
ResourceLoader(std::shared_ptr< ResourceManager > resourceManager=nullptr)
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
std::hash specialization for ResourceFactoryKey, used by the factory map.
Definition ResourceLoader.h:34
std::size_t operator()(const ResourceFactoryKey &key) const
Definition ResourceLoader.h:35
Cache key that uniquely identifies a ResourceFactory by format, type, and version.
Definition ResourceLoader.h:19
uint32_t resourceType
Numeric resource type identifier (see ResourceType).
Definition ResourceLoader.h:23
uint32_t resourceVersion
Version of the resource format this factory handles.
Definition ResourceLoader.h:25
uint32_t resourceFormat
Format tag (RESOURCE_FORMAT_BINARY or RESOURCE_FORMAT_XML).
Definition ResourceLoader.h:21
bool operator==(const ResourceFactoryKey &o) const
Definition ResourceLoader.h:27