More refactor

This commit is contained in:
Garry Newman 2016-10-05 12:44:01 +01:00
parent 778db29a68
commit 220a49e716
16 changed files with 63 additions and 118 deletions

View File

@ -14,7 +14,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
} }
} }
@ -25,7 +25,7 @@ namespace Facepunch.Steamworks.Test
{ {
var username = client.Username; var username = client.Username;
Console.WriteLine( username ); Console.WriteLine( username );
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
Assert.IsNotNull( username ); Assert.IsNotNull( username );
} }
} }
@ -37,7 +37,7 @@ namespace Facepunch.Steamworks.Test
{ {
var steamid = client.SteamId; var steamid = client.SteamId;
Console.WriteLine( steamid ); Console.WriteLine( steamid );
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
Assert.AreNotEqual( 0, steamid ); Assert.AreNotEqual( 0, steamid );
} }
} }

View File

@ -14,7 +14,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
client.Friends.Refresh(); client.Friends.Refresh();
@ -32,7 +32,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
foreach ( var friend in client.Friends.All ) foreach ( var friend in client.Friends.All )
{ {
@ -46,7 +46,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
var friend = client.Friends.All.First(); var friend = client.Friends.All.First();

View File

@ -13,7 +13,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
Assert.IsNull( client.Inventory.SerializedItems ); Assert.IsNull( client.Inventory.SerializedItems );
@ -33,7 +33,7 @@ namespace Facepunch.Steamworks.Test
using ( var server = new Facepunch.Steamworks.Server( 252490, 0, 30002, true, "VersionString" ) ) using ( var server = new Facepunch.Steamworks.Server( 252490, 0, 30002, true, "VersionString" ) )
{ {
server.LogOnAnonymous(); server.LogOnAnonymous();
Assert.IsTrue( server.Valid ); Assert.IsTrue( server.IsValid );
var result = server.Inventory.Deserialize( client.Inventory.SerializedItems ); var result = server.Inventory.Deserialize( client.Inventory.SerializedItems );

View File

@ -16,7 +16,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var server = new Facepunch.Steamworks.Server( 252490, 30001, 30002, 30003, false, "VersionString" ) ) using ( var server = new Facepunch.Steamworks.Server( 252490, 30001, 30002, 30003, false, "VersionString" ) )
{ {
Assert.IsTrue( server.Valid ); Assert.IsTrue( server.IsValid );
} }
} }
@ -25,7 +25,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {
Assert.IsTrue( client.Valid ); Assert.IsTrue( client.IsValid );
var ticket = client.Auth.GetAuthSessionTicket(); var ticket = client.Auth.GetAuthSessionTicket();
var ticketBinary = ticket.Data; var ticketBinary = ticket.Data;
@ -33,7 +33,7 @@ namespace Facepunch.Steamworks.Test
{ {
server.LogOnAnonymous(); server.LogOnAnonymous();
Assert.IsTrue( server.Valid ); Assert.IsTrue( server.IsValid );
var auth = server.Auth; var auth = server.Auth;

View File

@ -12,7 +12,7 @@ namespace Facepunch.Steamworks.Test
{ {
using ( var server = new Facepunch.Steamworks.Server( 252490, 0, 30002, true, "VersionString" ) ) using ( var server = new Facepunch.Steamworks.Server( 252490, 0, 30002, true, "VersionString" ) )
{ {
Assert.IsTrue( server.Valid ); Assert.IsTrue( server.IsValid );
server.LogOnAnonymous(); server.LogOnAnonymous();
ulong MySteamId = 76561197960279927; ulong MySteamId = 76561197960279927;

View File

@ -22,6 +22,20 @@ namespace Facepunch.Steamworks
} }
} }
public void SetupCommonInterfaces()
{
Networking = new Steamworks.Networking( this, native.networking );
Inventory = new Steamworks.Inventory( native.inventory, IsGameServer );
}
public bool IsValid
{
get { return native != null; }
}
public Networking Networking { get; internal set; }
public Inventory Inventory { get; internal set; }
internal Interop.NativeInterface native; internal Interop.NativeInterface native;
internal virtual bool IsGameServer { get { return false; } } internal virtual bool IsGameServer { get { return false; } }
@ -43,5 +57,17 @@ namespace Facepunch.Steamworks
var callback = new Facepunch.Steamworks.Interop.Callback<T>( IsGameServer, id, Callback ); var callback = new Facepunch.Steamworks.Interop.Callback<T>( IsGameServer, id, Callback );
Disposables.Add( callback ); Disposables.Add( callback );
} }
public Action OnUpdate;
public virtual void Update()
{
Inventory.Update();
Networking.Update();
if ( OnUpdate != null )
OnUpdate();
}
} }
} }

View File

@ -46,14 +46,17 @@ namespace Facepunch.Steamworks
return; return;
} }
Networking = new Steamworks.Networking( this, native.networking );
// //
// Set up warning hook callback // Set up warning hook callback
// //
SteamAPIWarningMessageHook ptr = InternalOnWarning; SteamAPIWarningMessageHook ptr = InternalOnWarning;
native.client.SetWarningMessageHook( Marshal.GetFunctionPointerForDelegate( ptr ) ); native.client.SetWarningMessageHook( Marshal.GetFunctionPointerForDelegate( ptr ) );
//
// Setup interfaces that client and server both have
//
SetupCommonInterfaces();
// //
// Cache common, unchanging info // Cache common, unchanging info
// //
@ -68,17 +71,6 @@ namespace Facepunch.Steamworks
Update(); Update();
} }
public override void Dispose()
{
if ( native != null)
{
native.Dispose();
native = null;
}
base.Dispose();
}
[UnmanagedFunctionPointer( CallingConvention.Cdecl )] [UnmanagedFunctionPointer( CallingConvention.Cdecl )]
public delegate void SteamAPIWarningMessageHook( int nSeverity, System.Text.StringBuilder pchDebugText ); public delegate void SteamAPIWarningMessageHook( int nSeverity, System.Text.StringBuilder pchDebugText );
@ -90,43 +82,20 @@ namespace Facepunch.Steamworks
} }
} }
internal event Action OnUpdate;
/// <summary> /// <summary>
/// Should be called at least once every frame /// Should be called at least once every frame
/// </summary> /// </summary>
public void Update() public override void Update()
{ {
if ( native == null ) if ( !IsValid )
return; return;
Valve.Steamworks.SteamAPI.RunCallbacks(); Valve.Steamworks.SteamAPI.RunCallbacks();
Voice.Update(); Voice.Update();
Inventory.Update();
Networking.Update();
if ( OnUpdate != null ) base.Update();
OnUpdate();
} }
public bool Valid
{
get { return native != null; }
}
internal Action InstallCallback( int type, Delegate action )
{
var del = Marshal.GetFunctionPointerForDelegate( action );
// var ptr = Marshal.GetFunctionPointerForDelegate( action );
// Valve.Steamworks.SteamAPI.RegisterCallback( del, type );
// Valve.Steamworks.SteamAPI.UnregisterCallback( del );
//return () => Valve.Steamworks.SteamAPI.UnregisterCallback( ptr );
return null;
}
public Networking Networking { get; internal set; }
} }
} }

View File

@ -38,7 +38,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public void Cancel() public void Cancel()
{ {
if ( client.Valid && Handle != 0 ) if ( client.IsValid && Handle != 0 )
{ {
client.native.user.CancelAuthTicket( Handle ); client.native.user.CancelAuthTicket( Handle );
Handle = 0; Handle = 0;

View File

@ -182,7 +182,7 @@ namespace Facepunch.Steamworks
// //
if ( Id != IntPtr.Zero ) if ( Id != IntPtr.Zero )
{ {
if ( client.Valid ) if ( client.IsValid )
client.native.servers.CancelQuery( Id ); client.native.servers.CancelQuery( Id );
Id = IntPtr.Zero; Id = IntPtr.Zero;

View File

@ -116,8 +116,8 @@
<Compile Include="BaseSteamworks.cs" /> <Compile Include="BaseSteamworks.cs" />
<Compile Include="Callbacks\Index.cs" /> <Compile Include="Callbacks\Index.cs" />
<Compile Include="Callbacks\User.cs" /> <Compile Include="Callbacks\User.cs" />
<Compile Include="Client\Inventory.Item.cs" /> <Compile Include="Interfaces\Inventory.Item.cs" />
<Compile Include="Client\Inventory.Result.cs" /> <Compile Include="Interfaces\Inventory.Result.cs" />
<Compile Include="Interfaces\Networking.cs" /> <Compile Include="Interfaces\Networking.cs" />
<Compile Include="Client\Overlay.cs" /> <Compile Include="Client\Overlay.cs" />
<Compile Include="Client\ServerList.cs" /> <Compile Include="Client\ServerList.cs" />
@ -128,10 +128,10 @@
<Compile Include="Client\App.cs" /> <Compile Include="Client\App.cs" />
<Compile Include="Client\Friends.cs" /> <Compile Include="Client\Friends.cs" />
<Compile Include="Client\Image.cs" /> <Compile Include="Client\Image.cs" />
<Compile Include="Client\Inventory.cs" /> <Compile Include="Interfaces\Inventory.cs" />
<Compile Include="Client\Screenshots.cs" /> <Compile Include="Client\Screenshots.cs" />
<Compile Include="Client\Stats.cs" /> <Compile Include="Client\Stats.cs" />
<Compile Include="Client\Inventory.Definition.cs" /> <Compile Include="Interfaces\Inventory.Definition.cs" />
<Compile Include="Client\Voice.cs" /> <Compile Include="Client\Voice.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Callbacks\Networking.cs" /> <Compile Include="Callbacks\Networking.cs" />
@ -142,7 +142,6 @@
<Compile Include="Interop\steam_api_interop.cs" /> <Compile Include="Interop\steam_api_interop.cs" />
<Compile Include="Server.cs" /> <Compile Include="Server.cs" />
<Compile Include="Server\Auth.cs" /> <Compile Include="Server\Auth.cs" />
<Compile Include="Server\Inventory.cs" />
<Compile Include="Server\Query.cs" /> <Compile Include="Server\Query.cs" />
<Compile Include="Server\Stats.cs" /> <Compile Include="Server\Stats.cs" />
</ItemGroup> </ItemGroup>

View File

@ -6,22 +6,6 @@ using System.Text;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public partial class Client : IDisposable
{
Inventory _inv;
public Inventory Inventory
{
get
{
if ( _inv == null )
_inv = new Inventory( native.inventory, false );
return _inv;
}
}
}
public partial class Inventory public partial class Inventory
{ {
/// <summary> /// <summary>
@ -125,7 +109,7 @@ namespace Facepunch.Steamworks
d.SetupCommonProperties(); d.SetupCommonProperties();
return d; return d;
} ).ToArray(); } ).ToArray();
} }
/// <summary> /// <summary>
@ -133,7 +117,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
internal void Update() internal void Update()
{ {
if ( Definitions == null ) if ( Definitions == null && !IsServer )
FetchItemDefinitions(); FetchItemDefinitions();
UpdateLocalRequest(); UpdateLocalRequest();
@ -222,6 +206,5 @@ namespace Facepunch.Steamworks
return new Result( this, resultHandle ); return new Result( this, resultHandle );
} }
} }
} }
} }

View File

@ -49,6 +49,11 @@ namespace Facepunch.Steamworks
var rr = GCHandle.Alloc( d ); var rr = GCHandle.Alloc( d );
native.utils.SetWarningMessageHook( d ); native.utils.SetWarningMessageHook( d );
//
// Setup interfaces that client and server both have
//
SetupCommonInterfaces();
// //
// Cache common, unchanging info // Cache common, unchanging info
// //
@ -89,29 +94,17 @@ namespace Facepunch.Steamworks
Console.WriteLine( "STEAM: {0}", text ); Console.WriteLine( "STEAM: {0}", text );
} }
internal event Action OnUpdate;
/// <summary> /// <summary>
/// Should be called at least once every frame /// Should be called at least once every frame
/// </summary> /// </summary>
public void Update() public override void Update()
{ {
if ( native == null ) if ( !IsValid )
return; return;
Valve.Interop.NativeEntrypoints.Extended.SteamGameServer_RunCallbacks(); Valve.Interop.NativeEntrypoints.Extended.SteamGameServer_RunCallbacks();
Valve.Steamworks.SteamAPI.RunCallbacks();
// Voice.Update();
// Inventory.Update();
// Networking.Update();
if ( OnUpdate != null ) base.Update();
OnUpdate();
}
public bool Valid
{
get { return native != null; }
} }
/// <summary> /// <summary>

View File

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Facepunch.Steamworks
{
public partial class Server
{
Inventory _inv;
public Inventory Inventory
{
get
{
if ( _inv == null )
_inv = new Inventory( native.inventory, true );
return _inv;
}
}
}
}