Skip to content

Commit 8f918d4

Browse files
authored
Changes so that torch update happens ASAP at startup. (#552)
* Changes so that torch update happens ASAP at startup. This should fix our problems with needing manual updates after some game updates. * revert back to use _fsManager * Wait for update process to complete before moving on to SE stuff.
1 parent efa2a80 commit 8f918d4

7 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎Torch.Server/Managers/InstanceManager.cs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public class InstanceManager : Manager
3737
public event Action<ConfigDedicatedViewModel> InstanceLoaded;
3838
public ConfigDedicatedViewModel DedicatedConfig { get; set; }
3939
private static readonly Logger Log = LogManager.GetLogger(nameof(InstanceManager));
40-
[Dependency]
41-
private FilesystemManager _filesystemManager;
42-
40+
4341
public InstanceManager(ITorchBase torchInstance) : base(torchInstance)
4442
{
4543

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO.Packaging;
66
using System.Linq;
77
using System.Net;
8+
using System.Reflection;
89
using System.Text;
910
using System.Text.RegularExpressions;
1011
using System.Threading;
@@ -23,26 +24,25 @@ public class UpdateManager : Manager
2324
private Timer _updatePollTimer;
2425
private string _torchDir = new FileInfo(typeof(UpdateManager).Assembly.Location).DirectoryName;
2526
private Logger _log = LogManager.GetCurrentClassLogger();
26-
[Dependency]
2727
private FilesystemManager _fsManager;
28+
private bool _isProcessing = false;
2829

2930
public UpdateManager(ITorchBase torchInstance) : base(torchInstance)
3031
{
31-
//_updatePollTimer = new Timer(TimerElapsed, this, TimeSpan.Zero, TimeSpan.FromMinutes(5));
32+
//fs is only really used here so might as well create it here
33+
_fsManager = new FilesystemManager(torchInstance);
3234
}
3335

3436
/// <inheritdoc />
3537
public override void Attach()
3638
{
37-
CheckAndUpdateTorch();
3839
}
3940

4041
private void TimerElapsed(object state)
4142
{
42-
CheckAndUpdateTorch();
4343
}
4444

45-
private async void CheckAndUpdateTorch()
45+
public async void CheckAndUpdateTorch()
4646
{
4747
if (Torch.Config.NoUpdate || !Torch.Config.GetTorchUpdates || (Torch.Config.BranchName == TorchBranchType.dev))
4848
return;
@@ -53,6 +53,8 @@ private async void CheckAndUpdateTorch()
5353

5454
try
5555
{
56+
_log.Info("Checking for Torch Update...");
57+
_isProcessing = true;
5658
var job = await JenkinsQuery.Instance.GetLatestVersion(Torch.Config.BranchName.ToString());
5759
if (job == null)
5860
{
@@ -68,21 +70,26 @@ private async void CheckAndUpdateTorch()
6870
if (!await JenkinsQuery.Instance.DownloadRelease(job, updateName))
6971
{
7072
_log.Warn("Failed to download new release!");
73+
_isProcessing = false;
7174
return;
7275
}
7376
UpdateFromZip(updateName, _torchDir);
7477
File.Delete(updateName);
75-
_log.Warn($"Torch version {job.Version} has been installed. Please restart to apply update.");
78+
_log.Warn($"Torch version {job.Version} has been installed. Restarting...");
79+
Torch.Restart();
7680
}
7781
else
7882
{
7983
_log.Info("Torch is up to date.");
8084
}
85+
_isProcessing = false;
86+
8187
}
8288
catch (Exception e)
8389
{
8490
_log.Error("An error occured downloading the Torch update.");
8591
_log.Error(e);
92+
_isProcessing = false;
8693
}
8794
}
8895

@@ -105,6 +112,15 @@ private void UpdateFromZip(string zipFile, string extractPath)
105112
//zip.ExtractToDirectory(extractPath); //throws exceptions sometimes?
106113
}
107114
}
115+
116+
/// <summary>
117+
/// Returns true if update data is being checked or processed.
118+
/// </summary>
119+
/// <returns></returns>
120+
public bool GetIsUpdating()
121+
{
122+
return _isProcessing;
123+
}
108124

109125
/// <inheritdoc />
110126
public override void Detach()

‎Torch.Server/Torch.Server.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Reference Include="System.Configuration.Install" />
147147
<Reference Include="System.Data" />
148148
<Reference Include="System.Drawing" />
149+
<Reference Include="System.IO.Compression" />
149150
<Reference Include="System.IO.Compression.FileSystem" />
150151
<Reference Include="System.Runtime.Serialization" />
151152
<Reference Include="System.Security.AccessControl, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
@@ -249,6 +250,7 @@
249250
<Compile Include="Managers\MultiplayerManagerDedicatedEventShim.cs" />
250251
<Compile Include="Managers\MultiplayerManagerDedicatedPatchShim.cs" />
251252
<Compile Include="Managers\RemoteAPIManager.cs" />
253+
<Compile Include="Managers\UpdateManager.cs" />
252254
<Compile Include="NativeMethods.cs" />
253255
<Compile Include="Initializer.cs" />
254256
<Compile Include="Patches\PromotePatch.cs" />

‎Torch.Server/TorchServer.cs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Torch.API.Managers;
2020
using Torch.API.Session;
2121
using Torch.Commands;
22+
using Torch.Managers;
2223
using Torch.Managers.PatchManager;
2324
using Torch.Mod;
2425
using Torch.Mod.Messages;
@@ -65,7 +66,10 @@ public TorchServer(TorchConfig config) : base(config)
6566
AddManager(DedicatedInstance);
6667
AddManager(new EntityControlManager(this));
6768
AddManager(new RemoteAPIManager(this));
68-
69+
AddManager(new UpdateManager(this));
70+
71+
Managers.GetManager<UpdateManager>().CheckAndUpdateTorch();
72+
6973
var sessionManager = Managers.GetManager<ITorchSessionManager>();
7074
sessionManager.AddFactory(x => new MultiplayerManagerDedicated(this));
7175

@@ -140,6 +144,13 @@ public float SimulationRatio
140144
/// <inheritdoc />
141145
public override void Init()
142146
{
147+
var updateManager = Managers.GetManager<UpdateManager>();
148+
149+
while (updateManager.GetIsUpdating())
150+
{
151+
//wait until complete, its jank, I know but eventually there will be a stand-alone updater.
152+
}
153+
143154
Log.Info("Initializing server");
144155
MySandboxGame.IsDedicated = true;
145156
base.Init();
@@ -207,15 +218,19 @@ public override void Restart(bool save = true)
207218
void DoRestart(Task<GameSaveResult> task, object torch0)
208219
{
209220
var torch = (TorchServer)torch0;
210-
torch.Stop();
211-
// TODO clone this
212221
var config = (TorchConfig)torch.Config;
222+
223+
if (IsRunning)
224+
{
225+
config.TempAutostart = true;
226+
torch.Stop();
227+
}
228+
213229
LogManager.Flush();
214230

215231
string exe = Assembly.GetExecutingAssembly().Location;
216232
Debug.Assert(exe != null);
217233
config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
218-
config.TempAutostart = true;
219234
Process.Start(exe, config.ToString());
220235

221236
Process.GetCurrentProcess().Kill();

‎Torch/Managers/FilesystemManager.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Torch.Managers
1111
{
12-
public class FilesystemManager : Manager
12+
public class FilesystemManager
1313
{
1414
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
1515
/// <summary>
@@ -22,7 +22,7 @@ public class FilesystemManager : Manager
2222
/// </summary>
2323
public string TorchDirectory { get; }
2424

25-
public FilesystemManager(ITorchBase torchInstance) : base(torchInstance)
25+
public FilesystemManager(ITorchBase torchInstance)
2626
{
2727
var torch = new FileInfo(typeof(FilesystemManager).Assembly.Location).Directory.FullName;
2828
TempDirectory = Directory.CreateDirectory(Path.Combine(torch, "tmp")).FullName;

‎Torch/Torch.csproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@
268268
<Compile Include="Managers\Manager.cs" />
269269
<Compile Include="Managers\NetworkManager\NetworkManager.cs" />
270270
<Compile Include="Managers\MultiplayerManagerBase.cs" />
271-
<Compile Include="Managers\UpdateManager.cs" />
272271
<Compile Include="Persistent.cs" />
273272
<Compile Include="Plugins\PluginManifest.cs" />
274273
<Compile Include="Utils\ModItemUtils.cs" />

‎Torch/TorchBase.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ protected TorchBase(ITorchConfig config)
163163

164164
Managers.AddManager(sessionManager);
165165
Managers.AddManager(new PatchManager(this));
166-
Managers.AddManager(new FilesystemManager(this));
167-
Managers.AddManager(new UpdateManager(this));
168166
Managers.AddManager(new EventManager(this));
169167
Managers.AddManager(Plugins);
170168
TorchAPI.Instance = this;

0 commit comments

Comments
 (0)