From 79b73ff57608364b36f41c38f13ca0623a1916b2 Mon Sep 17 00:00:00 2001 From: James King Date: Wed, 4 Sep 2019 03:16:16 +0100 Subject: [PATCH] KeyValue tags for workshop items / queries --- .../Interfaces/Workshop.Item.cs | 31 +++++++++++++++++-- .../Interfaces/Workshop.Query.cs | 22 +++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Facepunch.Steamworks/Interfaces/Workshop.Item.cs b/Facepunch.Steamworks/Interfaces/Workshop.Item.cs index 24fda87..0435a49 100644 --- a/Facepunch.Steamworks/Interfaces/Workshop.Item.cs +++ b/Facepunch.Steamworks/Interfaces/Workshop.Item.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.InteropServices; -using System.Text; using SteamNative; namespace Facepunch.Steamworks @@ -19,6 +17,7 @@ namespace Facepunch.Steamworks public ulong OwnerId { get; private set; } public float Score { get; private set; } public string[] Tags { get; private set; } + public Dictionary KeyValueTags { get; private set; } public string Title { get; private set; } public uint VotesDown { get; private set; } public uint VotesUp { get; private set; } @@ -48,6 +47,34 @@ namespace Facepunch.Steamworks return item; } + internal void ReadKeyValueTags( SteamUGCQueryCompleted_t data, uint index ) + { + var tempDict = new Dictionary>( StringComparer.InvariantCultureIgnoreCase ); + + var numKeyValTags = workshop.ugc.GetQueryUGCNumKeyValueTags( data.Handle, index ); + + for ( uint kvTagIndex = 0; kvTagIndex < numKeyValTags; ++kvTagIndex ) + { + if ( !workshop.ugc.GetQueryUGCKeyValueTag( data.Handle, index, kvTagIndex, out var key, out var value ) ) + continue; + + if ( !tempDict.TryGetValue( key, out var list ) ) + { + list = new List(); + tempDict.Add( key, list ); + } + + list.Add( value ); + } + + KeyValueTags = new Dictionary( StringComparer.InvariantCultureIgnoreCase ); + + foreach ( var keyValues in tempDict ) + { + KeyValueTags.Add( keyValues.Key, keyValues.Value.ToArray() ); + } + } + public bool Download( bool highPriority = true ) { if ( Installed ) return true; diff --git a/Facepunch.Steamworks/Interfaces/Workshop.Query.cs b/Facepunch.Steamworks/Interfaces/Workshop.Query.cs index 4b26d47..b521a26 100644 --- a/Facepunch.Steamworks/Interfaces/Workshop.Query.cs +++ b/Facepunch.Steamworks/Interfaces/Workshop.Query.cs @@ -35,6 +35,8 @@ namespace Facepunch.Steamworks public ulong? UserId { get; set; } + public bool ReturnKeyValueTags { get; set; } + /// /// If order is RankedByTrend, this value represents how many days to take /// into account. @@ -63,7 +65,7 @@ namespace Facepunch.Steamworks private int _resultSkip = 0; private List _results; - public void Run() + public void Run() { if ( Callback != null ) return; @@ -83,7 +85,7 @@ namespace Facepunch.Steamworks RunInternal(); } - unsafe void RunInternal() + void RunInternal() { if ( FileId.Count != 0 ) { @@ -111,12 +113,17 @@ namespace Facepunch.Steamworks if ( RequireTags.Count > 0 ) workshop.ugc.SetMatchAnyTag( Handle, !RequireAllTags ); + foreach ( var keyValueTag in RequireKeyValueTags ) + workshop.ugc.AddRequiredKeyValueTag( Handle, keyValueTag.Key, keyValueTag.Value ); + if ( RankedByTrendDays > 0 ) workshop.ugc.SetRankedByTrendDays( Handle, (uint) RankedByTrendDays ); foreach ( var tag in ExcludeTags ) workshop.ugc.AddExcludedTag( Handle, tag ); + workshop.ugc.SetReturnKeyValueTags( Handle, ReturnKeyValueTags ); + Callback = workshop.ugc.SendQueryUGCRequest( Handle, ResultCallback ); } @@ -150,6 +157,9 @@ namespace Facepunch.Steamworks item.WebsiteViews = GetStat( data.Handle, i, ItemStatistic.NumUniqueWebsiteViews ); item.ReportScore = GetStat( data.Handle, i, ItemStatistic.ReportScore ); + if ( ReturnKeyValueTags ) + item.ReadKeyValueTags( data, (uint)i ); + string url = null; if ( workshop.ugc.GetQueryUGCPreviewURL( data.Handle, (uint)i, out url ) ) item.PreviewImageUrl = url; @@ -202,7 +212,7 @@ namespace Facepunch.Steamworks /// /// Only return items with these tags /// - public List RequireTags { get; set; } = new List(); + public List RequireTags { get; } = new List(); /// /// If true, return items that have all RequireTags @@ -213,14 +223,14 @@ namespace Facepunch.Steamworks /// /// Don't return any items with this tag /// - public List ExcludeTags { get; set; } = new List(); + public List ExcludeTags { get; } = new List(); /// /// If you're querying for a particular file or files, add them to this. /// - public List FileId { get; set; } = new List(); - + public List FileId { get; } = new List(); + public List> RequireKeyValueTags { get; } = new List>(); /// /// Don't call this in production!