|
Deki 0.19.0
Modular C++ 2D engine for embedded displays and desktop
|
#include <deki/assets/AssetManager.h>
Public Member Functions | |
| 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"). | |
| 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). | |
| void | UnloadAll () |
| Frees every loaded asset. | |
| 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. | |
| 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. | |
| uint64_t | GetEpoch () const noexcept |
| Goes up whenever asset memory is freed or replaced. | |
| void | AdvanceEpoch () noexcept |
| Makes every AssetRef drop its cached pointer and resolve again on its next Get(). | |
| bool | IsTypeCacheable (const char *typeName) const |
| False only for types registered as non-cacheable. | |
| 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. | |
| bool | LoadGuidTable (const std::string &path) |
| Loads the editor's GUID-to-path table from a JSON file. | |
| 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 | RegisterGuid (const std::string &guid, const std::string &path) |
| Maps guid to the filesystem path path (editor). | |
| 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. | |
| const std::string & | GetCacheDirectory () const |
| 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). | |
| const std::string & | LookupPath (const std::string &guid) const |
| Full path (cache directory + entry) for a GUID, or "" when unknown. | |
Static Public Member Functions | |
| static AssetManager * | Get () |
| static void | Shutdown () |
| Frees all assets and the singleton. | |
| 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 bool | HasLoader (const char *typeName) |
| True when a loader is registered for typeName. | |
| static bool | ReadWholeFile (const std::string &path, std::vector< uint8_t > &out) |
| Reads a whole file through the filesystem provider. | |
| 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 |
|
static |
Frees all assets and the singleton.
| 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
|
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.
|
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::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.
| void Deki::AssetManager::UnloadAll | ( | ) |
Frees every loaded asset.
Every pointer returned before is invalid afterwards.
| 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.
| 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.
|
inlinenoexcept |
|
inlinenoexcept |
| bool Deki::AssetManager::IsTypeCacheable | ( | const char * | typeName | ) | const |
False only for types registered as non-cacheable.
| 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.
| bool Deki::AssetManager::LoadGuidTable | ( | const std::string & | path | ) |
Loads the editor's GUID-to-path table from a JSON file.
Returns false on failure.
| 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::RegisterGuid | ( | const std::string & | guid, |
| const std::string & | path ) |
Maps guid to the filesystem path path (editor).
| 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.
|
inline |
|
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.
| 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 through the filesystem provider.
False when the file cannot be opened or read in full (out is then empty).
|
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.