|
Deki 1.3.0
Modular C++ 2D engine for embedded displays and desktop
|
#include <AssetManager.h>
Public Member Functions | |
| template<typename T> | |
| T * | Load (const char *path, bool cache=false) |
| Load an asset by path (unified API). | |
| void * | LoadByGuidAndType (const std::string &guid, const char *assetType, bool cache=false) |
| Load asset by GUID and type name (for LifecycleManager). | |
| void * | LoadFromMemory (const std::string &guid, const char *assetType, const uint8_t *data, size_t size) |
| Load an asset from raw memory data (for pack file support). | |
| void | UnloadAll () |
| Unload all loaded assets. | |
| void | InvalidateAsset (const char *typeName, const std::string &guid) |
| Invalidate a cached asset by type and GUID. | |
| void | ClearCache (const char *typeName) |
| Clear all cached assets of a given type. | |
| void | DropCache () |
| Drop the lookup cache without freeing assets. | |
| uint64_t | GetEpoch () const noexcept |
| Get the current invalidation epoch. | |
| void | AdvanceEpoch () noexcept |
| Advance the invalidation epoch. | |
| bool | IsTypeCacheable (const char *typeName) const |
| Check if an asset type is cacheable. | |
| void | ReleaseAssetTracking (const char *typeName, void *asset) |
| Release tracking of a non-cacheable asset (caller takes ownership). | |
| bool | LoadGuidTable (const std::string &path) |
| Load GUID-to-path mapping table from JSON file (editor mode). | |
| bool | LoadAssetLookupTable (const uint8_t *data, size_t size) |
| Load binary asset lookup table (embedded runtime). | |
| void | RegisterGuid (const std::string &guid, const std::string &path) |
| Register a GUID-to-path mapping (for editor use). | |
| void | RemoveGuid (const std::string &guid) |
| Remove a GUID-to-path mapping (for editor use). | |
| void | SetCacheDirectory (const std::string &dir) |
| Set cache directory for asset loading. | |
| const std::string & | GetCacheDirectory () const |
| Get the cache directory. | |
| bool | IsReady () const |
| Check if the asset system is ready to load assets. | |
| const std::string & | LookupPath (const std::string &guid) const |
| Look up path for a GUID. | |
Static Public Member Functions | |
| static AssetManager * | Get () |
| Get the singleton instance. | |
| static void | Shutdown () |
| Shutdown and cleanup. | |
| static void | RegisterLoader (const char *typeName, AssetLoadFn loader, AssetUnloadFn unloader=nullptr, AssetMemLoadFn memLoader=nullptr, bool cacheable=true) |
| Register an asset loader for a type name. | |
| static bool | HasLoader (const char *typeName) |
| True if a runtime loader is registered for this asset type name. | |
| static bool | ReadWholeFile (const std::string &path, std::vector< uint8_t > &out) |
| Read a whole file through the filesystem provider. | |
|
static |
Get the singleton instance.
|
static |
Shutdown and cleanup.
| T * Deki::AssetManager::Load | ( | const char * | path, |
| bool | cache = false ) |
Load an asset by path (unified API).
This is the primary asset loading API. The type is part of the lookup key, allowing different asset types to share the same path.
For non-cacheable types (e.g., Scene), each call always returns a fresh instance. When cache=true, the raw file bytes are pinned in RAM so subsequent loads skip disk I/O but still return a new instance with the original data.
| T | Asset type — any registered loader type (Scene is core; packages register their own via AssetManager::RegisterLoader) |
| path | Asset path without "assets/" prefix and extension (e.g., "scenes/Demo") |
| cache | If true, pins the raw file bytes in RAM for faster subsequent loads. For non-cacheable types: always returns a fresh instance (from cached bytes or disk). For cacheable types: this parameter has no effect (always cached). Default: false. |
Example usage:
|
static |
Register an asset loader for a type name.
| typeName | The asset type name (must match T::AssetTypeName) |
| loader | Function that loads asset from filesystem path |
| unloader | Function that frees a loaded asset (optional, defaults to delete) |
| memLoader | Function that loads asset from raw memory data (optional) |
| cacheable | If true, loaded assets are cached and reused. If false, each load returns a fresh instance (caller manages lifetime). Default: true. |
|
static |
True if a runtime loader is registered for this asset type name.
Lets the editor treat any .asset whose "type" has a runtime loader (but no texture compiler) as a generic data asset (ScriptableObject-style): its JSON is compiled straight to a MessagePack cache, no per-type editor needed.
| void * Deki::AssetManager::LoadByGuidAndType | ( | const std::string & | guid, |
| const char * | assetType, | ||
| bool | cache = false ) |
Load asset by GUID and type name (for LifecycleManager).
| guid | The asset GUID |
| assetType | Type name registered via RegisterLoader |
| cache | If true, pins raw file bytes in RAM for non-cacheable types. Default: false. |
| void * Deki::AssetManager::LoadFromMemory | ( | const std::string & | guid, |
| const char * | assetType, | ||
| const uint8_t * | data, | ||
| size_t | size ) |
Load an asset from raw memory data (for pack file support).
| guid | Asset GUID (for caching) |
| assetType | Type name |
| data | Raw file data |
| size | Data size in bytes |
| void Deki::AssetManager::UnloadAll | ( | ) |
Unload all loaded assets.
Called when shutting down or cleaning up. After this call, all previously returned pointers are invalid.
| void Deki::AssetManager::InvalidateAsset | ( | const char * | typeName, |
| const std::string & | guid ) |
Invalidate a cached asset by type and GUID.
Removes the asset from the type cache and frees memory. Next load call will reload from disk.
| typeName | Asset type name (e.g., "Sprite", "BitmapFont") |
| guid | The asset GUID |
| void Deki::AssetManager::ClearCache | ( | const char * | typeName | ) |
Clear all cached assets of a given type.
Removes all assets of this type from cache, freeing memory. Next load call will reload from disk.
| typeName | Asset type name (e.g., "BitmapFont") |
| void Deki::AssetManager::DropCache | ( | ) |
Drop the lookup cache without freeing assets.
Clears m_TypeCache so the next load returns a fresh copy from disk, but does NOT free existing assets (they may still be referenced). Used by the editor when stopping play mode.
Does NOT bump the epoch: existing AssetRef::ptr values still point at live memory, so cached references must remain valid.
|
inlinenoexcept |
|
inlinenoexcept |
Advance the invalidation epoch.
Forces every AssetRef to drop its cached pointer and re-resolve on its next Get(). Call when the set of resolvable assets changes in a way that isn't a free/replace of existing memory — e.g. an asset is imported live (or restored via undo), so refs that previously resolved to null can reconnect instead of staying empty.
| bool Deki::AssetManager::IsTypeCacheable | ( | const char * | typeName | ) | const |
Check if an asset type is cacheable.
| typeName | Asset type name (e.g., "Scene" — package types register via RegisterLoader) |
| void Deki::AssetManager::ReleaseAssetTracking | ( | const char * | typeName, |
| void * | asset ) |
Release tracking of a non-cacheable asset (caller takes ownership).
Removes the asset from m_LoadedAssets so UnloadAll() won't double-free it. Call this when an external system (e.g., SceneSystem) takes ownership of an asset.
| typeName | Asset type name (e.g., "Scene") |
| asset | The asset pointer to release |
| bool Deki::AssetManager::LoadGuidTable | ( | const std::string & | path | ) |
Load GUID-to-path mapping table from JSON file (editor mode).
| path | Path to the GUID table JSON file |
| bool Deki::AssetManager::LoadAssetLookupTable | ( | const uint8_t * | data, |
| size_t | size ) |
Load binary asset lookup table (embedded runtime).
The data must remain valid for the lifetime of lookups. Typically loaded from SD card or internal flash.
| data | Pointer to binary table data |
| size | Size of data in bytes |
| void Deki::AssetManager::RegisterGuid | ( | const std::string & | guid, |
| const std::string & | path ) |
Register a GUID-to-path mapping (for editor use).
| guid | The asset GUID |
| path | The resolved filesystem path |
| void Deki::AssetManager::RemoveGuid | ( | const std::string & | guid | ) |
Remove a GUID-to-path mapping (for editor use).
After this the GUID resolves to nothing, so any AssetRef still holding it loads null (renders empty) instead of re-opening a now-missing file and flooding the log. Bumps the epoch so those refs re-resolve. Used when an asset is deleted.
| guid | The asset GUID to forget |
|
inline |
Set cache directory for asset loading.
In editor: set to project's cache directory On device: set to storage directory (e.g., "S:/")
| dir | Cache directory path |
|
inline |
Get the cache directory.
|
inline |
Check if the asset system is ready to load assets.
Returns true when either the cache directory is set (editor edit mode) or the binary asset lookup table is loaded (play mode / embedded runtime). Used by AssetRef::Get() to skip loading during early init (e.g., scene compilation inside ImportAllAssets before filesystem is available).
| const std::string & Deki::AssetManager::LookupPath | ( | const std::string & | guid | ) | const |
Look up path for a GUID.
| guid | The asset GUID |
|
static |
Read a whole file through the filesystem provider.
False when the file cannot be opened or read in full (out is then empty).