9
9
using System . IO ;
10
10
using System . Reflection ;
11
11
using System . Runtime . InteropServices ;
12
- using System . Threading ;
13
12
using System . Threading . Tasks ;
14
13
15
14
namespace SourceSharp . Core ;
16
15
17
16
public static class Bootstrap
18
17
{
19
- private static readonly CancellationTokenSource CancellationTokenSource = new ( ) ;
18
+ private static bool _isShutdown ;
19
+ private static Task ? _signalTask ;
20
20
21
21
[ UnmanagedCallersOnly ]
22
22
public static int InitializeSourceSharp ( ) => Initialize ( ) ;
23
23
24
24
[ UnmanagedCallersOnly ]
25
- public static void ShutdownSourceSharp ( ) => CancellationTokenSource . Cancel ( false ) ;
25
+ public static void ShutdownSourceSharp ( )
26
+ {
27
+ _isShutdown = true ;
28
+ _signalTask ? . Wait ( ) ;
29
+ }
26
30
27
31
public static int InitializeTest ( ) => Initialize ( ) ;
28
32
29
33
private static int Initialize ( )
30
34
{
35
+ _isShutdown = false ;
36
+
31
37
try
32
38
{
33
39
var root = Path . Combine ( Path . GetDirectoryName (
@@ -50,6 +56,9 @@ private static int Initialize()
50
56
ValidateScopes = true
51
57
} ) ;
52
58
59
+ Console . WriteLine ( "MaxClients is " + Bridges . SourceSharp . GetMaxClients ( ) ) ;
60
+ Console . WriteLine ( "MaxHumanPlayers is " + Bridges . SourceSharp . GetMaxHumanPlayers ( ) ) ;
61
+
53
62
Boot ( serviceProvider ) ;
54
63
55
64
return 0 ;
@@ -97,7 +106,7 @@ private static void Boot(IServiceProvider services)
97
106
// export caller invoker
98
107
Invoker . Initialize ( services ) ;
99
108
100
- Task . Run ( async ( ) => await SignalThread ( services ) ) ;
109
+ _signalTask = Task . Run ( async ( ) => await SignalThread ( services ) ) ;
101
110
}
102
111
103
112
private static async Task SignalThread ( IServiceProvider services )
@@ -108,19 +117,18 @@ private static async Task SignalThread(IServiceProvider services)
108
117
{
109
118
await Task . Delay ( TimeSpan . FromMicroseconds ( 1 ) ) ;
110
119
111
- if ( ! CancellationTokenSource . IsCancellationRequested )
120
+ if ( _isShutdown )
112
121
{
113
- pluginManager . Signal ( ) ;
114
- continue ;
122
+ // Shutdown !!!
123
+ foreach ( var module in services . GetAllServices < IModuleBase > ( ) )
124
+ {
125
+ module . Shutdown ( ) ;
126
+ }
127
+ services . GetRequiredService < IPluginManager > ( ) . Shutdown ( ) ;
128
+ return ;
115
129
}
116
130
117
- // Shutdown !!!
118
- foreach ( var module in services . GetAllServices < IModuleBase > ( ) )
119
- {
120
- module . Shutdown ( ) ;
121
- }
122
- services . GetRequiredService < IPluginManager > ( ) . Shutdown ( ) ;
123
- break ;
131
+ pluginManager . Signal ( ) ;
124
132
}
125
133
}
126
134
}
0 commit comments