public PluginBase PickPlugin(IPluginRepository repository) { // Determine pool of available plugins int pool = repository.Plugins.Length; // If there are no items in the pool, then use a failsafe if (pool == 0) { var pluginFactory = new FailsafePluginFactory(); return pluginFactory.GetPlugin(); } // Otherwise, pick a plugin at random var selectedItem = Engine.Randomizer.Randomize(pool - 1); return repository.Plugins[selectedItem]; }
private PluginBase[] InitializePlugins() { List<PluginBase> oReturn = new List<PluginBase>(); try { // Loop through items in the root folder looking for plugins foreach (var file in Directory.EnumerateFiles(rootPath, "*.dll")) { var thatAss = Assembly.LoadFile(file); foreach (var type in thatAss.GetTypes()) { var attributes = type.GetCustomAttributes(typeof (PluginFactoryAttribute), false) .Cast<PluginFactoryAttribute>() .ToArray(); if (attributes.Length == 1) { var ctor = attributes[0].FactoryType.GetConstructor(new Type[]{}); var factory = ctor.Invoke(new object[]{}) as IPluginFactory; oReturn.Add(factory.GetPlugin()); } } } } catch (Exception) { // Ignore error for now } // If the collection of plugins is null (due to error or otherwise), // add the failsafe item if (!oReturn.Any()) { var factory = new FailsafePluginFactory(); oReturn.Add(factory.GetPlugin()); } return oReturn.ToArray(); }