diff --git a/Facepunch.Steamworks/Callbacks/CallResult.cs b/Facepunch.Steamworks/Callbacks/CallResult.cs index 13fd500..fd6af2f 100644 --- a/Facepunch.Steamworks/Callbacks/CallResult.cs +++ b/Facepunch.Steamworks/Callbacks/CallResult.cs @@ -35,7 +35,7 @@ namespace Steamworks /// public void OnCompleted( Action continuation ) { - Dispatch.OnCallComplete( call, continuation, server ); + Dispatch.OnCallComplete( call, continuation, server ); } /// @@ -55,10 +55,12 @@ namespace Steamworks { if ( !utils.GetAPICallResult( call, ptr, size, (int)t.CallbackType, ref failed ) || failed ) { - Console.WriteLine( $"Api Call result returned false or {failed}" ); + Dispatch.OnDebugCallback?.Invoke( t.CallbackType, "!GetAPICallResult or failed", server ); return null; } + Dispatch.OnDebugCallback?.Invoke( t.CallbackType, Dispatch.CallbackToString( t.CallbackType, ptr, size ), server ); + return ((T)Marshal.PtrToStructure( ptr, typeof( T ) )); } finally diff --git a/Facepunch.Steamworks/Classes/Dispatch.cs b/Facepunch.Steamworks/Classes/Dispatch.cs index f5119a1..dfd7db5 100644 --- a/Facepunch.Steamworks/Classes/Dispatch.cs +++ b/Facepunch.Steamworks/Classes/Dispatch.cs @@ -130,6 +130,8 @@ namespace Steamworks /// private static void ProcessCallback( CallbackMsg_t msg, bool isServer ) { + OnDebugCallback?.Invoke( msg.Type, CallbackToString( msg.Type, msg.Data, msg.DataSize ), isServer ); + // Is this a special callback telling us that the call result is ready? if ( msg.Type == CallbackType.SteamAPICallCompleted ) { @@ -137,11 +139,6 @@ namespace Steamworks return; } - if ( OnDebugCallback != null ) - { - OnDebugCallback( msg.Type, CallbackToString( msg ), isServer ); - } - if ( Callbacks.TryGetValue( msg.Type, out var list ) ) { actionsToCall.Clear(); @@ -166,20 +163,30 @@ namespace Steamworks /// /// Given a callback, try to turn it into a string /// - private static string CallbackToString( CallbackMsg_t msg ) + internal static string CallbackToString( CallbackType type, IntPtr data, int expectedsize ) { - if ( !CallbackTypeFactory.All.TryGetValue( msg.Type, out var t ) ) - return "[not in sdk]"; + if ( !CallbackTypeFactory.All.TryGetValue( type, out var t ) ) + return $"[{type} not in sdk]"; - var strct = msg.Data.ToType( t ); + var strct = data.ToType( t ); if ( strct == null ) return "[null]"; var str = ""; - foreach ( var field in t.GetFields( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic ) ) + var fields = t.GetFields( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic ); + + var columnSize = fields.Max( x => x.Name.Length ) + 1; + + if ( columnSize < 10 ) + columnSize = 10; + + foreach ( var field in fields ) { - str += $"{field.Name}: \"{field.GetValue( strct )}\"\n"; + var spaces = (columnSize - field.Name.Length); + if ( spaces < 0 ) spaces = 0; + + str += $"{new String( ' ', spaces )}{field.Name}: {field.GetValue( strct )}\n"; } return str.Trim( '\n' ); @@ -197,7 +204,16 @@ namespace Steamworks // if ( !ResultCallbacks.TryGetValue( result.AsyncCall, out var callbackInfo ) ) { - // Do we care? Should we throw errors? + // + // This can happen if the callback result was immediately available + // so we just returned that without actually going through the callback + // dance. It's okay for this to fail. + // + + // + // But still let everyone know that this happened.. + // + OnDebugCallback?.Invoke( (CallbackType)result.Callback, $"[no callback waiting/required]", false ); return; } @@ -248,7 +264,7 @@ namespace Steamworks /// /// Watch for a steam api call /// - internal static void OnCallComplete( SteamAPICall_t call, Action continuation, bool server ) + internal static void OnCallComplete( SteamAPICall_t call, Action continuation, bool server ) where T : struct, ICallbackData { ResultCallbacks[call.Value] = new ResultCallback {