Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit b116269

Browse files
committed
fix(core): plugin double load, double unload
1 parent 6e7e62a commit b116269

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

Core/Bootstrap.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,31 @@
99
using System.IO;
1010
using System.Reflection;
1111
using System.Runtime.InteropServices;
12-
using System.Threading;
1312
using System.Threading.Tasks;
1413

1514
namespace SourceSharp.Core;
1615

1716
public static class Bootstrap
1817
{
19-
private static readonly CancellationTokenSource CancellationTokenSource = new();
18+
private static bool _isShutdown;
19+
private static Task? _signalTask;
2020

2121
[UnmanagedCallersOnly]
2222
public static int InitializeSourceSharp() => Initialize();
2323

2424
[UnmanagedCallersOnly]
25-
public static void ShutdownSourceSharp() => CancellationTokenSource.Cancel(false);
25+
public static void ShutdownSourceSharp()
26+
{
27+
_isShutdown = true;
28+
_signalTask?.Wait();
29+
}
2630

2731
public static int InitializeTest() => Initialize();
2832

2933
private static int Initialize()
3034
{
35+
_isShutdown = false;
36+
3137
try
3238
{
3339
var root = Path.Combine(Path.GetDirectoryName(
@@ -50,6 +56,9 @@ private static int Initialize()
5056
ValidateScopes = true
5157
});
5258

59+
Console.WriteLine("MaxClients is " + Bridges.SourceSharp.GetMaxClients());
60+
Console.WriteLine("MaxHumanPlayers is " + Bridges.SourceSharp.GetMaxHumanPlayers());
61+
5362
Boot(serviceProvider);
5463

5564
return 0;
@@ -97,7 +106,7 @@ private static void Boot(IServiceProvider services)
97106
// export caller invoker
98107
Invoker.Initialize(services);
99108

100-
Task.Run(async () => await SignalThread(services));
109+
_signalTask = Task.Run(async () => await SignalThread(services));
101110
}
102111

103112
private static async Task SignalThread(IServiceProvider services)
@@ -108,19 +117,18 @@ private static async Task SignalThread(IServiceProvider services)
108117
{
109118
await Task.Delay(TimeSpan.FromMicroseconds(1));
110119

111-
if (!CancellationTokenSource.IsCancellationRequested)
120+
if (_isShutdown)
112121
{
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;
115129
}
116130

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();
124132
}
125133
}
126134
}

Core/PluginManager.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ public void Initialize()
9898
{
9999
listener.OnPluginLoad(plugin);
100100
}
101-
102-
_plugins.Add(plugin);
103-
// _sourceSharp.PrintLine($"Plugin <{name}> checked.");
104101
}
105102
catch (Exception e)
106103
{
@@ -139,7 +136,7 @@ public void Shutdown()
139136
{
140137
foreach (var plugin in _plugins)
141138
{
142-
UnloadPlugin(plugin);
139+
UnloadPlugin(plugin, false);
143140
}
144141

145142
_plugins.Clear();
@@ -184,7 +181,7 @@ private void LoadPlugin(CPlugin plugin)
184181
}
185182
}
186183

187-
private void UnloadPlugin(CPlugin plugin)
184+
private void UnloadPlugin(CPlugin plugin, bool remove)
188185
{
189186
try
190187
{
@@ -208,7 +205,11 @@ private void UnloadPlugin(CPlugin plugin)
208205
plugin.Instance.OnShutdown();
209206
plugin.Loader.Dispose();
210207
plugin.UpdateStatus(PluginStatus.None);
211-
_plugins.Remove(plugin);
208+
209+
if (remove)
210+
{
211+
_plugins.Remove(plugin);
212+
}
212213
}
213214
catch (Exception e)
214215
{

0 commit comments

Comments
 (0)