Merge pull request #423 from kamyker/key-value-tags

Ugc item key value tags
This commit is contained in:
Garry Newman 2020-05-15 07:31:24 +01:00 committed by GitHub
commit 9101b82450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 10 deletions

View File

@ -69,8 +69,9 @@ namespace Steamworks.Ugc
public Editor WithPrivateVisibility() { Visibility = RemoteStoragePublishedFileVisibility.Private; return this; } public Editor WithPrivateVisibility() { Visibility = RemoteStoragePublishedFileVisibility.Private; return this; }
List<string> Tags; List<string> Tags;
Dictionary<string, string> KeyValueTags; Dictionary<string, List<string>> KeyValueTags;
HashSet<string> KeyValueTagsToRemove;
public Editor WithTag( string tag ) public Editor WithTag( string tag )
{ {
if ( Tags == null ) Tags = new List<string>(); if ( Tags == null ) Tags = new List<string>();
@ -80,10 +81,37 @@ namespace Steamworks.Ugc
return this; return this;
} }
/// <summary>
/// Adds a key-value tag pair to an item.
/// Keys can map to multiple different values (1-to-many relationship).
/// Key names are restricted to alpha-numeric characters and the '_' character.
/// Both keys and values cannot exceed 255 characters in length. Key-value tags are searchable by exact match only.
/// To replace all values associated to one key use RemoveKeyValueTags then AddKeyValueTag.
/// </summary>
public Editor AddKeyValueTag(string key, string value) public Editor AddKeyValueTag(string key, string value)
{ {
if (KeyValueTags == null) KeyValueTags = new Dictionary<string, string>(); if (KeyValueTags == null)
KeyValueTags.Add(key, value); KeyValueTags = new Dictionary<string, List<string>>();
if ( KeyValueTags.TryGetValue( key, out var list ) )
list.Add( value );
else
KeyValueTags[key] = new List<string>() { value };
return this;
}
/// <summary>
/// Removes a key and all values associated to it.
/// You can remove up to 100 keys per item update.
/// If you need remove more tags than that you'll need to make subsequent item updates.
/// </summary>
public Editor RemoveKeyValueTags( string key )
{
if ( KeyValueTagsToRemove == null )
KeyValueTagsToRemove = new HashSet<string>();
KeyValueTagsToRemove.Add( key );
return this; return this;
} }
@ -156,11 +184,19 @@ namespace Steamworks.Ugc
} }
} }
if (KeyValueTags != null && KeyValueTags.Count > 0) if ( KeyValueTagsToRemove != null)
{ {
foreach (var keyValueTag in KeyValueTags) foreach ( var key in KeyValueTagsToRemove )
SteamUGC.Internal.RemoveItemKeyValueTags( handle, key );
}
if ( KeyValueTags != null )
{
foreach ( var keyWithValues in KeyValueTags )
{ {
SteamUGC.Internal.AddItemKeyValueTag(handle, keyValueTag.Key, keyValueTag.Value); var key = keyWithValues.Key;
foreach ( var value in keyWithValues.Value )
SteamUGC.Internal.AddItemKeyValueTag( handle, key, value );
} }
} }

View File

@ -39,6 +39,11 @@ namespace Steamworks.Ugc
/// </summary> /// </summary>
public string[] Tags { get; internal set; } public string[] Tags { get; internal set; }
/// <summary>
/// A dictionary of key value tags for this item, only available from queries WithKeyValueTags(true)
/// </summary>
public Dictionary<string,string> KeyValueTags { get; internal set; }
/// <summary> /// <summary>
/// App Id of the app that created this item /// App Id of the app that created this item
/// </summary> /// </summary>

View File

@ -148,7 +148,8 @@ namespace Steamworks.Ugc
Handle = result.Value.Handle, Handle = result.Value.Handle,
ResultCount = (int) result.Value.NumResultsReturned, ResultCount = (int) result.Value.NumResultsReturned,
TotalCount = (int)result.Value.TotalMatchingResults, TotalCount = (int)result.Value.TotalMatchingResults,
CachedData = result.Value.CachedData CachedData = result.Value.CachedData,
ReturnsKeyValueTags = WantsReturnKeyValueTags ?? false,
}; };
} }
@ -241,7 +242,9 @@ namespace Steamworks.Ugc
bool? WantsReturnOnlyIDs; bool? WantsReturnOnlyIDs;
public QueryType WithOnlyIDs(bool b) { WantsReturnOnlyIDs = b; return this; } public QueryType WithOnlyIDs(bool b) { WantsReturnOnlyIDs = b; return this; }
bool? WantsReturnKeyValueTags; bool? WantsReturnKeyValueTags;
public QueryType WithKeyValueTag(bool b) { WantsReturnKeyValueTags = b; return this; } public QueryType WithKeyValueTags(bool b) { WantsReturnKeyValueTags = b; return this; }
[Obsolete( "Renamed to WithKeyValueTags" )]
public QueryType WithKeyValueTag(bool b) { WantsReturnKeyValueTags = b; return this; }
bool? WantsReturnLongDescription; bool? WantsReturnLongDescription;
public QueryType WithLongDescription(bool b) { WantsReturnLongDescription = b; return this; } public QueryType WithLongDescription(bool b) { WantsReturnLongDescription = b; return this; }
bool? WantsReturnMetadata; bool? WantsReturnMetadata;

View File

@ -12,6 +12,7 @@ namespace Steamworks.Ugc
public bool CachedData; public bool CachedData;
internal bool ReturnsKeyValueTags;
public IEnumerable<Item> Entries public IEnumerable<Item> Entries
{ {
get get
@ -43,9 +44,21 @@ namespace Steamworks.Ugc
item.PreviewImageUrl = preview; item.PreviewImageUrl = preview;
} }
if ( ReturnsKeyValueTags )
{
var keyValueTagsCount = SteamUGC.Internal.GetQueryUGCNumKeyValueTags( Handle, i );
item.KeyValueTags = new Dictionary<string, string>( (int)keyValueTagsCount );
for ( uint j = 0; j < keyValueTagsCount; j++ )
{
string key, value;
if ( SteamUGC.Internal.GetQueryUGCKeyValueTag( Handle, i, j, out key, out value ) )
item.KeyValueTags[key] = value;
}
}
// TODO GetQueryUGCAdditionalPreview // TODO GetQueryUGCAdditionalPreview
// TODO GetQueryUGCChildren // TODO GetQueryUGCChildren
// TODO GetQueryUGCKeyValueTag
// TODO GetQueryUGCMetadata // TODO GetQueryUGCMetadata