Advertisement
Blizzardo1

PluginInterface snapped from Blizzeta 6

Jun 26th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. /* Most of the code here, I do not own, Just thought, I'd share with you all out there.*/
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10.  
  11. using System.Diagnostics.Contracts;
  12. using System.Diagnostics;
  13.  
  14. namespace Blizzeta5
  15. {
  16.     public interface IPlugin : IProvider
  17.     {
  18.     }
  19.  
  20.     public interface ILoader
  21.     {
  22.         IProvider RequestProvider(string providerType);
  23.     }
  24.  
  25.     public interface IConsumer
  26.     {
  27.         void Initialize(PluginLoader loader);
  28.         string Name { get; }
  29.         string Author { get; }
  30.         string Description { get; }
  31.         string Version { get; }
  32.     }
  33.  
  34.     public interface ICommand
  35.     {
  36.         string Name { get; }
  37.         string Version { get; }
  38.         string Help { get; }
  39.         string Usage { get; }
  40.         string More { get; }
  41.         bool IsPublic { get; }
  42.         Permissions GetPermission();
  43.     }
  44.  
  45.     public interface IProvider
  46.     {
  47.         void Load(LightScriptEngine.ScriptContext Context);
  48.     }
  49.  
  50.     public class PluginLoader : ILoader
  51.     {
  52.         private List<Type> _providers = new List<Type>();
  53.        
  54.         private string PluginSearchPath;
  55.        
  56.         public PluginLoader(string PluginPath)
  57.         {
  58.             PluginSearchPath = PluginPath;
  59.             LoadProviders();
  60.             LoadConsumers();
  61.         }
  62.  
  63.         public IProvider RequestProvider(string providerType)
  64.         {
  65.             string pType = "BlizzetaAPI." + providerType;
  66.             try
  67.             {
  68.                 foreach (Type t in _providers)
  69.                 {
  70.                     if (t.FullName == pType)
  71.                     {
  72.                         return ((Activator.CreateInstance(t)) as IProvider);
  73.                     }
  74.                 }
  75.             }
  76.             catch (Exception mme)
  77.             {
  78.                 Server.pc(mme.Message, ConsoleColor.Red);
  79.                 Console.ReadKey(true);
  80.             }
  81.             return null;
  82.         }
  83.  
  84.         private void LoadProviders()
  85.         {
  86.             DirectoryInfo di = new DirectoryInfo(PluginSearchPath);
  87.             FileInfo[] assemblies = di.GetFiles("*.dll");
  88.             //Console.WriteLine("Loading Modules...");
  89.             foreach (FileInfo assembly in assemblies)
  90.             {
  91.                 Assembly a = Assembly.LoadFrom(assembly.FullName);
  92.  
  93.                 Type[] tarr = a.GetTypes();
  94.  
  95.                 foreach (Type type in tarr)
  96.                 {
  97.                     Type[] interfaces = type.GetInterfaces();
  98.  
  99.                     if (interfaces.Contains(typeof(IPlugin)))
  100.                     {
  101.                         Server.pc("{0} is a valid command!", ConsoleColor.Green, type.FullName);
  102.                         _providers.Add(type);
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.  
  108.         private void LoadConsumers()
  109.         {
  110.             DirectoryInfo di = new DirectoryInfo(PluginSearchPath);
  111.             FileInfo[] assemblies = di.GetFiles("*.dll");
  112.             //Console.WriteLine("Loading Consumers...");
  113.             foreach (FileInfo assembly in assemblies)
  114.             {
  115.                 Assembly a = Assembly.LoadFrom(assembly.FullName);
  116.                 try
  117.                 {
  118.  
  119.                     foreach (Type type in a.GetTypes())
  120.                     {
  121.                         if (type.GetInterfaces().Contains(typeof(IConsumer)))
  122.                         {
  123.                             IConsumer consumer = (IConsumer)Activator.CreateInstance(type);
  124.                             Server.pc("Loaded {0} by {1} - Version {2}; \"{3}\"", ConsoleColor.Gray, consumer.Name, consumer.Author, consumer.Version, consumer.Description);
  125.                             Console.WriteLine("Initializing {0}...", type);
  126.                             consumer.Initialize(this);
  127.  
  128.                         }
  129.                     }
  130.                 }
  131.                 catch (Exception ex)
  132.                 {
  133.                     System.Diagnostics.Debug.WriteLine(ex.Message);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         public void ReloadPlugin()
  139.         {
  140.             _providers = null;
  141.             LoadProviders();
  142.             LoadConsumers();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement