Files
se-launcher/PluginLoader/SteamAPI.cs
zznty fbf02ad716
All checks were successful
Build / Build Launcher (push) Successful in 2m17s
move client mods updates to actual steam api
2024-05-31 17:56:24 +07:00

63 lines
1.8 KiB
C#

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<ulong> 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<ulong> ids)
{
var count = 0;
using var callback = Callback<DownloadItemResult_t>.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);
});
var toDownload = ids.Where(b =>
(EItemState)SteamUGC.GetItemState(new(b)) != EItemState.k_EItemStateInstalled).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);
}
}
}