private static IJinxBotPlugin Instantiate(PluginInfo info, ProfilePluginConfiguration config) { IJinxBotPlugin plugin = Activator.CreateInstance(info.Type) as IJinxBotPlugin; if (plugin == null) throw new InvalidCastException("Could not cast \"" + info.Type.FullName + "\" to \"IJinxBotPlugin\"."); Dictionary<string, string> settings = config.Settings.ToDictionary(s => s.Name, s => s.Value); plugin.Startup(settings); return plugin; }
public JinxBotClient(ClientProfile profile) { m_activePlugins = new Dictionary<ProfilePluginConfiguration, IJinxBotPlugin>(); if (profile.SimulateClient) m_client = new SimulatedBattleNetClient(profile); else m_client = new BattleNetClient(profile); m_profile = profile; m_resourceProvider = ProfileResourceProvider.RegisterProvider(m_client); m_cmdTranslator = new CommandTranslator(this); bool hasSetCommandQueue = false; if (m_database == null) m_database = new JinxBotDefaultDatabase(); // finally, initialize ui m_window = new ProfileDocument(this); // initialize plugins m_commandHandlers = new List<ICommandHandler>(); foreach (ProfilePluginConfiguration pluginConfig in profile.PluginSettings) { hasSetCommandQueue = ProcessPlugin(hasSetCommandQueue, pluginConfig); } ProfilePluginConfiguration jsConfig = new ProfilePluginConfiguration { Assembly = "JinxBot.Plugins.Script.dll", Name = "JavaScript Plugin", Settings = new ProfilePluginSettingConfiguration[0], Type = "JinxBot.Plugins.Script.JinxBotJavaScriptPlugin" }; hasSetCommandQueue = ProcessPlugin(hasSetCommandQueue, jsConfig); if (!hasSetCommandQueue) { m_client.CommandQueue = new TimedMessageQueue(); } }
public static IJinxBotPlugin CreatePlugin(ProfilePluginConfiguration pluginConfiguration) { lock (m_lock) { if (!m_loadedAssemblies.ContainsKey(pluginConfiguration.Assembly)) if (!LoadAssembly(pluginConfiguration)) return null; if (!m_pluginTypes.ContainsKey(pluginConfiguration.Type)) if (!LoadType(pluginConfiguration)) return null; PluginInfo info = m_pluginTypes[pluginConfiguration.Type]; IJinxBotPlugin plugin = null; if (info.SupportsMultiClient && !string.IsNullOrEmpty(pluginConfiguration.MultiClientName)) { if (m_activeMultiClientPlugins.ContainsKey(pluginConfiguration.MultiClientName)) { plugin = m_activeMultiClientPlugins[pluginConfiguration.MultiClientName]; IMultiClientPlugin mcp = plugin as IMultiClientPlugin; m_instanceCounter[mcp] = m_instanceCounter[mcp] + 1; } else { plugin = Instantiate(info, pluginConfiguration); IMultiClientPlugin mcp = plugin as IMultiClientPlugin; if (mcp == null) throw new InvalidCastException("Plugin \"" + pluginConfiguration.Name + "\" is configured as a multi-client plugin but does not support that interface."); mcp.CreatePluginWindows(MainWindow); m_activeMultiClientPlugins.Add(pluginConfiguration.MultiClientName, mcp); m_instancesToNames.Add(mcp, pluginConfiguration.MultiClientName); m_instanceCounter.Add(mcp, 1); } } else { plugin = Instantiate(info, pluginConfiguration); } return plugin; } }
private bool ProcessPlugin(bool hasSetCommandQueue, ProfilePluginConfiguration pluginConfig) { IJinxBotPlugin plugin = PluginFactory.CreatePlugin(pluginConfig); m_activePlugins.Add(pluginConfig, plugin); // test if the plugin is a command queue ICommandQueue commandQueuePlugin = plugin as ICommandQueue; if (!hasSetCommandQueue && commandQueuePlugin != null) { m_client.CommandQueue = commandQueuePlugin; hasSetCommandQueue = true; } // test if the plugin is a database plugin IJinxBotDatabase databasePlugin = plugin as IJinxBotDatabase; if (databasePlugin != null) m_database = databasePlugin; // test if the plugin is a command handler ICommandHandler handler = plugin as ICommandHandler; if (handler != null) m_commandHandlers.Add(handler); // test if the plugin is multi-client IMultiClientPlugin mcp = plugin as IMultiClientPlugin; if (mcp != null) mcp.AddClient(this); else { ISingleClientPlugin scp = plugin as ISingleClientPlugin; if (scp != null) { scp.CreatePluginWindows(this.ProfileDocument); scp.RegisterEvents(this); } } return hasSetCommandQueue; }
private void EnumeratePlugins(ProfilePluginConfiguration[] profilePluginConfiguration) { }
public static void ClosePluginInstance(ProfilePluginConfiguration config, IJinxBotPlugin pluginInstance) { lock (m_lock) { SavePluginInstanceConfig(config, pluginInstance); IMultiClientPlugin mcp = pluginInstance as IMultiClientPlugin; if (mcp != null) { int count = m_instanceCounter[mcp]; if (count == 1) { // this is the last instance, so shut it down completely m_instanceCounter.Remove(mcp); mcp.DestroyPluginWindows(MainWindow); string name = m_instancesToNames[mcp]; m_instancesToNames.Remove(mcp); m_activeMultiClientPlugins.Remove(name); } else { m_instanceCounter[mcp] = count - 1; } } } }
private static bool LoadAssembly(ProfilePluginConfiguration pluginConfiguration) { string assemblyPath = Path.Combine(GetPluginsStoragePath(), pluginConfiguration.Assembly); if (!File.Exists(assemblyPath)) throw new FileNotFoundException("The specified plugin was not found."); try { Assembly asm = Assembly.LoadFile(assemblyPath); m_loadedAssemblies.Add(pluginConfiguration.Assembly, asm); return true; } catch { return false; } }
private static bool LoadType(ProfilePluginConfiguration pluginConfiguration) { Assembly asm = m_loadedAssemblies[pluginConfiguration.Assembly]; Type type = asm.GetType(pluginConfiguration.Type, false, false); if (type != null) { JinxBotPluginAttribute[] attr = type.GetCustomAttributes(typeof(JinxBotPluginAttribute), false) as JinxBotPluginAttribute[]; if (attr.Length == 0) return false; PluginInfo info = new PluginInfo(attr[0], type); m_pluginTypes.Add(pluginConfiguration.Type, info); return true; } return false; }
private static void SavePluginInstanceConfig(ProfilePluginConfiguration config, IJinxBotPlugin pluginInstance) { Dictionary<string, string> settings = config.Settings.ToDictionary(s => s.Name, s => s.Value); pluginInstance.Shutdown(settings); List<ProfilePluginSettingConfiguration> settingsToSave = new List<ProfilePluginSettingConfiguration>(); foreach (string key in settings.Keys) { settingsToSave.Add(new ProfilePluginSettingConfiguration { Name = key, Value = settings[key] }); } config.Settings = settingsToSave.ToArray(); }