Moved event stuff to a non-manager NS.

Core assembly concept: Assemblies that will never need to get unloaded (Torch.API, Torch, Torch.Server)
Event shims and patch shims on the core assemblies.
This commit is contained in:
Westin Miller
2017-10-09 20:52:22 -07:00
parent 2004f71290
commit 62d73cbf96
13 changed files with 145 additions and 39 deletions

View File

@@ -22,6 +22,7 @@ using Torch.API.Managers;
using Torch.API.ModAPI;
using Torch.API.Session;
using Torch.Commands;
using Torch.Event;
using Torch.Managers;
using Torch.Managers.ChatManager;
using Torch.Managers.PatchManager;
@@ -45,6 +46,8 @@ namespace Torch
{
static TorchBase()
{
RegisterCoreAssembly(typeof(ITorchBase).Assembly);
RegisterCoreAssembly(typeof(TorchBase).Assembly);
// We can safely never detach this since we don't reload assemblies.
new ReflectedManager().Attach();
}
@@ -100,6 +103,7 @@ namespace Torch
/// <exception cref="InvalidOperationException">Thrown if a TorchBase instance already exists.</exception>
protected TorchBase()
{
RegisterCoreAssembly(GetType().Assembly);
if (Instance != null)
throw new InvalidOperationException("A TorchBase instance already exists.");
@@ -126,6 +130,7 @@ namespace Torch
// Managers.AddManager(new KeenLogManager(this));
Managers.AddManager(new FilesystemManager(this));
Managers.AddManager(new UpdateManager(this));
Managers.AddManager(new EventManager(this));
Managers.AddManager(Plugins);
GameStateChanged += (game, state) =>
{
@@ -422,7 +427,7 @@ namespace Torch
internal static void Inject(PatchContext target)
{
ConstructorInfo ctor = typeof(MySandboxGame).GetConstructor(new[] {typeof(string[])});
ConstructorInfo ctor = typeof(MySandboxGame).GetConstructor(new[] { typeof(string[]) });
if (ctor == null)
throw new ArgumentException("Can't find constructor MySandboxGame(string[])");
target.GetPattern(ctor).Prefixes.Add(MethodRef(nameof(PrefixConstructor)));
@@ -476,5 +481,16 @@ namespace Torch
}
}
#endregion
private static readonly HashSet<Assembly> _registeredAssemblies = new HashSet<Assembly>();
private static void RegisterCoreAssembly(Assembly asm)
{
lock (_registeredAssemblies)
if (_registeredAssemblies.Add(asm))
{
EventManager.AddDispatchShims(asm);
PatchManager.AddPatchShims(asm);
}
}
}
}