-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZoneManager.Base.cs
More file actions
52 lines (45 loc) · 1.44 KB
/
ZoneManager.Base.cs
File metadata and controls
52 lines (45 loc) · 1.44 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
namespace SZones;
// this base handling shutdown and multiple instances of this component
partial class ZoneManager
{
private ZoneManager() { }
public override void LoadPlugin()
{
AssertInstance(false);
Instance = this;
base.LoadPlugin();
StartHandlingShutdown();
}
public override void UnloadPlugin(PluginState state = PluginState.Unloaded)
{
base.UnloadPlugin(state);
Instance = null;
}
private static bool InstanceExists() => Instance is not null;
private static void AssertInstance(bool exists)
{
if (InstanceExists() != exists)
throw new Exception($"{typeof(ZoneManager).FullName} instance already {(exists ? "not exists" : $"exists, use {nameof(ZoneManager)}.{nameof(Instance)} to access it's instance")}.");
}
public static ZoneManager Instance { get; private set; }
private void StopHandlingShutdown()
{
Application.quitting -= OnShutdown;
Provider.onCommenceShutdown -= OnShutdown;
}
private void StartHandlingShutdown()
{
StopHandlingShutdown();
Application.quitting += OnShutdown;
Provider.onCommenceShutdown += OnShutdown;
}
private void OnShutdown()
{
if (Instance is null)
return;
StopHandlingShutdown();
UnloadPlugin();
}
private void OnApplicationQuit() => OnShutdown();
private void OnDestroy() => OnShutdown();
}