86 lines
No EOL
3 KiB
C#
86 lines
No EOL
3 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>();
|
|
|
|
private static void PSysLog(string Message)
|
|
{
|
|
Console.WriteLine($"[PLUGINS] {Message}");
|
|
}
|
|
|
|
public static void InitializeSystem(string pluginDirectory)
|
|
{
|
|
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 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;
|
|
|
|
PSysLog($"> Loading plugin: {nPlugin.pluginName}");
|
|
|
|
|
|
if (pluginStore == null)
|
|
{
|
|
plugin.ResetMemory();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |