DEKI/ENGINE docs
Deki 0.19.0
Modular C++ 2D engine for embedded displays and desktop
Loading...
Searching...
No Matches

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

Member Function Documentation

◆ Get()

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

◆ Shutdown()

void Deki::AssetManager::Shutdown ( )
static

Frees all assets and the singleton.

◆ Load()

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

◆ RegisterLoader()

void Deki::AssetManager::RegisterLoader ( const char * typeName,
AssetLoadFn loader,
AssetUnloadFn unloader = nullptr,
AssetMemLoadFn memLoader = nullptr,
bool cacheable = true )
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.

◆ HasLoader()

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

◆ LoadByGuidAndType()

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

◆ LoadFromMemory()

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.

◆ UnloadAll()

void Deki::AssetManager::UnloadAll ( )

Frees every loaded asset.

Every pointer returned before is invalid afterwards.

◆ InvalidateAsset()

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.

◆ ClearCache()

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.

◆ DropCache()

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.

◆ GetEpoch()

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

Goes up whenever asset memory is freed or replaced.

AssetRef compares its cachedEpoch with it on every Get() to notice stale pointers after hot reloads, re-imports and the like.

◆ AdvanceEpoch()

void Deki::AssetManager::AdvanceEpoch ( )
inlinenoexcept

Makes every AssetRef drop its cached pointer and resolve again on its next Get().

Call it when the set of loadable assets changes without memory being freed, such as an asset imported live or restored by undo, so refs that resolved to null can find it.

◆ IsTypeCacheable()

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

False only for types registered as non-cacheable.

◆ ReleaseAssetTracking()

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.

◆ LoadGuidTable()

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

Loads the editor's GUID-to-path table from a JSON file.

Returns false on failure.

◆ LoadAssetLookupTable()

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.

◆ LoadAssetRoot()

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.

◆ RegisterGuid()

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

Maps guid to the filesystem path path (editor).

◆ RemoveGuid()

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.

◆ SetCacheDirectory()

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

Directory asset files are read from: the project's cache directory in the editor, the storage root (such as "S:/") on a device.

◆ GetCacheDirectory()

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

◆ IsReady()

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

◆ LookupPath()

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.

◆ ReadWholeFile() [1/2]

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

Reads a whole file through the filesystem provider.

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

◆ ReadWholeFile() [2/2]

bool Deki::AssetManager::ReadWholeFile ( const std::string & path,
Buffer< uint8_t > & out,
Memory::Region region )
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.


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