From 69773bb033abb4444f51ba20215602e2819fed23 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 10 May 2019 13:38:04 +0100 Subject: [PATCH] Squeeze all the UGC Queries into one struct --- Facepunch.Steamworks.Test/UgcQuery.cs | 8 +- Facepunch.Steamworks/SteamUgc.cs | 3 +- Facepunch.Steamworks/Structs/UgcQuery.cs | 63 ++++++- Facepunch.Steamworks/Structs/UgcQueryFile.cs | 69 ------- Facepunch.Steamworks/Structs/UgcQueryUser.cs | 181 ------------------- 5 files changed, 67 insertions(+), 257 deletions(-) delete mode 100644 Facepunch.Steamworks/Structs/UgcQueryFile.cs delete mode 100644 Facepunch.Steamworks/Structs/UgcQueryUser.cs diff --git a/Facepunch.Steamworks.Test/UgcQuery.cs b/Facepunch.Steamworks.Test/UgcQuery.cs index 8147535..6cdaf0d 100644 --- a/Facepunch.Steamworks.Test/UgcQuery.cs +++ b/Facepunch.Steamworks.Test/UgcQuery.cs @@ -68,8 +68,8 @@ public async Task QueryAllFromFriends() [TestMethod] public async Task QueryUserOwn() { - var q = Ugc.UserQuery.All - .FromSelf(); + var q = Ugc.Query.All + .CreatedByMe(); var result = await q.GetPageAsync( 1 ); Assert.IsNotNull( result ); @@ -86,8 +86,8 @@ public async Task QueryUserOwn() [TestMethod] public async Task QueryGarry() { - var q = Ugc.UserQuery.All - .FromUser( 76561197960279927 ); + var q = Ugc.Query.All + .CreatedBy( 76561197960279927 ); var result = await q.GetPageAsync( 1 ); Assert.IsNotNull( result ); diff --git a/Facepunch.Steamworks/SteamUgc.cs b/Facepunch.Steamworks/SteamUgc.cs index f68d31a..a2f377d 100644 --- a/Facepunch.Steamworks/SteamUgc.cs +++ b/Facepunch.Steamworks/SteamUgc.cs @@ -51,7 +51,8 @@ public static bool Download( PublishedFileId fileId, bool highPriority = false ) /// public static async Task QueryFileAsync( PublishedFileId fileId ) { - var result = await new Ugc.FileQuery( fileId ) + var result = await Ugc.Query.All + .WithFileId( fileId ) .GetPageAsync( 1 ); if ( !result.HasValue || result.Value.ResultCount != 1 ) diff --git a/Facepunch.Steamworks/Structs/UgcQuery.cs b/Facepunch.Steamworks/Structs/UgcQuery.cs index 58a0f59..7bbafe0 100644 --- a/Facepunch.Steamworks/Structs/UgcQuery.cs +++ b/Facepunch.Steamworks/Structs/UgcQuery.cs @@ -56,6 +56,45 @@ public Query( UgcType type ) : this() public Query RankedByPlaytimeSessionsTrend() { queryType = UGCQuery.RankedByPlaytimeSessionsTrend; return this; } public Query RankedByLifetimePlaytimeSessions() { queryType = UGCQuery.RankedByLifetimePlaytimeSessions; return this; } + #region UserQuery + + SteamId? steamid; + + UserUGCList userType; + UserUGCListSortOrder userSort; + + public Query CreatedBy( SteamId steamid ) + { + this.steamid = steamid; + return this; + } + + public Query CreatedByMe() + { + this.steamid = SteamClient.SteamId; + return this; + } + + public Query WherePublished() { userType = UserUGCList.Published; return this; } + public Query SortByCreationDate() { userSort = UserUGCListSortOrder.CreationOrderDesc; return this; } + public Query SortByCreationDateAsc() { userSort = UserUGCListSortOrder.CreationOrderAsc; return this; } + public Query SortByTitleAsc() { userSort = UserUGCListSortOrder.TitleAsc; return this; } + public Query SortByUpdateDate() { userSort = UserUGCListSortOrder.LastUpdatedDesc; return this; } + public Query SortBySubscriptionDate() { userSort = UserUGCListSortOrder.SubscriptionDateDesc; return this; } + public Query SortByVoteScore() { userSort = UserUGCListSortOrder.VoteScoreDesc; return this; } + public Query SortByModeration() { userSort = UserUGCListSortOrder.ForModeration; return this; } + + #endregion + + #region Files + PublishedFileId[] Files; + + public Query WithFileId( params PublishedFileId[] files ) + { + Files = files; + return this; + } + #endregion public async Task GetPageAsync( int page ) { @@ -65,7 +104,19 @@ public Query( UgcType type ) : this() if ( creatorApp == 0 ) creatorApp = consumerApp; UGCQueryHandle_t handle; - handle = SteamUGC.Internal.CreateQueryAllUGCRequest1( queryType, matchingType, creatorApp.Value, consumerApp.Value, (uint)page ); + + if ( Files != null ) + { + handle = SteamUGC.Internal.CreateQueryUGCDetailsRequest( Files, (uint)Files.Length ); + } + else if ( steamid.HasValue ) + { + handle = SteamUGC.Internal.CreateQueryUserUGCRequest( steamid.Value.AccountId, userType, matchingType, userSort, creatorApp.Value, consumerApp.Value, (uint)page ); + } + else + { + handle = SteamUGC.Internal.CreateQueryAllUGCRequest1( queryType, matchingType, creatorApp.Value, consumerApp.Value, (uint)page ); + } ApplyConstraints( handle ); @@ -87,7 +138,7 @@ public Query( UgcType type ) : this() #region SharedConstraints - public QueryType WithType( UgcType type ){ matchingType = type; return this; } + public QueryType WithType( UgcType type ) { matchingType = type; return this; } bool? WantsReturnOnlyIDs; public QueryType WithOnlyIDs( bool b ) { WantsReturnOnlyIDs = b; return this; } bool? WantsReturnKeyValueTags; @@ -109,6 +160,9 @@ public Query( UgcType type ) : this() string language; public QueryType InLanguage( string lang ) { language = lang; return this; } + int? trendDays; + public QueryType WithTrendDays( int days ) { trendDays = days; return this; } + List requiredTags; bool? matchAnyTag; List excludedTags; @@ -162,6 +216,11 @@ void ApplyConstraints( UGCQueryHandle_t handle ) { SteamUGC.Internal.SetMatchAnyTag( handle, matchAnyTag.Value ); } + + if ( trendDays.HasValue ) + { + SteamUGC.Internal.SetRankedByTrendDays( handle, (uint)trendDays.Value ); + } } #endregion diff --git a/Facepunch.Steamworks/Structs/UgcQueryFile.cs b/Facepunch.Steamworks/Structs/UgcQueryFile.cs deleted file mode 100644 index 1cf83b4..0000000 --- a/Facepunch.Steamworks/Structs/UgcQueryFile.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Steamworks.Data; -using QueryType = Steamworks.Ugc.FileQuery; - -namespace Steamworks.Ugc -{ - public struct FileQuery - { - PublishedFileId[] Files; - - public FileQuery( params PublishedFileId[] files ) : this() - { - Files = files; - } - - public async Task GetPageAsync( int page ) - { - if ( page <= 0 ) throw new System.Exception( "page should be > 0" ); - if ( Files == null ) throw new System.Exception( "Files is null" ); - if ( Files.Length == 0 ) throw new System.Exception( "Files is empty" ); - - UGCQueryHandle_t handle; - - handle = SteamUGC.Internal.CreateQueryUGCDetailsRequest( Files, (uint)Files.Length ); - - var result = await SteamUGC.Internal.SendQueryUGCRequest( handle ); - if ( !result.HasValue ) - return null; - - if ( result.Value.Result != Steamworks.Result.OK ) - return null; - - return new ResultPage - { - Handle = result.Value.Handle, - ResultCount = (int) result.Value.NumResultsReturned, - TotalCount = (int)result.Value.TotalMatchingResults, - CachedData = result.Value.CachedData - }; - } - - #region SharedConstraints - bool? WantsReturnOnlyIDs; - public QueryType WithOnlyIDs( bool b ) { WantsReturnOnlyIDs = b; return this; } - bool? WantsReturnKeyValueTags; - public QueryType WithKeyValueTag( bool b ) { WantsReturnKeyValueTags = b; return this; } - bool? WantsReturnLongDescription; - public QueryType WithLongDescription( bool b ) { WantsReturnLongDescription = b; return this; } - bool? WantsReturnMetadata; - public QueryType WithMetadata( bool b ) { WantsReturnMetadata = b; return this; } - bool? WantsReturnChildren; - public QueryType WithChildren( bool b ) { WantsReturnChildren = b; return this; } - bool? WantsReturnAdditionalPreviews; - public QueryType WithAdditionalPreviews( bool b ) { WantsReturnAdditionalPreviews = b; return this; } - bool? WantsReturnTotalOnly; - public QueryType WithTotalOnly( bool b ) { WantsReturnTotalOnly = b; return this; } - bool? WantsReturnPlaytimeStats; - public QueryType WithPlaytimeStats( bool b ) { WantsReturnPlaytimeStats = b; return this; } - int? maxCacheAge; - public QueryType AllowCachedResponse( int maxSecondsAge ) { maxCacheAge = maxSecondsAge; return this; } - string language; - public QueryType InLanguage( string lang ) { language = lang; return this; } - #endregion - - } -} \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/UgcQueryUser.cs b/Facepunch.Steamworks/Structs/UgcQueryUser.cs deleted file mode 100644 index 84ee88c..0000000 --- a/Facepunch.Steamworks/Structs/UgcQueryUser.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Steamworks.Data; -using QueryType = Steamworks.Ugc.UserQuery; - -namespace Steamworks.Ugc -{ - public struct UserQuery - { - public UserQuery( UgcType type, SteamId steamid = default ) : this() - { - if ( steamid == 0 ) - steamid = SteamClient.SteamId; - - this.steamid = steamid; - this.matchingType = type; - } - - SteamId steamid; - - UserUGCList userType; - UserUGCListSortOrder userSort; - UgcType matchingType; - - AppId consumerApp; - AppId creatorApp; - - public static UserQuery All => new UserQuery( UgcType.All, 0 ); - public static UserQuery Items => new UserQuery( UgcType.Items, 0 ); - /* - public static UserQuery ItemsMtx( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Items_Mtx }; - public static UserQuery ItemsReadyToUse( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Items_ReadyToUse }; - public static UserQuery Collections( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Collections }; - public static UserQuery Artwork( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Artwork }; - public static UserQuery Videos( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Videos }; - public static UserQuery Screenshots( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.Screenshots }; - public static UserQuery AllGuides( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.AllGuides }; - public static UserQuery WebGuides( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.WebGuides }; - public static UserQuery IntegratedGuides( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.IntegratedGuides }; - public static UserQuery UsableInGame( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.UsableInGame }; - public static UserQuery ControllerBindings( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.ControllerBindings }; - public static UserQuery GameManagedItems( SteamId steamid = default ) => new UserQuery( steamid ) { matchingType = UGCMatchingUGCType.GameManagedItems }; - */ - - public UserQuery SortByCreationDate() { userSort = UserUGCListSortOrder.CreationOrderDesc; return this; } - public UserQuery SortByCreationDateAsc() { userSort = UserUGCListSortOrder.CreationOrderAsc; return this; } - public UserQuery SortByTitleAsc() { userSort = UserUGCListSortOrder.TitleAsc; return this; } - public UserQuery SortByUpdateDate() { userSort = UserUGCListSortOrder.LastUpdatedDesc; return this; } - public UserQuery SortBySubscriptionDate() { userSort = UserUGCListSortOrder.SubscriptionDateDesc; return this; } - public UserQuery SortByVoteScore() { userSort = UserUGCListSortOrder.VoteScoreDesc; return this; } - public UserQuery SortByModeration() { userSort = UserUGCListSortOrder.ForModeration; return this; } - - - public UserQuery GetPublished() { userType = UserUGCList.Published; return this; } - - public UserQuery FromUser( SteamId steamid ) - { - this.steamid = steamid; - return this; - } - - public UserQuery FromSelf() - { - this.steamid = SteamClient.SteamId; - return this; - } - - - public async Task GetPageAsync( int page ) - { - if ( page <= 0 ) throw new System.Exception( "page should be > 0" ); - - if ( consumerApp == 0 ) consumerApp = SteamClient.AppId; - if ( creatorApp == 0 ) creatorApp = consumerApp; - - UGCQueryHandle_t handle; - - handle = SteamUGC.Internal.CreateQueryUserUGCRequest( steamid.AccountId, userType, matchingType, userSort, creatorApp.Value, consumerApp.Value, (uint)page ); - - ApplyConstraints( handle ); - - var result = await SteamUGC.Internal.SendQueryUGCRequest( handle ); - if ( !result.HasValue ) - return null; - - if ( result.Value.Result != Steamworks.Result.OK ) - return null; - - return new ResultPage - { - Handle = result.Value.Handle, - ResultCount = (int) result.Value.NumResultsReturned, - TotalCount = (int)result.Value.TotalMatchingResults, - CachedData = result.Value.CachedData - }; - } - - #region SharedConstraints - public QueryType WithType( UgcType type ) { matchingType = type; return this; } - bool? WantsReturnOnlyIDs; - public QueryType WithOnlyIDs( bool b ) { WantsReturnOnlyIDs = b; return this; } - bool? WantsReturnKeyValueTags; - public QueryType WithKeyValueTag( bool b ) { WantsReturnKeyValueTags = b; return this; } - bool? WantsReturnLongDescription; - public QueryType WithLongDescription( bool b ) { WantsReturnLongDescription = b; return this; } - bool? WantsReturnMetadata; - public QueryType WithMetadata( bool b ) { WantsReturnMetadata = b; return this; } - bool? WantsReturnChildren; - public QueryType WithChildren( bool b ) { WantsReturnChildren = b; return this; } - bool? WantsReturnAdditionalPreviews; - public QueryType WithAdditionalPreviews( bool b ) { WantsReturnAdditionalPreviews = b; return this; } - bool? WantsReturnTotalOnly; - public QueryType WithTotalOnly( bool b ) { WantsReturnTotalOnly = b; return this; } - bool? WantsReturnPlaytimeStats; - public QueryType WithPlaytimeStats( bool b ) { WantsReturnPlaytimeStats = b; return this; } - int? maxCacheAge; - public QueryType AllowCachedResponse( int maxSecondsAge ) { maxCacheAge = maxSecondsAge; return this; } - string language; - public QueryType InLanguage( string lang ) { language = lang; return this; } - - List requiredTags; - bool? matchAnyTag; - List excludedTags; - Dictionary requiredKv; - - /// - /// Found items must have at least one of the defined tags - /// - public QueryType MatchAnyTag() { matchAnyTag = true; return this; } - - /// - /// Found items must have all defined tags - /// - public QueryType MatchAllTags() { matchAnyTag = false; return this; } - - public QueryType WithTag( string tag ) - { - if ( requiredTags == null ) requiredTags = new List(); - requiredTags.Add( tag ); - return this; - } - - public QueryType WithoutTag( string tag ) - { - if ( excludedTags == null ) excludedTags = new List(); - excludedTags.Add( tag ); - return this; - } - - void ApplyConstraints( UGCQueryHandle_t handle ) - { - if ( requiredTags != null ) - { - foreach ( var tag in requiredTags ) - SteamUGC.Internal.AddRequiredTag( handle, tag ); - } - - if ( excludedTags != null ) - { - foreach ( var tag in excludedTags ) - SteamUGC.Internal.AddExcludedTag( handle, tag ); - } - - if ( requiredKv != null ) - { - foreach ( var tag in requiredKv ) - SteamUGC.Internal.AddRequiredKeyValueTag( handle, tag.Key, tag.Value ); - } - - if ( matchAnyTag.HasValue ) - { - SteamUGC.Internal.SetMatchAnyTag( handle, matchAnyTag.Value ); - } - } - - #endregion - - } -} \ No newline at end of file