Unregister events

This commit is contained in:
Garry Newman 2019-04-30 11:38:30 +01:00
parent 3bf1950068
commit a32ab732d1
3 changed files with 50 additions and 9 deletions

View File

@ -7,6 +7,32 @@ using Steamworks.Data;
namespace Steamworks namespace Steamworks
{ {
internal static class Events
{
internal static List<IDisposable> AllClient = new List<IDisposable>();
internal static List<IDisposable> AllServer = new List<IDisposable>();
internal static void DisposeAllClient()
{
foreach ( var a in AllClient.ToArray() )
{
a.Dispose();
}
AllClient.Clear();
}
internal static void DisposeAllServer()
{
foreach ( var a in AllServer.ToArray() )
{
a.Dispose();
}
AllServer.Clear();
}
}
// //
// Created on registration of a callback // Created on registration of a callback
// //
@ -14,13 +40,20 @@ namespace Steamworks
{ {
public Action<T> Action; public Action<T> Action;
bool IsAllocated;
List<GCHandle> Allocations = new List<GCHandle>(); List<GCHandle> Allocations = new List<GCHandle>();
internal IntPtr vTablePtr; internal IntPtr vTablePtr;
internal GCHandle PinnedCallback; internal GCHandle PinnedCallback;
public void Dispose() public void Dispose()
{ {
UnregisterCallback(); if ( !IsAllocated ) return;
IsAllocated = false;
if ( !PinnedCallback.IsAllocated )
throw new System.Exception( "Callback isn't allocated!?" );
SteamClient.UnregisterCallback( PinnedCallback.AddrOfPinnedObject() );
foreach ( var a in Allocations ) foreach ( var a in Allocations )
{ {
@ -28,10 +61,9 @@ namespace Steamworks
a.Free(); a.Free();
} }
Allocations.Clear(); Allocations = null;
if ( PinnedCallback.IsAllocated ) PinnedCallback.Free();
PinnedCallback.Free();
if ( vTablePtr != IntPtr.Zero ) if ( vTablePtr != IntPtr.Zero )
{ {
@ -40,12 +72,9 @@ namespace Steamworks
} }
} }
private void UnregisterCallback() ~Event()
{ {
if ( !PinnedCallback.IsAllocated ) Dispose();
return;
SteamClient.UnregisterCallback( PinnedCallback.AddrOfPinnedObject() );
} }
public virtual bool IsValid { get { return true; } } public virtual bool IsValid { get { return true; } }
@ -107,6 +136,13 @@ namespace Steamworks
// Register the callback with Steam // Register the callback with Steam
// //
SteamClient.RegisterCallback( PinnedCallback.AddrOfPinnedObject(), cb.CallbackId ); SteamClient.RegisterCallback( PinnedCallback.AddrOfPinnedObject(), cb.CallbackId );
IsAllocated = true;
if ( gameserver )
Events.AllServer.Add( this );
else
Events.AllClient.Add( this );
} }
[MonoPInvokeCallback] internal void OnResultThis( IntPtr self, IntPtr param ) => OnResult( param ); [MonoPInvokeCallback] internal void OnResultThis( IntPtr self, IntPtr param ) => OnResult( param );

View File

@ -29,6 +29,7 @@ namespace Steamworks
initialized = true; initialized = true;
SteamApps.InstallEvents(); SteamApps.InstallEvents();
SteamUtils.InstallEvents(); SteamUtils.InstallEvents();
SteamParental.InstallEvents(); SteamParental.InstallEvents();
@ -63,6 +64,8 @@ namespace Steamworks
public static void Shutdown() public static void Shutdown()
{ {
Events.DisposeAllClient();
initialized = false; initialized = false;
SteamApps.Shutdown(); SteamApps.Shutdown();

View File

@ -78,6 +78,8 @@ namespace Steamworks
public static void Shutdown() public static void Shutdown()
{ {
Events.DisposeAllServer();
initialized = false; initialized = false;
_internal = null; _internal = null;