|
Deki 0.40.1
Modular C++ engine with a visual editor, for 2D and 3D
|
Loads, caches and frees assets, by path or GUID. More...
#include <deki/assets/AssetManager.h>
Public Member Functions | |
| void | AdvanceEpoch () noexcept |
| Makes every AssetRef drop its cached pointer and resolve again on its next Get(). | |
| void | ClearCache (const char *typeName) |
| Frees every cached asset of type typeName (such as "BitmapFont"), so the next loads read from disk again. | |
| void | DropCache () |
| Forgets the cache without freeing the assets, which may still be in use; the next load reads a fresh copy from disk. | |
| const std::string & | GetCacheDirectory () const |
| uint64_t | GetEpoch () const noexcept |
| Goes up whenever asset memory is freed or replaced. | |
| void | InvalidateAsset (const char *typeName, const std::string &guid) |
| Frees the cached asset guid of type typeName (such as "Sprite"), so the next load reads it from disk again. | |
| bool | IsReady () const |
| True once assets can be loaded: the cache directory is set (editor edit mode) or the binary lookup table is loaded (play mode, device). | |
| bool | IsTypeCacheable (const char *typeName) const |
| False only for types registered as non-cacheable. | |
| template<typename T> | |
| T * | Load (const char *path, bool cache=false) |
| Loads the asset of type T at path, written without the "assets/" prefix and extension (such as "scenes/Demo"). | |
| bool | LoadAssetLookupTable (const uint8_t *data, size_t size) |
| Uses the runtime's binary lookup table in data, which must stay valid while lookups run. | |
| bool | LoadAssetRoot (const char *root) |
| Uses a build's exported assets under root (Storage::AssetRoot): reads root/asset_table.bin and root/pack_index.bin and loads every asset from root. | |
| void * | LoadByGuidAndType (const std::string &guid, const char *assetType, bool cache=false) |
| Loads the asset guid of type assetType (as registered with RegisterLoader), or returns nullptr. | |
| void * | LoadFromMemory (const std::string &guid, const char *assetType, const uint8_t *data, size_t size) |
| Loads an asset from its file's bytes (pack files). | |
| bool | LoadGuidTable (const std::string &path) |
| Loads the editor's GUID-to-path table from a JSON file. | |
| const std::string & | LookupPath (const std::string &guid) const |
| Full path (cache directory + entry) for a GUID, or "" when unknown. | |
| void | RegisterGuid (const std::string &guid, const std::string &path) |
| Maps guid to the filesystem path path (editor). | |
| void | ReleaseAssetTracking (const char *typeName, void *asset) |
| Stops tracking a non-cacheable asset whose ownership another system (such as SceneSystem) has taken, so UnloadAll() does not free it twice. | |
| void | RemoveGuid (const std::string &guid) |
| Forgets guid when its asset is deleted (editor). | |
| void | SetCacheDirectory (const std::string &dir) |
| Directory asset files are read from: the project's cache directory in the editor, the storage root (such as "S:/") on a device. | |
| void | UnloadAll () |
| Frees every loaded asset. | |
| size_t | UnloadUnused (const std::vector< std::string > &usedGuids) |
| Frees every cached asset whose GUID is not in usedGuids, and moves the epoch, so an AssetRef that pointed at one loads it again when next asked. | |
Static Public Member Functions | |
| static AssetManager * | Get () |
| static bool | HasLoader (const char *typeName) |
| True when a loader is registered for typeName. | |
| static bool | ReadWholeFile (const std::string &path, Buffer< uint8_t > &out, Memory::Region region) |
| Reads a whole file into a Buffer in region, through the engine's filesystem. | |
| static bool | ReadWholeFile (const std::string &path, std::vector< uint8_t > &out) |
| Reads a whole file through the filesystem provider. | |
| static void | RegisterLoader (const char *typeName, AssetLoadFn loader, AssetUnloadFn unloader=nullptr, AssetMemLoadFn memLoader=nullptr, bool cacheable=true) |
| Registers how to load assets of type typeName (must match T::kAssetTypeName). | |
| static void | Shutdown () |
| Frees all assets and the singleton. | |
Loads, caches and frees assets, by path or GUID.
|
inlinenoexcept |
| void Deki::AssetManager::ClearCache | ( | const char * | typeName | ) |
Frees every cached asset of type typeName (such as "BitmapFont"), so the next loads read from disk again.
| void Deki::AssetManager::DropCache | ( | ) |
Forgets the cache without freeing the assets, which may still be in use; the next load reads a fresh copy from disk.
The editor calls it when play mode stops. Leaves the epoch alone, since existing AssetRef::ptr values still point at live memory.
|
static |
|
inline |
|
inlinenoexcept |
|
static |
True when a loader is registered for typeName.
The editor treats any .asset whose "type" has a loader (and no texture compiler) as a generic data asset, like a ScriptableObject: its JSON compiles straight to a MessagePack cache with no per-type editor.
| void Deki::AssetManager::InvalidateAsset | ( | const char * | typeName, |
| const std::string & | guid ) |
Frees the cached asset guid of type typeName (such as "Sprite"), so the next load reads it from disk again.
|
inline |
True once assets can be loaded: the cache directory is set (editor edit mode) or the binary lookup table is loaded (play mode, device).
AssetRef::Get() skips loading until then, such as while ImportAllAssets compiles scenes before the filesystem is up.
| bool Deki::AssetManager::IsTypeCacheable | ( | const char * | typeName | ) | const |
False only for types registered as non-cacheable.
| T * Deki::AssetManager::Load | ( | const char * | path, |
| bool | cache = false ) |
Loads the asset of type T at path, written without the "assets/" prefix and extension (such as "scenes/Demo").
Returns nullptr on failure. T is any type with a registered loader (Scene is built in; packages register theirs with RegisterLoader). The type is part of the lookup key, so assets of different types can share a path.
Cacheable types are loaded once and shared, and cache does nothing for them. Non-cacheable types (such as Scene) return a fresh instance on every call; with cache, the file's bytes stay in RAM so later loads skip the disk.
auto* scene = AssetManager::Get()->Load<Scene>("scenes/Demo"); // fresh from disk auto* scene = AssetManager::Get()->Load<Scene>("scenes/Demo", true); // fresh from cached bytes
| bool Deki::AssetManager::LoadAssetLookupTable | ( | const uint8_t * | data, |
| size_t | size ) |
Uses the runtime's binary lookup table in data, which must stay valid while lookups run.
Returns false on failure.
| bool Deki::AssetManager::LoadAssetRoot | ( | const char * | root | ) |
Uses a build's exported assets under root (Storage::AssetRoot): reads root/asset_table.bin and root/pack_index.bin and loads every asset from root.
The engine calls it at startup for internal storage; the package that mounts external storage calls it once the card is up. Returns false, with the reason logged, when the table cannot be read.
| void * Deki::AssetManager::LoadByGuidAndType | ( | const std::string & | guid, |
| const char * | assetType, | ||
| bool | cache = false ) |
Loads the asset guid of type assetType (as registered with RegisterLoader), or returns nullptr.
cache works as in Load<T>().
| void * Deki::AssetManager::LoadFromMemory | ( | const std::string & | guid, |
| const char * | assetType, | ||
| const uint8_t * | data, | ||
| size_t | size ) |
Loads an asset from its file's bytes (pack files).
guid is the cache key. Returns nullptr on failure.
| bool Deki::AssetManager::LoadGuidTable | ( | const std::string & | path | ) |
Loads the editor's GUID-to-path table from a JSON file.
Returns false on failure.
| const std::string & Deki::AssetManager::LookupPath | ( | const std::string & | guid | ) | const |
Full path (cache directory + entry) for a GUID, or "" when unknown.
The reference points into a reused buffer and is valid until the next LookupPath call; it avoids a string copy on every AssetRef resolution.
|
static |
Reads a whole file into a Buffer in region, through the engine's filesystem.
Use it for anything big (a mesh, a texture): a std::vector lands on the internal heap and aborts the board when it does not fit, while this logs the size and returns false.
|
static |
Reads a whole file through the filesystem provider.
False when the file cannot be opened or read in full (out is then empty).
| void Deki::AssetManager::RegisterGuid | ( | const std::string & | guid, |
| const std::string & | path ) |
Maps guid to the filesystem path path (editor).
|
static |
Registers how to load assets of type typeName (must match T::kAssetTypeName).
unloader frees one (delete when null); memLoader, optional, loads from bytes in memory (pack files). A cacheable type is loaded once and shared; otherwise each load returns a fresh instance that the caller owns.
| void Deki::AssetManager::ReleaseAssetTracking | ( | const char * | typeName, |
| void * | asset ) |
Stops tracking a non-cacheable asset whose ownership another system (such as SceneSystem) has taken, so UnloadAll() does not free it twice.
| void Deki::AssetManager::RemoveGuid | ( | const std::string & | guid | ) |
Forgets guid when its asset is deleted (editor).
An AssetRef still holding it then loads null and draws nothing, instead of reopening a missing file and flooding the log. Bumps the epoch so those refs resolve again.
|
inline |
Directory asset files are read from: the project's cache directory in the editor, the storage root (such as "S:/") on a device.
|
static |
Frees all assets and the singleton.
| void Deki::AssetManager::UnloadAll | ( | ) |
Frees every loaded asset.
Every pointer returned before is invalid afterwards.
| size_t Deki::AssetManager::UnloadUnused | ( | const std::vector< std::string > & | usedGuids | ) |
Frees every cached asset whose GUID is not in usedGuids, and moves the epoch, so an AssetRef that pointed at one loads it again when next asked.
Returns how many were freed. Engine::ReleaseUnusedAssets works out the list from the live objects.