using Sandbox.Services.Players; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; namespace Sandbox; public static partial class Storage { public enum SortOrder { RankedByVote = 0, RankedByPublicationDate = 1, AcceptedForGameRankedByAcceptanceDate = 2, RankedByTrend = 3, FavoritedByFriendsRankedByPublicationDate = 4, CreatedByFriendsRankedByPublicationDate = 5, RankedByNumTimesReported = 6, CreatedByFollowedUsersRankedByPublicationDate = 7, NotYetRated = 8, RankedByTotalVotesAsc = 9, RankedByVotesUp = 10, RankedByTextSearch = 11, RankedByTotalUniqueSubscriptions = 12, RankedByPlaytimeTrend = 13, RankedByTotalPlaytime = 14, RankedByAveragePlaytimeTrend = 15, RankedByLifetimeAveragePlaytime = 16, RankedByPlaytimeSessionsTrend = 17, RankedByLifetimePlaytimeSessions = 18, RankedByLastUpdatedDate = 19, }; /// /// Query the Steam Workshop for items /// public class Query { /// /// Tags that the item must have all of to be included in results. /// public HashSet TagsRequired { get; set; } = new( StringComparer.OrdinalIgnoreCase ); /// /// Tags that the item must not have any of to be included in results. /// public HashSet TagsExcluded { get; set; } = new( StringComparer.OrdinalIgnoreCase ); /// /// KeyValues that the item must match to be included in results. /// public Dictionary KeyValues { get; set; } = new( StringComparer.OrdinalIgnoreCase ); /// /// Search Text /// public string SearchText { get; set; } /// /// Max Cache Age in seconds /// public int MaxCacheAge { get; set; } /// /// Sort Order /// public SortOrder SortOrder { get; set; } = SortOrder.RankedByVote; /// /// Number of days to consider for rank trend calculations /// public int RankTrendDays { get; set; } = 30; /// /// Run the query /// public async Task Run( CancellationToken token = default ) { var json = JsonSerializer.Serialize( this ); using var q = NativeEngine.CUgcQuery.CreateQuery( json, "" ); while ( !q.m_complete ) { await Task.Delay( 100, token ); } var resultJson = q.GetResultJson(); return Json.Deserialize( resultJson ); } } /// /// The results of a Steam Workshop query /// public class QueryResult { public int ResultCount { get; set; } public int TotalCount { get; set; } public string NextCursor { get; set; } public List Items { get; set; } } /// /// Details about a UGC item returned from a Steam Workshop query /// public class QueryItem { public ulong Id { get; set; } public string Title { get; set; } public string Description { get; set; } public int Visibility { get; set; } public bool Banned { get; set; } public bool Accepted { get; set; } public ulong FileHandle { get; set; } public string Preview { get; set; } public string Filename { get; set; } public ulong Size { get; set; } public string Url { get; set; } public int VotesUp { get; set; } public int VotesDown { get; set; } public float Score { get; set; } public string Metadata { get; set; } public Profile Owner { get; set; } [JsonConverter( typeof( UnixTimestampConverter ) )] public DateTimeOffset Created { get; set; } [JsonConverter( typeof( UnixTimestampConverter ) )] public DateTimeOffset Updated { get; set; } public List Tags { get; set; } public Dictionary KeyValues { get; set; } /// /// Install this item. This can return null if it's not of the right format. /// public async Task Install( CancellationToken token = default ) { var fs = await InstallWorkshopFile( Id, token ); if ( fs == null ) return null; return CreateEntryFromFileSystem( fs ); } } }