Beispiel #1
0
        internal void ActivatePlugins()
        {
            List <string> paths = Directory.GetFiles(AoLController.Game.Files.PluginDirectory, "*.dll").ToList();

            var dictionary = new Dictionary <PluginInformation, KeyValuePair <Type, List <Type> > >();

            _contexts.Clear();

            foreach (string pluginpath in paths)
            {
                try
                {
                    Assembly assembly = Assembly.Load(File.ReadAllBytes(pluginpath));
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (!typeof(IPlugin).IsAssignableFrom(type))
                        {
                            continue;
                        }

                        PluginInformation infos = type.GetCustomAttribute <PluginInformation>();

                        if (infos == null)
                        {
                            AoLController.Game.Logger.Info($"The File {assembly.GetName().Name} has a class which inherits from IPlugin but has no PluginInformation ... Default Values will be added");
                            infos = new PluginInformation();
                        }

                        List <Type> allTypes = assembly.GetTypes().ToList();
                        allTypes.Remove(type);
                        dictionary.Add(infos, new KeyValuePair <Type, List <Type> >(type, allTypes));
                        break;
                    }
                }
                catch (Exception e)
                {
                    AoLController.Game.Logger.Error($"AoL-Controller: Loading Assembly of {pluginpath} failed!!\n{e}");
                }
            }

            foreach (KeyValuePair <PluginInformation, KeyValuePair <Type, List <Type> > > infoTypePair in dictionary.OrderByDescending(x => x.Key.LoadPriority))
            {
                try
                {
                    AoLController.Game.Logger.Info($"{infoTypePair.Key.Name} will now be activated!");
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(infoTypePair.Value.Key);
                    plugin.Information     = infoTypePair.Key;
                    plugin.PluginDirectory = AoLController.Game.Files.PluginDirectory;
                    _contexts.Add(new PluginLoadContext(plugin, infoTypePair.Value.Key, infoTypePair.Key, infoTypePair.Value.Value));
                    _plugins.Add(plugin);
                    Plugins.Add(infoTypePair.Key);
                }
                catch (Exception e)
                {
                    AoLController.Game.Logger.Error($"AoL-Controller: Activation of {infoTypePair.Value.Key.Assembly.GetName().Name} failed!!\n{e}");
                }
            }

            LoadPlugins();
        }
Beispiel #2
0
 internal PluginLoadContext(IPlugin plugin, Type type, PluginInformation pluginInformation, List <Type> classes)
 {
     Plugin      = plugin;
     PluginType  = type;
     Information = pluginInformation;
     Classes     = classes;
 }