/* Most of the code here, I do not own, Just thought, I'd share with you all out there.*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Diagnostics.Contracts;
using System.Diagnostics;
namespace Blizzeta5
{
public interface IPlugin : IProvider
{
}
public interface ILoader
{
IProvider RequestProvider(string providerType);
}
public interface IConsumer
{
void Initialize(PluginLoader loader);
string Name { get; }
string Author { get; }
string Description { get; }
string Version { get; }
}
public interface ICommand
{
string Name { get; }
string Version { get; }
string Help { get; }
string Usage { get; }
string More { get; }
bool IsPublic { get; }
Permissions GetPermission();
}
public interface IProvider
{
void Load(LightScriptEngine.ScriptContext Context);
}
public class PluginLoader : ILoader
{
private List<Type> _providers = new List<Type>();
private string PluginSearchPath;
public PluginLoader(string PluginPath)
{
PluginSearchPath = PluginPath;
LoadProviders();
LoadConsumers();
}
public IProvider RequestProvider(string providerType)
{
string pType = "BlizzetaAPI." + providerType;
try
{
foreach (Type t in _providers)
{
if (t.FullName == pType)
{
return ((Activator.CreateInstance(t)) as IProvider);
}
}
}
catch (Exception mme)
{
Server.pc(mme.Message, ConsoleColor.Red);
Console.ReadKey(true);
}
return null;
}
private void LoadProviders()
{
DirectoryInfo di = new DirectoryInfo(PluginSearchPath);
FileInfo[] assemblies = di.GetFiles("*.dll");
//Console.WriteLine("Loading Modules...");
foreach (FileInfo assembly in assemblies)
{
Assembly a = Assembly.LoadFrom(assembly.FullName);
Type[] tarr = a.GetTypes();
foreach (Type type in tarr)
{
Type[] interfaces = type.GetInterfaces();
if (interfaces.Contains(typeof(IPlugin)))
{
Server.pc("{0} is a valid command!", ConsoleColor.Green, type.FullName);
_providers.Add(type);
}
}
}
}
private void LoadConsumers()
{
DirectoryInfo di = new DirectoryInfo(PluginSearchPath);
FileInfo[] assemblies = di.GetFiles("*.dll");
//Console.WriteLine("Loading Consumers...");
foreach (FileInfo assembly in assemblies)
{
Assembly a = Assembly.LoadFrom(assembly.FullName);
try
{
foreach (Type type in a.GetTypes())
{
if (type.GetInterfaces().Contains(typeof(IConsumer)))
{
IConsumer consumer = (IConsumer)Activator.CreateInstance(type);
Server.pc("Loaded {0} by {1} - Version {2}; \"{3}\"", ConsoleColor.Gray, consumer.Name, consumer.Author, consumer.Version, consumer.Description);
Console.WriteLine("Initializing {0}...", type);
consumer.Initialize(this);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
public void ReloadPlugin()
{
_providers = null;
LoadProviders();
LoadConsumers();
}
}
}
Comments
0 B
|👍
/👎