Refactored init

This commit is contained in:
Garry Newman 2016-07-15 11:26:09 +01:00
parent ee502cce8b
commit 53dfddabf0
9 changed files with 112 additions and 84 deletions

View File

@ -42,7 +42,7 @@ namespace Facepunch.Steamworks
fixed ( byte* b = data ) fixed ( byte* b = data )
{ {
uint ticketLength = 0; 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 ) if ( ticket == 0 )
return null; return null;
@ -61,7 +61,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public void CancelAuthTicket( Ticket ticket ) public void CancelAuthTicket( Ticket ticket )
{ {
client._user.CancelAuthTicket( ticket.Handle ); client.native.user.CancelAuthTicket( ticket.Handle );
ticket.Handle = 0; ticket.Handle = 0;
ticket.Data = null; ticket.Data = null;
} }

View File

@ -113,7 +113,7 @@ namespace Facepunch.Steamworks
{ {
fixed ( byte* p = data ) 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 @@ namespace Facepunch.Steamworks
{ {
uint DataAvailable = 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; return false;
if ( ms.Capacity < DataAvailable ) if ( ms.Capacity < DataAvailable )
@ -133,7 +133,7 @@ namespace Facepunch.Steamworks
fixed ( byte* p = ms.GetBuffer() ) fixed ( byte* p = ms.GetBuffer() )
{ {
ulong steamid = 1; 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; return false;
ms.SetLength( DataAvailable ); ms.SetLength( DataAvailable );

View File

@ -25,17 +25,17 @@ namespace Facepunch.Steamworks
{ {
internal Client client; internal Client client;
public void OpenProfile( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "steamid", steamid ); } public void OpenProfile( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "steamid", steamid ); }
public void OpenChat( ulong steamid ){ client._friends.ActivateGameOverlayToUser( "chat", steamid ); } public void OpenChat( ulong steamid ){ client.native.friends.ActivateGameOverlayToUser( "chat", steamid ); }
public void OpenTrade( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "jointrade", steamid ); } public void OpenTrade( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "jointrade", steamid ); }
public void OpenStats( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "stats", steamid ); } public void OpenStats( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "stats", steamid ); }
public void OpenAchievements( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "achievements", steamid ); } public void OpenAchievements( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "achievements", steamid ); }
public void AddFriend( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendadd", steamid ); } public void AddFriend( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendadd", steamid ); }
public void RemoveFriend( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendremove", steamid ); } public void RemoveFriend( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendremove", steamid ); }
public void AcceptFriendRequest( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendrequestaccept", steamid ); } public void AcceptFriendRequest( ulong steamid ) { client.native.friends.ActivateGameOverlayToUser( "friendrequestaccept", steamid ); }
public void IgnoreFriendRequest( ulong steamid ) { client._friends.ActivateGameOverlayToUser( "friendrequestignore", 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 ); }
} }
} }

View File

@ -136,7 +136,7 @@ namespace Facepunch.Steamworks
if ( !Finished && Id != IntPtr.Zero ) if ( !Finished && Id != IntPtr.Zero )
{ {
if ( client.Valid ) if ( client.Valid )
client._servers.CancelQuery( Id ); client.native.servers.CancelQuery( Id );
Id = IntPtr.Zero; Id = IntPtr.Zero;
} }
@ -170,7 +170,7 @@ namespace Facepunch.Steamworks
if ( RequestId != Id ) if ( RequestId != Id )
throw new Exception( "Request ID is invalid!" ); 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 ) ); Unresponsive.Add( Server.FromSteam( info ) );
} }
@ -179,7 +179,7 @@ namespace Facepunch.Steamworks
if ( RequestId != Id ) if ( RequestId != Id )
throw new Exception( "Request ID is invalid!" ); 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 ) ); Responded.Add( Server.FromSteam( info ) );
System.Diagnostics.Debug.WriteLine( info.m_szServerName ); System.Diagnostics.Debug.WriteLine( info.m_szServerName );

View File

@ -53,7 +53,7 @@ namespace Facepunch.Steamworks
client = client 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; return request;
} }

View File

@ -39,7 +39,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public uint OptimalSampleRate public uint OptimalSampleRate
{ {
get { return client._user.GetVoiceOptimalSampleRate(); } get { return client.native.user.GetVoiceOptimalSampleRate(); }
} }
private bool _wantsrecording = false; private bool _wantsrecording = false;
@ -58,11 +58,11 @@ namespace Facepunch.Steamworks
if ( value ) if ( value )
{ {
client._user.StartVoiceRecording(); client.native.user.StartVoiceRecording();
} }
else else
{ {
client._user.StopVoiceRecording(); client.native.user.StopVoiceRecording();
} }
} }
} }
@ -86,7 +86,7 @@ namespace Facepunch.Steamworks
bufferRegularLastWrite = 0; bufferRegularLastWrite = 0;
bufferCompressedLastWrite = 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, OnCompressedData != null, (IntPtr) pbufferRegular, (uint) bufferRegular.Length, ref bufferRegularLastWrite,
DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate );

View File

@ -6,17 +6,67 @@ using System.Runtime.InteropServices;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public partial class Client : IDisposable public partial class Client : IDisposable
{
internal class Internal : IDisposable
{ {
private uint _hpipe; private uint _hpipe;
private uint _huser; private uint _huser;
internal Valve.Steamworks.ISteamClient _client; internal Valve.Steamworks.ISteamClient client;
internal Valve.Steamworks.ISteamUser _user; internal Valve.Steamworks.ISteamUser user;
internal Valve.Steamworks.ISteamApps _apps; internal Valve.Steamworks.ISteamApps apps;
internal Valve.Steamworks.ISteamFriends _friends; internal Valve.Steamworks.ISteamFriends friends;
internal Valve.Steamworks.ISteamMatchmakingServers _servers; internal Valve.Steamworks.ISteamMatchmakingServers servers;
internal Valve.Steamworks.ISteamInventory _inventory; internal Valve.Steamworks.ISteamInventory inventory;
internal Valve.Steamworks.ISteamNetworking _networking; 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;
/// <summary> /// <summary>
/// Current running program's AppId /// Current running program's AppId
@ -47,11 +97,16 @@ namespace Facepunch.Steamworks
public Client( uint appId ) public Client( uint appId )
{ {
Valve.Steamworks.SteamAPI.Init( 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; return;
} }
@ -59,49 +114,22 @@ namespace Facepunch.Steamworks
// Set up warning hook callback // Set up warning hook callback
// //
SteamAPIWarningMessageHook ptr = InternalOnWarning; 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; AppId = appId;
Username = _friends.GetPersonaName(); Username = native.friends.GetPersonaName();
SteamId = _user.GetSteamID(); SteamId = native.user.GetSteamID();
} }
public void Dispose() public void Dispose()
{ {
if ( _client != null ) if ( native != null)
{ {
if ( _hpipe > 0 ) native.Dispose();
{ native = null;
if ( _huser > 0 )
_client.ReleaseUser( _hpipe, _huser );
_client.BReleaseSteamPipe( _hpipe );
_huser = 0;
_hpipe = 0;
}
_friends = null;
_client.BShutdownIfAllPipesClosed();
_client = null;
} }
} }
@ -129,7 +157,7 @@ namespace Facepunch.Steamworks
public bool Valid public bool Valid
{ {
get { return _client != null; } get { return native != null; }
} }
internal Action InstallCallback( int type, Delegate action ) internal Action InstallCallback( int type, Delegate action )

View File

@ -32,7 +32,7 @@ namespace Facepunch.Steamworks
public void MarkContentCorrupt( bool missingFilesOnly = false ) public void MarkContentCorrupt( bool missingFilesOnly = false )
{ {
client._apps.MarkContentCorrupt( missingFilesOnly ); client.native.apps.MarkContentCorrupt( missingFilesOnly );
} }

View File

@ -46,7 +46,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public void DropHeartbeat() public void DropHeartbeat()
{ {
client._inventory.SendItemDropHeartbeat(); client.native.inventory.SendItemDropHeartbeat();
} }
/// <summary> /// <summary>
@ -58,8 +58,8 @@ namespace Facepunch.Steamworks
public void TriggerItemDrop( Definition definition ) public void TriggerItemDrop( Definition definition )
{ {
int result = 0; int result = 0;
client._inventory.TriggerItemDrop( ref result, definition.Id ); client.native.inventory.TriggerItemDrop( ref result, definition.Id );
client._inventory.DestroyResult( result ); client.native.inventory.DestroyResult( result );
} }
@ -74,7 +74,7 @@ namespace Facepunch.Steamworks
if ( updateRequest != 0 ) if ( updateRequest != 0 )
return; return;
client._inventory.GetAllItems( ref updateRequest ); client.native.inventory.GetAllItems( ref updateRequest );
} }
internal void LoadItemDefinitions() internal void LoadItemDefinitions()
@ -82,10 +82,10 @@ namespace Facepunch.Steamworks
// //
// Make sure item definitions are loaded, because we're going to be using them. // Make sure item definitions are loaded, because we're going to be using them.
// //
client._inventory.LoadItemDefinitions(); client.native.inventory.LoadItemDefinitions();
int[] ids; int[] ids;
if ( !client._inventory.GetItemDefinitionIDs( out ids ) ) if ( !client.native.inventory.GetItemDefinitionIDs( out ids ) )
return; return;
Definitions = ids.Select( x => Definitions = ids.Select( x =>
@ -105,7 +105,7 @@ namespace Facepunch.Steamworks
internal T DefinitionProperty<T>( int i, string name ) internal T DefinitionProperty<T>( int i, string name )
{ {
string val = string.Empty; 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) ); return (T) Convert.ChangeType( val, typeof( T) );
} }
@ -113,7 +113,7 @@ namespace Facepunch.Steamworks
{ {
if ( updateRequest != 0 ) if ( updateRequest != 0 )
{ {
client._inventory.DestroyResult( updateRequest ); client.native.inventory.DestroyResult( updateRequest );
updateRequest = 0; updateRequest = 0;
} }
} }
@ -129,7 +129,7 @@ namespace Facepunch.Steamworks
if ( updateRequest == 0 ) if ( updateRequest == 0 )
return; 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 ) if ( status == Valve.Steamworks.EResult.k_EResultPending )
return; return;
@ -165,7 +165,7 @@ namespace Facepunch.Steamworks
private unsafe void RetrieveInventory() private unsafe void RetrieveInventory()
{ {
Valve.Steamworks.SteamItemDetails_t[] items = null; Valve.Steamworks.SteamItemDetails_t[] items = null;
client._inventory.GetResultItems( updateRequest, out items ); client.native.inventory.GetResultItems( updateRequest, out items );
Items = items.Select( x => Items = items.Select( x =>
{ {
@ -184,12 +184,12 @@ namespace Facepunch.Steamworks
// Get a serialized version // Get a serialized version
// //
uint size = 0; uint size = 0;
client._inventory.SerializeResult( updateRequest, IntPtr.Zero, ref size ); client.native.inventory.SerializeResult( updateRequest, IntPtr.Zero, ref size );
SerializedItems = new byte[size]; SerializedItems = new byte[size];
fixed( byte* b = SerializedItems ) 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 ) ); SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) );
@ -243,7 +243,7 @@ namespace Facepunch.Steamworks
{ {
string val = string.Empty; string val = string.Empty;
if ( !client._inventory.GetItemDefinitionProperty( Id, name, out val ) ) if ( !client.native.inventory.GetItemDefinitionProperty( Id, name, out val ) )
return default( T ); return default( T );
try try