IW4M-Admin/Admin/Plugins.cs

76 lines
2.9 KiB
C#
Raw Normal View History

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;
public static List<Plugin> potentialNotifies;
public static bool Load()
{
string[] dllFileNames = null;
2015-08-20 17:54:38 -04:00
potentialCommands = new List<Command>();
potentialNotifies = new List<Plugin>();
if (Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins"))
dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
else
{
Program.getManager().mainLog.Write("Plugin folder does not exist!", Log.Level.All);
return false;
}
if (dllFileNames == null || dllFileNames.Length == 0)
{
Program.getManager().mainLog.Write("No plugins to load", Log.Level.All);
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);
}
foreach (Assembly Plugin in assemblies)
{
if (Plugin != null)
{
Type[] types = Plugin.GetTypes();
foreach(Type assemblyType in types)
{
if(assemblyType.IsClass && assemblyType.BaseType.Name == "Notify")
2015-08-20 15:23:13 -04:00
{
Object notifyObject = Activator.CreateInstance(assemblyType);
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-20 15:23:13 -04:00
Program.getManager().mainLog.Write("Loaded event plugin \"" + assemblyType.Name + "\"", Log.Level.All);
}
else if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
{
Object commandObject = Activator.CreateInstance(assemblyType);
Command newCommand = (Command)commandObject;
2015-08-20 17:54:38 -04:00
potentialCommands.Add(newCommand);
Program.getManager().mainLog.Write("Loaded command plugin \"" + newCommand.Name + "\"", Log.Level.All);
2015-08-20 15:23:13 -04:00
}
else
2015-08-20 15:23:13 -04:00
Program.getManager().mainLog.Write("Ignoring invalid plugin \"" + assemblyType.Name + "\"", Log.Level.All);
}
}
}
return true;
}
}
}