Skip to content

Commit

Permalink
[Asset]Add dependency tracking API.
Browse files Browse the repository at this point in the history
  • Loading branch information
JX-Master committed Sep 22, 2024
1 parent a1c43d9 commit 6178813
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Modules/Luna/Asset/Asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ namespace Luna
//! This can be `nullptr` if the user calls @ref set_asset_data with `data` equals to `nullptr`. In such case,
//! this function behaves like unloading existing asset data object.
RV(*on_set_asset_data)(object_t userdata, asset_t asset, object_t data) = nullptr;
//! Called when assets referred by the specified asset is required.
//! @param[in] userdata The userdata.
//! @param[in] asset The asset handle of the asset to be queried.
//! @param[out] referred_assets Returns the assets referred by this asset.
//! This vector may not be empty when this function is called. If this vector is not empty, the returned
//! assets shall be pushed to the end of this vector, and existing elements shall not be modified.
void(*on_get_referred_assets)(object_t userdata, asset_t asset, Vector<asset_t>& referred_assets) = nullptr;
};

//! Registers one asset type so the asset system can handle the asset of that type.
Expand Down Expand Up @@ -319,6 +326,13 @@ namespace Luna
//! @param[in] asset The asset handle of the asset to operate.
LUNA_ASSET_API RV save_asset(asset_t asset);

//! Gets referred assets of the specified asset.
//! @param[in] asset The handle of the asset to query.
//! @param[out] out_referred_assets Returns the referred assets of the specified asset.
//! If this vector is not empty, the returned assets will be pushed to the end of the vector, and existing
//! elements will not be modified.
LUNA_ASSET_API void get_referred_assets(asset_t asset, Vector<asset_t>& out_referred_assets);

//! Closes the asset registry.
//! @details This call removes all registered assets and asset types, and invalidates all asset handles.
LUNA_ASSET_API void close();
Expand Down
15 changes: 15 additions & 0 deletions Modules/Luna/Asset/Source/Asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,21 @@ namespace Luna
lucatchret;
return ok;
}
LUNA_ASSET_API void get_referred_assets(asset_t asset, Vector<asset_t>& out_referred_assets)
{
if(!asset.handle) return;
AssetEntry* entry = (AssetEntry*)asset.handle;
LockGuard g(entry->lock);
if (entry->type.empty()) return;
auto desc = get_asset_type_desc(entry->type);
if(failed(desc)) return;
ObjRef data = entry->data;
g.unlock();
if(desc.get().on_get_referred_assets)
{
desc.get().on_get_referred_assets(desc.get().userdata.get(), asset, out_referred_assets);
}
}
LUNA_ASSET_API void close()
{
MutexGuard guard(g_assets_mutex);
Expand Down

0 comments on commit 6178813

Please sign in to comment.