Cleaning up

This commit is contained in:
Garry Newman 2016-10-07 11:50:49 +01:00
parent e56ab1f62a
commit a203523824
13 changed files with 134 additions and 53 deletions

View File

@ -22,12 +22,14 @@ namespace Facepunch.Steamworks.Test
[TestMethod] [TestMethod]
public void Init_10000() public void Init_10000()
{ {
for ( int i = 0; i < 100; i++ ) for ( int i = 0; i < 50; i++ )
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.IsValid ); Assert.IsTrue( client.IsValid );
} }
GC.Collect();
} }
} }

View File

@ -25,6 +25,15 @@ namespace Facepunch.Steamworks
} }
Disposables.Clear(); Disposables.Clear();
Workshop.Dispose();
Workshop = null;
Inventory.Dispose();
Inventory = null;
Networking.Dispose();
Networking = null;
if ( native != null ) if ( native != null )
{ {
native.Dispose(); native.Dispose();

View File

@ -22,6 +22,8 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public string BetaName { get; private set; } public string BetaName { get; private set; }
public Voice Voice { get; internal set; }
public Client( uint appId ) public Client( uint appId )
{ {
Valve.Steamworks.SteamAPIInterop.SteamAPI_Init(); Valve.Steamworks.SteamAPIInterop.SteamAPI_Init();
@ -49,6 +51,12 @@ namespace Facepunch.Steamworks
// //
SetupCommonInterfaces(); SetupCommonInterfaces();
//
// Client only interfaces
//
Voice = new Voice( this );
// //
// Cache common, unchanging info // Cache common, unchanging info
// //
@ -89,5 +97,15 @@ namespace Facepunch.Steamworks
base.Update(); base.Update();
} }
public override void Dispose()
{
Voice.Dispose();
Voice = null;
base.Dispose();
Valve.Interop.NativeEntrypoints.Extended.SteamAPI_Shutdown();
}
} }
} }

View File

@ -106,6 +106,7 @@ namespace Facepunch.Steamworks
internal void OnServerRulesReceiveFinished( bool Success ) internal void OnServerRulesReceiveFinished( bool Success )
{ {
RulesRequest.Dispose();
RulesRequest = null; RulesRequest = null;
if ( OnReceivedRules != null ) if ( OnReceivedRules != null )

View File

@ -7,22 +7,6 @@ using System.Text;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public partial class Client : IDisposable
{
private Voice _voice;
public Voice Voice
{
get
{
if ( _voice == null )
_voice = new Voice( this );
return _voice;
}
}
}
public class Voice : IDisposable public class Voice : IDisposable
{ {
const int ReadBufferSize = 1024 * 128; const int ReadBufferSize = 1024 * 128;

View File

@ -31,9 +31,9 @@ namespace Facepunch.Steamworks
public T GetProperty<T>( string name ) public T GetProperty<T>( string name )
{ {
string val = string.Empty; string val = GetStringProperty( name );
if ( !inventory.GetItemDefinitionProperty( Id, name, out val ) ) if ( string.IsNullOrEmpty( val ) )
return default( T ); return default( T );
try try
@ -46,10 +46,20 @@ namespace Facepunch.Steamworks
} }
} }
public string GetStringProperty( string name )
{
string val = string.Empty;
if ( !inventory.GetItemDefinitionProperty( Id, name, out val ) )
return string.Empty;
return val;
}
internal void SetupCommonProperties() internal void SetupCommonProperties()
{ {
Name = GetProperty<string>( "name" ); Name = GetStringProperty( "name" );
Description = GetProperty<string>( "description" ); Description = GetStringProperty( "description" );
Created = GetProperty<DateTime>( "timestamp" ); Created = GetProperty<DateTime>( "timestamp" );
Modified = GetProperty<DateTime>( "modified" ); Modified = GetProperty<DateTime>( "modified" );
} }

View File

@ -99,6 +99,7 @@ namespace Facepunch.Steamworks
{ {
inventory.inventory.DestroyResult( Handle ); inventory.inventory.DestroyResult( Handle );
Handle = -1; Handle = -1;
inventory = null;
} }
} }

View File

@ -6,7 +6,7 @@ using System.Text;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public partial class Inventory public partial class Inventory : IDisposable
{ {
/// <summary> /// <summary>
/// Called when the local client's items are first retrieved, and when they change. /// Called when the local client's items are first retrieved, and when they change.
@ -32,7 +32,9 @@ namespace Facepunch.Steamworks
public DateTime SerializedExpireTime; public DateTime SerializedExpireTime;
internal Valve.Steamworks.ISteamInventory inventory; internal Valve.Steamworks.ISteamInventory inventory;
private Result LocalPlayerRequest; private Result LocalPlayerRequest;
private bool IsServer { get; set; } private bool IsServer { get; set; }
internal Inventory( Valve.Steamworks.ISteamInventory c, bool server ) internal Inventory( Valve.Steamworks.ISteamInventory c, bool server )
@ -44,6 +46,20 @@ namespace Facepunch.Steamworks
FetchItemDefinitions(); FetchItemDefinitions();
} }
public void Dispose()
{
if ( LocalPlayerRequest != null )
{
LocalPlayerRequest.Dispose();
LocalPlayerRequest = null;
}
inventory = null;
Items = null;
SerializedItems = null;
}
/// <summary> /// <summary>
/// Call this at least every two minutes, every frame doesn't hurt. /// Call this at least every two minutes, every frame doesn't hurt.
/// You should call it when you consider it active play time. /// You should call it when you consider it active play time.

View File

@ -8,7 +8,7 @@ using Valve.Steamworks;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public class Networking public class Networking : IDisposable
{ {
public Action<ulong, MemoryStream, int> OnP2PData; public Action<ulong, MemoryStream, int> OnP2PData;
public Func<ulong, bool> OnIncomingConnection; public Func<ulong, bool> OnIncomingConnection;
@ -24,6 +24,15 @@ namespace Facepunch.Steamworks
sw.AddCallback<P2PSessionConnectFail>( onP2PConnectionFailed, P2PSessionConnectFail.CallbackId ); sw.AddCallback<P2PSessionConnectFail>( onP2PConnectionFailed, P2PSessionConnectFail.CallbackId );
} }
public void Dispose()
{
networking = null;
OnIncomingConnection = null;
OnConnectionFailed = null;
OnP2PData = null;
}
internal void Update() internal void Update()
{ {
for ( int i = 0; i < 32; i++ ) for ( int i = 0; i < 32; i++ )

View File

@ -5,7 +5,7 @@ using Valve.Steamworks;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public partial class Workshop public partial class Workshop : IDisposable
{ {
internal const ulong InvalidHandle = 0xffffffffffffffff; internal const ulong InvalidHandle = 0xffffffffffffffff;
@ -26,6 +26,16 @@ namespace Facepunch.Steamworks
steamworks.AddCallback<ItemInstalled>( onItemInstalled, ItemInstalled.CallbackId ); steamworks.AddCallback<ItemInstalled>( onItemInstalled, ItemInstalled.CallbackId );
} }
public void Dispose()
{
ugc = null;
steamworks = null;
remoteStorage = null;
OnFileDownloaded = null;
OnItemInstalled = null;
}
private void onItemInstalled( ItemInstalled obj ) private void onItemInstalled( ItemInstalled obj )
{ {
if ( OnItemInstalled != null ) if ( OnItemInstalled != null )

View File

@ -24,7 +24,7 @@ namespace Facepunch.Steamworks.Interop.VTable.This
[MarshalAs(UnmanagedType.FunctionPtr)] [MarshalAs(UnmanagedType.FunctionPtr)]
public GetSize m_GetCallbackSizeBytes; public GetSize m_GetCallbackSizeBytes;
internal static IntPtr Get( Action<IntPtr> onRunCallback, Func<int> getSize ) internal static IntPtr Get( Action<IntPtr> onRunCallback, Func<int> getSize, Interop.Callback cb )
{ {
var size = Marshal.SizeOf( typeof( Callback ) ); var size = Marshal.SizeOf( typeof( Callback ) );
var ptr = Marshal.AllocHGlobal( size ); var ptr = Marshal.AllocHGlobal( size );
@ -32,8 +32,8 @@ namespace Facepunch.Steamworks.Interop.VTable.This
Callback.Result da = ( _, p ) => onRunCallback( p ); Callback.Result da = ( _, p ) => onRunCallback( p );
Callback.GetSize dc = ( _ ) => getSize(); Callback.GetSize dc = ( _ ) => getSize();
var a = GCHandle.Alloc( da ); cb.AddHandle( GCHandle.Alloc( da ) );
var c = GCHandle.Alloc( dc ); cb.AddHandle( GCHandle.Alloc( dc ) );
var table = new Callback() var table = new Callback()
{ {

View File

@ -28,7 +28,27 @@ using System.Text;
namespace Facepunch.Steamworks.Interop namespace Facepunch.Steamworks.Interop
{ {
internal partial class Callback<T> : IDisposable internal partial class Callback : IDisposable
{
List<GCHandle> Handles = new List<GCHandle>();
public virtual void Dispose()
{
foreach ( var handle in Handles )
{
handle.Free();
}
Handles = null;
}
internal void AddHandle( GCHandle gCHandle )
{
Handles.Add( gCHandle );
}
}
internal partial class Callback<T> : Callback
{ {
public int CallbackId = 0; public int CallbackId = 0;
public bool GameServer = false; public bool GameServer = false;
@ -50,7 +70,7 @@ namespace Facepunch.Steamworks.Interop
Valve.Steamworks.SteamAPI.RegisterCallback( callbackPin.AddrOfPinnedObject(), CallbackId ); Valve.Steamworks.SteamAPI.RegisterCallback( callbackPin.AddrOfPinnedObject(), CallbackId );
} }
public virtual void Dispose() public override void Dispose()
{ {
if ( callbackPin.IsAllocated ) if ( callbackPin.IsAllocated )
{ {
@ -63,6 +83,8 @@ namespace Facepunch.Steamworks.Interop
Marshal.FreeHGlobal( vTablePtr ); Marshal.FreeHGlobal( vTablePtr );
vTablePtr = IntPtr.Zero; vTablePtr = IntPtr.Zero;
} }
base.Dispose();
} }
private void OnRunCallback( IntPtr ptr ) private void OnRunCallback( IntPtr ptr )
@ -95,7 +117,7 @@ namespace Facepunch.Steamworks.Interop
{ {
if ( Config.UseThisCall ) if ( Config.UseThisCall )
{ {
vTablePtr = VTable.This.Callback.Get( OnRunCallback, GetSize ); vTablePtr = VTable.This.Callback.Get( OnRunCallback, GetSize, this );
return; return;
} }

View File

@ -6,7 +6,7 @@ using System.Text;
namespace Facepunch.Steamworks.Interop namespace Facepunch.Steamworks.Interop
{ {
class ServerRules class ServerRules : IDisposable
{ {
// Pins and pointers for the created vtable // Pins and pointers for the created vtable
private GCHandle vTablePin; private GCHandle vTablePin;
@ -35,6 +35,27 @@ namespace Facepunch.Steamworks.Interop
Valve.Interop.NativeEntrypoints.SteamAPI_ISteamMatchmakingServers_ServerRules( Server.Client.native.servers.GetIntPtr(), address, (short) queryPort, GetPtr() ); Valve.Interop.NativeEntrypoints.SteamAPI_ISteamMatchmakingServers_ServerRules( Server.Client.native.servers.GetIntPtr(), address, (short) queryPort, GetPtr() );
} }
public void Dispose()
{
if ( vTablePtr != IntPtr.Zero )
{
Marshal.FreeHGlobal( vTablePtr );
vTablePtr = IntPtr.Zero;
}
if ( vTablePin.IsAllocated )
vTablePin.Free();
if ( RulesRespondPin.IsAllocated )
vTablePin.Free();
if ( FailedRespondPin.IsAllocated )
vTablePin.Free();
if ( CompletePin.IsAllocated )
vTablePin.Free();
}
void InstallVTable() void InstallVTable()
{ {
@ -88,28 +109,6 @@ namespace Facepunch.Steamworks.Interop
} }
} }
void Unpin()
{
if ( vTablePtr != IntPtr.Zero )
{
Marshal.FreeHGlobal( vTablePtr );
vTablePtr = IntPtr.Zero;
}
if ( vTablePin.IsAllocated )
vTablePin.Free();
if ( RulesRespondPin.IsAllocated )
vTablePin.Free();
if ( FailedRespondPin.IsAllocated )
vTablePin.Free();
if ( CompletePin.IsAllocated )
vTablePin.Free();
}
private void InternalOnRulesResponded( string k, string v ) private void InternalOnRulesResponded( string k, string v )
{ {
Server.Rules.Add( k, v ); Server.Rules.Add( k, v );