Compare commits

...

2 Commits

Author SHA1 Message Date
zznty
d301955609 move ui lifetime to a manager for better lifetime control
All checks were successful
Release / Get Version (push) Successful in 4s
Release / Build and Publish Nuget (push) Successful in 2m21s
Release / Build and Publish Package (push) Successful in 2m58s
2024-10-23 16:52:10 +07:00
zznty
264daf7515 fix torch not restarting when start occurs second time 2024-10-23 16:51:41 +07:00
5 changed files with 51 additions and 22 deletions

View File

@@ -93,27 +93,11 @@ namespace Torch.Server
#endif
_server.Init();
var uiThread = new Thread(() =>
{
var ui = new TorchUI(_server);
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
ui.ShowDialog();
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();
if (Config.Autostart || Config.TempAutostart)
{
Config.TempAutostart = false;
_server.Start();
}
uiThread.Join();
if (!Config.Autostart && !Config.TempAutostart) return;
Config.TempAutostart = false;
_server.Start();
}
}

View File

@@ -0,0 +1,37 @@
using System.Threading;
using System.Windows.Threading;
using JetBrains.Annotations;
using Torch.Managers;
namespace Torch.Server.Managers;
public class UiManager(TorchServer torchInstance) : Manager(torchInstance)
{
[CanBeNull] private Thread _uiThread;
[CanBeNull] private Dispatcher _dispatcher;
public override void Attach()
{
_uiThread = new Thread(() =>
{
var ui = new TorchUI(torchInstance);
_dispatcher = Dispatcher.CurrentDispatcher;
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(_dispatcher));
ui.ShowDialog();
});
_uiThread.SetApartmentState(ApartmentState.STA);
_uiThread.Start();
}
public override void Detach()
{
_dispatcher?.InvokeShutdown();
if (Thread.CurrentThread != _uiThread)
_uiThread?.Join();
}
}

View File

@@ -56,6 +56,8 @@ namespace Torch.Server
if (config.EntityManagerEnabled)
AddManager(new EntityControlManager(this));
AddManager(new RemoteAPIManager(this));
if (!ApplicationContext.Current.IsService && !config.NoGui)
AddManager(new UiManager(this));
var sessionManager = Managers.GetManager<ITorchSessionManager>();
sessionManager.AddFactory(_ => new MultiplayerManagerDedicated(this));
@@ -185,7 +187,7 @@ namespace Torch.Server
if (IsRunning || HasRun)
{
Restart();
Restart(false);
return;
}

View File

@@ -65,7 +65,8 @@ internal static class AssemblyRewriter
{
using var assembly = AssemblyDefinition.ReadAssembly(inputStream, new()
{
AssemblyResolver = resolver
AssemblyResolver = resolver,
ReadSymbols = true
});
foreach (var fieldDefinition in FindAllToRewrite(assembly.MainModule))
{

View File

@@ -292,16 +292,21 @@ namespace Torch
public void Dispose()
{
}
private bool _disposed = false;
/// <inheritdoc />
public virtual void Destroy()
{
if (_disposed) return;
Game.SignalDestroy();
if (!Game.WaitFor(VRageGame.GameState.Destroyed))
Log.Warn("Failed to wait for the game to be destroyed");
Game = null;
Managers.Detach();
_disposed = true;
}
#endregion