Deki 1.3.0
Modular C++ 2D engine for embedded displays and desktop
Loading...
Searching...
No Matches

#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.

Member Function Documentation

◆ Get()

AssetManager * Deki::AssetManager::Get ( )
static

Get the singleton instance.

◆ Shutdown()

void Deki::AssetManager::Shutdown ( )
static

Shutdown and cleanup.

◆ Load()

template<typename T>
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.

Template Parameters
TAsset type — any registered loader type (Scene is core; packages register their own via AssetManager::RegisterLoader)
Parameters
pathAsset path without "assets/" prefix and extension (e.g., "scenes/Demo")
cacheIf 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.
Returns
Loaded asset or nullptr on failure

Example usage:

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 AssetManager * Get()
Get the singleton instance.
T * Load(const char *path, bool cache=false)
Load an asset by path (unified API).
Definition AssetManager.h:336
Represents a scene containing objects and components.
Definition Scene.h:32

◆ RegisterLoader()

void Deki::AssetManager::RegisterLoader ( const char * typeName,
AssetLoadFn loader,
AssetUnloadFn unloader = nullptr,
AssetMemLoadFn memLoader = nullptr,
bool cacheable = true )
static

Register an asset loader for a type name.

Parameters
typeNameThe asset type name (must match T::AssetTypeName)
loaderFunction that loads asset from filesystem path
unloaderFunction that frees a loaded asset (optional, defaults to delete)
memLoaderFunction that loads asset from raw memory data (optional)
cacheableIf true, loaded assets are cached and reused. If false, each load returns a fresh instance (caller manages lifetime). Default: true.

◆ HasLoader()

bool Deki::AssetManager::HasLoader ( const char * typeName)
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.

◆ LoadByGuidAndType()

void * Deki::AssetManager::LoadByGuidAndType ( const std::string & guid,
const char * assetType,
bool cache = false )

Load asset by GUID and type name (for LifecycleManager).

Parameters
guidThe asset GUID
assetTypeType name registered via RegisterLoader
cacheIf true, pins raw file bytes in RAM for non-cacheable types. Default: false.
Returns
Loaded asset as void* or nullptr on failure

◆ LoadFromMemory()

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).

Parameters
guidAsset GUID (for caching)
assetTypeType name
dataRaw file data
sizeData size in bytes
Returns
Loaded asset or nullptr

◆ UnloadAll()

void Deki::AssetManager::UnloadAll ( )

Unload all loaded assets.

Called when shutting down or cleaning up. After this call, all previously returned pointers are invalid.

◆ InvalidateAsset()

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.

Parameters
typeNameAsset type name (e.g., "Sprite", "BitmapFont")
guidThe asset GUID

◆ ClearCache()

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.

Parameters
typeNameAsset type name (e.g., "BitmapFont")

◆ DropCache()

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.

◆ GetEpoch()

uint64_t Deki::AssetManager::GetEpoch ( ) const
inlinenoexcept

Get the current invalidation epoch.

Bumped whenever asset memory is freed or replaced. AssetRef compares its cachedEpoch against this on every Get() to detect stale pointers after hot reloads, asset re-imports, etc.

◆ AdvanceEpoch()

void Deki::AssetManager::AdvanceEpoch ( )
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.

◆ IsTypeCacheable()

bool Deki::AssetManager::IsTypeCacheable ( const char * typeName) const

Check if an asset type is cacheable.

Parameters
typeNameAsset type name (e.g., "Scene" — package types register via RegisterLoader)
Returns
true if the type is cacheable (default), false if explicitly non-cacheable

◆ ReleaseAssetTracking()

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.

Parameters
typeNameAsset type name (e.g., "Scene")
assetThe asset pointer to release

◆ LoadGuidTable()

bool Deki::AssetManager::LoadGuidTable ( const std::string & path)

Load GUID-to-path mapping table from JSON file (editor mode).

Parameters
pathPath to the GUID table JSON file
Returns
true on success

◆ LoadAssetLookupTable()

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.

Parameters
dataPointer to binary table data
sizeSize of data in bytes
Returns
true on success

◆ RegisterGuid()

void Deki::AssetManager::RegisterGuid ( const std::string & guid,
const std::string & path )

Register a GUID-to-path mapping (for editor use).

Parameters
guidThe asset GUID
pathThe resolved filesystem path

◆ RemoveGuid()

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.

Parameters
guidThe asset GUID to forget

◆ SetCacheDirectory()

void Deki::AssetManager::SetCacheDirectory ( const std::string & dir)
inline

Set cache directory for asset loading.

In editor: set to project's cache directory On device: set to storage directory (e.g., "S:/")

Parameters
dirCache directory path

◆ GetCacheDirectory()

const std::string & Deki::AssetManager::GetCacheDirectory ( ) const
inline

Get the cache directory.

◆ IsReady()

bool Deki::AssetManager::IsReady ( ) const
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).

◆ LookupPath()

const std::string & Deki::AssetManager::LookupPath ( const std::string & guid) const

Look up path for a GUID.

Parameters
guidThe asset GUID
Returns
The full path (cache directory + value) or empty string if not found Resolved cache path for a GUID, or "" when unknown. The reference is into a reusable buffer: valid until the next LookupPath call. Returning by value copied that buffer on every AssetRef resolution.

◆ ReadWholeFile()

bool Deki::AssetManager::ReadWholeFile ( const std::string & path,
std::vector< uint8_t > & out )
static

Read a whole file through the filesystem provider.

False when the file cannot be opened or read in full (out is then empty).


The documentation for this class was generated from the following file: