CallResults

This commit is contained in:
Garry Newman 2016-10-05 16:27:02 +01:00
parent 9a676bc3dc
commit aad8c2b03a
5 changed files with 106 additions and 16 deletions

View File

@ -2,11 +2,21 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Facepunch.Steamworks.Interop;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public class BaseSteamworks : IDisposable public class BaseSteamworks : IDisposable
{ {
/// <summary>
/// Current running program's AppId
/// </summary>
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() public virtual void Dispose()
{ {
foreach ( var d in Disposables ) foreach ( var d in Disposables )
@ -26,6 +36,7 @@ namespace Facepunch.Steamworks
{ {
Networking = new Steamworks.Networking( this, native.networking ); Networking = new Steamworks.Networking( this, native.networking );
Inventory = new Steamworks.Inventory( native.inventory, IsGameServer ); Inventory = new Steamworks.Inventory( native.inventory, IsGameServer );
Workshop = new Steamworks.Workshop( this, native.ugc );
} }
public bool IsValid public bool IsValid
@ -33,9 +44,6 @@ namespace Facepunch.Steamworks
get { return native != null; } 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; } }
@ -52,6 +60,9 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public Action<MessageType, string> OnMessage; public Action<MessageType, string> OnMessage;
/// <summary>
/// Global callback type
/// </summary>
internal void AddCallback<T>( Action<T> Callback, int id ) internal void AddCallback<T>( Action<T> Callback, int id )
{ {
var callback = new Facepunch.Steamworks.Interop.Callback<T>( IsGameServer, id, Callback ); var callback = new Facepunch.Steamworks.Interop.Callback<T>( IsGameServer, id, Callback );
@ -63,11 +74,57 @@ namespace Facepunch.Steamworks
public virtual void Update() public virtual void Update()
{ {
RunCallbackQueue();
Inventory.Update(); Inventory.Update();
Networking.Update(); Networking.Update();
if ( OnUpdate != null ) if ( OnUpdate != null )
OnUpdate(); OnUpdate();
} }
List<CallResult> Callbacks = new List<CallResult>();
/// <summary>
/// Call results are results to specific actions
/// </summary>
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;
}
} }
} }

View File

@ -7,11 +7,6 @@ namespace Facepunch.Steamworks
{ {
public partial class Client : BaseSteamworks public partial class Client : BaseSteamworks
{ {
/// <summary>
/// Current running program's AppId
/// </summary>
public uint AppId { get; private set; }
/// <summary> /// <summary>
/// Current user's Username /// Current user's Username
/// </summary> /// </summary>
@ -27,9 +22,6 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public string BetaName { get; private set; } public string BetaName { get; private set; }
public Client( uint appId ) public Client( uint appId )
{ {
Valve.Steamworks.SteamAPIInterop.SteamAPI_Init(); Valve.Steamworks.SteamAPIInterop.SteamAPI_Init();

View File

@ -136,6 +136,7 @@
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Callbacks\Networking.cs" /> <Compile Include="Callbacks\Networking.cs" />
<Compile Include="Interop\Callback.cs" /> <Compile Include="Interop\Callback.cs" />
<Compile Include="Interop\CallResult.cs" />
<Compile Include="Interop\Native.cs" /> <Compile Include="Interop\Native.cs" />
<Compile Include="Interop\ServerRules.cs" /> <Compile Include="Interop\ServerRules.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -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<T> : CallResult
{
public abstract int CallbackId { get; }
public Action<T> 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 );
}
}
}

View File

@ -8,11 +8,6 @@ namespace Facepunch.Steamworks
{ {
public partial class Server : BaseSteamworks public partial class Server : BaseSteamworks
{ {
/// <summary>
/// Current running program's AppId
/// </summary>
public uint AppId { get; private set; }
/// <summary> /// <summary>
/// Current user's Username /// Current user's Username
/// </summary> /// </summary>