[Maintenance] fix unload exceptions
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using NLog;
|
||||||
using Torch.API.Managers;
|
using Torch.API.Managers;
|
||||||
using Torch.Managers;
|
using Torch.Managers;
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ namespace Maintenance.Managers;
|
|||||||
|
|
||||||
public class MaintenanceScheduleManager(string storagePath) : IManager
|
public class MaintenanceScheduleManager(string storagePath) : IManager
|
||||||
{
|
{
|
||||||
|
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
[Manager.Dependency]
|
[Manager.Dependency]
|
||||||
private readonly MaintenanceManager _maintenanceManager = null!;
|
private readonly MaintenanceManager _maintenanceManager = null!;
|
||||||
|
|
||||||
@@ -65,6 +68,22 @@ public class MaintenanceScheduleManager(string storagePath) : IManager
|
|||||||
{
|
{
|
||||||
var token = _cancellationTokenSource.Token;
|
var token = _cancellationTokenSource.Token;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await SchedulerLoop(token);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
Log.Info("Maintenance scheduler loop was cancelled");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Fatal(e, "Exception in maintenance scheduler loop. Shutting down the scheduler.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SchedulerLoop(CancellationToken token)
|
||||||
|
{
|
||||||
var timerSecondsSection = _configManager.Configuration.GetSection(ConfigKeys.TimerBroadcastForSeconds);
|
var timerSecondsSection = _configManager.Configuration.GetSection(ConfigKeys.TimerBroadcastForSeconds);
|
||||||
var timerSeconds = timerSecondsSection.GetChildren().Select(b => b.Get<int>()).ToArray();
|
var timerSeconds = timerSecondsSection.GetChildren().Select(b => b.Get<int>()).ToArray();
|
||||||
|
|
||||||
|
@@ -31,25 +31,47 @@ public static class SteamQueryPatch
|
|||||||
{
|
{
|
||||||
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
|
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
[ReflectedSetter(Name = "Running", TypeName = "VRage.Steam.MySteamGameServer, VRage.Steam")]
|
||||||
|
private static readonly Action<IMyGameServer, bool> RunningSetter = null!;
|
||||||
|
|
||||||
[ReflectedMethodInfo(null, "SteamServerEntryPoint", TypeName = "VRage.Steam.MySteamGameServer, VRage.Steam")]
|
[ReflectedMethodInfo(null, "SteamServerEntryPoint", TypeName = "VRage.Steam.MySteamGameServer, VRage.Steam")]
|
||||||
private static readonly MethodInfo EntryPointMethod = null!;
|
private static readonly MethodInfo EntryPointMethod = null!;
|
||||||
|
|
||||||
[ReflectedMethodInfo(typeof(SteamQueryPatch), nameof(Prefix))]
|
[ReflectedMethodInfo(typeof(SteamQueryPatch), nameof(EntryPointPrefix))]
|
||||||
private static readonly MethodInfo PrefixMethod = null!;
|
private static readonly MethodInfo EntryPointPrefixMethod = null!;
|
||||||
|
|
||||||
|
[ReflectedMethodInfo(null, "Shutdown", TypeName = "VRage.Steam.MySteamGameServer, VRage.Steam")]
|
||||||
|
private static readonly MethodInfo ShutdownMethod = null!;
|
||||||
|
|
||||||
|
[ReflectedMethodInfo(typeof(SteamQueryPatch), nameof(ShutdownPrefix))]
|
||||||
|
private static readonly MethodInfo ShutdownPrefixMethod = null!;
|
||||||
|
|
||||||
#pragma warning disable CS0618 // Type or member is obsolete
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
private static ITorchBase Torch => TorchBase.Instance;
|
private static ITorchBase Torch => TorchBase.Instance;
|
||||||
#pragma warning restore CS0618 // Type or member is obsolete
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
|
|
||||||
|
private static readonly ManualResetEventSlim ExitEvent = new(false);
|
||||||
|
|
||||||
private static bool MaintenanceEnabled =>
|
private static bool MaintenanceEnabled =>
|
||||||
Torch.CurrentSession?.Managers.GetManager<MaintenanceManager>().MaintenanceEnabled is true;
|
Torch.CurrentSession?.Managers.GetManager<MaintenanceManager>().MaintenanceEnabled is true;
|
||||||
|
|
||||||
public static void Patch(PatchContext context)
|
public static void Patch(PatchContext context)
|
||||||
{
|
{
|
||||||
context.GetPattern(EntryPointMethod).Prefixes.Add(PrefixMethod);
|
context.GetPattern(EntryPointMethod).Prefixes.Add(EntryPointPrefixMethod);
|
||||||
|
context.GetPattern(ShutdownMethod).Prefixes.Add(ShutdownPrefixMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool Prefix(IMyGameServer __instance, object argument)
|
private static bool ShutdownPrefix(IMyGameServer __instance, Thread __field_m_serverThread)
|
||||||
|
{
|
||||||
|
if (!__instance.Running) return false;
|
||||||
|
|
||||||
|
RunningSetter(__instance, false);
|
||||||
|
ExitEvent.Wait();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool EntryPointPrefix(IMyGameServer __instance, object argument)
|
||||||
{
|
{
|
||||||
var socket = (Socket)argument;
|
var socket = (Socket)argument;
|
||||||
RunServerAsync(__instance, socket);
|
RunServerAsync(__instance, socket);
|
||||||
@@ -142,6 +164,11 @@ public static class SteamQueryPatch
|
|||||||
await socket.SendToAsync(new(buffer, 0, length), SocketFlags.None, remoteEndPoint);
|
await socket.SendToAsync(new(buffer, 0, length), SocketFlags.None, remoteEndPoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
socket.Dispose();
|
||||||
|
GameServer.Shutdown();
|
||||||
|
|
||||||
|
ExitEvent.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
@@ -2,5 +2,5 @@
|
|||||||
<PluginManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
<PluginManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<Name>Maintenance</Name>
|
<Name>Maintenance</Name>
|
||||||
<Guid>42AF9955-AAA7-442F-9BF4-AAC4FA4A923F</Guid>
|
<Guid>42AF9955-AAA7-442F-9BF4-AAC4FA4A923F</Guid>
|
||||||
<Version>v1.0.2</Version>
|
<Version>v1.0.3</Version>
|
||||||
</PluginManifest>
|
</PluginManifest>
|
Reference in New Issue
Block a user