Add utility to get loaded assets

This commit is contained in:
Wojtek Figat
2026-02-05 12:37:03 +01:00
parent 7e9ee0610a
commit 66894b71fa
3 changed files with 52 additions and 22 deletions

View File

@@ -682,30 +682,28 @@ void AudioBackendOAL::Base_OnActiveDeviceChanged()
if (ALC::Inited)
{
// Reload all audio clips to recreate their buffers
for (Asset* asset : Content::GetAssets())
for (AudioClip* audioClip : Content::GetAssets<AudioClip>())
{
if (auto* audioClip = ScriptingObject::Cast<AudioClip>(asset))
audioClip->WaitForLoaded();
ScopeLock lock(audioClip->Locker);
// Clear old buffer IDs
for (uint32& bufferID : audioClip->Buffers)
bufferID = 0;
if (audioClip->IsStreamable())
{
ScopeLock lock(audioClip->Locker);
// Let the streaming recreate missing buffers
audioClip->RequestStreamingUpdate();
}
else
{
// Reload audio clip
auto assetLock = audioClip->Storage->Lock();
audioClip->LoadChunk(0);
audioClip->Buffers[0] = AudioBackend::Buffer::Create();
audioClip->WriteBuffer(0);
// Clear old buffer IDs
for (uint32& bufferID : audioClip->Buffers)
bufferID = 0;
if (audioClip->IsStreamable())
{
// Let the streaming recreate missing buffers
audioClip->RequestStreamingUpdate();
}
else
{
// Reload audio clip
auto assetLock = audioClip->Storage->Lock();
audioClip->LoadChunk(0);
audioClip->Buffers[0] = AudioBackend::Buffer::Create();
audioClip->WriteBuffer(0);
}
}
}

View File

@@ -684,6 +684,19 @@ Array<Asset*> Content::GetAssets()
return assets;
}
Array<Asset*> Content::GetAssets(const MClass* type)
{
Array<Asset*> assets;
AssetsLocker.Lock();
for (auto& e : Assets)
{
if (e.Value->Is(type))
assets.Add(e.Value);
}
AssetsLocker.Unlock();
return assets;
}
const Dictionary<Guid, Asset*>& Content::GetAssetsRaw()
{
AssetsLocker.Lock();

View File

@@ -122,7 +122,26 @@ public:
/// Gets the assets (loaded or during load).
/// </summary>
/// <returns>The collection of assets.</returns>
static Array<Asset*, HeapAllocation> GetAssets();
API_FUNCTION() static Array<Asset*, HeapAllocation> GetAssets();
/// <summary>
/// Gets the assets (loaded or during load).
/// </summary>
/// <param name="type">Type of the assets to search for. Includes any assets derived from the type.</param>
/// <returns>Found actors list.</returns>
API_FUNCTION() static Array<Asset*, HeapAllocation> GetAssets(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type);
/// <summary>
/// Gets the assets (loaded or during load).
/// </summary>
/// <typeparam name="T">Type of the object.</typeparam>
/// <returns>Found actors list.</returns>
template<typename T>
static Array<T*, HeapAllocation> GetAssets()
{
Array<Asset*, HeapAllocation> assets = GetAssets(T::GetStaticClass());
return *(Array<T*, HeapAllocation>*) & assets;
}
/// <summary>
/// Gets the raw dictionary of assets (loaded or during load).