libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
ResourceManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <unordered_set>
5#include <string>
6#include <list>
7#include <vector>
8#include <mutex>
9#include <queue>
10#include <variant>
16#include "ship/core/Component.h"
18
19#define BS_THREAD_POOL_ENABLE_PRIORITY
20#define BS_THREAD_POOL_ENABLE_PAUSE
21#include <BS_thread_pool.hpp>
22
23namespace Ship {
24struct File;
25class Keystore;
26
35 ResourceFilter(const std::list<std::string>& includeMasks, const std::list<std::string>& excludeMasks,
36 const uintptr_t owner, const std::shared_ptr<Archive> parent);
37
39 const std::list<std::string> IncludeMasks;
41 const std::list<std::string> ExcludeMasks;
43 const uintptr_t Owner = 0;
45 const std::shared_ptr<Archive> Parent = nullptr;
46};
47
61class ResourceManager : public Component {
62 friend class ResourceLoader;
63 typedef enum class ResourceLoadError { None, NotCached, NotFound } ResourceLoadError;
64
65 public:
66 explicit ResourceManager(std::shared_ptr<ThreadPool> threadPool = nullptr,
67 std::shared_ptr<Keystore> keystore = nullptr);
69
71 std::shared_ptr<ArchiveManager> GetArchiveManager();
73 std::shared_ptr<ResourceLoader> GetResourceLoader();
74
81 std::shared_ptr<IResource> GetCachedResource(const std::string& filePath, bool loadExact = false);
82
89 std::shared_ptr<IResource> GetCachedResource(const ResourceIdentifier& identifier, bool loadExact = false);
90
98 std::shared_ptr<IResource> LoadResource(const std::string& filePath, bool loadExact = false,
99 std::shared_ptr<ResourceInitData> initData = nullptr);
100
109 template <typename T>
110 std::shared_ptr<T> LoadResource(const std::string& filePath, bool loadExact = false,
111 std::shared_ptr<ResourceInitData> initData = nullptr) {
112 return std::static_pointer_cast<T>(LoadResource(filePath, loadExact, std::move(initData)));
113 }
114
122 std::shared_ptr<IResource> LoadResource(const ResourceIdentifier& identifier, bool loadExact = false,
123 std::shared_ptr<ResourceInitData> initData = nullptr);
124
133 template <typename T>
134 std::shared_ptr<T> LoadResource(const ResourceIdentifier& identifier, bool loadExact = false,
135 std::shared_ptr<ResourceInitData> initData = nullptr) {
136 return std::static_pointer_cast<T>(LoadResource(identifier, loadExact, std::move(initData)));
137 }
138
146 std::shared_ptr<IResource> LoadResource(uint64_t crc, bool loadExact = false,
147 std::shared_ptr<ResourceInitData> initData = nullptr);
148
159 std::shared_ptr<IResource> LoadResourceProcess(const std::string& filePath, bool loadExact = false,
160 std::shared_ptr<ResourceInitData> initData = nullptr);
161
169 std::shared_ptr<IResource> LoadResourceProcess(const ResourceIdentifier& identifier, bool loadExact = false,
170 std::shared_ptr<ResourceInitData> initData = nullptr);
171
180 std::shared_future<std::shared_ptr<IResource>>
181 LoadResourceAsync(const std::string& filePath, bool loadExact = false, BS::priority_t priority = BS::pr::normal,
182 std::shared_ptr<ResourceInitData> initData = nullptr);
183
192 std::shared_future<std::shared_ptr<IResource>>
193 LoadResourceAsync(const ResourceIdentifier& identifier, bool loadExact = false,
194 BS::priority_t priority = BS::pr::normal, std::shared_ptr<ResourceInitData> initData = nullptr);
195
201 size_t UnloadResource(const ResourceIdentifier& identifier);
202
208 size_t UnloadResource(const std::string& filePath);
209
217 bool WriteResource(const ResourceIdentifier& identifier, const std::vector<uint8_t>& data, bool unloadFile);
218
226 bool WriteResource(uint64_t hash, const std::vector<uint8_t>& data, bool unloadFile);
227
233 std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const std::string& searchMask);
234
240 std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const ResourceFilter& filter);
241
248 std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
249 LoadResourcesAsync(const std::string& searchMask, BS::priority_t priority = BS::pr::normal);
250
257 std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
258 LoadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority = BS::pr::normal);
259
267 void DirtyResources(const std::string& searchMask);
268
273 void DirtyResources(const ResourceFilter& filter);
274
279 void UnloadResources(const std::string& searchMask);
280
285 void UnloadResources(const ResourceFilter& filter);
286
292 void UnloadResourcesAsync(const std::string& searchMask, BS::priority_t priority = BS::pr::normal);
293
299 void UnloadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority = BS::pr::normal);
300
306 bool OtrSignatureCheck(const char* fileName);
307
313
318 void SetAltAssetsEnabled(bool isEnabled);
319
325 std::shared_ptr<File> LoadFileProcess(const ResourceIdentifier& identifier);
326
332 std::shared_ptr<File> LoadFileProcess(const std::string& filePath);
333
339 std::shared_ptr<File> LoadFileProcess(uint64_t hash);
340
346 size_t GetResourceSize(std::shared_ptr<IResource> resource);
347
353 size_t GetResourceSize(const char* name);
354
360 size_t GetResourceSize(uint64_t crc);
361
366 bool GetResourceIsCustom(std::shared_ptr<IResource> resource);
367
372 bool GetResourceIsCustom(const char* name);
373
378 bool GetResourceIsCustom(uint64_t crc);
379
385 void* GetResourceRawPointer(std::shared_ptr<IResource> resource);
386
392 void* GetResourceRawPointer(const char* name);
393
399 void* GetResourceRawPointer(uint64_t crc);
400
401 protected:
409 void OnInit(const nlohmann::json& initArgs = nlohmann::json::object()) override;
410
411 std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResourcesProcess(const ResourceFilter& filter);
413 std::variant<ResourceLoadError, std::shared_ptr<IResource>> CheckCache(const ResourceIdentifier& identifier,
414 bool loadExact = false);
415 std::variant<ResourceLoadError, std::shared_ptr<IResource>> CheckCache(const std::string& filePath,
416 bool loadExact = false);
417
418 std::shared_ptr<IResource> GetCachedResource(std::variant<ResourceLoadError, std::shared_ptr<IResource>> cacheLine);
419
420 private:
421 std::unordered_map<ResourceIdentifier, std::variant<ResourceLoadError, std::shared_ptr<IResource>>,
423 mResourceCache;
424 std::shared_ptr<ResourceLoader> mResourceLoader;
425 std::shared_ptr<ArchiveManager> mArchiveManager;
426 std::mutex mMutex;
427 bool mAltAssetsEnabled = false;
428 // Private information for which owner and archive are default.
429 uintptr_t mDefaultCacheOwner = 0;
430 std::shared_ptr<Archive> mDefaultCacheArchive = nullptr;
431 std::shared_ptr<ThreadPool> mThreadPool;
432 std::shared_ptr<Keystore> mKeystore;
433
434 std::shared_ptr<ThreadPool> GetThreadPool();
435};
436} // namespace Ship
A named Part with a parent/child hierarchy and optional thread safety.
Definition Component.h:34
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
Central manager for loading, caching, and unloading game resources.
Definition ResourceManager.h:61
std::shared_ptr< File > LoadFileProcess(const std::string &filePath)
Loads raw file bytes from the archive by path.
std::shared_future< std::shared_ptr< std::vector< std::shared_ptr< IResource > > > > LoadResourcesAsync(const ResourceFilter &filter, BS::priority_t priority=BS::pr::normal)
Asynchronously loads all resources matching a filter.
void OnInit(const nlohmann::json &initArgs=nlohmann::json::object()) override
Component initialization hook. Mounts archives and starts the thread pool.
void DirtyResources(const ResourceFilter &filter)
Marks as dirty all cached resources matching a filter.
size_t UnloadResource(const ResourceIdentifier &identifier)
Removes a resource from the cache by ResourceIdentifier.
size_t GetResourceSize(const char *name)
Returns the byte size of the payload of a resource identified by path.
std::shared_ptr< std::vector< std::shared_ptr< IResource > > > LoadResources(const std::string &searchMask)
Loads all resources whose paths match the given glob mask.
std::shared_ptr< IResource > LoadResource(const std::string &filePath, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously, returning it from the cache if already loaded.
std::variant< ResourceLoadError, std::shared_ptr< IResource > > CheckCache(const ResourceIdentifier &identifier, bool loadExact=false)
size_t GetResourceSize(std::shared_ptr< IResource > resource)
Returns the byte size of the payload of a loaded resource.
std::shared_ptr< File > LoadFileProcess(const ResourceIdentifier &identifier)
Loads raw file bytes from the archive, bypassing resource deserialization.
std::shared_ptr< ArchiveManager > GetArchiveManager()
Returns the ArchiveManager that manages the mounted archives.
bool IsAltAssetsEnabled()
Returns whether alt-asset (mod) loading is currently enabled.
std::shared_ptr< File > LoadFileProcess(uint64_t hash)
Loads raw file bytes from the archive by hash.
void UnloadResourcesProcess(const ResourceFilter &filter)
std::shared_future< std::shared_ptr< IResource > > LoadResourceAsync(const std::string &filePath, bool loadExact=false, BS::priority_t priority=BS::pr::normal, std::shared_ptr< ResourceInitData > initData=nullptr)
Schedules an asynchronous resource load and returns a future.
void UnloadResources(const ResourceFilter &filter)
Synchronously unloads all resources matching a filter.
size_t GetResourceSize(uint64_t crc)
Returns the byte size of the payload of a resource identified by CRC.
std::shared_ptr< IResource > GetCachedResource(const ResourceIdentifier &identifier, bool loadExact=false)
Returns a resource from the cache using a ResourceIdentifier.
std::shared_ptr< T > LoadResource(const std::string &filePath, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously, cast to the concrete type T.
Definition ResourceManager.h:110
std::variant< ResourceLoadError, std::shared_ptr< IResource > > CheckCache(const std::string &filePath, bool loadExact=false)
std::shared_ptr< T > LoadResource(const ResourceIdentifier &identifier, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously by ResourceIdentifier, cast to the concrete type T.
Definition ResourceManager.h:134
std::shared_future< std::shared_ptr< IResource > > LoadResourceAsync(const ResourceIdentifier &identifier, bool loadExact=false, BS::priority_t priority=BS::pr::normal, std::shared_ptr< ResourceInitData > initData=nullptr)
Schedules an asynchronous resource load by ResourceIdentifier.
bool GetResourceIsCustom(std::shared_ptr< IResource > resource)
Returns true if the given resource originated from an alt-assets override.
std::shared_ptr< ResourceLoader > GetResourceLoader()
Returns the ResourceLoader responsible for deserializing file data.
bool WriteResource(const ResourceIdentifier &identifier, const std::vector< uint8_t > &data, bool unloadFile)
Writes raw data into an archive and optionally evicts the stale cache entry.
std::shared_ptr< IResource > LoadResourceProcess(const ResourceIdentifier &identifier, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously by ResourceIdentifier, bypassing the cache.
std::shared_ptr< IResource > GetCachedResource(const std::string &filePath, bool loadExact=false)
Returns a resource from the cache if present, without loading from disk.
void DirtyResources(const std::string &searchMask)
Marks as dirty all cached resources whose paths match a glob mask.
void * GetResourceRawPointer(const char *name)
Returns a type-erased raw pointer to the payload of a resource by path.
std::shared_ptr< std::vector< std::shared_ptr< IResource > > > LoadResourcesProcess(const ResourceFilter &filter)
bool WriteResource(uint64_t hash, const std::vector< uint8_t > &data, bool unloadFile)
Writes raw data into an archive by hash and optionally evicts the stale cache entry.
void SetAltAssetsEnabled(bool isEnabled)
Enables or disables alt-asset (mod) loading.
std::shared_future< std::shared_ptr< std::vector< std::shared_ptr< IResource > > > > LoadResourcesAsync(const std::string &searchMask, BS::priority_t priority=BS::pr::normal)
Asynchronously loads all resources matching a glob mask.
bool OtrSignatureCheck(const char *fileName)
Checks whether a file is a valid OTR archive by reading its header signature.
bool GetResourceIsCustom(uint64_t crc)
Returns true if the resource identified by CRC is an alt-asset override.
std::shared_ptr< std::vector< std::shared_ptr< IResource > > > LoadResources(const ResourceFilter &filter)
Loads all resources matching the given filter.
void UnloadResourcesAsync(const ResourceFilter &filter, BS::priority_t priority=BS::pr::normal)
Asynchronously unloads all resources matching a filter.
std::shared_ptr< IResource > LoadResourceProcess(const std::string &filePath, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously, bypassing the cache.
ResourceManager(std::shared_ptr< ThreadPool > threadPool=nullptr, std::shared_ptr< Keystore > keystore=nullptr)
bool GetResourceIsCustom(const char *name)
Returns true if the resource at the given path is an alt-asset override.
void UnloadResources(const std::string &searchMask)
Synchronously unloads all resources matching a glob mask.
std::shared_ptr< IResource > GetCachedResource(std::variant< ResourceLoadError, std::shared_ptr< IResource > > cacheLine)
void * GetResourceRawPointer(uint64_t crc)
Returns a type-erased raw pointer to the payload of a resource by CRC.
void UnloadResourcesAsync(const std::string &searchMask, BS::priority_t priority=BS::pr::normal)
Asynchronously unloads all resources matching a glob mask.
size_t UnloadResource(const std::string &filePath)
Removes a resource from the cache by path.
void * GetResourceRawPointer(std::shared_ptr< IResource > resource)
Returns a type-erased raw pointer to the resource payload.
std::shared_ptr< IResource > LoadResource(uint64_t crc, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously by content-hash CRC.
std::shared_ptr< IResource > LoadResource(const ResourceIdentifier &identifier, bool loadExact=false, std::shared_ptr< ResourceInitData > initData=nullptr)
Loads a resource synchronously by ResourceIdentifier.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
@ NotFound
The specified Part was not found.
Specifies which resources to include or exclude when performing a bulk load/unload.
Definition ResourceManager.h:34
const uintptr_t Owner
Opaque owner handle; 0 matches resources from any owner.
Definition ResourceManager.h:43
const std::shared_ptr< Archive > Parent
Archive to scope the query to; nullptr matches resources from any archive.
Definition ResourceManager.h:45
ResourceFilter(const std::list< std::string > &includeMasks, const std::list< std::string > &excludeMasks, const uintptr_t owner, const std::shared_ptr< Archive > parent)
const std::list< std::string > ExcludeMasks
Glob patterns that cause a matching resource to be excluded even when it matches IncludeMasks.
Definition ResourceManager.h:41
const std::list< std::string > IncludeMasks
Glob patterns that a resource path must match to be included.
Definition ResourceManager.h:39
std::hash specialization for ResourceIdentifier, used by unordered containers.
Definition ResourceIdentifier.h:114