158 lines
No EOL
6.2 KiB
C#
158 lines
No EOL
6.2 KiB
C#
using System.Reflection;
|
|
using LibAC.NBT;
|
|
using LibAC.NBT.API;
|
|
|
|
namespace ForgeCoreAPI;
|
|
|
|
public class PluginSystem
|
|
{
|
|
public static string PluginDirectory { get; set; } = "Plugins";
|
|
|
|
public static List<PluginContainer> Plugins = new List<PluginContainer>();
|
|
public static string LibraryDir { get; set; } = "Libs";
|
|
|
|
private static void PSysLog(string Message)
|
|
{
|
|
Console.WriteLine($"[PLUGINS] {Message}");
|
|
}
|
|
|
|
public static void InitializeSystem(string pluginDirectory, string libraryDirectory = "Libs")
|
|
{
|
|
CompoundTag pluginsData = new CompoundTag();
|
|
if (!File.Exists("PluginStorage.dat"))
|
|
{
|
|
// We have no existing saved config for plugins, or this is the first run
|
|
// Leave pluginsData empty
|
|
} else pluginsData = NbtIo.Read("PluginStorage.dat");
|
|
|
|
// Search the primary executable for non-removable plugins
|
|
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
|
|
{
|
|
foreach (var type in asm.GetTypes())
|
|
{
|
|
if (type.GetCustomAttribute<ForgeCorePluginAttribute>() != null)
|
|
{
|
|
ForgeCorePluginAttribute attr = type.GetCustomAttribute<ForgeCorePluginAttribute>();
|
|
PluginContainer container = new PluginContainer();
|
|
container.pluginName = attr.pluginName;
|
|
container.providesShutdownMethod = attr.providesShutdownMethod;
|
|
|
|
IPlugin plugin = Activator.CreateInstance(type) as IPlugin;
|
|
container.plugin = plugin;
|
|
plugin.Initialize();
|
|
|
|
|
|
PSysLog($"> Loading plugin: {container.pluginName}");
|
|
|
|
Tag? store = pluginsData.Get(container.pluginName);
|
|
if (store == null)
|
|
{
|
|
plugin.ResetMemory();
|
|
}
|
|
else
|
|
{
|
|
CompoundTag plugTag = (CompoundTag)store;
|
|
CompoundTag data = (CompoundTag)plugTag.Get("data");
|
|
|
|
bool enabled = NbtUtils.ReadBoolean(plugTag, "enabled");
|
|
container.enabled = enabled;
|
|
plugin.LoadConfig(data);
|
|
PSysLog($"> Loaded plugin: {container.pluginName}");
|
|
|
|
if (enabled)
|
|
{
|
|
plugin.Enable();
|
|
PSysLog($"> Enabled plugin: {container.pluginName}");
|
|
}
|
|
else
|
|
{
|
|
plugin.Disable();
|
|
PSysLog($"> Disabled plugin: {container.pluginName}");
|
|
}
|
|
}
|
|
|
|
Plugins.Add(container);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Search library folder for any libraries required to be loaded before the plugins
|
|
string[] libs = Directory.GetFiles(libraryDirectory, "*.dll");
|
|
foreach (string lib in libs)
|
|
{
|
|
Assembly.LoadFile(lib);
|
|
// We do not need to do anything else with the loaded library.
|
|
}
|
|
|
|
|
|
// Search the Plugins directory for DLL files
|
|
string[] files = Directory.GetFiles(pluginDirectory, "*.dll");
|
|
|
|
// Begin loading assemblies
|
|
foreach (var file in files)
|
|
{
|
|
Assembly asm = Assembly.LoadFile(file);
|
|
// Scan types for ForgeCorePlugin attribute
|
|
|
|
foreach (var types in asm.GetTypes())
|
|
{
|
|
if (types.GetCustomAttribute<ForgeCorePluginAttribute>() != null)
|
|
{
|
|
PluginContainer nPlugin = new PluginContainer();
|
|
IPlugin plugin = Activator.CreateInstance(types) as IPlugin;
|
|
plugin.Initialize();
|
|
nPlugin.plugin = plugin;
|
|
|
|
|
|
ForgeCorePluginAttribute attrib = types.GetCustomAttribute<ForgeCorePluginAttribute>();
|
|
|
|
Tag? pluginStore = pluginsData.Get(attrib.pluginName);
|
|
nPlugin.pluginName = attrib.pluginName;
|
|
nPlugin.providesShutdownMethod = attrib.providesShutdownMethod;
|
|
|
|
PSysLog($"> Loading plugin: {nPlugin.pluginName}");
|
|
|
|
|
|
if (pluginStore == null)
|
|
{
|
|
plugin.ResetMemory();
|
|
plugin.Enable();
|
|
nPlugin.enabled = true;
|
|
PSysLog($"> Loaded plugin: {nPlugin.pluginName}");
|
|
PSysLog($"> No existing settings found for plugin: {nPlugin.pluginName}");
|
|
}
|
|
else
|
|
{
|
|
CompoundTag tag = (CompoundTag) pluginStore;
|
|
CompoundTag dataTag = tag["data"] as CompoundTag;
|
|
|
|
plugin.LoadConfig(dataTag);
|
|
PSysLog($"> Loaded plugin: {nPlugin.pluginName}");
|
|
if (NbtUtils.ReadBoolean(tag, "enabled"))
|
|
{
|
|
nPlugin.enabled = true;
|
|
plugin.Enable();
|
|
|
|
PSysLog($"> Enabled plugin: {nPlugin.pluginName}");
|
|
}
|
|
else
|
|
{
|
|
plugin.Disable();
|
|
nPlugin.enabled = false;
|
|
PSysLog($"> Disabled plugin: {nPlugin.pluginName}");
|
|
}
|
|
}
|
|
|
|
Plugins.Add(nPlugin);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if any plugin provides a shutdown method
|
|
foreach (var plugin in Plugins)
|
|
{
|
|
if (plugin.providesShutdownMethod) SharedSessionData.GetInstance().HasShutdownMethod = true;
|
|
}
|
|
}
|
|
} |