2015-08-20 01:06:44 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using SharedLibrary;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin
|
|
|
|
|
{
|
|
|
|
|
public class PluginImporter
|
|
|
|
|
{
|
2015-08-20 17:54:38 -04:00
|
|
|
|
public static List<Command> potentialCommands;
|
2015-08-21 21:11:35 -04:00
|
|
|
|
public static List<Plugin> potentialNotifies;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
|
|
|
|
public static bool Load()
|
|
|
|
|
{
|
|
|
|
|
string[] dllFileNames = null;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
potentialCommands = new List<Command>();
|
2015-08-21 21:11:35 -04:00
|
|
|
|
potentialNotifies = new List<Plugin>();
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
|
|
|
|
if (Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins"))
|
|
|
|
|
dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-08-20 13:52:30 -04:00
|
|
|
|
Program.getManager().mainLog.Write("Plugin folder does not exist!", Log.Level.All);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dllFileNames == null || dllFileNames.Length == 0)
|
|
|
|
|
{
|
2015-08-20 13:52:30 -04:00
|
|
|
|
Program.getManager().mainLog.Write("No plugins to load", Log.Level.All);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
|
|
|
|
|
foreach (string dllFile in dllFileNames)
|
|
|
|
|
{
|
|
|
|
|
AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
|
|
|
|
|
Assembly assembly = Assembly.Load(an);
|
|
|
|
|
assemblies.Add(assembly);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 02:04:30 -04:00
|
|
|
|
int totalLoaded = 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)
|
|
|
|
|
{
|
2015-08-22 02:04:30 -04:00
|
|
|
|
if(assemblyType.IsClass && assemblyType.BaseType.Name == "Plugin")
|
2015-08-20 15:23:13 -04:00
|
|
|
|
{
|
|
|
|
|
Object notifyObject = Activator.CreateInstance(assemblyType);
|
2015-08-21 21:11:35 -04:00
|
|
|
|
Plugin newNotify = (Plugin)notifyObject;
|
2015-08-20 15:23:13 -04:00
|
|
|
|
potentialNotifies.Add(newNotify);
|
2015-08-20 17:54:38 -04:00
|
|
|
|
newNotify.onLoad();
|
2015-08-22 02:04:30 -04:00
|
|
|
|
Program.getManager().mainLog.Write("Loaded plugin \"" + newNotify.Name + "\"" + " [" + newNotify.Version + "]", Log.Level.Debug);
|
|
|
|
|
totalLoaded++;
|
2015-08-20 15:23:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
Object commandObject = Activator.CreateInstance(assemblyType);
|
|
|
|
|
Command newCommand = (Command)commandObject;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
potentialCommands.Add(newCommand);
|
2015-08-22 02:04:30 -04:00
|
|
|
|
Program.getManager().mainLog.Write("Registered command \"" + newCommand.Name + "\"", Log.Level.Debug);
|
|
|
|
|
totalLoaded++;
|
2015-08-20 15:23:13 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
|
|
|
|
Program.getManager().mainLog.Write("Loaded " + totalLoaded + " plugins.", Log.Level.Production);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|