mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 12:14:16 +01:00
88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Serilog;
|
|
|
|
namespace Dalamud.Plugin
|
|
{
|
|
public class PluginManager {
|
|
private readonly Dalamud dalamud;
|
|
private readonly string pluginDirectory;
|
|
private readonly string defaultPluginDirectory;
|
|
|
|
public List<IDalamudPlugin> Plugins;
|
|
|
|
public PluginManager(Dalamud dalamud, string pluginDirectory, string defaultPluginDirectory) {
|
|
this.dalamud = dalamud;
|
|
this.pluginDirectory = pluginDirectory;
|
|
this.defaultPluginDirectory = defaultPluginDirectory;
|
|
}
|
|
|
|
public void UnloadPlugins() {
|
|
if (this.Plugins == null)
|
|
return;
|
|
|
|
for (var i = 0; i < this.Plugins.Count; i++) {
|
|
this.Plugins[i].Dispose();
|
|
this.Plugins[i] = null;
|
|
}
|
|
}
|
|
|
|
public void LoadPlugins() {
|
|
LoadPluginsAt(new DirectoryInfo(this.defaultPluginDirectory));
|
|
LoadPluginsAt(new DirectoryInfo(this.pluginDirectory));
|
|
}
|
|
|
|
private void LoadPluginsAt(DirectoryInfo folder) {
|
|
if (folder.Exists)
|
|
{
|
|
Log.Debug("Loading plugins at {0}", folder);
|
|
|
|
var pluginDlls = folder.GetFiles("*.dll", SearchOption.AllDirectories);
|
|
|
|
var assemblies = new List<Assembly>(pluginDlls.Length);
|
|
foreach (var dllFile in pluginDlls)
|
|
{
|
|
Log.Debug("Loading assembly at {0}", dllFile);
|
|
var assemblyName = AssemblyName.GetAssemblyName(dllFile.FullName);
|
|
var pluginAssembly = Assembly.Load(assemblyName);
|
|
assemblies.Add(pluginAssembly);
|
|
}
|
|
|
|
var interfaceType = typeof(IDalamudPlugin);
|
|
var foundImplementations = new List<Type>();
|
|
foreach (var assembly in assemblies) {
|
|
if (assembly != null) {
|
|
Log.Debug("Loading types for {0}", assembly.FullName);
|
|
var types = assembly.GetTypes();
|
|
foreach (var type in types) {
|
|
if (type.IsInterface || type.IsAbstract) {
|
|
continue;
|
|
}
|
|
|
|
if (type.GetInterface(interfaceType.FullName) != null) {
|
|
foundImplementations.Add(type);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
this.Plugins = new List<IDalamudPlugin>(foundImplementations.Count);
|
|
foreach (var pluginType in foundImplementations)
|
|
{
|
|
var plugin = (IDalamudPlugin)Activator.CreateInstance(pluginType);
|
|
|
|
var dalamudInterface = new DalamudPluginInterface(this.dalamud, pluginType.Assembly.GetName().Name);
|
|
|
|
plugin.Initialize(dalamudInterface);
|
|
Log.Information("Loaded plugin: {0}", plugin.Name);
|
|
this.Plugins.Add(plugin);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|