libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::ResourceLoader Class Reference

Parses raw file data into IResource objects using registered ResourceFactory instances. More...

#include <ResourceLoader.h>

Public Member Functions

 ResourceLoader (std::shared_ptr< ResourceManager > resourceManager=nullptr)
 
virtual ~ResourceLoader ()
 
std::shared_ptr< IResourceLoadResource (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< IResourceLoadResource (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.
 
uint32_t GetResourceType (const std::string &type)
 Looks up the numeric type ID for a given type name string.
 

Protected Member Functions

void RegisterGlobalResourceFactories ()
 Registers the built-in factories (Blob, JSON, Shader). Called during construction.
 
std::shared_ptr< ResourceFactoryGetFactory (uint32_t format, uint32_t type, uint32_t version)
 Returns the factory registered for the given (format, type, version) triple.
 
std::shared_ptr< ResourceFactoryGetFactory (uint32_t format, std::string typeName, uint32_t version)
 Returns the factory registered for the given (format, typeName, version) triple.
 
std::shared_ptr< ResourceInitDataReadResourceInitData (const std::string &filePath, std::shared_ptr< File > metaFileToLoad)
 Reads and parses the resource header from the meta-file to produce ResourceInitData.
 
std::shared_ptr< ResourceInitDataReadResourceInitDataLegacy (const std::string &filePath, std::shared_ptr< File > fileToLoad)
 Reads a legacy (pre-versioned) binary resource header.
 
std::shared_ptr< ResourceInitDataReadResourceInitDataXml (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< 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.
 

Static Protected Member Functions

static std::shared_ptr< ResourceInitDataCreateDefaultResourceInitData ()
 Creates a ResourceInitData with default/zeroed fields.
 
static std::shared_ptr< ResourceInitDataReadResourceInitDataBinary (const std::string &filePath, std::shared_ptr< BinaryReader > headerReader)
 Reads a standard binary resource header from a BinaryReader.
 
static std::shared_ptr< ResourceInitDataReadResourceInitDataPng (const std::string &filePath, std::shared_ptr< BinaryReader > headerReader)
 Reads a PNG image header to produce ResourceInitData.
 

Detailed Description

Parses raw file data into IResource objects using registered ResourceFactory instances.

ResourceLoader sits between the archive layer and the ResourceManager. Given a File and its associated ResourceInitData, it selects the appropriate ResourceFactory (matched by format, type, and version) and delegates deserialization to that factory.

New resource types are registered at startup via RegisterResourceFactory(). Built-in factories (Blob, JSON, Shader) are registered automatically in RegisterGlobalResourceFactories().

Constructor & Destructor Documentation

◆ ResourceLoader()

Ship::ResourceLoader::ResourceLoader ( std::shared_ptr< ResourceManager resourceManager = nullptr)
explicit

◆ ~ResourceLoader()

virtual Ship::ResourceLoader::~ResourceLoader ( )
virtual

Member Function Documentation

◆ CreateBinaryReader()

std::shared_ptr< BinaryReader > Ship::ResourceLoader::CreateBinaryReader ( std::shared_ptr< File fileToLoad,
std::shared_ptr< ResourceInitData initData 
)
protected

Creates a BinaryReader wrapping the buffer of a loaded File.

Parameters
fileToLoadFile whose buffer should be wrapped.
initDataInit data used to configure byte-order.
Returns
Configured BinaryReader.

◆ CreateDefaultResourceInitData()

static std::shared_ptr< ResourceInitData > Ship::ResourceLoader::CreateDefaultResourceInitData ( )
staticprotected

Creates a ResourceInitData with default/zeroed fields.

◆ CreateXMLReader()

std::shared_ptr< tinyxml2::XMLDocument > Ship::ResourceLoader::CreateXMLReader ( std::shared_ptr< File fileToLoad,
std::shared_ptr< ResourceInitData initData 
)
protected

Parses the buffer of a loaded File as XML and returns the resulting document.

Parameters
fileToLoadFile whose buffer should be parsed.
initDataInit data (unused currently, reserved for future use).
Returns
Parsed XMLDocument, or nullptr if parsing failed.

◆ GetFactory() [1/2]

std::shared_ptr< ResourceFactory > Ship::ResourceLoader::GetFactory ( uint32_t  format,
std::string  typeName,
uint32_t  version 
)
protected

Returns the factory registered for the given (format, typeName, version) triple.

Returns
Matching factory, or nullptr if none is registered.

◆ GetFactory() [2/2]

std::shared_ptr< ResourceFactory > Ship::ResourceLoader::GetFactory ( uint32_t  format,
uint32_t  type,
uint32_t  version 
)
protected

Returns the factory registered for the given (format, type, version) triple.

Returns
Matching factory, or nullptr if none is registered.

◆ GetResourceType()

uint32_t Ship::ResourceLoader::GetResourceType ( const std::string &  type)

Looks up the numeric type ID for a given type name string.

Parameters
typeHuman-readable type name (e.g. "Texture").
Returns
Numeric type ID, or 0 if the type name is not registered.

◆ LoadResource() [1/2]

std::shared_ptr< IResource > Ship::ResourceLoader::LoadResource ( const ResourceIdentifier identifier,
std::shared_ptr< File fileToLoad,
std::shared_ptr< ResourceInitData initData = nullptr 
)

Deserializes a File into a fully constructed IResource.

Reads the resource header from fileToLoad, selects the matching factory, and calls ResourceFactory::ReadResource().

Parameters
identifierResource identifier (path/hash + owner + archive context).
fileToLoadRaw file buffer as returned by the archive layer.
initDataOptional metadata overrides; pass nullptr to infer from the header.
Returns
Deserialized IResource, or nullptr if no matching factory was found or parsing failed.

◆ LoadResource() [2/2]

std::shared_ptr< IResource > Ship::ResourceLoader::LoadResource ( std::string  filePath,
std::shared_ptr< File fileToLoad,
std::shared_ptr< ResourceInitData initData = nullptr 
)

Compatibility overload that loads a resource by virtual path.

◆ ReadResourceInitData()

std::shared_ptr< ResourceInitData > Ship::ResourceLoader::ReadResourceInitData ( const std::string &  filePath,
std::shared_ptr< File metaFileToLoad 
)
protected

Reads and parses the resource header from the meta-file to produce ResourceInitData.

Parameters
filePathVirtual path (for error messages).
metaFileToLoadFile containing the header (may be a separate ".meta" sidecar).
Returns
Populated ResourceInitData, or nullptr if the header is invalid.

◆ ReadResourceInitDataBinary()

static std::shared_ptr< ResourceInitData > Ship::ResourceLoader::ReadResourceInitDataBinary ( const std::string &  filePath,
std::shared_ptr< BinaryReader >  headerReader 
)
staticprotected

Reads a standard binary resource header from a BinaryReader.

Parameters
filePathVirtual path (for error messages).
headerReaderReader positioned at the start of the binary header.
Returns
Populated ResourceInitData, or nullptr on failure.

◆ ReadResourceInitDataLegacy()

std::shared_ptr< ResourceInitData > Ship::ResourceLoader::ReadResourceInitDataLegacy ( const std::string &  filePath,
std::shared_ptr< File fileToLoad 
)
protected

Reads a legacy (pre-versioned) binary resource header.

Parameters
filePathVirtual path (for error messages).
fileToLoadFile containing the legacy header.
Returns
Populated ResourceInitData, or nullptr on failure.

◆ ReadResourceInitDataPng()

static std::shared_ptr< ResourceInitData > Ship::ResourceLoader::ReadResourceInitDataPng ( const std::string &  filePath,
std::shared_ptr< BinaryReader >  headerReader 
)
staticprotected

Reads a PNG image header to produce ResourceInitData.

Parameters
filePathVirtual path (for error messages).
headerReaderReader positioned at the start of the PNG file.
Returns
Populated ResourceInitData, or nullptr on failure.

◆ ReadResourceInitDataXml()

std::shared_ptr< ResourceInitData > Ship::ResourceLoader::ReadResourceInitDataXml ( const std::string &  filePath,
std::shared_ptr< tinyxml2::XMLDocument >  document 
)
protected

Reads an XML resource header from an XMLDocument.

Parameters
filePathVirtual path (for error messages).
documentParsed XML document containing the header.
Returns
Populated ResourceInitData, or nullptr on failure.

◆ RegisterGlobalResourceFactories()

void Ship::ResourceLoader::RegisterGlobalResourceFactories ( )
protected

Registers the built-in factories (Blob, JSON, Shader). Called during construction.

◆ RegisterResourceFactory()

bool Ship::ResourceLoader::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.

Parameters
factoryThe factory implementation to register.
formatFormat tag (RESOURCE_FORMAT_BINARY / RESOURCE_FORMAT_XML).
typeNameHuman-readable type name string (e.g. "Texture").
typeNumeric type identifier.
versionResource format version this factory handles.
Returns
true if registration succeeded; false if a factory with the same key already exists.

The documentation for this class was generated from the following file: