CallResults are queried

This commit is contained in:
Garry Newman 2018-02-14 14:15:02 +00:00
parent a82f6fc8ed
commit 04c7eb59dc
12 changed files with 7206 additions and 13275 deletions

View File

@ -171,6 +171,11 @@ namespace Facepunch.Steamworks.Test
Thread.Sleep(10);
client.Update();
if ( board.IsError )
{
throw new Exception( "Board is Error" );
}
if (time.Elapsed.TotalSeconds > 10)
{
throw new Exception("board.IsValid took too long");

View File

@ -25,6 +25,7 @@ namespace Facepunch.Steamworks
internal Interop.NativeInterface native;
private List<SteamNative.CallbackHandle> CallbackHandles = new List<SteamNative.CallbackHandle>();
private List<SteamNative.CallResult> CallResults = new List<SteamNative.CallResult>();
protected BaseSteamworks( uint appId )
@ -46,6 +47,12 @@ namespace Facepunch.Steamworks
}
CallbackHandles.Clear();
foreach ( var h in CallResults )
{
h.Dispose();
}
CallResults.Clear();
if ( Workshop != null )
{
Workshop.Dispose();
@ -98,6 +105,16 @@ namespace Facepunch.Steamworks
CallbackHandles.Add( handle );
}
internal void RegisterCallResult( SteamNative.CallResult handle )
{
CallResults.Add( handle );
}
internal void UnregisterCallResult( SteamNative.CallResult handle )
{
CallResults.Remove( handle );
}
public virtual void Update()
{
Inventory.Update();
@ -114,6 +131,11 @@ namespace Facepunch.Steamworks
{
if ( OnUpdate != null )
OnUpdate();
for( int i=0; i < CallResults.Count; i++ )
{
CallResults[i].Try();
}
}
/// <summary>

View File

@ -107,6 +107,9 @@ namespace Facepunch.Steamworks
internal void OnBoardCreated( LeaderboardFindResult_t result, bool error )
{
Console.WriteLine( $"result.LeaderboardFound: {result.LeaderboardFound}" );
Console.WriteLine( $"result.SteamLeaderboard: {result.SteamLeaderboard}" );
if ( error || ( result.LeaderboardFound == 0 ) )
{
IsError = true;
@ -234,7 +237,7 @@ namespace Facepunch.Steamworks
}
} );
return handle.CallResultHandle != 0;
return handle.IsValid;
}
file.Share( () =>

View File

@ -50,21 +50,23 @@ namespace SteamNative
//
internal class CallbackHandle : IDisposable
{
internal BaseSteamworks steamworks;
internal SteamAPICall_t CallResultHandle;
internal bool CallResult;
internal BaseSteamworks Steamworks;
// Get Rid
internal GCHandle FuncA;
internal GCHandle FuncB;
internal GCHandle FuncC;
internal IntPtr vTablePtr;
internal GCHandle PinnedCallback;
internal CallbackHandle( Facepunch.Steamworks.BaseSteamworks steamworks )
{
Steamworks = steamworks;
}
public void Dispose()
{
if ( CallResult )
UnregisterCallResult();
else
UnregisterCallback();
UnregisterCallback();
if ( FuncA.IsAllocated )
FuncA.Free();
@ -90,19 +92,81 @@ namespace SteamNative
if ( !PinnedCallback.IsAllocated )
return;
steamworks.native.api.SteamAPI_UnregisterCallback( PinnedCallback.AddrOfPinnedObject() );
Steamworks.native.api.SteamAPI_UnregisterCallback( PinnedCallback.AddrOfPinnedObject() );
}
private void UnregisterCallResult()
{
if ( CallResultHandle == 0 )
return;
if ( !PinnedCallback.IsAllocated )
return;
steamworks.native.api.SteamAPI_UnregisterCallResult( PinnedCallback.AddrOfPinnedObject(), CallResultHandle );
}
public virtual bool IsValid { get { return true; } }
}
internal abstract class CallResult : CallbackHandle
{
internal SteamAPICall_t Call;
public override bool IsValid { get { return Call > 0; } }
internal CallResult( Facepunch.Steamworks.BaseSteamworks steamworks, SteamAPICall_t call ) : base( steamworks )
{
Call = call;
}
internal void Try()
{
bool failed = false;
if ( !Steamworks.native.utils.IsAPICallCompleted( Call, ref failed ))
return;
Steamworks.UnregisterCallResult( this );
RunCallback();
}
internal abstract void RunCallback();
}
internal class CallResult<T> : CallResult
{
private static byte[] resultBuffer = new byte[1024 * 16];
internal delegate T ConvertFromPointer( IntPtr p );
Action<T, bool> CallbackFunction;
ConvertFromPointer ConvertFromPointerFunction;
internal int ResultSize = -1;
internal int CallbackId = 0;
internal CallResult( Facepunch.Steamworks.BaseSteamworks steamworks, SteamAPICall_t call, Action<T, bool> callbackFunction, ConvertFromPointer fromPointer, int resultSize, int callbackId ) : base( steamworks, call )
{
ResultSize = resultSize;
CallbackId = callbackId;
CallbackFunction = callbackFunction;
ConvertFromPointerFunction = fromPointer;
Steamworks.RegisterCallResult( this );
}
public override string ToString()
{
return $"CallResult( {typeof(T).Name}, {CallbackId}, {ResultSize}b )";
}
unsafe internal override void RunCallback()
{
bool failed = false;
fixed ( byte* ptr = resultBuffer )
{
if ( !Steamworks.native.utils.GetAPICallResult( Call, (IntPtr)ptr, resultBuffer.Length, CallbackId, ref failed ) || failed )
{
CallbackFunction( default(T), true );
return;
}
var val = ConvertFromPointerFunction( (IntPtr)ptr );
CallbackFunction( val, false );
}
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -9,10 +9,12 @@ namespace Generator
public partial class CodeWriter
{
bool LargePack;
bool X86;
private void PlatformClass( string type, string libraryName, bool LargePack )
{
this.LargePack = LargePack;
X86 = type.EndsWith( "32" );
StartBlock( $"internal static partial class Platform" );
{
@ -187,7 +189,10 @@ namespace Generator
if ( argstring != "" ) argstring = $" {argstring} ";
Write( $"[DllImportAttribute( \"{library}\" )] " );
if ( X86 )
Write( $"[DllImport( \"{library}\", CallingConvention = CallingConvention.Cdecl )] " );
else
Write( $"[DllImport( \"{library}\" )] " );
if ( ret.Return() == "bool" ) WriteLine( "[return: MarshalAs(UnmanagedType.U1)]" );

View File

@ -58,7 +58,7 @@ namespace Generator
{
if ( !string.IsNullOrEmpty( c.CallbackId ) )
{
WriteLine( "public const int CallbackId = " + c.CallbackId + ";" );
WriteLine( "internal const int CallbackId = " + c.CallbackId + ";" );
}
//
@ -70,7 +70,7 @@ namespace Generator
WriteLine( "//" );
WriteLine( "// Read this struct from a pointer, usually from Native. It will automatically do the awesome stuff." );
WriteLine( "//" );
StartBlock( $"public static {c.Name} FromPointer( IntPtr p )" );
StartBlock( $"internal static {c.Name} FromPointer( IntPtr p )" );
{
WriteLine( $"if ( Platform.PackSmall ) return (PackSmall) Marshal.PtrToStructure( p, typeof(PackSmall) );" );
@ -78,6 +78,18 @@ namespace Generator
}
EndBlock();
WriteLine();
WriteLine( "//" );
WriteLine( "// Get the size of the structure we're going to be using." );
WriteLine( "//" );
StartBlock( $"internal static int StructSize()" );
{
WriteLine( $"if ( Platform.PackSmall ) return System.Runtime.InteropServices.Marshal.SizeOf( typeof(PackSmall) );" );
WriteLine( $"return System.Runtime.InteropServices.Marshal.SizeOf( typeof({c.Name}) );" );
}
EndBlock();
if ( defaultPack == 8 )
defaultPack = 4;
@ -199,17 +211,16 @@ namespace Generator
WriteLine($"[MarshalAs(UnmanagedType.ByValArray, SizeConst = {num}, ArraySubType = UnmanagedType.U4)]");
}
WriteLine( $"public {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
WriteLine( $"internal {t} {CleanMemberName( m.Name )}; // {m.Name} {m.Type}" );
}
}
private void Callback( SteamApiDefinition.StructDef c )
{
WriteLine();
StartBlock( $"public static void RegisterCallback( Facepunch.Steamworks.BaseSteamworks steamworks, Action<{c.Name}, bool> CallbackFunction )" );
StartBlock( $"internal static void RegisterCallback( Facepunch.Steamworks.BaseSteamworks steamworks, Action<{c.Name}, bool> CallbackFunction )" );
{
WriteLine( $"var handle = new CallbackHandle();" );
WriteLine( $"handle.steamworks = steamworks;" );
WriteLine( $"var handle = new CallbackHandle( steamworks );" );
WriteLine( $"" );
CallbackCallresultShared( c, false );
@ -230,24 +241,21 @@ namespace Generator
private void CallResult( SteamApiDefinition.StructDef c )
{
WriteLine();
StartBlock( $"public static CallbackHandle CallResult( Facepunch.Steamworks.BaseSteamworks steamworks, SteamAPICall_t call, Action<{c.Name}, bool> CallbackFunction )" );
StartBlock( $"internal static CallResult<{c.Name}> CallResult( Facepunch.Steamworks.BaseSteamworks steamworks, SteamAPICall_t call, Action<{c.Name}, bool> CallbackFunction )" );
{
WriteLine( $"var handle = new CallbackHandle();" );
WriteLine( $"handle.steamworks = steamworks;" );
WriteLine( $"handle.CallResultHandle = call;" );
WriteLine( $"handle.CallResult = true;" );
WriteLine( $"" );
WriteLine( $"return new CallResult<{c.Name}>( steamworks, call, CallbackFunction, FromPointer, StructSize(), CallbackId );" );
// WriteLine( $"" );
CallbackCallresultShared( c, true );
// CallbackCallresultShared( c, true );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Register the callback with Steam" );
WriteLine( "//" );
WriteLine( $"steamworks.native.api.SteamAPI_RegisterCallResult( handle.PinnedCallback.AddrOfPinnedObject(), call );" );
// WriteLine( "" );
// WriteLine( "//" );
// WriteLine( "// Register the callback with Steam" );
// WriteLine( "//" );
// WriteLine( $"steamworks.native.api.SteamAPI_RegisterCallResult( handle.PinnedCallback.AddrOfPinnedObject(), call );" );
WriteLine();
WriteLine( "return handle;" );
// WriteLine();
//WriteLine( "return handle;" );
}
EndBlock();
}