Added functions to get name of a backing asset from SHResourceManager

This commit is contained in:
Kah Wei 2022-11-15 18:15:49 +08:00
parent 75f103c372
commit c69ad04f1e
5 changed files with 84 additions and 4 deletions

View File

@ -143,7 +143,17 @@ namespace SHADE
return result; return result;
} }
/**************************************************************************** std::optional<SHADE::SHAsset> SHAssetManager::GetAsset(AssetID id) noexcept
{
if (assetCollection.contains(id))
{
return assetCollection[id];
}
return {};
}
/****************************************************************************
* \brief Create record for new asset. CAN ONLY CREATE FOR CUSTOM * \brief Create record for new asset. CAN ONLY CREATE FOR CUSTOM
* ASSETS CREATED BY THE ENGINE. * ASSETS CREATED BY THE ENGINE.
* *

View File

@ -50,6 +50,7 @@ namespace SHADE
* \return const& to unordered_map<AssetName, AssetID> * \return const& to unordered_map<AssetName, AssetID>
****************************************************************************/ ****************************************************************************/
static std::vector<SHAsset> GetAllAssets() noexcept; static std::vector<SHAsset> GetAllAssets() noexcept;
static std::optional<SHAsset> GetAsset(AssetID id) noexcept;
/**************************************************************************** /****************************************************************************
* \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM * \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM

View File

@ -103,4 +103,17 @@ namespace SHADE
return {}; return {};
} }
std::optional<std::string> SHResourceManager::GetAssetName(Handle<void> handle)
{
const Handle GENERIC_HANDLE = Handle(handle);
auto assetId = GetAssetID(GENERIC_HANDLE);
if (assetId.has_value())
{
const auto ASSET_INFO = SHAssetManager::GetAsset(assetId.value());
if (ASSET_INFO.has_value())
return ASSET_INFO.value().name;
}
return {};
}
} }

View File

@ -64,9 +64,7 @@ namespace SHADE
/// Note that for specific types, the retrieved Handle may not be valid until after /// Note that for specific types, the retrieved Handle may not be valid until after
/// FinaliseChanges() is called. /// FinaliseChanges() is called.
/// </summary> /// </summary>
/// <typeparam name="ResourceType"> /// <typeparam name="ResourceType">Type of resource to load.</typeparam>
/// Type of resource to load.
/// </typeparam>
/// <param name="assetId">Asset ID of the resource to load.</param> /// <param name="assetId">Asset ID of the resource to load.</param>
/// <returns>Handle to a loaded runtime asset.</returns> /// <returns>Handle to a loaded runtime asset.</returns>
template<typename ResourceType> template<typename ResourceType>
@ -74,6 +72,17 @@ namespace SHADE
template<> template<>
static inline Handle<SHMaterial> LoadOrGet<SHMaterial>(AssetID assetId); static inline Handle<SHMaterial> LoadOrGet<SHMaterial>(AssetID assetId);
/// <summary> /// <summary>
/// Retrieves an existing loaded object of the specified type if it has already been
/// loaded prior.
/// </summary>
/// <typeparam name="ResourceType">Type of resource to load.</typeparam>
/// <param name="assetId">Asset ID of the resource to retrieve.</param>
/// <returns>
/// Handle to a loaded runtime asset. Null handle if not loaded before.
/// </returns>
template<typename ResourceType>
static Handle<ResourceType> Get(AssetID assetId);
/// <summary>
/// Unloads an existing loaded asset. Attempting to unload an invalid Handle will /// Unloads an existing loaded asset. Attempting to unload an invalid Handle will
/// simply do nothing except emit a warning. /// simply do nothing except emit a warning.
/// Faster than the untemplated version. /// Faster than the untemplated version.
@ -121,6 +130,31 @@ namespace SHADE
/// value. /// value.
/// </return> /// </return>
static std::optional<AssetID> GetAssetID(Handle<void> handle); static std::optional<AssetID> GetAssetID(Handle<void> handle);
/// <summary>
/// Retrieves the name associated with the AssetID that is associated with the
/// specified Handle.
/// Faster than the untemplated version.
/// </summary>
/// <typeparam name="ResourceType">Type of resource to get the name of.</typeparam>
/// <param name="handle">Handle to get the name of.</param>
/// <return>
/// Name for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
template<typename T>
static std::optional<std::string> GetAssetName(Handle<T> handle);
/// <summary>
/// Retrieves the name associated with the AssetID that is associated with the
/// specified Handle.
/// Compared to the templated version, this function is slower as it requires
/// searching through the storage of all resource types.
/// </summary>
/// <param name="handle">Handle to get the name of.</param>
/// <return>
/// Name for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
static std::optional<std::string> GetAssetName(Handle<void> handle);
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -93,6 +93,16 @@ namespace SHADE
return handle; return handle;
} }
template<typename ResourceType>
Handle<ResourceType> SHResourceManager::Get(AssetID assetId)
{
auto [typedHandleMap, typedAssetIdMap] = getAssetHandleMap<ResourceType>();
if (typedHandleMap.get().contains(assetId))
return Handle<ResourceType>(typedHandleMap.get()[assetId]);
else
return Handle<ResourceType>();
}
template<typename ResourceType> template<typename ResourceType>
void SHResourceManager::Unload(Handle<ResourceType> asset) void SHResourceManager::Unload(Handle<ResourceType> asset)
{ {
@ -139,6 +149,18 @@ namespace SHADE
return {}; return {};
} }
template<typename T>
std::optional<std::string> SHADE::SHResourceManager::GetAssetName(Handle<T> handle)
{
auto assetId = GetAssetID<T>(handle);
if (assetId.has_value())
{
const auto ASSET_INFO = SHAssetManager::GetAsset(assetId.value());
if (ASSET_INFO.has_value())
return ASSET_INFO.value().name;
}
return {};
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */