2017-06-12 17:47:31 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
using System.Linq;
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Plugins
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
|
|
|
|
public class PluginImporter
|
|
|
|
|
{
|
|
|
|
|
public static List<Command> ActiveCommands = new List<Command>();
|
|
|
|
|
public static List<IPlugin> ActivePlugins = new List<IPlugin>();
|
2018-05-28 21:30:31 -04:00
|
|
|
|
public static List<Assembly> PluginAssemblies = new List<Assembly>();
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
|
|
|
|
public static bool Load(IManager Manager)
|
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
string pluginDir = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}";
|
|
|
|
|
string[] dllFileNames = null;
|
|
|
|
|
string[] scriptFileNames = null;
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(pluginDir))
|
|
|
|
|
{
|
|
|
|
|
dllFileNames = Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}", "*.dll");
|
|
|
|
|
scriptFileNames = Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}", "*.js");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dllFileNames = new string[0];
|
|
|
|
|
scriptFileNames = new string[0];
|
|
|
|
|
}
|
2017-06-12 17:47:31 -04:00
|
|
|
|
|
2018-08-26 20:20:47 -04:00
|
|
|
|
if (dllFileNames.Length == 0 &&
|
|
|
|
|
scriptFileNames.Length == 0)
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteDebug(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_NOTFOUND"]);
|
2017-06-12 17:47:31 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 20:20:47 -04:00
|
|
|
|
// load up the script plugins
|
|
|
|
|
foreach (string fileName in scriptFileNames)
|
|
|
|
|
{
|
|
|
|
|
var plugin = new ScriptPlugin(fileName);
|
|
|
|
|
plugin.Initialize(Manager).Wait();
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteDebug($"Loaded script plugin \"{ plugin.Name }\" [{plugin.Version}]");
|
2018-08-26 20:20:47 -04:00
|
|
|
|
ActivePlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 17:47:31 -04:00
|
|
|
|
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
|
|
|
|
|
foreach (string dllFile in dllFileNames)
|
|
|
|
|
{
|
2017-09-29 22:42:24 -04:00
|
|
|
|
assemblies.Add(Assembly.LoadFrom(dllFile));
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LoadedCommands = 0;
|
|
|
|
|
foreach (Assembly Plugin in assemblies)
|
|
|
|
|
{
|
|
|
|
|
if (Plugin != null)
|
|
|
|
|
{
|
|
|
|
|
Type[] types = Plugin.GetTypes();
|
|
|
|
|
foreach (Type assemblyType in types)
|
|
|
|
|
{
|
|
|
|
|
if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
|
|
|
|
|
{
|
|
|
|
|
Object commandObject = Activator.CreateInstance(assemblyType);
|
|
|
|
|
Command newCommand = (Command)commandObject;
|
|
|
|
|
ActiveCommands.Add(newCommand);
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteDebug($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_REGISTERCMD"]} \"{newCommand.Name}\"");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
LoadedCommands++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (assemblyType.GetInterface("IPlugin", false) == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Object notifyObject = Activator.CreateInstance(assemblyType);
|
|
|
|
|
IPlugin newNotify = (IPlugin)notifyObject;
|
|
|
|
|
if (ActivePlugins.Find(x => x.Name == newNotify.Name) == null)
|
|
|
|
|
{
|
|
|
|
|
ActivePlugins.Add(newNotify);
|
2018-05-28 21:30:31 -04:00
|
|
|
|
PluginAssemblies.Add(Plugin);
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception E)
|
|
|
|
|
{
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteWarning($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Plugin.Location} - {E.Message}");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Manager.GetLogger(0).WriteInfo($"Loaded {ActivePlugins.Count} plugins and registered {LoadedCommands} commands.");
|
2017-06-12 17:47:31 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|