Skip to content

Commit fe5dfa0

Browse files
committed
Another restart fix and invoke tweaks
1 parent 25e6f27 commit fe5dfa0

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎Torch.API/ITorchBase.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.CompilerServices;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Torch.API.Managers;
@@ -65,18 +66,18 @@ public interface ITorchBase
6566
/// <summary>
6667
/// Invoke an action on the game thread.
6768
/// </summary>
68-
void Invoke(Action action);
69+
void Invoke(Action action, [CallerMemberName] string caller = "");
6970

7071
/// <summary>
7172
/// Invoke an action on the game thread and block until it has completed.
7273
/// If this is called on the game thread the action will execute immediately.
7374
/// </summary>
74-
void InvokeBlocking(Action action);
75+
void InvokeBlocking(Action action, [CallerMemberName] string caller = "");
7576

7677
/// <summary>
7778
/// Invoke an action on the game thread asynchronously.
7879
/// </summary>
79-
Task InvokeAsync(Action action);
80+
Task InvokeAsync(Action action, [CallerMemberName] string caller = "");
8081

8182
/// <summary>
8283
/// Start the Torch instance.

‎Torch.Server/Initializer.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ private void HandleException(object sender, UnhandledExceptionEventArgs e)
183183
LogException(ex);
184184
Console.WriteLine("Exiting in 5 seconds.");
185185
Thread.Sleep(5000);
186+
LogManager.Flush();
186187
if (_config.RestartOnCrash)
187188
{
188189
var exe = typeof(Program).Assembly.Location;
189190
_config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
190191
Process.Start(exe, _config.ToString());
191192
}
192-
//1627 = Function failed during execution.
193-
Environment.Exit(1627);
193+
Process.GetCurrentProcess().Kill();
194194
}
195195
}
196196
}

‎Torch.Server/TorchServer.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading;
1515
using System.Threading.Tasks;
1616
using Microsoft.Xml.Serialization.GeneratedAssembly;
17+
using NLog;
1718
using Sandbox.Engine.Analytics;
1819
using Sandbox.Game.Multiplayer;
1920
using Sandbox.ModAPI;
@@ -297,9 +298,11 @@ public override void Stop()
297298
MySandboxGame.Static.Exit();
298299

299300
Log.Info("Server stopped.");
301+
LogManager.Flush();
300302
_stopHandle.Set();
301303
State = ServerState.Stopped;
302304
IsRunning = false;
305+
Process.GetCurrentProcess().Kill();
303306
}
304307

305308
/// <summary>
@@ -309,9 +312,12 @@ public override void Restart()
309312
{
310313
var exe = Assembly.GetExecutingAssembly().Location;
311314
((TorchConfig)Config).WaitForPID = Process.GetCurrentProcess().Id.ToString();
315+
Config.Autostart = true;
312316
Process.Start(exe, Config.ToString());
317+
Save(0).Wait();
313318
Stop();
314-
Environment.Exit(0);
319+
LogManager.Flush();
320+
Process.GetCurrentProcess().Kill();
315321
}
316322

317323
/// <inheritdoc/>

‎Torch/Commands/TorchCommands.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ private IEnumerable RestartCountdown(int countdown)
151151
{
152152
Context.Torch.Invoke(() =>
153153
{
154-
Context.Torch.Save(0).Wait();
155154
Context.Torch.Restart();
156155
});
157156
yield break;

‎Torch/TorchBase.cs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using NLog;
12+
using ProtoBuf.Meta;
1213
using Sandbox;
1314
using Sandbox.Engine.Multiplayer;
1415
using Sandbox.Game;
@@ -191,16 +192,18 @@ public Task SaveGameAsync(Action<SaveGameStatus> callback)
191192
/// Invokes an action on the game thread.
192193
/// </summary>
193194
/// <param name="action"></param>
194-
public void Invoke(Action action)
195+
[MethodImpl(MethodImplOptions.NoInlining)]
196+
public void Invoke(Action action, [CallerMemberName] string caller = "")
195197
{
196-
MySandboxGame.Static.Invoke(action, "Torch");
198+
MySandboxGame.Static.Invoke(action, caller);
197199
}
198200

199201
/// <summary>
200202
/// Invokes an action on the game thread asynchronously.
201203
/// </summary>
202204
/// <param name="action"></param>
203-
public Task InvokeAsync(Action action)
205+
[MethodImpl(MethodImplOptions.NoInlining)]
206+
public Task InvokeAsync(Action action, [CallerMemberName] string caller = "")
204207
{
205208
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
206209
{
@@ -209,14 +212,15 @@ public Task InvokeAsync(Action action)
209212
return Task.CompletedTask;
210213
}
211214

212-
return Task.Run(() => InvokeBlocking(action));
215+
return Task.Run(() => InvokeBlocking(action, caller));
213216
}
214217

215218
/// <summary>
216219
/// Invokes an action on the game thread and blocks until it is completed.
217220
/// </summary>
218221
/// <param name="action"></param>
219-
public void InvokeBlocking(Action action)
222+
[MethodImpl(MethodImplOptions.NoInlining)]
223+
public void InvokeBlocking(Action action, [CallerMemberName] string caller = "")
220224
{
221225
if (action == null)
222226
return;
@@ -240,7 +244,7 @@ public void InvokeBlocking(Action action)
240244
{
241245
e.Set();
242246
}
243-
}, "Torch");
247+
}, caller);
244248

245249
if (!e.WaitOne(60000))
246250
throw new TimeoutException("The game action timed out.");

0 commit comments

Comments
 (0)