2015-08-20 01:06:44 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using SharedLibrary;
|
2017-05-27 00:22:50 -04:00
|
|
|
|
using SharedLibrary.Interfaces;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin
|
|
|
|
|
{
|
|
|
|
|
public class PluginImporter
|
|
|
|
|
{
|
2015-08-22 12:41:14 -04:00
|
|
|
|
public static List<Command> potentialCommands = new List<Command>();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public static List<IPlugin> potentialPlugins = new List<IPlugin>();
|
|
|
|
|
public static IPlugin webFront = null;
|
|
|
|
|
//private static AppDomain pluginDomain;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
|
|
|
|
public static bool Load()
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
//pluginDomain = AppDomain.CreateDomain("Plugins");
|
2015-08-20 01:06:44 -04:00
|
|
|
|
string[] dllFileNames = null;
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins"))
|
|
|
|
|
dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteWarning("Plugin folder does not exist!");
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dllFileNames == null || dllFileNames.Length == 0)
|
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteDebug("No plugins to load");
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
|
|
|
|
|
foreach (string dllFile in dllFileNames)
|
|
|
|
|
{
|
2015-08-22 12:41:14 -04:00
|
|
|
|
byte[] rawDLL = File.ReadAllBytes(dllFile); // because we want to update the plugin without restarting
|
|
|
|
|
Assembly assembly = Assembly.Load(rawDLL);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
assemblies.Add(assembly);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:29:20 -04:00
|
|
|
|
int LoadedPlugins = 0;
|
|
|
|
|
int LoadedCommands = 0;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
foreach (Assembly Plugin in assemblies)
|
|
|
|
|
{
|
|
|
|
|
if (Plugin != null)
|
|
|
|
|
{
|
|
|
|
|
Type[] types = Plugin.GetTypes();
|
|
|
|
|
foreach(Type assemblyType in types)
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
|
2015-08-20 15:23:13 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Object commandObject = Activator.CreateInstance(assemblyType);
|
|
|
|
|
Command newCommand = (Command)commandObject;
|
|
|
|
|
potentialCommands.Add(newCommand);
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteDebug("Registered command \"" + newCommand.Name + "\"");
|
|
|
|
|
LoadedCommands++;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (assemblyType.GetInterface("IPlugin", false) == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2015-08-20 15:23:13 -04:00
|
|
|
|
Object notifyObject = Activator.CreateInstance(assemblyType);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
IPlugin newNotify = (IPlugin)notifyObject;
|
2015-10-10 19:31:24 -04:00
|
|
|
|
if (potentialPlugins.Find(x => x.Name == newNotify.Name) == null)
|
2015-08-22 12:41:14 -04:00
|
|
|
|
{
|
2015-10-10 19:31:24 -04:00
|
|
|
|
potentialPlugins.Add(newNotify);
|
2017-05-27 18:08:04 -04:00
|
|
|
|
newNotify.OnLoadAsync();
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]");
|
|
|
|
|
LoadedPlugins++;
|
2015-08-22 12:41:14 -04:00
|
|
|
|
}
|
2015-08-20 15:23:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
catch (Exception E)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteWarning($"Could not load plugin {Plugin.Location} - {E.Message}");
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Manager.GetInstance().Logger.WriteInfo($"Loaded {LoadedPlugins} plugins and registered {LoadedCommands} commands.");
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
|
|
|
|
/*
|
2015-08-22 12:41:14 -04:00
|
|
|
|
public static void Unload()
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
|
|
|
|
foreach (IPlugin P in potentialPlugins)
|
2015-08-22 12:41:14 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
P.onUnload();
|
2015-08-22 12:41:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception E)
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Manager.GetInstance().mainLog.Write("There was an error unloading \"" + P.Name + "\" plugin", Log.Level.Debug);
|
|
|
|
|
Manager.GetInstance().mainLog.Write("Error Message: " + E.Message, Log.Level.Debug);
|
|
|
|
|
Manager.GetInstance().mainLog.Write("Error Trace: " + E.StackTrace, Log.Level.Debug);
|
2015-08-22 12:41:14 -04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
potentialCommands = new List<Command>();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
potentialPlugins = new List<IPlugin>();
|
|
|
|
|
AppDomain.Unload(pluginDomain);
|
|
|
|
|
}*/
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|