public void ParseMsg(string msg) { try { if (!CommonUtil.IsValidCMD(ref msg)) { return; } Cmd cmd = CommonUtil.GetCmd(msg); bool isInternalCmd = CommonUtil.IsInternalCmd(cmd.Name); this.taskInfo = msg; if (isInternalCmd) {//内部命令 InternalCmdExecutor intercmder = new InternalCmdExecutor(this, new InterCmd(cmd.Name, cmd.Para)); intercmder.Execute(); } else { if (PluginMgr.IsContainsCmd(cmd.Name)) { Iplugin p = PluginMgr.GetPlugin(cmd.Name); this.needParseResult = p.NeedParseResult; Executor executor = new TaskExecutor(this, cmd.Para, p); executor.Execute(); } } } catch (Exception ex) { RobotCore.Log.Error(ex.ToString()); } }
private static void AsyncSendEmail(object obj) { string[] arr = (string[])obj; if (arr == null || arr.Length < 2) { RobotCore.Log.Warn("AsyncSendEmail by Invid arg"); } else { string title = arr[0]; string msg = arr[1]; string to = arr[2]; Iplugin p = PluginMgr.GetPlugin("mail"); if (string.IsNullOrEmpty(to)) { to = p.Remark; } if (p != null) { p.Execute(to, title, msg); } else { Log.Warn("Can not find mail plugin dll,send to:" + to + ",title:" + title + Environment.NewLine + msg + " failed!"); } } }
private static IList <Iplugin> GetPluginList() { IList <Iplugin> list = new List <Iplugin>(); string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PLUGINDIR); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); return(list); } string[] dllfiles = Directory.GetFiles(dir, "*.DLL"); for (int i = 0; i < dllfiles.Length; i++) { Assembly assembly = Assembly.LoadFile(dllfiles[i]); foreach (Type t in assembly.GetExportedTypes()) { if (t.IsClass && typeof(Iplugin).IsAssignableFrom(t)) { Iplugin plugin = (Iplugin)Activator.CreateInstance(t); list.Add(plugin); } } }//end of for return(list); }
public Executor(IRobotAnalyzer _convAnalyzer, string _cmdContent, Iplugin _plugin) { this.analyzer = _convAnalyzer; this.cmdContent = _cmdContent; this.plugin = _plugin; asychExecute = new asynchDelegate(plugin.Execute); }
private void listBoxPlugins_SelectedIndexChanged(object sender, EventArgs e) { Iplugin plugin = (Iplugin)listBoxPlugins.SelectedValue; txtboxPluginDetail.Clear(); StringBuilder sb = new StringBuilder(); sb.Append("名称:" + plugin.Name + Environment.NewLine); sb.Append(plugin.Description + Environment.NewLine); sb.Append("备注:" + plugin.Remark + Environment.NewLine); txtboxPluginDetail.Text = sb.ToString(); }
/// <summary> /// 读取并且实现插件内容 /// </summary> public static void LoadAllPlugins(ItfMain fm) { string[] files = Directory.GetFiles(Application.StartupPath + "\\plugins\\"); string file = null; if (files.Count() > 1) { FrmSelectPlugin fsp = new FrmSelectPlugin(files); if (fsp.ShowDialog() == DialogResult.Cancel) { System.Environment.Exit(0); } file = fsp.returnFile; fm.runLog.Text = "已加载插件:" + Path.GetFileNameWithoutExtension(file); } else { file = files[0]; fm.runLog.Text = "已加载插件:" + Path.GetFileNameWithoutExtension(file); } string ext = Path.GetExtension(file); if (ext == ".dll") { try { Assembly tmp = Assembly.LoadFile(file); Type[] types = tmp.GetTypes(); bool ok = false; foreach (Type t in types) { if (IsValidPlugin(t)) { Iplugin plugin = (Iplugin)tmp.CreateInstance(t.FullName); plugins.Add(plugin); ok = true; if (ok) { break; } } } } catch (Exception e) { throw e; } } }
public void ParseMsg(string msg) { try { RobotCore.Log.Info(string.Format("{0}对你的Fetion机器人说:{1}", contact.DisplayName, msg)); bool isPermitUser = RobotCore.PermissionMgr.IsPermitUser(conversation.From.ToString()); bool isValidCmd = CommonUtil.IsValidCMD(ref msg); if (!isValidCmd) { SendCmdInvalid(); return; } Cmd cmd = CommonUtil.GetCmd(msg); bool isInternalCmd = CommonUtil.IsInternalCmd(cmd.Name); if (isInternalCmd) {//内部命令 if (!isPermitUser && !CommonUtil.IsOpenInternalCmd(cmd.Name)) { SendSuggest(); } else { InternalCmdExecutor intercmder = new InternalCmdExecutor(this, new InterCmd(cmd.Name, cmd.Para)); intercmder.Execute(); } } else {//插件命令 bool hasPlugin = PluginMgr.IsContainsCmd(cmd.Name); if (hasPlugin) { Iplugin p = PluginMgr.GetPlugin(cmd.Name); if (!isPermitUser && p.IsHostOnly) {//未授权用户执行特定需授权插件中的命令 this.Send(string.Format("这个命令只有主人才能执行哦(w)")); return; } Executor executor = new ConvExecutor(this, cmd.Para, p); executor.Execute(); } else { SendSuggest(); } } } catch (Exception ex) { WriteLog("RobotConvAnalyzer ParseMsg occur error:" + ex.ToString());; } }
public static Iplugin GetPlugin(string cmd) { Iplugin plugin = null; foreach (Iplugin p in PlugList) { if (p.Name == cmd) { plugin = p; break; } } return(plugin); }
private void InitChildNodes(Iplugin plugin) { panelMain.Controls.Clear(); foreach (KeyValuePair <string, EventHandler> item in plugin.ChildNodes) { Button btn = new Button(); btn.Text = item.Key; btn.Click += item.Value; btn.Dock = DockStyle.Left; btn.BackColor = System.Drawing.Color.White; //btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; btn.Font = new System.Drawing.Font("宋体", 9.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); btn.ForeColor = System.Drawing.Color.SteelBlue; btn.UseVisualStyleBackColor = false; } }
private void InitModule(Iplugin plugin) { List <Button> _list = new List <Button>(); //PictureBox picture = new PictureBox(); //picture.BackColor = System.Drawing.Color.Transparent; //picture.Image = plugin.ModulePicture; //picture.InitialImage = null; //picture.Dock = DockStyle.Left; //// picture.Location = new System.Drawing.Point(42, 12); //picture.Size = new System.Drawing.Size(65, 71); //picture.TabStop = false; //panelTop.Controls.Add(picture); //picture.MouseEnter += (sender, e) => //{ // picture.Image = plugin.ModulePictureEnter; //}; //picture.MouseLeave += (sender, e) => //{ // picture.Image = plugin.ModulePicture; //}; //picture.MouseDown += (sender, e) => //{ // picture.Image = plugin.ModulePictureClick; //}; //picture.MouseUp += (sender, e) => //{ // picture.Image = plugin.ModulePictureEnter; //}; //picture.Click += (sender, e) => //{ // InitChildNodes(plugin); //}; KeyValuePair <string, EventHandler> item = plugin.ChildNodes.First(); ToolStripMenuItem _menuitem = new ToolStripMenuItem(); _menuitem.Text = item.Key; _menuitem.Click += item.Value; Mainmenu.Items.Add(_menuitem); }
static void Main(string[] args) { string @namespace = "Plugins"; var q = from t in Assembly.GetExecutingAssembly().GetTypes() where t.IsClass && t.Namespace == @namespace select t; q.ToList(); List <Iplugin> myList = new List <Iplugin>(); foreach (var item in q) { Iplugin temp = (Iplugin)Activator.CreateInstance(item); myList.Add(temp);// a list with all my plugins } foreach (var item in myList) { item.doSomthing("string"); } }
public ConvExecutor(IRobotAnalyzer _convAnalyzer, string _cmdContent, Iplugin _plugin) : base(_convAnalyzer, _cmdContent, _plugin) { }
public static void LoadAllPlugins() { PluginLoader.plugins.Clear(); string[] files = Directory.GetFiles(Application.StartupPath + "\\plugin\\"); int i = 0; PluginInfoAttribute typeAttribute = new PluginInfoAttribute(); foreach (string file in files) { string ext = file.Substring(file.LastIndexOf(".")); if (ext != ".dll") { continue; } try { Assembly tmp = Assembly.LoadFile(file); Type[] types = tmp.GetTypes(); bool ok = false; foreach (Type t in types) { if (IsValidPlugin(t)) { Iplugin plugin = (Iplugin)tmp.CreateInstance(t.FullName); plugins.Add(plugin); object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false); PluginInfoAttribute attribute = null; foreach (object attb in attbs) { if (attb is PluginInfoAttribute) { attribute = (PluginInfoAttribute)attb; attribute.Index = i; i++; ok = true; break; } } if (attribute != null) { piProperties.Add(attribute); plugin.PluginInfo = attribute; } else { throw new Exception("为定义插件属性"); } if (ok) { break; } } } } catch (Exception err) { throw err; } } plugins.Sort((p1, p2) => { return(p2.PluginInfo.Index - p1.PluginInfo.Index); }); }