KeyValue tags for workshop items / queries

This commit is contained in:
James King 2019-09-04 03:16:16 +01:00
parent 538bed982a
commit 79b73ff576
2 changed files with 45 additions and 8 deletions

View File

@ -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<string, string[]> 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<string, List<string>>( 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<string>();
tempDict.Add( key, list );
}
list.Add( value );
}
KeyValueTags = new Dictionary<string, string[]>( StringComparer.InvariantCultureIgnoreCase );
foreach ( var keyValues in tempDict )
{
KeyValueTags.Add( keyValues.Key, keyValues.Value.ToArray() );
}
}
public bool Download( bool highPriority = true )
{
if ( Installed ) return true;

View File

@ -35,6 +35,8 @@ namespace Facepunch.Steamworks
public ulong? UserId { get; set; }
public bool ReturnKeyValueTags { get; set; }
/// <summary>
/// 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<Item> _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
/// <summary>
/// Only return items with these tags
/// </summary>
public List<string> RequireTags { get; set; } = new List<string>();
public List<string> RequireTags { get; } = new List<string>();
/// <summary>
/// If true, return items that have all RequireTags
@ -213,14 +223,14 @@ namespace Facepunch.Steamworks
/// <summary>
/// Don't return any items with this tag
/// </summary>
public List<string> ExcludeTags { get; set; } = new List<string>();
public List<string> ExcludeTags { get; } = new List<string>();
/// <summary>
/// If you're querying for a particular file or files, add them to this.
/// </summary>
public List<ulong> FileId { get; set; } = new List<ulong>();
public List<ulong> FileId { get; } = new List<ulong>();
public List<KeyValuePair<string, string>> RequireKeyValueTags { get; } = new List<KeyValuePair<string, string>>();
/// <summary>
/// Don't call this in production!