public static ChaskisEventFactory CreateEventFactory() { if (factoryInstance == null) { factoryInstance = ChaskisEventFactory.CreateInstance(new List <string>(FactoryPluginNames)); } return(factoryInstance); }
public void OneTimeSetUp() { this.ircConfig = TestHelpers.GetTestIrcConfig(); this.factoryInstance = TestHelpers.CreateEventFactory(); //this.sourcePluginName = "USERLISTBOT"; string creatorPluginName = TestHelpers.FactoryPluginNames[1]; this.creator = this.factoryInstance.EventCreators[creatorPluginName]; this.creatorPluginName = creatorPluginName.ToUpper(); }
public void FixtureSetup() { this.factory = TestHelpers.CreateEventFactory(); this.pluginName = TestHelpers.FactoryPluginNames[0]; this.targetPluginName = TestHelpers.FactoryPluginNames[1]; this.expectedArgs = new Dictionary <string, string>(); this.expectedArgs.Add("arg1", "arg1Value"); this.expectedArgs.Add("arg2", "arg2Value"); this.expectedPassthroughArgs = new Dictionary <string, string>(); this.expectedPassthroughArgs.Add("earg1", "earg 1 value"); this.expectedPassthroughArgs.Add("earg2", "earg 2 value"); }
public void DoubleInstanceFailure() { Assert.Throws <InvalidOperationException>( () => ChaskisEventFactory.CreateInstance(TestHelpers.FactoryPluginNames) ); }
// -------- Functions -------- /// <summary> /// Loads the given list of plugins. /// Any errors are logged to the passed in logger. /// </summary> /// <param name="assemblyList">List of assemblies we are to load.</param> /// <param name="existingPlugins">Already created plugins that do not need to be inited via reflection.</param> /// <param name="ircConfig">The irc config we are using.</param> /// <param name="chaskisConfigRoot">The root of the chaskis config.</param> public bool LoadPlugins( IList <AssemblyConfig> assemblyList, IList <PluginConfig> existingPlugins, IIrcConfig ircConfig, IChaskisEventScheduler scheduler, IChaskisEventSender eventSender, HttpClient httpClient, string chaskisConfigRoot ) { bool success = true; foreach (AssemblyConfig assemblyConfig in assemblyList) { try { Assembly dll = Assembly.LoadFrom(assemblyConfig.AssemblyPath); // Grab all the plugins, which have the ChaskisPlugin Attribute attached to them. var types = from type in dll.GetTypes() where type.IsDefined(typeof(ChaskisPlugin), false) select type; foreach (Type type in types) { // Make instance object instance = Activator.CreateInstance(type); IPlugin plugin = instance as IPlugin; if (plugin == null) { string errorString = string.Format( "Can not cast {0} to {1}, make sure your {0} class implements {1}", type.Name, nameof(IPlugin) ); throw new InvalidCastException(errorString); } ChaskisPlugin chaskisPlugin = type.GetCustomAttribute <ChaskisPlugin>(); this.plugins.Add( chaskisPlugin.PluginName, new PluginConfig( assemblyConfig.AssemblyPath, chaskisPlugin.PluginName, assemblyConfig.BlackListedChannels, plugin, new GenericLogger() ) ); StaticLogger.Log.WriteLine("Successfully loaded plugin: " + chaskisPlugin.PluginName); } } catch (Exception e) { StringBuilder errorString = new StringBuilder(); errorString.AppendLine("*************"); errorString.AppendLine("Warning! Error when loading assembly " + assemblyConfig.AssemblyPath + ":"); errorString.AppendLine(e.Message); errorString.AppendLine(); errorString.AppendLine(e.StackTrace); errorString.AppendLine(); if (e.InnerException != null) { errorString.AppendLine("\tInner Exception:"); errorString.AppendLine("\t\t" + e.InnerException.Message); errorString.AppendLine("\t\t" + e.InnerException.StackTrace); } errorString.AppendLine("*************"); success = false; StaticLogger.Log.ErrorWriteLine(errorString.ToString()); } } foreach (PluginConfig existingPlugin in existingPlugins) { this.plugins.Add(existingPlugin.Name, existingPlugin); } this.eventFactory = ChaskisEventFactory.CreateInstance(this.plugins.Keys.ToList()); foreach (KeyValuePair <string, PluginConfig> plugin in this.plugins) { try { PluginInitor initor = new PluginInitor { PluginPath = plugin.Value.AssemblyPath, IrcConfig = ircConfig, EventScheduler = scheduler, ChaskisEventSender = eventSender, ChaskisConfigRoot = chaskisConfigRoot, ChaskisEventCreator = this.eventFactory.EventCreators[plugin.Key], HttpClient = httpClient, Log = plugin.Value.Log }; initor.Log.OnWriteLine += delegate(string msg) { StaticLogger.Log.WriteLine("{0}> {1}", plugin.Value.Name, msg); }; initor.Log.OnErrorWriteLine += delegate(string msg) { StaticLogger.Log.ErrorWriteLine("{0}> {1}", plugin.Value.Name, msg); }; plugin.Value.Plugin.Init(initor); StaticLogger.Log.WriteLine("Successfully inited plugin: " + plugin.Value.Name); } catch (Exception e) { StringBuilder errorString = new StringBuilder(); errorString.AppendLine("*************"); errorString.AppendLine("Warning! Error when initing plugin " + plugin.Key + ":"); errorString.AppendLine(e.Message); errorString.AppendLine(); errorString.AppendLine(e.StackTrace); errorString.AppendLine(); Exception innerException = e.InnerException; if (innerException != null) { errorString.AppendLine("\tInner Exception:"); errorString.AppendLine("\t\t" + e.InnerException.Message); errorString.AppendLine("\t\t" + e.InnerException.StackTrace); innerException = innerException.InnerException; } errorString.AppendLine("*************"); success = false; StaticLogger.Log.ErrorWriteLine(errorString.ToString()); } } return(success); }