Added optional onFailure callbacks to leaderboard and remote file methods that accept a callback (for #23)

This commit is contained in:
James King 2017-06-12 11:59:18 +01:00
parent 0a9f3f2281
commit 0a115f8ea4
4 changed files with 69 additions and 51 deletions

View File

@ -42,6 +42,9 @@ namespace Facepunch.Steamworks.Test
client.Update(); client.Update();
} }
Assert.IsFalse( board.IsError );
Assert.IsNotNull( board.Results );
foreach ( var entry in board.Results ) foreach ( var entry in board.Results )
{ {
Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" ); Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" );
@ -73,10 +76,8 @@ namespace Facepunch.Steamworks.Test
var done = false; var done = false;
board.FetchScores( Steamworks.Leaderboard.RequestType.Global, 0, 20, ( success, results ) => board.FetchScores( Steamworks.Leaderboard.RequestType.Global, 0, 20, results =>
{ {
Assert.IsTrue( success );
foreach ( var entry in results ) foreach ( var entry in results )
{ {
Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" ); Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" );
@ -86,7 +87,7 @@ namespace Facepunch.Steamworks.Test
} }
done = true; done = true;
} ); }, error => Assert.Fail( error.ToString() ) );
while ( !done ) while ( !done )
{ {
@ -154,14 +155,13 @@ namespace Facepunch.Steamworks.Test
const int score = 5678; const int score = 5678;
board.AddScore( false, score, null, ( success, result ) => board.AddScore( false, score, null, result =>
{ {
Assert.IsTrue( success );
Assert.IsTrue( result.ScoreChanged ); Assert.IsTrue( result.ScoreChanged );
Assert.AreEqual( result.Score, score ); Assert.AreEqual( result.Score, score );
done = true; done = true;
} ); }, error => Assert.Fail( error.ToString() ) );
while ( !done ) while ( !done )
{ {
@ -195,18 +195,15 @@ namespace Facepunch.Steamworks.Test
var file = client.RemoteStorage.CreateFile( "score/example.txt" ); var file = client.RemoteStorage.CreateFile( "score/example.txt" );
file.WriteAllText( attachment ); file.WriteAllText( attachment );
Assert.IsTrue( board.AddScore( false, score, null, ( success, result ) => Assert.IsTrue( board.AddScore( false, score, null, result =>
{ {
Assert.IsTrue( success );
Assert.IsTrue( result.ScoreChanged ); Assert.IsTrue( result.ScoreChanged );
Assert.IsTrue( board.AttachRemoteFile( file, attached => Assert.IsTrue( board.AttachRemoteFile( file, () =>
{ {
Assert.IsTrue( attached );
done = true; done = true;
} ) ); }, error => Assert.Fail( error.ToString() ) ) );
} ) ); }, error => Assert.Fail( error.ToString() ) ) );
while ( !done ) while ( !done )
{ {
@ -216,19 +213,18 @@ namespace Facepunch.Steamworks.Test
done = false; done = false;
Assert.IsTrue( board.FetchScores( Steamworks.Leaderboard.RequestType.GlobalAroundUser, 0, 0, ( success, entries ) => Assert.IsTrue( board.FetchScores( Steamworks.Leaderboard.RequestType.GlobalAroundUser, 0, 0, entries =>
{ {
Assert.AreEqual( 1, entries.Length ); Assert.AreEqual( 1, entries.Length );
Assert.IsNotNull( entries[0].AttachedFile ); Assert.IsNotNull( entries[0].AttachedFile );
Assert.IsTrue( entries[0].AttachedFile.Download( downloaded => Assert.IsTrue( entries[0].AttachedFile.Download( () =>
{ {
Assert.IsTrue( downloaded );
Assert.AreEqual( attachment, entries[0].AttachedFile.ReadAllText() ); Assert.AreEqual( attachment, entries[0].AttachedFile.ReadAllText() );
done = true; done = true;
} ) ); }, error => Assert.Fail( error.ToString() ) ) );
} ) ); }, error => Assert.Fail( error.ToString() ) ) );
while ( !done ) while ( !done )
{ {

View File

@ -5,6 +5,8 @@ using System.Text;
namespace Facepunch.Steamworks.Callbacks namespace Facepunch.Steamworks.Callbacks
{ {
public delegate void FailureCallback( Result reason );
public enum Result : int public enum Result : int
{ {
OK = 1, // success OK = 1, // success

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Facepunch.Steamworks.Callbacks;
using SteamNative; using SteamNative;
using Result = SteamNative.Result;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
@ -117,7 +119,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
/// <param name="success">If true, the score was submitted</param> /// <param name="success">If true, the score was submitted</param>
/// <param name="result">If successful, information about the new entry</param> /// <param name="result">If successful, information about the new entry</param>
public delegate void AddScoreCallback( bool success, AddScoreResult result ); public delegate void AddScoreCallback( AddScoreResult result );
/// <summary> /// <summary>
/// Information about a newly submitted score. /// Information about a newly submitted score.
@ -136,9 +138,9 @@ namespace Facepunch.Steamworks
/// they have no bearing on sorting at all /// they have no bearing on sorting at all
/// If onlyIfBeatsOldScore is true, the score will only be updated if it beats the existing score, else it will always /// If onlyIfBeatsOldScore is true, the score will only be updated if it beats the existing score, else it will always
/// be updated. /// be updated.
/// Information about the newly submitted score is passed to the optional <paramref name="callback"/>. /// Information about the newly submitted score is passed to the optional <paramref name="onSuccess"/>.
/// </summary> /// </summary>
public bool AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback callback = null ) public bool AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( !IsValid ) return false;
@ -147,16 +149,23 @@ namespace Facepunch.Steamworks
var flags = LeaderboardUploadScoreMethod.ForceUpdate; var flags = LeaderboardUploadScoreMethod.ForceUpdate;
if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest; if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest;
client.native.userstats.UploadLeaderboardScore( BoardId, flags, score, subscores, subscores.Length, callback != null ? (Action<LeaderboardScoreUploaded_t, bool>) (( result, error ) => client.native.userstats.UploadLeaderboardScore( BoardId, flags, score, subscores, subscores.Length, ( result, error ) =>
{ {
callback( !error && result.Success != 0, new AddScoreResult if ( !error && result.Success != 0 )
{
onSuccess?.Invoke( new AddScoreResult
{ {
Score = result.Score, Score = result.Score,
ScoreChanged = result.ScoreChanged != 0, ScoreChanged = result.ScoreChanged != 0,
GlobalRankNew = result.GlobalRankNew, GlobalRankNew = result.GlobalRankNew,
GlobalRankPrevious = result.GlobalRankPrevious GlobalRankPrevious = result.GlobalRankPrevious
} ); } );
}) : null ); }
else
{
onFailure?.Invoke( error ? Callbacks.Result.IOFailure : Callbacks.Result.Fail );
}
} );
return true; return true;
} }
@ -164,14 +173,14 @@ namespace Facepunch.Steamworks
/// <summary> /// <summary>
/// Callback invoked by <see cref="Leaderboard.AttachRemoteFile"/> when file attachment is complete. /// Callback invoked by <see cref="Leaderboard.AttachRemoteFile"/> when file attachment is complete.
/// </summary> /// </summary>
public delegate void AttachRemoteFileCallback( bool success ); public delegate void AttachRemoteFileCallback();
/// <summary> /// <summary>
/// Attempt to attach a <see cref="RemoteStorage"/> file to the current user's leaderboard entry. /// Attempt to attach a <see cref="RemoteStorage"/> file to the current user's leaderboard entry.
/// Can be useful for storing replays along with scores. /// Can be useful for storing replays along with scores.
/// </summary> /// </summary>
/// <returns>True if the file attachment process has started</returns> /// <returns>True if the file attachment process has started</returns>
public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback callback = null ) public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( !IsValid ) return false;
@ -179,19 +188,26 @@ namespace Facepunch.Steamworks
{ {
var handle = client.native.userstats.AttachLeaderboardUGC( BoardId, file.UGCHandle, ( result, error ) => var handle = client.native.userstats.AttachLeaderboardUGC( BoardId, file.UGCHandle, ( result, error ) =>
{ {
callback?.Invoke( !error && result.Result == Result.OK ); if ( !error && result.Result == Result.OK )
{
onSuccess?.Invoke();
}
else
{
onFailure?.Invoke( result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result) result.Result );
}
} ); } );
return handle.CallResultHandle != 0; return handle.CallResultHandle != 0;
} }
file.Share( success => file.Share( () =>
{ {
if ( !success || !file.IsShared || !AttachRemoteFile( file, callback ) ) if ( !file.IsShared || !AttachRemoteFile( file, onSuccess, onFailure ) )
{ {
callback?.Invoke( false ); onFailure?.Invoke( Callbacks.Result.Fail );
} }
} ); }, onFailure );
return true; return true;
} }
@ -236,13 +252,13 @@ namespace Facepunch.Steamworks
/// Callback invoked by <see cref="FetchScores(RequestType, int, int, FetchScoresCallback)"/> when /// Callback invoked by <see cref="FetchScores(RequestType, int, int, FetchScoresCallback)"/> when
/// a query is complete. /// a query is complete.
/// </summary> /// </summary>
public delegate void FetchScoresCallback( bool success, Entry[] results ); public delegate void FetchScoresCallback( Entry[] results );
/// <summary> /// <summary>
/// Fetch a subset of scores. The scores are passed to <paramref name="callback"/>. /// Fetch a subset of scores. The scores are passed to <paramref name="callback"/>.
/// </summary> /// </summary>
/// <returns>Returns true if we have started the query</returns> /// <returns>Returns true if we have started the query</returns>
public bool FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback callback ) public bool FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback onSuccess, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( !IsValid ) return false;
@ -250,7 +266,7 @@ namespace Facepunch.Steamworks
{ {
if ( error ) if ( error )
{ {
callback( false, null ); onFailure?.Invoke( Callbacks.Result.IOFailure );
} }
else else
{ {
@ -258,7 +274,7 @@ namespace Facepunch.Steamworks
else _sEntryBuffer.Clear(); else _sEntryBuffer.Clear();
ReadScores( result, _sEntryBuffer ); ReadScores( result, _sEntryBuffer );
callback( true, _sEntryBuffer.ToArray() ); onSuccess( _sEntryBuffer.ToArray() );
} }
} ); } );

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Facepunch.Steamworks.Callbacks;
using SteamNative; using SteamNative;
using Result = SteamNative.Result;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
@ -133,7 +135,7 @@ namespace Facepunch.Steamworks
/// <summary> /// <summary>
/// Callback invoked by <see cref="RemoteFile.Download"/> when a file download is complete. /// Callback invoked by <see cref="RemoteFile.Download"/> when a file download is complete.
/// </summary> /// </summary>
public delegate void DownloadCallback( bool success ); public delegate void DownloadCallback();
/// <summary> /// <summary>
/// Gets the number of bytes downloaded and the total number of bytes expected while /// Gets the number of bytes downloaded and the total number of bytes expected while
@ -149,7 +151,7 @@ namespace Facepunch.Steamworks
/// Attempts to start downloading a shared file. /// Attempts to start downloading a shared file.
/// </summary> /// </summary>
/// <returns>True if the download has successfully started</returns> /// <returns>True if the download has successfully started</returns>
public bool Download( DownloadCallback callback = null ) public bool Download( DownloadCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( !_isUgc ) return false; if ( !_isUgc ) return false;
if ( _isDownloading ) return false; if ( _isDownloading ) return false;
@ -163,7 +165,7 @@ namespace Facepunch.Steamworks
if ( error || result.Result != Result.OK ) if ( error || result.Result != Result.OK )
{ {
callback?.Invoke( false ); onFailure?.Invoke( result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result) result.Result );
return; return;
} }
@ -180,7 +182,7 @@ namespace Facepunch.Steamworks
} }
} }
callback?.Invoke( true ); onSuccess?.Invoke();
} ); } );
return true; return true;
@ -229,13 +231,13 @@ namespace Facepunch.Steamworks
/// <summary> /// <summary>
/// Callback invoked by <see cref="RemoteFile.Share"/> when file sharing is complete. /// Callback invoked by <see cref="RemoteFile.Share"/> when file sharing is complete.
/// </summary> /// </summary>
public delegate void ShareCallback( bool success ); public delegate void ShareCallback();
/// <summary> /// <summary>
/// Attempt to publish this file for other users to download. /// Attempt to publish this file for other users to download.
/// </summary> /// </summary>
/// <returns>True if we have started attempting to share</returns> /// <returns>True if we have started attempting to share</returns>
public bool Share( ShareCallback callback = null ) public bool Share( ShareCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( _isUgc ) return false; if ( _isUgc ) return false;
@ -244,13 +246,15 @@ namespace Facepunch.Steamworks
remoteStorage.native.FileShare( FileName, ( result, error ) => remoteStorage.native.FileShare( FileName, ( result, error ) =>
{ {
var success = !error && result.Result == Result.OK; if ( !error && result.Result == Result.OK )
if ( success )
{ {
_handle.Value = result.File; _handle.Value = result.File;
onSuccess?.Invoke();
}
else
{
onFailure?.Invoke( result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result) result.Result );
} }
callback?.Invoke( success );
} ); } );
return true; return true;