move client mods updates to actual steam api
All checks were successful
Build / Build Launcher (push) Successful in 2m17s

This commit is contained in:
zznty
2024-05-31 17:56:24 +07:00
parent 43a4e6f62b
commit fbf02ad716
9 changed files with 42 additions and 92 deletions

View File

@@ -1,5 +1,4 @@
using System.Reflection; using HarmonyLib;
using HarmonyLib;
using Sandbox; using Sandbox;
using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Networking; using Sandbox.Engine.Networking;
@@ -9,6 +8,7 @@ using Sandbox.Game;
using SpaceEngineers.Game; using SpaceEngineers.Game;
using SpaceEngineers.Game.Achievements; using SpaceEngineers.Game.Achievements;
using SpaceEngineers.Game.GUI; using SpaceEngineers.Game.GUI;
using Steamworks;
using VRage; using VRage;
using VRage.Audio; using VRage.Audio;
using VRage.EOS; using VRage.EOS;
@@ -36,6 +36,7 @@ public class Launcher : IDisposable
// early init for plugin loader // early init for plugin loader
ProtoBuf.Meta.RuntimeTypeModel.Create(true); ProtoBuf.Meta.RuntimeTypeModel.Create(true);
_harmony.PatchAll(typeof(Launcher).Assembly); _harmony.PatchAll(typeof(Launcher).Assembly);
SteamAPI.Init();
MyPlugins.LoadPlugins([typeof(PluginLoader.Main).Assembly]); MyPlugins.LoadPlugins([typeof(PluginLoader.Main).Assembly]);
PluginLoader.Main.Instance.Splash?.SetText("Initializing game..."); PluginLoader.Main.Instance.Splash?.SetText("Initializing game...");
PluginLoader.Main.Instance.Splash?.SetBarValue(0); PluginLoader.Main.Instance.Splash?.SetBarValue(0);

View File

@@ -1,4 +1,3 @@
using System.Reflection.Emit;
using HarmonyLib; using HarmonyLib;
using Sandbox.Game.World; using Sandbox.Game.World;

View File

@@ -1,30 +1,4 @@
using System.Collections.Immutable; namespace CringeLauncher.Patches;
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;
#if false #if false
[HarmonyPatch] [HarmonyPatch]

View File

@@ -1,7 +1,5 @@
using HarmonyLib; using HarmonyLib;
using VRage.Library.Collections;
using VRage.Network; using VRage.Network;
using VRage.Utils;
namespace CringeLauncher.Patches; namespace CringeLauncher.Patches;

View File

@@ -1,6 +1,5 @@
using System.Reflection; using System.Reflection;
using HarmonyLib; using HarmonyLib;
using MonoMod.Utils;
namespace CringeLauncher.Utils; namespace CringeLauncher.Utils;

View File

@@ -1,10 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Windows.UI.Popups; using Windows.UI.Popups;
using HarmonyLib;
using NLog; using NLog;
using PluginLoader.SEPM; using PluginLoader.SEPM;
using Sandbox; using Sandbox;

View File

@@ -1,6 +1,4 @@
using System.Net; namespace PluginLoader.Network;
namespace PluginLoader.Network;
public static class GitHub public static class GitHub
{ {

View File

@@ -5,7 +5,6 @@ using System.Xml.Serialization;
using PluginLoader.Data; using PluginLoader.Data;
using PluginLoader.Network; using PluginLoader.Network;
using ProtoBuf; using ProtoBuf;
using ProtoBuf.Meta;
namespace PluginLoader; namespace PluginLoader;

View File

@@ -1,18 +1,10 @@
using System.Reflection; using NLog;
using System.Text;
using HarmonyLib;
using Sandbox.Engine.Networking;
using Steamworks; using Steamworks;
using VRage.Game;
using VRage.GameServices;
using VRage.Utils;
namespace PluginLoader; namespace PluginLoader;
public static class SteamAPI public static class SteamAPI
{ {
private static MethodInfo? _downloadModsBlocking;
public static bool IsSubscribed(ulong id) public static bool IsSubscribed(ulong id)
{ {
var state = (EItemState)SteamUGC.GetItemState(new(id)); var state = (EItemState)SteamUGC.GetItemState(new(id));
@@ -27,52 +19,45 @@ public static class SteamAPI
public static void Update(IEnumerable<ulong> ids) public static void Update(IEnumerable<ulong> ids)
{ {
var enumerable = ids as ulong[] ?? ids.ToArray(); var enumerable = ids as ulong[] ?? ids.ToArray();
if (!enumerable.Any())
return; LogFile.Log.Info("Updating {Count} workshop items", enumerable.Length);
var modItems = try
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(); 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); 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;
} }
} }