From 3752e753167d326b1cb5bb7ae5061a677079e969 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 9 Jun 2017 11:00:01 +0100 Subject: [PATCH] Setting Rich Presence --- .../Client/RemoteStorage.cs | 1 - .../Client/RichPresence.cs | 56 +++++++++++++++++++ .../Facepunch.Steamworks.Test.csproj | 1 + Facepunch.Steamworks/Client.cs | 8 +++ Facepunch.Steamworks/Client/User.cs | 50 +++++++++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Facepunch.Steamworks.Test/Client/RichPresence.cs create mode 100644 Facepunch.Steamworks/Client/User.cs diff --git a/Facepunch.Steamworks.Test/Client/RemoteStorage.cs b/Facepunch.Steamworks.Test/Client/RemoteStorage.cs index 11e209c..67bdb15 100644 --- a/Facepunch.Steamworks.Test/Client/RemoteStorage.cs +++ b/Facepunch.Steamworks.Test/Client/RemoteStorage.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Facepunch.Steamworks.Test diff --git a/Facepunch.Steamworks.Test/Client/RichPresence.cs b/Facepunch.Steamworks.Test/Client/RichPresence.cs new file mode 100644 index 0000000..8e5cf81 --- /dev/null +++ b/Facepunch.Steamworks.Test/Client/RichPresence.cs @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Facepunch.Steamworks.Test +{ + [TestClass] + [DeploymentItem( "steam_api.dll" )] + [DeploymentItem( "steam_api64.dll" )] + [DeploymentItem( "steam_appid.txt" )] + public class RichPresence + { + [TestMethod] + public void MissingKeyIsNull() + { + using ( var client = new Steamworks.Client( 252490 ) ) + { + var key = client.User.GetRichPresence( "Missing Key" ); + Assert.IsNull( key ); + } + } + + [TestMethod] + public void ReadBackSetKey() + { + using ( var client = new Steamworks.Client( 252490 ) ) + { + client.User.SetRichPresence( "One", "Two" ); + + var value = client.User.GetRichPresence( "One" ); + Assert.IsNotNull( value ); + Assert.AreEqual( value, "Two" ); + } + } + + [TestMethod] + public void ClearingKeys() + { + using ( var client = new Steamworks.Client( 252490 ) ) + { + client.User.SetRichPresence( "One", "Two" ); + + var value = client.User.GetRichPresence( "One" ); + Assert.IsNotNull( value ); + Assert.AreEqual( value, "Two" ); + + client.User.ClearRichPresence(); + + value = client.User.GetRichPresence( "One" ); + Assert.IsNull( value ); + } + } + } +} \ No newline at end of file diff --git a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj index 82d2554..891970f 100644 --- a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj +++ b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj @@ -94,6 +94,7 @@ + diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index 9b49fbc..c541c3c 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -59,6 +59,7 @@ namespace Facepunch.Steamworks public Achievements Achievements { get; private set; } public Stats Stats { get; private set; } public MicroTransactions MicroTransactions { get; private set; } + public User User { get; private set; } public Client( uint appId ) { @@ -89,6 +90,7 @@ namespace Facepunch.Steamworks Stats = new Stats( this ); Achievements = new Achievements( this ); MicroTransactions = new MicroTransactions( this ); + User = new User( this ); Workshop.friends = Friends; @@ -178,6 +180,12 @@ namespace Facepunch.Steamworks MicroTransactions = null; } + if ( User != null ) + { + User.Dispose(); + User = null; + } + if ( Instance == this ) { Instance = null; diff --git a/Facepunch.Steamworks/Client/User.cs b/Facepunch.Steamworks/Client/User.cs new file mode 100644 index 0000000..daf3363 --- /dev/null +++ b/Facepunch.Steamworks/Client/User.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using SteamNative; + +namespace Facepunch.Steamworks +{ + public class User : IDisposable + { + internal Client client; + internal Dictionary richPresence = new Dictionary(); + + internal User( Client c ) + { + client = c; + } + + public void Dispose() + { + client = null; + } + + public string GetRichPresence( string key ) + { + string val = null; + + if ( richPresence.TryGetValue( key, out val ) ) + return val; + + return null; + } + + public void SetRichPresence( string key, string value ) + { + richPresence[key] = value; + client.native.friends.SetRichPresence( key, value ); + } + + public void ClearRichPresence() + { + richPresence.Clear(); + client.native.friends.ClearRichPresence(); + } + } +}