using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Steamworks.Data; namespace Steamworks { /// /// Functions for accessing and manipulating Steam user information. /// This is also where the APIs for Steam Voice are exposed. /// public static class SteamUGC { static ISteamUGC _internal; internal static ISteamUGC Internal { get { if ( _internal == null ) { _internal = new ISteamUGC(); _internal.Init(); } return _internal; } } internal static void Shutdown() { _internal = null; } internal static void InstallEvents(bool server=false) { ItemInstalled_t.Install(x => { if (x.AppID == SteamClient.AppId) { GlobalOnItemInstalled?.Invoke(x.PublishedFileId); if (onItemInstalled?.ContainsKey(x.PublishedFileId) ?? false) { onItemInstalled[x.PublishedFileId]?.Invoke(); onItemInstalled.Remove(x.PublishedFileId); } } }, server); } public static async Task DeleteFileAsync( PublishedFileId fileId ) { var r = await Internal.DeleteItem( fileId ); return r?.Result == Result.OK; } public static bool Download( PublishedFileId fileId, Action onInstalled = null, bool highPriority = false ) { if (onInstalled != null) { onItemInstalled ??= new Dictionary(); if (!onItemInstalled.ContainsKey(fileId)) { onItemInstalled.Add(fileId, onInstalled); } else { onItemInstalled[fileId] += onInstalled; } } return Internal.DownloadItem( fileId, highPriority ); } /// /// Utility function to fetch a single item. Internally this uses Ugc.FileQuery - /// which you can use to query multiple items if you need to. /// public static async Task QueryFileAsync( PublishedFileId fileId ) { var result = await Ugc.Query.All .WithFileId( fileId ) .GetPageAsync( 1 ); if ( !result.HasValue || result.Value.ResultCount != 1 ) return null; var item = result.Value.Entries.First(); result.Value.Dispose(); return item; } private static Dictionary onItemInstalled; public static event Action GlobalOnItemInstalled; public static uint NumSubscribedItems { get { return Internal.GetNumSubscribedItems(); } } } }