diff --git a/Facepunch.Steamworks.Test/Client/Leaderboard.cs b/Facepunch.Steamworks.Test/Client/Leaderboard.cs index 48f23d8..5563c26 100644 --- a/Facepunch.Steamworks.Test/Client/Leaderboard.cs +++ b/Facepunch.Steamworks.Test/Client/Leaderboard.cs @@ -42,6 +42,9 @@ namespace Facepunch.Steamworks.Test client.Update(); } + Assert.IsFalse( board.IsError ); + Assert.IsNotNull( board.Results ); + foreach ( var entry in board.Results ) { Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" ); @@ -73,10 +76,8 @@ namespace Facepunch.Steamworks.Test 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 ) { Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" ); @@ -86,7 +87,7 @@ namespace Facepunch.Steamworks.Test } done = true; - } ); + }, error => Assert.Fail( error.ToString() ) ); while ( !done ) { @@ -154,14 +155,13 @@ namespace Facepunch.Steamworks.Test 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.AreEqual( result.Score, score ); done = true; - } ); + }, error => Assert.Fail( error.ToString() ) ); while ( !done ) { @@ -195,18 +195,15 @@ namespace Facepunch.Steamworks.Test var file = client.RemoteStorage.CreateFile( "score/example.txt" ); 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( board.AttachRemoteFile( file, attached => + Assert.IsTrue( board.AttachRemoteFile( file, () => { - Assert.IsTrue( attached ); - done = true; - } ) ); - } ) ); + }, error => Assert.Fail( error.ToString() ) ) ); + }, error => Assert.Fail( error.ToString() ) ) ); while ( !done ) { @@ -216,19 +213,18 @@ namespace Facepunch.Steamworks.Test 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.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() ); done = true; - } ) ); - } ) ); + }, error => Assert.Fail( error.ToString() ) ) ); + }, error => Assert.Fail( error.ToString() ) ) ); while ( !done ) { diff --git a/Facepunch.Steamworks/Callbacks/Index.cs b/Facepunch.Steamworks/Callbacks/Index.cs index fce857f..97d5ef5 100644 --- a/Facepunch.Steamworks/Callbacks/Index.cs +++ b/Facepunch.Steamworks/Callbacks/Index.cs @@ -5,6 +5,8 @@ using System.Text; namespace Facepunch.Steamworks.Callbacks { + public delegate void FailureCallback( Result reason ); + public enum Result : int { OK = 1, // success diff --git a/Facepunch.Steamworks/Client/Leaderboard.cs b/Facepunch.Steamworks/Client/Leaderboard.cs index 3532e20..440477e 100644 --- a/Facepunch.Steamworks/Client/Leaderboard.cs +++ b/Facepunch.Steamworks/Client/Leaderboard.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using Facepunch.Steamworks.Callbacks; using SteamNative; +using Result = SteamNative.Result; namespace Facepunch.Steamworks { @@ -117,7 +119,7 @@ namespace Facepunch.Steamworks /// /// If true, the score was submitted /// If successful, information about the new entry - public delegate void AddScoreCallback( bool success, AddScoreResult result ); + public delegate void AddScoreCallback( AddScoreResult result ); /// /// Information about a newly submitted score. @@ -136,9 +138,9 @@ namespace Facepunch.Steamworks /// 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 /// be updated. - /// Information about the newly submitted score is passed to the optional . + /// Information about the newly submitted score is passed to the optional . /// - 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; @@ -147,16 +149,23 @@ namespace Facepunch.Steamworks var flags = LeaderboardUploadScoreMethod.ForceUpdate; if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest; - client.native.userstats.UploadLeaderboardScore( BoardId, flags, score, subscores, subscores.Length, callback != null ? (Action) (( 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 ) { - Score = result.Score, - ScoreChanged = result.ScoreChanged != 0, - GlobalRankNew = result.GlobalRankNew, - GlobalRankPrevious = result.GlobalRankPrevious - } ); - }) : null ); + onSuccess?.Invoke( new AddScoreResult + { + Score = result.Score, + ScoreChanged = result.ScoreChanged != 0, + GlobalRankNew = result.GlobalRankNew, + GlobalRankPrevious = result.GlobalRankPrevious + } ); + } + else + { + onFailure?.Invoke( error ? Callbacks.Result.IOFailure : Callbacks.Result.Fail ); + } + } ); return true; } @@ -164,14 +173,14 @@ namespace Facepunch.Steamworks /// /// Callback invoked by when file attachment is complete. /// - public delegate void AttachRemoteFileCallback( bool success ); + public delegate void AttachRemoteFileCallback(); /// /// Attempt to attach a file to the current user's leaderboard entry. /// Can be useful for storing replays along with scores. /// /// True if the file attachment process has started - public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback callback = null ) + public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback onSuccess = null, FailureCallback onFailure = null ) { if ( !IsValid ) return false; @@ -179,19 +188,26 @@ namespace Facepunch.Steamworks { 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; } - 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; } @@ -236,13 +252,13 @@ namespace Facepunch.Steamworks /// Callback invoked by when /// a query is complete. /// - public delegate void FetchScoresCallback( bool success, Entry[] results ); + public delegate void FetchScoresCallback( Entry[] results ); /// /// Fetch a subset of scores. The scores are passed to . /// /// Returns true if we have started the query - 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; @@ -250,7 +266,7 @@ namespace Facepunch.Steamworks { if ( error ) { - callback( false, null ); + onFailure?.Invoke( Callbacks.Result.IOFailure ); } else { @@ -258,7 +274,7 @@ namespace Facepunch.Steamworks else _sEntryBuffer.Clear(); ReadScores( result, _sEntryBuffer ); - callback( true, _sEntryBuffer.ToArray() ); + onSuccess( _sEntryBuffer.ToArray() ); } } ); diff --git a/Facepunch.Steamworks/Client/RemoteStorage.File.cs b/Facepunch.Steamworks/Client/RemoteStorage.File.cs index b88695b..d554afe 100644 --- a/Facepunch.Steamworks/Client/RemoteStorage.File.cs +++ b/Facepunch.Steamworks/Client/RemoteStorage.File.cs @@ -3,7 +3,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using Facepunch.Steamworks.Callbacks; using SteamNative; +using Result = SteamNative.Result; namespace Facepunch.Steamworks { @@ -133,7 +135,7 @@ namespace Facepunch.Steamworks /// /// Callback invoked by when a file download is complete. /// - public delegate void DownloadCallback( bool success ); + public delegate void DownloadCallback(); /// /// 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. /// /// True if the download has successfully started - public bool Download( DownloadCallback callback = null ) + public bool Download( DownloadCallback onSuccess = null, FailureCallback onFailure = null ) { if ( !_isUgc ) return false; if ( _isDownloading ) return false; @@ -163,7 +165,7 @@ namespace Facepunch.Steamworks if ( error || result.Result != Result.OK ) { - callback?.Invoke( false ); + onFailure?.Invoke( result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result) result.Result ); return; } @@ -180,7 +182,7 @@ namespace Facepunch.Steamworks } } - callback?.Invoke( true ); + onSuccess?.Invoke(); } ); return true; @@ -229,13 +231,13 @@ namespace Facepunch.Steamworks /// /// Callback invoked by when file sharing is complete. /// - public delegate void ShareCallback( bool success ); + public delegate void ShareCallback(); /// /// Attempt to publish this file for other users to download. /// /// True if we have started attempting to share - public bool Share( ShareCallback callback = null ) + public bool Share( ShareCallback onSuccess = null, FailureCallback onFailure = null ) { if ( _isUgc ) return false; @@ -244,13 +246,15 @@ namespace Facepunch.Steamworks remoteStorage.native.FileShare( FileName, ( result, error ) => { - var success = !error && result.Result == Result.OK; - if ( success ) + if ( !error && result.Result == Result.OK ) { _handle.Value = result.File; + onSuccess?.Invoke(); + } + else + { + onFailure?.Invoke( result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result) result.Result ); } - - callback?.Invoke( success ); } ); return true;