-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathUnityHostLifetime.cs
More file actions
113 lines (94 loc) · 4.32 KB
/
UnityHostLifetime.cs
File metadata and controls
113 lines (94 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenMod.API;
using OpenMod.Core.Helpers;
using UnityEngine;
using UnityEngine.LowLevel;
namespace OpenMod.UnityEngine
{
public class UnityHostLifetime : IHostLifetime, IDisposable
{
private readonly IHostApplicationLifetime m_ApplicationLifetime;
private readonly ILogger<UnityHostLifetime> m_Logger;
private readonly IOpenModHost m_OpenModHost;
private readonly HostOptions m_HostOptions;
private readonly ManualResetEvent m_ShutdownBlock;
public UnityHostLifetime(IHostApplicationLifetime applicationLifetime, ILogger<UnityHostLifetime> logger,
IOptions<HostOptions> hostOptions, IOpenModHost openModHost)
{
m_ApplicationLifetime = applicationLifetime;
m_Logger = logger;
m_OpenModHost = openModHost;
m_HostOptions = hostOptions.Value;
m_ShutdownBlock = new ManualResetEvent(false);
}
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
if (!PlayerLoopHelper.IsInjectedUniTaskPlayerLoop())
{
var unitySynchronizationContextField =
typeof(PlayerLoopHelper).GetField("unitySynchronizationContext",
BindingFlags.Static | BindingFlags.NonPublic);
// For older version of UniTask
unitySynchronizationContextField ??=
typeof(PlayerLoopHelper).GetField("unitySynchronizationContetext",
BindingFlags.Static | BindingFlags.NonPublic)
?? throw new Exception("Could not find PlayerLoopHelper.unitySynchronizationContext field");
unitySynchronizationContextField.SetValue(null, SynchronizationContext.Current);
var mainThreadIdField =
typeof(PlayerLoopHelper).GetField("mainThreadId", BindingFlags.Static | BindingFlags.NonPublic)
?? throw new Exception("Could not find PlayerLoopHelper.mainThreadId field");
mainThreadIdField.SetValue(null, Thread.CurrentThread.ManagedThreadId);
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
PlayerLoopHelper.Initialize(ref playerLoop);
}
// Handle UniTask exception
UniTaskScheduler.UnobservedTaskException += UniTaskExceptionHandler;
// Do not switch thread
UniTaskScheduler.DispatchUnityMainThread = false;
Application.quitting += OnApplicationQuitting;
Console.CancelKeyPress += OnCancelKeyPress;
return Task.CompletedTask;
}
private void UniTaskExceptionHandler(Exception exception)
{
m_Logger.LogError(exception, "Caught UnobservedTaskException");
}
private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
AsyncHelper.RunSync(m_OpenModHost.ShutdownAsync);
}
private void OnApplicationQuitting()
{
m_ApplicationLifetime.StopApplication();
if (!m_ShutdownBlock.WaitOne(m_HostOptions.ShutdownTimeout))
{
m_Logger.LogInformation("Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks");
}
m_ShutdownBlock.WaitOne();
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
// Suppress that since we shut down gracefully. https://github.com/aspnet/AspNetCore/issues/6526
Environment.ExitCode = 0;
}
public Task StopAsync(CancellationToken cancellationToken)
{
// There's nothing to do here
return Task.CompletedTask;
}
public void Dispose()
{
m_ShutdownBlock.Set();
UniTaskScheduler.UnobservedTaskException -= UniTaskExceptionHandler;
UniTaskScheduler.DispatchUnityMainThread = true;
Application.quitting -= OnApplicationQuitting;
Console.CancelKeyPress -= OnCancelKeyPress;
}
}
}