Callback update

This commit is contained in:
Garry Newman 2016-10-28 17:24:41 +01:00
parent 8d605e99fa
commit 5da0690c5c
6 changed files with 26 additions and 17 deletions

View File

@ -81,13 +81,13 @@ public enum MessageType : int
/// <summary>
/// Global callback type
/// </summary>
internal void AddCallback<T, TSmall>( Action<T> Callback, int id )
internal void AddCallback<T, TSmall>( Action<T, bool> Callback, int id ) where T : new()
{
var callback = new Callback<T, TSmall>( native.api, IsGameServer, id, Callback );
Disposables.Add( callback );
}
internal void AddCallback<T>( Action<T> Callback, int id )
internal void AddCallback<T>( Action<T, bool> Callback, int id ) where T : new()
{
AddCallback<T, T>( Callback, id );
}

View File

@ -47,7 +47,7 @@ internal void Update()
}
}
private void onP2PConnectionRequest( P2PSessionRequest o )
private void onP2PConnectionRequest( P2PSessionRequest o, bool b )
{
if ( OnIncomingConnection != null )
{
@ -83,7 +83,7 @@ public enum SessionError : byte
Max = 5
};
private void onP2PConnectionFailed( P2PSessionConnectFail o )
private void onP2PConnectionFailed( P2PSessionConnectFail o, bool b )
{
if ( OnConnectionFailed != null )
{

View File

@ -37,13 +37,13 @@ public void Dispose()
OnItemInstalled = null;
}
private void onItemInstalled( ItemInstalled obj )
private void onItemInstalled( ItemInstalled obj, bool failed )
{
if ( OnItemInstalled != null )
OnItemInstalled( obj.FileId );
}
private void onDownloadResult( DownloadResult obj )
private void onDownloadResult( DownloadResult obj, bool failed )
{
if ( OnFileDownloaded != null )
OnFileDownloaded( obj.FileId, obj.Result );

View File

@ -10,26 +10,29 @@ namespace Facepunch.Steamworks.Interop.VTable.This
internal struct Callback
{
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate void Result( IntPtr thisptr, IntPtr pvParam );
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate void ResultBool( IntPtr thisptr, IntPtr pvParam, bool bIOFailure, SteamNative.SteamAPICall_t hSteamAPICall );
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate int GetSize( IntPtr thisptr );
[MarshalAs(UnmanagedType.FunctionPtr)] public Result RunCallResult;
[MarshalAs(UnmanagedType.FunctionPtr)] public ResultBool RunCallResult;
[MarshalAs(UnmanagedType.FunctionPtr)] public Result RunCallback;
[MarshalAs(UnmanagedType.FunctionPtr)] public GetSize GetCallbackSizeBytes;
internal static IntPtr Get( Action<IntPtr, IntPtr> onRunCallback, int size, Interop.Callback cb )
internal static IntPtr Get( Action<IntPtr, IntPtr, bool> onRunCallback, int size, Interop.Callback cb )
{
var ptr = Marshal.AllocHGlobal( Marshal.SizeOf( typeof( Callback ) ) );
Callback.Result da = ( _, p ) => { onRunCallback( _, p ); };
Callback.Result da = ( _, p ) => { onRunCallback( _, p, false ); };
Callback.ResultBool db = ( _, p, iofailure, call ) => { onRunCallback( _, p, iofailure ); };
Callback.GetSize dc = ( _ ) => { return size; };
cb.AddHandle( GCHandle.Alloc( da ) );
cb.AddHandle( GCHandle.Alloc( db ) );
cb.AddHandle( GCHandle.Alloc( dc ) );
var table = new Callback()
{
RunCallback = da,
RunCallResult = da,
RunCallResult = db,
GetCallbackSizeBytes = dc
};

View File

@ -48,20 +48,20 @@ internal void AddHandle( GCHandle gCHandle )
}
}
internal partial class Callback<T, TSmall> : Callback
internal partial class Callback<T, TSmall> : Callback where T : new()
{
private SteamNative.SteamApi api;
public int CallbackId = 0;
public bool GameServer = false;
public Action<T> Function;
public Action<T, bool> Function;
private IntPtr vTablePtr = IntPtr.Zero;
private GCHandle callbackPin;
private readonly int m_size = Marshal.SizeOf(typeof(T));
public Callback( SteamNative.SteamApi api, bool gameserver, int callbackid, Action<T> func )
public Callback( SteamNative.SteamApi api, bool gameserver, int callbackid, Action<T, bool> func )
{
this.api = api;
GameServer = gameserver;
@ -90,16 +90,22 @@ public override void Dispose()
base.Dispose();
}
private void OnRunCallback( IntPtr thisObject, IntPtr ptr )
private void OnRunCallback( IntPtr thisObject, IntPtr ptr, bool failure )
{
if ( callbackPin == null ) throw new System.Exception( "Callback wasn't pinned!" );
if ( vTablePtr == IntPtr.Zero ) throw new System.Exception( "vTablePtr wasn't pinned!" );
if ( thisObject != IntPtr.Zero && thisObject != callbackPin.AddrOfPinnedObject() ) throw new System.Exception( "This wasn't valid!" );
if ( thisObject != IntPtr.Zero && thisObject != callbackPin.AddrOfPinnedObject() ) throw new System.Exception( "This wasn't valid! ("+ thisObject.ToInt64() + ") ( "+ callbackPin.AddrOfPinnedObject().ToInt64() + " )" );
if ( SteamNative.Platform.PackSmall && typeof(T) != typeof( TSmall ) ) throw new System.Exception( "Callback should use PackSmall" );
if ( failure )
{
Function( new T(), failure );
return;
}
T data = (T) Marshal.PtrToStructure( ptr, typeof(T) );
Function( data );
Function( data, failure );
}

View File

@ -58,7 +58,7 @@ internal ServerAuth( Server s )
server.AddCallback<ValidateAuthTicketResponse>( OnAuthTicketValidate, ValidateAuthTicketResponse.CallbackId );
}
void OnAuthTicketValidate( ValidateAuthTicketResponse data )
void OnAuthTicketValidate( ValidateAuthTicketResponse data, bool b )
{
if ( OnAuthChange != null )
OnAuthChange( data.SteamID, data.OwnerSteamID, (Status) data.AuthResponse );