Files
se-launcher/PluginLoader/SteamAPI.cs
zznty 43a4e6f62b
All checks were successful
Build / Build Launcher (push) Successful in 2m39s
get rid of keen threading cringe
2024-05-31 17:22:27 +07:00

78 lines
2.4 KiB
C#

using System.Reflection;
using System.Text;
using HarmonyLib;
using Sandbox.Engine.Networking;
using Steamworks;
using VRage.Game;
using VRage.GameServices;
using VRage.Utils;
namespace PluginLoader;
public static class SteamAPI
{
private static MethodInfo? _downloadModsBlocking;
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();
if (!enumerable.Any())
return;
var modItems =
new List<MyObjectBuilder_Checkpoint.ModItem>(
enumerable.Select(x => new MyObjectBuilder_Checkpoint.ModItem(x, "Steam")));
LogFile.Log.Debug($"Updating {modItems.Count} workshop items");
// Source: MyWorkshop.DownloadWorldModsBlocking
var result = new MyWorkshop.ResultData();
var task = Task.Run(() => result = UpdateInternal(modItems));
while (!task.IsCompleted)
{
MyGameService.Update();
Thread.Sleep(10);
}
if (result.Result is MyGameServiceCallResult.OK) return;
if (task.Exception is not null)
{
LogFile.Log.Error(task.Exception, "An error occurred while updating workshop items");
}
else
{
LogFile.Log.Error("Unable to update workshop items due to {Result}", result.Result);
}
}
public static MyWorkshop.ResultData UpdateInternal(List<MyObjectBuilder_Checkpoint.ModItem> mods)
{
// Source: MyWorkshop.DownloadWorldModsBlockingInternal
MyLog.Default.IncreaseIndent();
var list = new List<WorkshopId>(mods.Select(x => new WorkshopId(x.PublishedFileId, x.PublishedServiceName)));
if (_downloadModsBlocking == null)
_downloadModsBlocking = AccessTools.Method(typeof(MyWorkshop), "DownloadModsBlocking");
var resultData = (MyWorkshop.ResultData)_downloadModsBlocking.Invoke(mods, new object[]
{
mods, new MyWorkshop.ResultData { Result = MyGameServiceCallResult.OK }, list, new MyWorkshop.CancelToken()
})!;
MyLog.Default.DecreaseIndent();
return resultData;
}
}