From 53dfddabf0d8e949af2f383f152c978ced38026b Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 15 Jul 2016 11:26:09 +0100 Subject: [PATCH] Refactored init --- Facepunch.Steamworks/Client.Auth.cs | 4 +- Facepunch.Steamworks/Client.Networking.cs | 6 +- Facepunch.Steamworks/Client.Overlay.cs | 20 +-- .../Client.ServerList.Request.cs | 6 +- Facepunch.Steamworks/Client.ServerList.cs | 2 +- Facepunch.Steamworks/Client.Voice.cs | 8 +- Facepunch.Steamworks/Client.cs | 122 +++++++++++------- Facepunch.Steamworks/Client/App.cs | 2 +- Facepunch.Steamworks/Client/Inventory.cs | 26 ++-- 9 files changed, 112 insertions(+), 84 deletions(-) diff --git a/Facepunch.Steamworks/Client.Auth.cs b/Facepunch.Steamworks/Client.Auth.cs index 0e72aec..e951c67 100644 --- a/Facepunch.Steamworks/Client.Auth.cs +++ b/Facepunch.Steamworks/Client.Auth.cs @@ -42,7 +42,7 @@ public unsafe Ticket GetAuthSessionTicket() fixed ( byte* b = data ) { uint ticketLength = 0; - uint ticket = client._user.GetAuthSessionTicket( (IntPtr) b, data.Length, ref ticketLength ); + uint ticket = client.native.user.GetAuthSessionTicket( (IntPtr) b, data.Length, ref ticketLength ); if ( ticket == 0 ) return null; @@ -61,7 +61,7 @@ public unsafe Ticket GetAuthSessionTicket() /// public void CancelAuthTicket( Ticket ticket ) { - client._user.CancelAuthTicket( ticket.Handle ); + client.native.user.CancelAuthTicket( ticket.Handle ); ticket.Handle = 0; ticket.Data = null; } diff --git a/Facepunch.Steamworks/Client.Networking.cs b/Facepunch.Steamworks/Client.Networking.cs index c103e3a..35ee8fa 100644 --- a/Facepunch.Steamworks/Client.Networking.cs +++ b/Facepunch.Steamworks/Client.Networking.cs @@ -113,7 +113,7 @@ public unsafe bool SendP2PPacket( ulong steamid, byte[] data, int length, EP2PSe { fixed ( byte* p = data ) { - return client._networking.SendP2PPacket( steamid, (IntPtr) p, (uint)length, (uint)eP2PSendType, nChannel ); + return client.native.networking.SendP2PPacket( steamid, (IntPtr) p, (uint)length, (uint)eP2PSendType, nChannel ); } } @@ -121,7 +121,7 @@ private unsafe bool ReadP2PPacket( MemoryStream ms, int channel = 0 ) { uint DataAvailable = 0; - if ( !client._networking.IsP2PPacketAvailable( ref DataAvailable, channel ) || DataAvailable == 0 ) + if ( !client.native.networking.IsP2PPacketAvailable( ref DataAvailable, channel ) || DataAvailable == 0 ) return false; if ( ms.Capacity < DataAvailable ) @@ -133,7 +133,7 @@ private unsafe bool ReadP2PPacket( MemoryStream ms, int channel = 0 ) fixed ( byte* p = ms.GetBuffer() ) { ulong steamid = 1; - if ( !client._networking.ReadP2PPacket( (IntPtr)p, (uint)DataAvailable, ref DataAvailable, ref steamid, channel ) || DataAvailable == 0 ) + if ( !client.native.networking.ReadP2PPacket( (IntPtr)p, (uint)DataAvailable, ref DataAvailable, ref steamid, channel ) || DataAvailable == 0 ) return false; ms.SetLength( DataAvailable ); diff --git a/Facepunch.Steamworks/Client.Overlay.cs b/Facepunch.Steamworks/Client.Overlay.cs index 8cded37..66a635c 100644 --- a/Facepunch.Steamworks/Client.Overlay.cs +++ b/Facepunch.Steamworks/Client.Overlay.cs @@ -25,17 +25,17 @@ public class Overlay { internal Client client; - public void OpenProfile( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "steamid", steamid ); } - public void OpenChat( ulong steamid ){ client._friends.ActivateGameOverlayToUser( "chat", steamid ); } - public void OpenTrade( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "jointrade", steamid ); } - public void OpenStats( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "stats", steamid ); } - public void OpenAchievements( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "achievements", steamid ); } + public void OpenProfile( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "steamid", steamid ); } + public void OpenChat( ulong steamid ){ client.native.friends.ActivateGameOverlayToUser( "chat", steamid ); } + public void OpenTrade( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "jointrade", steamid ); } + public void OpenStats( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "stats", steamid ); } + public void OpenAchievements( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "achievements", steamid ); } - public void AddFriend( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendadd", steamid ); } - public void RemoveFriend( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendremove", steamid ); } - public void AcceptFriendRequest( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendrequestaccept", steamid ); } - public void IgnoreFriendRequest( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendrequestignore", steamid ); } + public void AddFriend( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendadd", steamid ); } + public void RemoveFriend( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendremove", steamid ); } + public void AcceptFriendRequest( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendrequestaccept", steamid ); } + public void IgnoreFriendRequest( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendrequestignore", steamid ); } - public void OpenUrl( string url ) { client._friends.ActivateGameOverlayToWebPage( url ); } + public void OpenUrl( string url ) { client.native.friends.ActivateGameOverlayToWebPage( url ); } } } diff --git a/Facepunch.Steamworks/Client.ServerList.Request.cs b/Facepunch.Steamworks/Client.ServerList.Request.cs index 300b03f..50b8a49 100644 --- a/Facepunch.Steamworks/Client.ServerList.Request.cs +++ b/Facepunch.Steamworks/Client.ServerList.Request.cs @@ -136,7 +136,7 @@ public void Dispose() if ( !Finished && Id != IntPtr.Zero ) { if ( client.Valid ) - client._servers.CancelQuery( Id ); + client.native.servers.CancelQuery( Id ); Id = IntPtr.Zero; } @@ -170,7 +170,7 @@ private void NonResponsive( IntPtr thisptr, IntPtr RequestId, int iServer ) if ( RequestId != Id ) throw new Exception( "Request ID is invalid!" ); - var info = client._servers.GetServerDetails( Id, iServer ); + var info = client.native.servers.GetServerDetails( Id, iServer ); Unresponsive.Add( Server.FromSteam( info ) ); } @@ -179,7 +179,7 @@ private void OnServerResponded( IntPtr thisptr, IntPtr RequestId, int iServer ) if ( RequestId != Id ) throw new Exception( "Request ID is invalid!" ); - var info = client._servers.GetServerDetails( Id, iServer ); + var info = client.native.servers.GetServerDetails( Id, iServer ); Responded.Add( Server.FromSteam( info ) ); System.Diagnostics.Debug.WriteLine( info.m_szServerName ); diff --git a/Facepunch.Steamworks/Client.ServerList.cs b/Facepunch.Steamworks/Client.ServerList.cs index ad2ff2f..2e4d2f3 100644 --- a/Facepunch.Steamworks/Client.ServerList.cs +++ b/Facepunch.Steamworks/Client.ServerList.cs @@ -53,7 +53,7 @@ public unsafe Request Test() client = client }; - request.Id = client._servers.RequestInternetServerList( client.AppId, new IntPtr[] { }, request.GetVTablePointer() ); + request.Id = client.native.servers.RequestInternetServerList( client.AppId, new IntPtr[] { }, request.GetVTablePointer() ); return request; } diff --git a/Facepunch.Steamworks/Client.Voice.cs b/Facepunch.Steamworks/Client.Voice.cs index cc003ea..5a9a155 100644 --- a/Facepunch.Steamworks/Client.Voice.cs +++ b/Facepunch.Steamworks/Client.Voice.cs @@ -39,7 +39,7 @@ public class Voice /// public uint OptimalSampleRate { - get { return client._user.GetVoiceOptimalSampleRate(); } + get { return client.native.user.GetVoiceOptimalSampleRate(); } } private bool _wantsrecording = false; @@ -58,11 +58,11 @@ public bool WantsRecording if ( value ) { - client._user.StartVoiceRecording(); + client.native.user.StartVoiceRecording(); } else { - client._user.StopVoiceRecording(); + client.native.user.StopVoiceRecording(); } } } @@ -86,7 +86,7 @@ internal unsafe void Update() bufferRegularLastWrite = 0; bufferCompressedLastWrite = 0; - Valve.Steamworks.EVoiceResult result = (Valve.Steamworks.EVoiceResult) client._user.GetVoice( OnUncompressedData != null, (IntPtr) pbufferCompressed, (uint) bufferCompressed.Length, ref bufferCompressedLastWrite, + Valve.Steamworks.EVoiceResult result = (Valve.Steamworks.EVoiceResult) client.native.user.GetVoice( OnUncompressedData != null, (IntPtr) pbufferCompressed, (uint) bufferCompressed.Length, ref bufferCompressedLastWrite, OnCompressedData != null, (IntPtr) pbufferRegular, (uint) bufferRegular.Length, ref bufferRegularLastWrite, DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index 2e26673..a4d6c10 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -7,16 +7,66 @@ namespace Facepunch.Steamworks { public partial class Client : IDisposable { - private uint _hpipe; - private uint _huser; - internal Valve.Steamworks.ISteamClient _client; - internal Valve.Steamworks.ISteamUser _user; - internal Valve.Steamworks.ISteamApps _apps; - internal Valve.Steamworks.ISteamFriends _friends; - internal Valve.Steamworks.ISteamMatchmakingServers _servers; - internal Valve.Steamworks.ISteamInventory _inventory; - internal Valve.Steamworks.ISteamNetworking _networking; + + internal class Internal : IDisposable + { + private uint _hpipe; + private uint _huser; + + internal Valve.Steamworks.ISteamClient client; + internal Valve.Steamworks.ISteamUser user; + internal Valve.Steamworks.ISteamApps apps; + internal Valve.Steamworks.ISteamFriends friends; + internal Valve.Steamworks.ISteamMatchmakingServers servers; + internal Valve.Steamworks.ISteamInventory inventory; + internal Valve.Steamworks.ISteamNetworking networking; + + internal bool Init() + { + client = Valve.Steamworks.SteamAPI.SteamClient(); + + if ( client.GetIntPtr() == IntPtr.Zero ) + { + client = null; + return false; + } + + _hpipe = client.CreateSteamPipe(); + _huser = client.ConnectToGlobalUser( _hpipe ); + + friends = client.GetISteamFriends( _huser, _hpipe, "SteamFriends015" ); + user = client.GetISteamUser( _huser, _hpipe, "SteamUser019" ); + servers = client.GetISteamMatchmakingServers( _huser, _hpipe, "SteamMatchMakingServers002" ); + inventory = client.GetISteamInventory( _huser, _hpipe, "STEAMINVENTORY_INTERFACE_V001" ); + networking = client.GetISteamNetworking( _huser, _hpipe, "SteamNetworking005" ); + apps = client.GetISteamApps( _huser, _hpipe, "STEAMAPPS_INTERFACE_VERSION008" ); + + return true; + } + + public void Dispose() + { + if ( _hpipe > 0 && client != null ) + { + if ( _huser > 0 ) + client.ReleaseUser( _hpipe, _huser ); + + client.BReleaseSteamPipe( _hpipe ); + + _huser = 0; + _hpipe = 0; + } + + if ( client != null ) + { + client.BShutdownIfAllPipesClosed(); + client = null; + } + } + } + + internal Internal native; /// /// Current running program's AppId @@ -47,11 +97,16 @@ public enum MessageType : int public Client( uint appId ) { Valve.Steamworks.SteamAPI.Init( appId ); - _client = Valve.Steamworks.SteamAPI.SteamClient(); - if ( _client.GetIntPtr() == IntPtr.Zero ) + native = new Internal(); + + // + // Get other interfaces + // + if ( !native.Init() ) { - _client = null; + native.Dispose(); + native = null; return; } @@ -59,49 +114,22 @@ public Client( uint appId ) // Set up warning hook callback // SteamAPIWarningMessageHook ptr = InternalOnWarning; - _client.SetWarningMessageHook( Marshal.GetFunctionPointerForDelegate( ptr ) ); + native.client.SetWarningMessageHook( Marshal.GetFunctionPointerForDelegate( ptr ) ); // - // Get pipes + // Cache common, unchanging info // - _hpipe = _client.CreateSteamPipe(); - _huser = _client.ConnectToGlobalUser( _hpipe ); - - - // - // Get other interfaces - // - _friends = _client.GetISteamFriends( _huser, _hpipe, "SteamFriends015" ); - _user = _client.GetISteamUser( _huser, _hpipe, "SteamUser019" ); - _servers = _client.GetISteamMatchmakingServers( _huser, _hpipe, "SteamMatchMakingServers002" ); - _inventory = _client.GetISteamInventory( _huser, _hpipe, "STEAMINVENTORY_INTERFACE_V001" ); - _networking = _client.GetISteamNetworking( _huser, _hpipe, "SteamNetworking005" ); - _apps = _client.GetISteamApps( _huser, _hpipe, "STEAMAPPS_INTERFACE_VERSION008" ); - AppId = appId; - Username = _friends.GetPersonaName(); - SteamId = _user.GetSteamID(); + Username = native.friends.GetPersonaName(); + SteamId = native.user.GetSteamID(); } public void Dispose() { - if ( _client != null ) + if ( native != null) { - if ( _hpipe > 0 ) - { - if ( _huser > 0 ) - _client.ReleaseUser( _hpipe, _huser ); - - _client.BReleaseSteamPipe( _hpipe ); - - _huser = 0; - _hpipe = 0; - } - - _friends = null; - - _client.BShutdownIfAllPipesClosed(); - _client = null; + native.Dispose(); + native = null; } } @@ -129,7 +157,7 @@ public void Update() public bool Valid { - get { return _client != null; } + get { return native != null; } } internal Action InstallCallback( int type, Delegate action ) diff --git a/Facepunch.Steamworks/Client/App.cs b/Facepunch.Steamworks/Client/App.cs index 7dd6556..fc4dc99 100644 --- a/Facepunch.Steamworks/Client/App.cs +++ b/Facepunch.Steamworks/Client/App.cs @@ -32,7 +32,7 @@ internal App( Client c ) public void MarkContentCorrupt( bool missingFilesOnly = false ) { - client._apps.MarkContentCorrupt( missingFilesOnly ); + client.native.apps.MarkContentCorrupt( missingFilesOnly ); } diff --git a/Facepunch.Steamworks/Client/Inventory.cs b/Facepunch.Steamworks/Client/Inventory.cs index 576b456..666b752 100644 --- a/Facepunch.Steamworks/Client/Inventory.cs +++ b/Facepunch.Steamworks/Client/Inventory.cs @@ -46,7 +46,7 @@ internal Inventory( Client c ) /// public void DropHeartbeat() { - client._inventory.SendItemDropHeartbeat(); + client.native.inventory.SendItemDropHeartbeat(); } /// @@ -58,8 +58,8 @@ public void DropHeartbeat() public void TriggerItemDrop( Definition definition ) { int result = 0; - client._inventory.TriggerItemDrop( ref result, definition.Id ); - client._inventory.DestroyResult( result ); + client.native.inventory.TriggerItemDrop( ref result, definition.Id ); + client.native.inventory.DestroyResult( result ); } @@ -74,7 +74,7 @@ public void Refresh() if ( updateRequest != 0 ) return; - client._inventory.GetAllItems( ref updateRequest ); + client.native.inventory.GetAllItems( ref updateRequest ); } internal void LoadItemDefinitions() @@ -82,10 +82,10 @@ internal void LoadItemDefinitions() // // Make sure item definitions are loaded, because we're going to be using them. // - client._inventory.LoadItemDefinitions(); + client.native.inventory.LoadItemDefinitions(); int[] ids; - if ( !client._inventory.GetItemDefinitionIDs( out ids ) ) + if ( !client.native.inventory.GetItemDefinitionIDs( out ids ) ) return; Definitions = ids.Select( x => @@ -105,7 +105,7 @@ internal void LoadItemDefinitions() internal T DefinitionProperty( int i, string name ) { string val = string.Empty; - client._inventory.GetItemDefinitionProperty( i, name, out val ); + client.native.inventory.GetItemDefinitionProperty( i, name, out val ); return (T) Convert.ChangeType( val, typeof( T) ); } @@ -113,7 +113,7 @@ internal void DestroyResult() { if ( updateRequest != 0 ) { - client._inventory.DestroyResult( updateRequest ); + client.native.inventory.DestroyResult( updateRequest ); updateRequest = 0; } } @@ -129,7 +129,7 @@ private void UpdateRequest() if ( updateRequest == 0 ) return; - var status = (Valve.Steamworks.EResult) client._inventory.GetResultStatus( updateRequest ); + var status = (Valve.Steamworks.EResult) client.native.inventory.GetResultStatus( updateRequest ); if ( status == Valve.Steamworks.EResult.k_EResultPending ) return; @@ -165,7 +165,7 @@ private void UpdateRequest() private unsafe void RetrieveInventory() { Valve.Steamworks.SteamItemDetails_t[] items = null; - client._inventory.GetResultItems( updateRequest, out items ); + client.native.inventory.GetResultItems( updateRequest, out items ); Items = items.Select( x => { @@ -184,12 +184,12 @@ private unsafe void RetrieveInventory() // Get a serialized version // uint size = 0; - client._inventory.SerializeResult( updateRequest, IntPtr.Zero, ref size ); + client.native.inventory.SerializeResult( updateRequest, IntPtr.Zero, ref size ); SerializedItems = new byte[size]; fixed( byte* b = SerializedItems ) { - client._inventory.SerializeResult( updateRequest, (IntPtr) b, ref size ); + client.native.inventory.SerializeResult( updateRequest, (IntPtr) b, ref size ); } SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) ); @@ -243,7 +243,7 @@ public T GetProperty( string name ) { string val = string.Empty; - if ( !client._inventory.GetItemDefinitionProperty( Id, name, out val ) ) + if ( !client.native.inventory.GetItemDefinitionProperty( Id, name, out val ) ) return default( T ); try