diff --git a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj index 821ee0f..6507d08 100644 --- a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj +++ b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj @@ -89,6 +89,7 @@ + diff --git a/Facepunch.Steamworks.Test/UserStatsTest.cs b/Facepunch.Steamworks.Test/UserStatsTest.cs new file mode 100644 index 0000000..6950311 --- /dev/null +++ b/Facepunch.Steamworks.Test/UserStatsTest.cs @@ -0,0 +1,32 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Steamworks +{ + [TestClass] + [DeploymentItem( "steam_api64.dll" )] + public class UserStatsTest + { + [TestMethod] + public void AchievementList() + { + foreach ( var a in SteamUserStats.Achievements ) + { + Console.WriteLine( $"{a.Value}" ); + Console.WriteLine( $" a.State: {a.State}" ); + Console.WriteLine( $" a.UnlockTime: {a.UnlockTime}" ); + Console.WriteLine( $" a.GlobalUnlockedPercentage: {a.GlobalUnlockedPercentage}" ); + + var icon = a.GetIcon(); + + Console.WriteLine( $" a.Icon: {icon}" ); + } + } + + } + +} diff --git a/Facepunch.Steamworks/SteamUserStats.cs b/Facepunch.Steamworks/SteamUserStats.cs new file mode 100644 index 0000000..3eb7590 --- /dev/null +++ b/Facepunch.Steamworks/SteamUserStats.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Steamworks.Data; + +namespace Steamworks +{ + public static class SteamUserStats + { + static ISteamUserStats _internal; + internal static ISteamUserStats Internal + { + get + { + if ( _internal == null ) + _internal = new ISteamUserStats(); + + return _internal; + } + } + + /// + /// Get the available achievements + /// + public static IEnumerable Achievements + { + get + { + for( int i=0; i< Internal.GetNumAchievements(); i++ ) + { + yield return new Achievement( Internal.GetAchievementName( (uint) i ) ); + } + } + } + } +} \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/Achievement.cs b/Facepunch.Steamworks/Structs/Achievement.cs new file mode 100644 index 0000000..a7c85ac --- /dev/null +++ b/Facepunch.Steamworks/Structs/Achievement.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Steamworks.Data +{ + public struct Achievement + { + public string Value; + + public Achievement( string name ) + { + Value = name; + } + + public override string ToString() => Value; + + /// + /// True if unlocked + /// + public bool State + { + get + { + var state = false; + SteamUserStats.Internal.GetAchievement( Value, ref state ); + return state; + } + } + + + /// + /// Should hold the unlock time if State is true + /// + public DateTime? UnlockTime + { + get + { + var state = false; + uint time = 0; + + if ( !SteamUserStats.Internal.GetAchievementAndUnlockTime( Value, ref state, ref time ) || !state ) + return null; + + return Epoch.ToDateTime( time ); + } + } + + /// + /// Gets the icon of the achievement + /// + public Image? GetIcon() + { + return SteamUtils.GetImage( SteamUserStats.Internal.GetAchievementIcon( Value ) ); + } + + /// + /// Returns the percentage of users who have unlocked the specified achievement, or -1 if no data available. + /// + public float GlobalUnlockedPercentage + { + get + { + float pct = 0; + + if ( !SteamUserStats.Internal.GetAchievementAchievedPercent( Value, ref pct ) ) + return -1.0f; + + return pct; + } + } + + /// + /// Make this achievement earned + /// + public bool Trigger( bool apply = true ) + { + var r = SteamUserStats.Internal.SetAchievement( Value ); + + if ( apply && r ) + { + SteamUserStats.Internal.StoreStats(); + } + + return r; + } + + /// + /// Reset this achievement to not achieved + /// + public bool Clear() + { + return SteamUserStats.Internal.ClearAchievement( Value ); + } + } +} \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/Image.cs b/Facepunch.Steamworks/Structs/Image.cs index 97e9418..57b00cd 100644 --- a/Facepunch.Steamworks/Structs/Image.cs +++ b/Facepunch.Steamworks/Structs/Image.cs @@ -23,6 +23,11 @@ namespace Steamworks.Data return c; } + + public override string ToString() + { + return $"{Width}x{Height} ({Data.Length}bytes)"; + } } public struct Color