3#include <unordered_map>
4#include <unordered_set>
19#define BS_THREAD_POOL_ENABLE_PRIORITY
20#define BS_THREAD_POOL_ENABLE_PAUSE
21#include <BS_thread_pool.hpp>
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);
45 const std::shared_ptr<Archive>
Parent =
nullptr;
63 typedef enum class ResourceLoadError { None, NotCached,
NotFound } ResourceLoadError;
67 std::shared_ptr<Keystore> keystore =
nullptr);
81 std::shared_ptr<IResource>
GetCachedResource(
const std::string& filePath,
bool loadExact =
false);
98 std::shared_ptr<IResource>
LoadResource(
const std::string& filePath,
bool loadExact =
false,
99 std::shared_ptr<ResourceInitData> initData =
nullptr);
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)));
123 std::shared_ptr<ResourceInitData> initData =
nullptr);
133 template <
typename T>
135 std::shared_ptr<ResourceInitData> initData =
nullptr) {
136 return std::static_pointer_cast<T>(
LoadResource(identifier, loadExact, std::move(initData)));
146 std::shared_ptr<IResource>
LoadResource(uint64_t crc,
bool loadExact =
false,
147 std::shared_ptr<ResourceInitData> initData =
nullptr);
160 std::shared_ptr<ResourceInitData> initData =
nullptr);
170 std::shared_ptr<ResourceInitData> initData =
nullptr);
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);
192 std::shared_future<std::shared_ptr<IResource>>
194 BS::priority_t priority = BS::pr::normal, std::shared_ptr<ResourceInitData> initData =
nullptr);
226 bool WriteResource(uint64_t hash,
const std::vector<uint8_t>& data,
bool unloadFile);
233 std::shared_ptr<std::vector<std::shared_ptr<IResource>>>
LoadResources(
const std::string& searchMask);
248 std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
257 std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
409 void OnInit(
const nlohmann::json& initArgs = nlohmann::json::object())
override;
414 bool loadExact =
false);
415 std::variant<ResourceLoadError, std::shared_ptr<IResource>>
CheckCache(
const std::string& filePath,
416 bool loadExact =
false);
418 std::shared_ptr<IResource>
GetCachedResource(std::variant<ResourceLoadError, std::shared_ptr<IResource>> cacheLine);
421 std::unordered_map<ResourceIdentifier, std::variant<ResourceLoadError, std::shared_ptr<IResource>>,
424 std::shared_ptr<ResourceLoader> mResourceLoader;
425 std::shared_ptr<ArchiveManager> mArchiveManager;
427 bool mAltAssetsEnabled =
false;
429 uintptr_t mDefaultCacheOwner = 0;
430 std::shared_ptr<Archive> mDefaultCacheArchive =
nullptr;
431 std::shared_ptr<ThreadPool> mThreadPool;
432 std::shared_ptr<Keystore> mKeystore;
434 std::shared_ptr<ThreadPool> GetThreadPool();
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