Create module subsystem under TShockAPI.Modules
This should allow for TShock.cs to be split up to mini-modules, or "micro-plugins"
This commit is contained in:
parent
d3adc6db42
commit
d2a060b76e
3 changed files with 142 additions and 0 deletions
34
TShockAPI/Modules/Module.cs
Normal file
34
TShockAPI/Modules/Module.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
TShock, a server mod for Terraria
|
||||||
|
Copyright (C) 2011-2019 Pryaxis & TShock Contributors
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
using System;
|
||||||
|
namespace TShockAPI.Modules
|
||||||
|
{
|
||||||
|
public abstract class Module : IDisposable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Entry point of the module
|
||||||
|
/// </summary>
|
||||||
|
public abstract void Initialise();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resource cleanup, e.g. hooks and events
|
||||||
|
/// </summary>
|
||||||
|
public virtual void Dispose() { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
101
TShockAPI/Modules/ModuleManager.cs
Normal file
101
TShockAPI/Modules/ModuleManager.cs
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
TShock, a server mod for Terraria
|
||||||
|
Copyright (C) 2011-2019 Pryaxis & TShock Contributors
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace TShockAPI.Modules
|
||||||
|
{
|
||||||
|
public class ModuleManager : IDisposable
|
||||||
|
{
|
||||||
|
private List<Module> _modules = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Discovers <see cref="Module"/> derived classes from across the assembly
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Type definitions of the modules that can be created</returns>
|
||||||
|
IEnumerable<Type> CollectModules() => Assembly.GetExecutingAssembly()
|
||||||
|
.GetTypes()
|
||||||
|
.Where(t => typeof(Module).IsAssignableFrom(t) && !t.IsAbstract)
|
||||||
|
;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialises <see cref="Module"/> derived classes defined across the assembly
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameters">Additional constructor arguments allowed for modules</param>
|
||||||
|
public void Initialise(object[] parameters)
|
||||||
|
{
|
||||||
|
foreach (var moduleType in CollectModules())
|
||||||
|
InitialiseModule(moduleType, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialises a module by its type definition
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="moduleType">The type of the module</param>
|
||||||
|
/// <param name="parameters">Additional constructor arguments allowed for modules</param>
|
||||||
|
public void InitialiseModule(Type moduleType, object[] parameters)
|
||||||
|
{
|
||||||
|
if (!typeof(Module).IsAssignableFrom(moduleType))
|
||||||
|
throw new NotSupportedException($"Cannot load module {moduleType.FullName} as it does not derive from {typeof(Module).FullName}");
|
||||||
|
|
||||||
|
var args = new List<object>();
|
||||||
|
ConstructorInfo constructor = null;
|
||||||
|
|
||||||
|
foreach (var ctor in moduleType.GetConstructors())
|
||||||
|
{
|
||||||
|
args.Clear();
|
||||||
|
var ctorParams = ctor.GetParameters();
|
||||||
|
|
||||||
|
foreach (var prm in ctorParams)
|
||||||
|
{
|
||||||
|
var matching_objects = parameters.Where(p => prm.ParameterType.IsAssignableFrom(p.GetType()));
|
||||||
|
if (matching_objects.Count() == 1)
|
||||||
|
args.Add(matching_objects.Single());
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// skip this ctor since we cannot find a suitable parameter for it.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.Count() == ctorParams.Length)
|
||||||
|
constructor = ctor;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constructor is not null)
|
||||||
|
{
|
||||||
|
var module = (Module)constructor.Invoke(args.ToArray());
|
||||||
|
_modules.Add(module);
|
||||||
|
module.Initialise();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disposes of the module and the manager instance
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var module in _modules)
|
||||||
|
module.Dispose();
|
||||||
|
_modules.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -46,6 +46,7 @@ using TShockAPI.Localization;
|
||||||
using TShockAPI.Configuration;
|
using TShockAPI.Configuration;
|
||||||
using Terraria.GameContent.Creative;
|
using Terraria.GameContent.Creative;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using TShockAPI.Modules;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
|
|
@ -149,6 +150,8 @@ namespace TShockAPI
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static event Action Initialized;
|
public static event Action Initialized;
|
||||||
|
|
||||||
|
public static ModuleManager ModuleManager { get; } = new ModuleManager();
|
||||||
|
|
||||||
/// <summary>Version - The version required by the TerrariaAPI to be passed back for checking & loading the plugin.</summary>
|
/// <summary>Version - The version required by the TerrariaAPI to be passed back for checking & loading the plugin.</summary>
|
||||||
/// <value>value - The version number specified in the Assembly, based on the VersionNum variable set in this class.</value>
|
/// <value>value - The version number specified in the Assembly, based on the VersionNum variable set in this class.</value>
|
||||||
public override Version Version
|
public override Version Version
|
||||||
|
|
@ -421,6 +424,8 @@ namespace TShockAPI
|
||||||
|
|
||||||
EnglishLanguage.Initialize();
|
EnglishLanguage.Initialize();
|
||||||
|
|
||||||
|
ModuleManager.Initialise(new object[] { this });
|
||||||
|
|
||||||
if (Config.Settings.RestApiEnabled)
|
if (Config.Settings.RestApiEnabled)
|
||||||
RestApi.Start();
|
RestApi.Start();
|
||||||
|
|
||||||
|
|
@ -463,6 +468,8 @@ namespace TShockAPI
|
||||||
}
|
}
|
||||||
SaveManager.Instance.Dispose();
|
SaveManager.Instance.Dispose();
|
||||||
|
|
||||||
|
ModuleManager.Dispose();
|
||||||
|
|
||||||
ServerApi.Hooks.GamePostInitialize.Deregister(this, OnPostInit);
|
ServerApi.Hooks.GamePostInitialize.Deregister(this, OnPostInit);
|
||||||
ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate);
|
ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate);
|
||||||
ServerApi.Hooks.GameHardmodeTileUpdate.Deregister(this, OnHardUpdate);
|
ServerApi.Hooks.GameHardmodeTileUpdate.Deregister(this, OnHardUpdate);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue