From aad8c2b03a0bf06ceca0d008df555667746074cd Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 5 Oct 2016 16:27:02 +0100 Subject: [PATCH] CallResults --- Facepunch.Steamworks/BaseSteamworks.cs | 63 ++++++++++++++++++- Facepunch.Steamworks/Client.cs | 8 --- .../Facepunch.Steamworks.csproj | 1 + Facepunch.Steamworks/Interop/CallResult.cs | 45 +++++++++++++ Facepunch.Steamworks/Server.cs | 5 -- 5 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 Facepunch.Steamworks/Interop/CallResult.cs diff --git a/Facepunch.Steamworks/BaseSteamworks.cs b/Facepunch.Steamworks/BaseSteamworks.cs index 5940335..6ce5da6 100644 --- a/Facepunch.Steamworks/BaseSteamworks.cs +++ b/Facepunch.Steamworks/BaseSteamworks.cs @@ -2,11 +2,21 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Facepunch.Steamworks.Interop; namespace Facepunch.Steamworks { public class BaseSteamworks : IDisposable { + /// + /// Current running program's AppId + /// + public uint AppId { get; internal set; } + + public Networking Networking { get; internal set; } + public Inventory Inventory { get; internal set; } + public Workshop Workshop { get; internal set; } + public virtual void Dispose() { foreach ( var d in Disposables ) @@ -26,6 +36,7 @@ namespace Facepunch.Steamworks { Networking = new Steamworks.Networking( this, native.networking ); Inventory = new Steamworks.Inventory( native.inventory, IsGameServer ); + Workshop = new Steamworks.Workshop( this, native.ugc ); } public bool IsValid @@ -33,9 +44,6 @@ namespace Facepunch.Steamworks get { return native != null; } } - public Networking Networking { get; internal set; } - public Inventory Inventory { get; internal set; } - internal Interop.NativeInterface native; internal virtual bool IsGameServer { get { return false; } } @@ -52,6 +60,9 @@ namespace Facepunch.Steamworks /// public Action OnMessage; + /// + /// Global callback type + /// internal void AddCallback( Action Callback, int id ) { var callback = new Facepunch.Steamworks.Interop.Callback( IsGameServer, id, Callback ); @@ -63,11 +74,57 @@ namespace Facepunch.Steamworks public virtual void Update() { + RunCallbackQueue(); + Inventory.Update(); Networking.Update(); if ( OnUpdate != null ) OnUpdate(); } + + List Callbacks = new List(); + + /// + /// Call results are results to specific actions + /// + internal void AddCallResult( CallResult c ) + { + if ( FinishCallback( c ) ) + return; + + Callbacks.Add( c ); + } + + void RunCallbackQueue() + { + for ( int i=0; i< Callbacks.Count(); i++ ) + { + if ( !FinishCallback( Callbacks[i] ) ) + continue; + + Callbacks.RemoveAt( i ); + i--; + } + } + + bool FinishCallback( CallResult call ) + { + bool failed = true; + + if ( !native.utils.IsAPICallCompleted( call.Handle, ref failed ) ) + return false; + + if ( failed ) + { + // + // TODO - failure reason? + // + return true; + } + + call.Run( native.utils ); + return true; + } } } \ No newline at end of file diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index 052940c..9cdb2f6 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -7,11 +7,6 @@ namespace Facepunch.Steamworks { public partial class Client : BaseSteamworks { - /// - /// Current running program's AppId - /// - public uint AppId { get; private set; } - /// /// Current user's Username /// @@ -27,9 +22,6 @@ namespace Facepunch.Steamworks /// public string BetaName { get; private set; } - - - public Client( uint appId ) { Valve.Steamworks.SteamAPIInterop.SteamAPI_Init(); diff --git a/Facepunch.Steamworks/Facepunch.Steamworks.csproj b/Facepunch.Steamworks/Facepunch.Steamworks.csproj index da4a3a7..a59cc55 100644 --- a/Facepunch.Steamworks/Facepunch.Steamworks.csproj +++ b/Facepunch.Steamworks/Facepunch.Steamworks.csproj @@ -136,6 +136,7 @@ + diff --git a/Facepunch.Steamworks/Interop/CallResult.cs b/Facepunch.Steamworks/Interop/CallResult.cs new file mode 100644 index 0000000..73bc612 --- /dev/null +++ b/Facepunch.Steamworks/Interop/CallResult.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using Valve.Steamworks; + +namespace Facepunch.Steamworks.Interop +{ + internal unsafe abstract class CallResult : IDisposable + { + public ulong Handle; + + public void Dispose() + { + Handle = 0; + } + + internal abstract void Run( ISteamUtils utils ); + } + + internal unsafe abstract class CallResult : CallResult + { + public abstract int CallbackId { get; } + public Action OnResult; + + internal override void Run( ISteamUtils utils ) + { + var datasize = Marshal.SizeOf( typeof( T ) ); + var data = stackalloc byte[ datasize ]; + bool failed = false; + + if ( !utils.GetAPICallResult( Handle, (IntPtr) data, datasize, CallbackId, ref failed ) || failed ) + { + Console.WriteLine( "FAILURE" ); + return; + } + + var dataObject = (T)Marshal.PtrToStructure( (IntPtr) data, typeof( T ) ); + + if ( OnResult != null ) + OnResult( dataObject ); + } + } +} \ No newline at end of file diff --git a/Facepunch.Steamworks/Server.cs b/Facepunch.Steamworks/Server.cs index b641141..5ae173f 100644 --- a/Facepunch.Steamworks/Server.cs +++ b/Facepunch.Steamworks/Server.cs @@ -8,11 +8,6 @@ namespace Facepunch.Steamworks { public partial class Server : BaseSteamworks { - /// - /// Current running program's AppId - /// - public uint AppId { get; private set; } - /// /// Current user's Username ///