using NLog; using Steamworks; namespace PluginLoader; public static class SteamAPI { public static bool IsSubscribed(ulong id) { var state = (EItemState)SteamUGC.GetItemState(new(id)); return (state & EItemState.k_EItemStateSubscribed) == EItemState.k_EItemStateSubscribed; } public static void SubscribeToItem(ulong id) { SteamUGC.SubscribeItem(new(id)); } public static void Update(IEnumerable ids) { var enumerable = ids as ulong[] ?? ids.ToArray(); LogFile.Log.Info("Updating {Count} workshop items", enumerable.Length); try { UpdateInternal(enumerable); } catch (Exception e) { LogFile.Log.Error(e, "An error occurred while updating workshop items"); throw; } } private static void UpdateInternal(IEnumerable ids) { var count = 0; using var callback = Callback.Create(t => { if (t.m_eResult == EResult.k_EResultOK) Interlocked.Increment(ref count); LogFile.Log.Log(t.m_eResult == EResult.k_EResultOK ? LogLevel.Info : LogLevel.Error, "Download finished for {Id} with {State}", t.m_nPublishedFileId.m_PublishedFileId, t.m_eResult); }); //items could have other flags besides installed var toDownload = ids.Where(b => ((EItemState)SteamUGC.GetItemState(new(b)) & EItemState.k_EItemStateInstalled) == 0).ToArray(); foreach (var id in toDownload) { LogFile.Log.Info("Updating workshop item {Id}", id); SteamUGC.DownloadItem(new(id), true); } while (count < toDownload.Length) { Steamworks.SteamAPI.RunCallbacks(); Thread.Sleep(10); } } }