Skip to content

Commit

Permalink
Cache all audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Aug 13, 2023
1 parent cb9cb24 commit 7f8ff06
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
6 changes: 0 additions & 6 deletions Assets/Scripts/Pal3/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ private IEnumerator PlayAudioClipAsync(AudioSource audioSource,
if (audioSource.clip != null)
{
audioSource.Stop();
Destroy(audioSource.clip);
}

audioSource.clip = audioClip;
Expand Down Expand Up @@ -275,11 +274,6 @@ private void DestroyAllSfxAudioSource()
if (audioSourceParentTransform.GetComponent<AudioSource>() is { } audioSource)
{
audioSource.Stop();

if (audioSource.clip != null)
{
Destroy(audioSource.clip);
}
}

Destroy(audioSourceParentTransform.gameObject);
Expand Down
22 changes: 6 additions & 16 deletions Assets/Scripts/Pal3/Data/GameResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public sealed class GameResourceProvider : IDisposable,

private readonly Dictionary<Type, Dictionary<string, object>> _gameResourceFileCache = new ();

// Cache player actor movement sfx audio clips
private readonly HashSet<string> _audioClipCacheList = AudioConstants.PlayerActorMovementSfxAudioFileNames;

// No need to deallocate the shadow texture since it is been used almost every where.
private static readonly Texture2D ShadowTexture = Resources.Load<Texture2D>("Textures/shadow");

Expand Down Expand Up @@ -250,28 +247,21 @@ public IEnumerator LoadAudioClipAsync(string filePath,
bool streamAudio,
Action<AudioClip> onLoaded)
{
var fileName = Path.GetFileName(filePath);
var shouldCache = _audioClipCacheList.Contains(fileName);
string cacheKey = filePath.ToLower();

if (shouldCache && _audioClipCache.ContainsKey(fileName))
if (_audioClipCache.ContainsKey(cacheKey) &&
_audioClipCache[cacheKey] != null)
{
if (_audioClipCache[fileName] != null) // check if clip has been destroyed
{
onLoaded?.Invoke(_audioClipCache[fileName]);
yield break;
}
onLoaded?.Invoke(_audioClipCache[cacheKey]);
yield break;
}

yield return AudioClipLoader.LoadAudioClipAsync(filePath,
audioType,
streamAudio,
audioClip =>
{
if (shouldCache && audioClip != null)
{
_audioClipCache[fileName] = audioClip;
}
_audioClipCache[cacheKey] = audioClip;
onLoaded?.Invoke(audioClip);
});
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Pal3/GameResourceInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ private IEnumerator InitResourceAsync()
}

// Create and register IFileReader<T> instances using reflection
//foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// Get all types that implement any version of IFileReader<>
Expand Down

0 comments on commit 7f8ff06

Please sign in to comment.