diff --git a/CringeLauncher/Launcher.cs b/CringeLauncher/Launcher.cs index 37518ad..f41d2d7 100644 --- a/CringeLauncher/Launcher.cs +++ b/CringeLauncher/Launcher.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using HarmonyLib; +using HarmonyLib; using Sandbox; using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Networking; @@ -9,6 +8,7 @@ using Sandbox.Game; using SpaceEngineers.Game; using SpaceEngineers.Game.Achievements; using SpaceEngineers.Game.GUI; +using Steamworks; using VRage; using VRage.Audio; using VRage.EOS; @@ -36,6 +36,7 @@ public class Launcher : IDisposable // early init for plugin loader ProtoBuf.Meta.RuntimeTypeModel.Create(true); _harmony.PatchAll(typeof(Launcher).Assembly); + SteamAPI.Init(); MyPlugins.LoadPlugins([typeof(PluginLoader.Main).Assembly]); PluginLoader.Main.Instance.Splash?.SetText("Initializing game..."); PluginLoader.Main.Instance.Splash?.SetBarValue(0); diff --git a/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs b/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs index ff9055b..2f046bb 100644 --- a/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs +++ b/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs @@ -1,4 +1,3 @@ -using System.Reflection.Emit; using HarmonyLib; using Sandbox.Game.World; diff --git a/CringeLauncher/Patches/ModScriptCompilerPatch.cs b/CringeLauncher/Patches/ModScriptCompilerPatch.cs index 2dcc1a8..d40dd31 100644 --- a/CringeLauncher/Patches/ModScriptCompilerPatch.cs +++ b/CringeLauncher/Patches/ModScriptCompilerPatch.cs @@ -1,30 +1,4 @@ -using System.Collections.Immutable; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.Loader; -using System.Text; -using HarmonyLib; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Emit; -using NLog; -using Sandbox; -using Sandbox.Game; -using Sandbox.Game.Entities.Blocks; -using Sandbox.Game.EntityComponents; -using Sandbox.Game.Gui; -using Sandbox.Game.Localization; -using Sandbox.Game.World; -using Sandbox.Graphics.GUI; -using Sandbox.ModAPI; -using Sandbox.ModAPI.Ingame; -using VRage; -using VRage.ModAPI; -using VRage.Scripting; -using Message = VRage.Scripting.Message; - -namespace CringeLauncher.Patches; +namespace CringeLauncher.Patches; #if false [HarmonyPatch] diff --git a/CringeLauncher/Patches/TypeTableRegisterPatch.cs b/CringeLauncher/Patches/TypeTableRegisterPatch.cs index 2d2355b..9824653 100644 --- a/CringeLauncher/Patches/TypeTableRegisterPatch.cs +++ b/CringeLauncher/Patches/TypeTableRegisterPatch.cs @@ -1,7 +1,5 @@ using HarmonyLib; -using VRage.Library.Collections; using VRage.Network; -using VRage.Utils; namespace CringeLauncher.Patches; diff --git a/CringeLauncher/Utils/MethodTools.cs b/CringeLauncher/Utils/MethodTools.cs index d2481c9..adea0c2 100644 --- a/CringeLauncher/Utils/MethodTools.cs +++ b/CringeLauncher/Utils/MethodTools.cs @@ -1,6 +1,5 @@ using System.Reflection; using HarmonyLib; -using MonoMod.Utils; namespace CringeLauncher.Utils; diff --git a/PluginLoader/LoaderTools.cs b/PluginLoader/LoaderTools.cs index 72c6611..6af56ef 100644 --- a/PluginLoader/LoaderTools.cs +++ b/PluginLoader/LoaderTools.cs @@ -1,10 +1,7 @@ using System.Diagnostics; -using System.Reflection; -using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; using Windows.UI.Popups; -using HarmonyLib; using NLog; using PluginLoader.SEPM; using Sandbox; diff --git a/PluginLoader/Network/GitHub.cs b/PluginLoader/Network/GitHub.cs index b6b7442..226ed97 100644 --- a/PluginLoader/Network/GitHub.cs +++ b/PluginLoader/Network/GitHub.cs @@ -1,6 +1,4 @@ -using System.Net; - -namespace PluginLoader.Network; +namespace PluginLoader.Network; public static class GitHub { diff --git a/PluginLoader/PluginList.cs b/PluginLoader/PluginList.cs index bb64524..539844c 100644 --- a/PluginLoader/PluginList.cs +++ b/PluginLoader/PluginList.cs @@ -5,7 +5,6 @@ using System.Xml.Serialization; using PluginLoader.Data; using PluginLoader.Network; using ProtoBuf; -using ProtoBuf.Meta; namespace PluginLoader; diff --git a/PluginLoader/SteamAPI.cs b/PluginLoader/SteamAPI.cs index 1cbb059..2d71f61 100644 --- a/PluginLoader/SteamAPI.cs +++ b/PluginLoader/SteamAPI.cs @@ -1,18 +1,10 @@ -using System.Reflection; -using System.Text; -using HarmonyLib; -using Sandbox.Engine.Networking; +using NLog; 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)); @@ -27,52 +19,45 @@ public static class SteamAPI public static void Update(IEnumerable ids) { var enumerable = ids as ulong[] ?? ids.ToArray(); - if (!enumerable.Any()) - return; + + LogFile.Log.Info("Updating {Count} workshop items", enumerable.Length); - var modItems = - new List( - 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) + try { - MyGameService.Update(); + 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); + }); + + 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); } - - 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 mods) - { - // Source: MyWorkshop.DownloadWorldModsBlockingInternal - - MyLog.Default.IncreaseIndent(); - - var list = new List(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; } } \ No newline at end of file