Skip to content

Commit 5f94174

Browse files
authored
Game update detection (#557)
* Detect any game updates and restart if found. * Change log level and change wording.
1 parent 951fe60 commit 5f94174

5 files changed

Lines changed: 122 additions & 1 deletion

File tree

‎Torch.API/ITorchConfig.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface ITorchConfig
3333
bool SendLogsToKeen { get; set; }
3434
bool DeleteMiniDumps { get; set; }
3535
string LoginToken { get; set; }
36+
bool RestartOnGameUpdate { get; set; }
37+
int GameUpdateRestartDelayMins { get; set; }
3638

3739
void Save(string path = null);
3840
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Xml.Linq;
6+
using NLog;
7+
using NLog.Fluent;
8+
using Sandbox.Game;
9+
using Torch.API;
10+
using Torch.API.Managers;
11+
using Torch.API.ModAPI;
12+
using Torch.Managers;
13+
using Torch.Managers.ChatManager;
14+
using Torch.Server;
15+
16+
namespace Torch.Server.Managers
17+
{
18+
public class GameUpdateManager : Manager
19+
{
20+
private CancellationTokenSource _cancellationTokenSource;
21+
private readonly HttpClient _httpClient;
22+
private const string UpdateCheckUrl = "https://mirror.keenswh.com/news/SpaceEngineersChangelog.xml";
23+
24+
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
25+
26+
27+
public GameUpdateManager(ITorchBase torchInstance) : base(torchInstance)
28+
{
29+
_httpClient = new HttpClient();
30+
}
31+
32+
/// <summary>
33+
/// Starts the game update manager.
34+
/// </summary>
35+
public override void Attach()
36+
{
37+
_log.Info("Starting game update manager");
38+
base.Attach();
39+
int updateIntervalSeconds = TorchBase.Instance.Config.GameUpdateRestartDelayMins * 60;
40+
41+
_cancellationTokenSource = new CancellationTokenSource();
42+
43+
Task.Run(async () =>
44+
{
45+
while (!_cancellationTokenSource.Token.IsCancellationRequested)
46+
{
47+
await CheckForUpdates();
48+
49+
await Task.Delay(TimeSpan.FromSeconds(updateIntervalSeconds), _cancellationTokenSource.Token);
50+
}
51+
}, _cancellationTokenSource.Token);
52+
}
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
public override void Detach()
58+
{
59+
_cancellationTokenSource?.Cancel();
60+
base.Detach();
61+
}
62+
63+
private async Task CheckForUpdates()
64+
{
65+
if(TorchBase.Instance.GameState != TorchGameState.Loaded || !TorchBase.Instance.Config.RestartOnGameUpdate)
66+
return;
67+
68+
var response = await _httpClient.GetStringAsync(UpdateCheckUrl);
69+
var xml = XDocument.Parse(response);
70+
71+
var latestEntry = xml.Root.Element("Entry");
72+
var latestVersion = latestEntry?.Attribute("version")?.Value;
73+
74+
var currentVersion = MyPerGameSettings.BasicGameInfo.GameVersion.Value;
75+
76+
if (latestVersion != null && int.Parse(latestVersion) > currentVersion)
77+
{
78+
_log.Warn($"Game update detectected!");
79+
try
80+
{
81+
Torch.CurrentSession?.Managers.GetManager<ChatManagerServer>()?.SendMessageAsOther("Server",
82+
$"A new version of Space Engineers is available! The server will restart in {TorchBase.Instance.Config.GameUpdateRestartDelayMins} minutes to update to version {latestVersion}.");
83+
84+
// Wait 2 minutes or until the task is cancelled
85+
await Task.Delay(TimeSpan.FromMinutes(2), _cancellationTokenSource.Token);
86+
87+
// Restart the server
88+
Torch.Restart();
89+
}
90+
catch (TaskCanceledException)
91+
{
92+
// Task was cancelled, do nothing
93+
}
94+
catch
95+
{
96+
// ignored
97+
}
98+
}
99+
}
100+
}
101+
}

‎Torch.Server/Torch.Server.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<Compile Include="FlowDocumentTarget.cs" />
246246
<Compile Include="ListBoxExtensions.cs" />
247247
<Compile Include="Managers\EntityControlManager.cs" />
248+
<Compile Include="Managers\GameUpdateManager.cs" />
248249
<Compile Include="Managers\MultiplayerManagerDedicated.cs" />
249250
<Compile Include="Managers\InstanceManager.cs" />
250251
<Compile Include="Managers\MultiplayerManagerDedicatedEventShim.cs" />

‎Torch.Server/TorchConfig.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class TorchConfig : CommandLine, ITorchConfig, INotifyPropertyChanged
2525
private string _instanceName = "Instance";
2626
private bool _autostart;
2727
private bool _restartOnCrash;
28+
private bool _restartOnGameUpdate;
29+
private int _gameUpdateRestartDelayMins = 5;
2830
private bool _noGui;
2931
private bool _getPluginUpdates = true;
3032
private bool _getTorchUpdates = true;
@@ -86,7 +88,21 @@ public string InstancePath
8688
[Arg("restartoncrash", "Automatically restart the server if it crashes.")]
8789
[Display(Name = "Restart On Crash", Description = "Automatically restart the server if it crashes.", GroupName = "Server")]
8890
public bool RestartOnCrash { get => _restartOnCrash; set => Set(value, ref _restartOnCrash); }
89-
91+
92+
/// <summary>
93+
/// Enable Game update detection. If enabled, server will restart when game updates are found.
94+
/// </summary>
95+
[Arg("gameupdatedetection", "Automatically restart the server if the game updates.")]
96+
[Display(Name = "Restart On Game Update", Description = "Automatically restart the server if the game updates.", GroupName = "Update Detection")]
97+
public bool RestartOnGameUpdate { get => _restartOnGameUpdate; set => Set(value, ref _restartOnGameUpdate); }
98+
99+
/// <summary>
100+
/// How long (in minutes) to wait before restarting after a game update is detected.
101+
/// </summary>
102+
[Arg("gameupdaterestartdelay", "How long (in minutes) to wait before restarting after a game update is detected.")]
103+
[Display(Name = "Game Update Restart Delay", Description = "How long (in minutes) to wait before restarting after a game update is detected.", GroupName = "Update Detection")]
104+
public int GameUpdateRestartDelayMins { get => _gameUpdateRestartDelayMins; set => Set(value, ref _gameUpdateRestartDelayMins); }
105+
90106
/// <inheritdoc />
91107
[Arg("nogui", "Do not show the Torch UI.")]
92108
[Display(Name = "No GUI", Description = "Do not show the Torch UI.", GroupName = "Window")]

‎Torch.Server/TorchServer.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public TorchServer(TorchConfig config) : base(config)
6767
AddManager(new EntityControlManager(this));
6868
AddManager(new RemoteAPIManager(this));
6969
AddManager(new UpdateManager(this));
70+
AddManager(new GameUpdateManager(this));
7071

7172
Managers.GetManager<UpdateManager>().CheckAndUpdateTorch();
7273

0 commit comments

Comments
 (0)