2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2023-11-21 01:52:44 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2021-02-27 00:17:26 +00:00
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public abstract class AudioResource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public AudioDevice Device { get; }
|
|
|
|
|
|
|
|
|
|
public bool IsDisposed { get; private set; }
|
|
|
|
|
|
2023-11-21 01:52:44 +00:00
|
|
|
|
private GCHandle SelfReference;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
2023-11-21 01:52:44 +00:00
|
|
|
|
protected AudioResource(AudioDevice device)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Device = device;
|
|
|
|
|
|
2023-11-21 01:52:44 +00:00
|
|
|
|
SelfReference = GCHandle.Alloc(this, GCHandleType.Weak);
|
|
|
|
|
Device.AddResourceReference(SelfReference);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 02:56:22 +00:00
|
|
|
|
protected virtual void DisposeManagedState() { }
|
|
|
|
|
protected virtual void DisposeUnmanagedState() { }
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
2023-11-21 01:52:44 +00:00
|
|
|
|
protected void Dispose(bool disposing)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
2023-11-21 02:56:22 +00:00
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
DisposeManagedState();
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
2023-11-21 02:56:22 +00:00
|
|
|
|
Device.RemoveResourceReference(SelfReference);
|
|
|
|
|
SelfReference.Free();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisposeUnmanagedState();
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~AudioResource()
|
|
|
|
|
{
|
2023-11-21 02:56:22 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
// If the graphics device associated with this resource was already disposed, we assume
|
|
|
|
|
// that your game is in the middle of shutting down.
|
|
|
|
|
if (!IsDisposed && Device != null && !Device.IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
// If you see this log message, you leaked a graphics resource without disposing it!
|
|
|
|
|
// This means your game may eventually run out of native memory for mysterious reasons.
|
|
|
|
|
Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed.");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-27 00:17:26 +00:00
|
|
|
|
}
|