Skip to content

Commit 714824d

Browse files
committed
Fix start/stop buttons, improve DS init order, add console tab
1 parent 2cb9210 commit 714824d

10 files changed

Lines changed: 304 additions & 122 deletions

File tree

‎Torch.Server/Initializer.cs‎

Lines changed: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12+
using System.Windows;
1213
using System.Windows.Threading;
1314
using NLog;
1415
using Torch.Utils;
@@ -84,27 +85,21 @@ public bool Initialize(string[] args)
8485
public void Run()
8586
{
8687
_server = new TorchServer(_config);
88+
8789
try
8890
{
89-
90-
var initTask = Task.Run(() => { _server.Init(); });
91+
_server.Init();
9192
if (!_config.NoGui)
9293
{
9394
_server.Init();
9495

95-
if (!_config.NoGui)
96-
{
97-
var ui = new TorchUI(_server);
98-
if (_config.Autostart)
99-
_server.Start();
100-
ui.ShowDialog();
101-
}
102-
else
103-
_server.Start();
96+
if (_config.Autostart)
97+
Task.Run(() => _server.Start());
98+
99+
new TorchUI(_server).ShowDialog();
104100
}
105101
else
106102
{
107-
initTask.Wait();
108103
_server.Start();
109104
}
110105
}
@@ -116,101 +111,105 @@ public void Run()
116111
}
117112
}
118113

119-
private TorchConfig InitConfig()
114+
private TorchConfig InitConfig()
115+
{
116+
var configName = "Torch.cfg";
117+
var configPath = Path.Combine(Directory.GetCurrentDirectory(), configName);
118+
if (File.Exists(configName))
120119
{
121-
var configName = "Torch.cfg";
122-
var configPath = Path.Combine(Directory.GetCurrentDirectory(), configName);
123-
if (File.Exists(configName))
124-
{
125-
Log.Info($"Loading config {configPath}");
126-
return TorchConfig.LoadFrom(configPath);
127-
}
128-
else
129-
{
130-
Log.Info($"Generating default config at {configPath}");
131-
var config = new TorchConfig {InstancePath = Path.GetFullPath("Instance")};
132-
config.Save(configPath);
133-
return config;
134-
}
120+
Log.Info($"Loading config {configPath}");
121+
return TorchConfig.LoadFrom(configPath);
135122
}
136-
137-
private static void RunSteamCmd()
123+
else
138124
{
139-
var log = LogManager.GetLogger("SteamCMD");
125+
Log.Info($"Generating default config at {configPath}");
126+
var config = new TorchConfig {InstancePath = Path.GetFullPath("Instance")};
127+
config.Save(configPath);
128+
return config;
129+
}
130+
}
140131

141-
if (!Directory.Exists(STEAMCMD_DIR))
142-
{
143-
Directory.CreateDirectory(STEAMCMD_DIR);
144-
}
132+
private static void RunSteamCmd()
133+
{
134+
var log = LogManager.GetLogger("SteamCMD");
145135

146-
if (!File.Exists(RUNSCRIPT_PATH))
147-
File.WriteAllText(RUNSCRIPT_PATH, RUNSCRIPT);
136+
if (!Directory.Exists(STEAMCMD_DIR))
137+
{
138+
Directory.CreateDirectory(STEAMCMD_DIR);
139+
}
140+
141+
if (!File.Exists(RUNSCRIPT_PATH))
142+
File.WriteAllText(RUNSCRIPT_PATH, RUNSCRIPT);
148143

149-
if (!File.Exists(STEAMCMD_PATH))
144+
if (!File.Exists(STEAMCMD_PATH))
145+
{
146+
try
150147
{
151-
try
152-
{
153-
log.Info("Downloading SteamCMD.");
154-
using (var client = new WebClient())
155-
client.DownloadFile("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip", STEAMCMD_ZIP);
148+
log.Info("Downloading SteamCMD.");
149+
using (var client = new WebClient())
150+
client.DownloadFile("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip", STEAMCMD_ZIP);
156151

157-
ZipFile.ExtractToDirectory(STEAMCMD_ZIP, STEAMCMD_DIR);
158-
File.Delete(STEAMCMD_ZIP);
159-
log.Info("SteamCMD downloaded successfully!");
160-
}
161-
catch
162-
{
163-
log.Error("Failed to download SteamCMD, unable to update the DS.");
164-
return;
165-
}
152+
ZipFile.ExtractToDirectory(STEAMCMD_ZIP, STEAMCMD_DIR);
153+
File.Delete(STEAMCMD_ZIP);
154+
log.Info("SteamCMD downloaded successfully!");
166155
}
167-
168-
log.Info("Checking for DS updates.");
169-
var steamCmdProc = new ProcessStartInfo(STEAMCMD_PATH, "+runscript runscript.txt")
170-
{
171-
WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), STEAMCMD_DIR),
172-
UseShellExecute = false,
173-
RedirectStandardOutput = true,
174-
StandardOutputEncoding = Encoding.ASCII
175-
};
176-
var cmd = Process.Start(steamCmdProc);
177-
178-
// ReSharper disable once PossibleNullReferenceException
179-
while (!cmd.HasExited)
156+
catch
180157
{
181-
log.Info(cmd.StandardOutput.ReadLine());
182-
Thread.Sleep(100);
158+
log.Error("Failed to download SteamCMD, unable to update the DS.");
159+
return;
183160
}
184161
}
185162

186-
private void LogException(Exception ex)
163+
log.Info("Checking for DS updates.");
164+
var steamCmdProc = new ProcessStartInfo(STEAMCMD_PATH, "+runscript runscript.txt")
187165
{
188-
if (ex.InnerException != null)
189-
{
190-
LogException(ex.InnerException);
191-
}
166+
WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), STEAMCMD_DIR),
167+
UseShellExecute = false,
168+
RedirectStandardOutput = true,
169+
StandardOutputEncoding = Encoding.ASCII
170+
};
171+
var cmd = Process.Start(steamCmdProc);
172+
173+
// ReSharper disable once PossibleNullReferenceException
174+
while (!cmd.HasExited)
175+
{
176+
log.Info(cmd.StandardOutput.ReadLine());
177+
Thread.Sleep(100);
178+
}
179+
}
192180

193-
Log.Fatal(ex);
194-
if (ex is ReflectionTypeLoadException exti)
195-
foreach (Exception exl in exti.LoaderExceptions)
196-
LogException(exl);
181+
private void LogException(Exception ex)
182+
{
183+
if (ex.InnerException != null)
184+
{
185+
LogException(ex.InnerException);
197186
}
198187

199-
private void HandleException(object sender, UnhandledExceptionEventArgs e)
188+
Log.Fatal(ex);
189+
if (ex is ReflectionTypeLoadException exti)
190+
foreach (Exception exl in exti.LoaderExceptions)
191+
LogException(exl);
192+
}
193+
194+
private void HandleException(object sender, UnhandledExceptionEventArgs e)
195+
{
196+
var ex = (Exception)e.ExceptionObject;
197+
LogException(ex);
198+
LogManager.Flush();
199+
if (_config.RestartOnCrash)
200200
{
201-
var ex = (Exception)e.ExceptionObject;
202-
LogException(ex);
203-
Console.WriteLine("Exiting in 5 seconds.");
201+
Console.WriteLine("Restarting in 5 seconds.");
204202
Thread.Sleep(5000);
205-
LogManager.Flush();
206-
if (_config.RestartOnCrash)
207-
{
208-
var exe = typeof(Program).Assembly.Location;
209-
_config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
210-
Process.Start(exe, _config.ToString());
211-
}
212-
213-
Process.GetCurrentProcess().Kill();
203+
var exe = typeof(Program).Assembly.Location;
204+
_config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
205+
Process.Start(exe, _config.ToString());
214206
}
207+
else
208+
{
209+
MessageBox.Show("Torch encountered a fatal error and needs to close. Please check the logs for details.");
210+
}
211+
212+
Process.GetCurrentProcess().Kill();
215213
}
216-
}
214+
}
215+
}

‎Torch.Server/Managers/InstanceManager.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
using NLog;
1111
using Sandbox.Engine.Networking;
1212
using Sandbox.Engine.Utils;
13+
using Sandbox.Game;
14+
using Sandbox.Game.Gui;
1315
using Torch.API;
1416
using Torch.API.Managers;
1517
using Torch.Managers;
1618
using Torch.Server.ViewModels;
19+
using VRage;
1720
using VRage.FileSystem;
1821
using VRage.Game;
22+
using VRage.Game.ObjectBuilder;
1923
using VRage.ObjectBuilders;
24+
using VRage.Plugins;
2025

2126
namespace Torch.Server.Managers
2227
{
@@ -37,6 +42,8 @@ public InstanceManager(ITorchBase torchInstance) : base(torchInstance)
3742

3843
public void LoadInstance(string path, bool validate = true)
3944
{
45+
Log.Info($"Loading instance {path}");
46+
4047
if (validate)
4148
ValidateInstance(path);
4249

@@ -56,6 +63,7 @@ public void LoadInstance(string path, bool validate = true)
5663
config.Load(configPath);
5764

5865
DedicatedConfig = new ConfigDedicatedViewModel(config);
66+
5967
var worldFolders = Directory.EnumerateDirectories(Path.Combine(Torch.Config.InstancePath, "Saves"));
6068

6169
foreach (var f in worldFolders)

‎Torch.Server/MultiTextWriter.cs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Torch.Server
7+
{
8+
public class MultiTextWriter : TextWriter
9+
{
10+
private IEnumerable<TextWriter> writers;
11+
public MultiTextWriter(IEnumerable<TextWriter> writers)
12+
{
13+
this.writers = writers.ToList();
14+
}
15+
public MultiTextWriter(params TextWriter[] writers)
16+
{
17+
this.writers = writers;
18+
}
19+
20+
public override void Write(char value)
21+
{
22+
foreach (var writer in writers)
23+
writer.Write(value);
24+
}
25+
26+
public override void Write(string value)
27+
{
28+
foreach (var writer in writers)
29+
writer.Write(value);
30+
}
31+
32+
public override void Flush()
33+
{
34+
foreach (var writer in writers)
35+
writer.Flush();
36+
}
37+
38+
public override void Close()
39+
{
40+
foreach (var writer in writers)
41+
writer.Close();
42+
}
43+
44+
public override Encoding Encoding
45+
{
46+
get { return Encoding.ASCII; }
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)