diff --git a/Facepunch.Steamworks/Callbacks/Events.cs b/Facepunch.Steamworks/Callbacks/Events.cs index 590fd70..1099a58 100644 --- a/Facepunch.Steamworks/Callbacks/Events.cs +++ b/Facepunch.Steamworks/Callbacks/Events.cs @@ -7,6 +7,32 @@ using Steamworks.Data; namespace Steamworks { + internal static class Events + { + internal static List AllClient = new List(); + internal static List AllServer = new List(); + + 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 // @@ -14,13 +40,20 @@ namespace Steamworks { public Action Action; + bool IsAllocated; List Allocations = new List(); internal IntPtr vTablePtr; internal GCHandle PinnedCallback; 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 ) { @@ -28,10 +61,9 @@ namespace Steamworks a.Free(); } - Allocations.Clear(); + Allocations = null; - if ( PinnedCallback.IsAllocated ) - PinnedCallback.Free(); + PinnedCallback.Free(); if ( vTablePtr != IntPtr.Zero ) { @@ -40,12 +72,9 @@ namespace Steamworks } } - private void UnregisterCallback() + ~Event() { - if ( !PinnedCallback.IsAllocated ) - return; - - SteamClient.UnregisterCallback( PinnedCallback.AddrOfPinnedObject() ); + Dispose(); } public virtual bool IsValid { get { return true; } } @@ -107,6 +136,13 @@ namespace Steamworks // Register the callback with Steam // 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 ); diff --git a/Facepunch.Steamworks/SteamClient.cs b/Facepunch.Steamworks/SteamClient.cs index 240c1fc..c7f5450 100644 --- a/Facepunch.Steamworks/SteamClient.cs +++ b/Facepunch.Steamworks/SteamClient.cs @@ -29,6 +29,7 @@ namespace Steamworks initialized = true; + SteamApps.InstallEvents(); SteamUtils.InstallEvents(); SteamParental.InstallEvents(); @@ -63,6 +64,8 @@ namespace Steamworks public static void Shutdown() { + Events.DisposeAllClient(); + initialized = false; SteamApps.Shutdown(); diff --git a/Facepunch.Steamworks/SteamServer.cs b/Facepunch.Steamworks/SteamServer.cs index a441d14..6a1fd49 100644 --- a/Facepunch.Steamworks/SteamServer.cs +++ b/Facepunch.Steamworks/SteamServer.cs @@ -78,6 +78,8 @@ namespace Steamworks public static void Shutdown() { + Events.DisposeAllServer(); + initialized = false; _internal = null;