Files
sbox-public/engine/Sandbox.Engine/Systems/Filesystem/Storage/Storage.Query.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

147 lines
4.0 KiB
C#

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,
};
/// <summary>
/// Query the Steam Workshop for items
/// </summary>
public class Query
{
/// <summary>
/// Tags that the item must have all of to be included in results.
/// </summary>
public HashSet<string> TagsRequired { get; set; } = new( StringComparer.OrdinalIgnoreCase );
/// <summary>
/// Tags that the item must not have any of to be included in results.
/// </summary>
public HashSet<string> TagsExcluded { get; set; } = new( StringComparer.OrdinalIgnoreCase );
/// <summary>
/// KeyValues that the item must match to be included in results.
/// </summary>
public Dictionary<string, string> KeyValues { get; set; } = new( StringComparer.OrdinalIgnoreCase );
/// <summary>
/// Search Text
/// </summary>
public string SearchText { get; set; }
/// <summary>
/// Max Cache Age in seconds
/// </summary>
public int MaxCacheAge { get; set; }
/// <summary>
/// Sort Order
/// </summary>
public SortOrder SortOrder { get; set; } = SortOrder.RankedByVote;
/// <summary>
/// Number of days to consider for rank trend calculations
/// </summary>
public int RankTrendDays { get; set; } = 30;
/// <summary>
/// Run the query
/// </summary>
public async Task<QueryResult> 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<QueryResult>( resultJson );
}
}
/// <summary>
/// The results of a Steam Workshop query
/// </summary>
public class QueryResult
{
public int ResultCount { get; set; }
public int TotalCount { get; set; }
public string NextCursor { get; set; }
public List<QueryItem> Items { get; set; }
}
/// <summary>
/// Details about a UGC item returned from a Steam Workshop query
/// </summary>
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<string> Tags { get; set; }
public Dictionary<string, string> KeyValues { get; set; }
/// <summary>
/// Install this item. This can return null if it's not of the right format.
/// </summary>
public async Task<Entry> Install( CancellationToken token = default )
{
var fs = await InstallWorkshopFile( Id, token );
if ( fs == null ) return null;
return CreateEntryFromFileSystem( fs );
}
}
}