Ejemplo n.º 1
0
        private static PluginInfoAttribute ConvertAttributeToPluginInfo(ICSharpCode.NRefactory.Ast.Attribute attribute)
        {
            Debug.Assert(attribute.Name == PLUGIN_INFO);

            var pluginInfo = new PluginInfoAttribute();

            foreach (var argument in attribute.NamedArguments)
            {
                var expression = argument.Expression as PrimitiveExpression;
                if (expression != null)
                {
                    if (argument.Name == NAME)
                    {
                        pluginInfo.Name = expression.Value as string;
                    }
                    else if (argument.Name == CATEGORY)
                    {
                        pluginInfo.Category = expression.Value as string;
                    }
                    else if (argument.Name == VERSION)
                    {
                        pluginInfo.Version = expression.Value as string;
                    }
                }
            }

            return(pluginInfo);
        }
        public void ConstructionWithKeyAndValue()
        {
            var key = "theKey";
              var value = "theValue";

              PluginInfoAttribute tested = new PluginInfoAttribute(key, value);
              Assert.AreEqual(key, tested.Key);
              Assert.AreEqual(value, tested.Value);
        }
        public void ConstructionWithKeyAndValue()
        {
            var key   = "theKey";
            var value = "theValue";

            PluginInfoAttribute tested = new PluginInfoAttribute(key, value);

            Assert.AreEqual(key, tested.Key);
            Assert.AreEqual(value, tested.Value);
        }
Ejemplo n.º 4
0
        private Dictionary <string, List <PluginInfo> > loadPlugins()
        {
            Dictionary <string, List <PluginInfo> > pluginInfos = new Dictionary <string, List <PluginInfo> >();

            foreach (Type pluginType in this.LoadedTypes.Values)
            {
                foreach (string interfaceName in PluginExtension.Interfaces)
                {
                    if (pluginType.GetInterface(interfaceName) != null)
                    {
                        if (!pluginInfos.ContainsKey(interfaceName))
                        {
                            pluginInfos.Add(interfaceName, new List <PluginInfo>());
                        }

                        PluginInfoAttribute attr = pluginType.GetPluginInfoAttribute();

                        PluginInfo p = new PluginInfo()
                        {
                            Model          = this.Model.GetPluginInfoModel(),
                            Caption        = attr.Caption,
                            DescriptionURL = attr.DescriptionUrl,
                            Icon           = pluginType.GetImage(0),
                            PluginType     = pluginType,
                            EncryptionType = pluginType.GetEncryptionTypeAttribute(),
                            Category       = PluginInfo.GetName(interfaceName),
                        };

                        pluginInfos[interfaceName].Add(p);

                        if (interfaceName == typeof(Cryptool.PluginBase.Editor.IEditor).FullName)
                        {
                            if (pluginType.Name == "Wizard")
                            {
                                this.PlaceHolderEditor = (IEditor)Activator.CreateInstance(pluginType);
                                TabItem item = new TabItem();
                                item.Content = (object)PlaceHolderEditor.Presentation;
                                this.PlaceHolderTabControl.Items.Add(item);
                            }
                        }
                    }
                }
            }
            return(pluginInfos);
        }
Ejemplo n.º 5
0
        Tuple <LoadPluginResultEnum, PluginInfoAttribute, IPlugin> TryLoadPlugin(string filename)
        {
            IPlugin             ret  = null;
            PluginInfoAttribute attr = null;

            try
            {
                var asm = Assembly.LoadFrom(filename);
                var tps = asm.GetTypes().Where(z => typeof(IPlugin).IsAssignableFrom(z)).ToArray();
                foreach (var zitem in tps)
                {
                    var aaa = zitem.GetCustomAttributes(true);
                    if (aaa.Any(z => z.GetType() == typeof(PluginInfoAttribute)))
                    {
                        attr = aaa.First(z => z.GetType() == typeof(PluginInfoAttribute)) as PluginInfoAttribute;
                    }

                    if (PluginsInstances.Any(z => z.GetType() == zitem))
                    {
                        return(new Tuple <LoadPluginResultEnum, PluginInfoAttribute, IPlugin>(LoadPluginResultEnum.AlreadyExist, attr, null));
                    }

                    var plg = Activator.CreateInstance(zitem) as IPlugin;
                    ret = plg;
                    plg.Activate(new PluginContext()
                    {
                        Container = mdi.MainForm
                    });
                    PluginsInstances.Add(plg);
                    break;//first plugin only? can one lib contains multiple plugins?
                }
            }
            catch (Exception ex)
            {
            }
            if (ret != null)
            {
                return(new Tuple <LoadPluginResultEnum, PluginInfoAttribute, IPlugin>(LoadPluginResultEnum.Success, attr, ret));
            }
            else
            {
                return(new Tuple <LoadPluginResultEnum, PluginInfoAttribute, IPlugin>(LoadPluginResultEnum.Fail, attr, ret));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 将插件嵌入到应用中。为插件生成操作按钮
        /// </summary>
        /// <param name="app"></param>
        /// <param name="插件ToolStripMenuItem">插件依托的操作按钮</param>
        public static void LoadPlugins(IApplicationObject app, ToolStripMenuItem 插件ToolStripMenuItem)
        {
            List <ToolStripMenuItem> pluginItemList = new List <ToolStripMenuItem>();

            foreach (IPlugin item in PluginLoader.plugins)
            {
                PluginInfoAttribute pluginInfo = (PluginInfoAttribute)Attribute.GetCustomAttribute(item.GetType(), typeof(PluginInfoAttribute));
                if (item.Connect(app) != ConnectionResult.Connection_Success)
                {
                    app.Alert("未成功加载插件" + pluginInfo.Name + "" + pluginInfo.Version);
                }
                else
                {
                    //为插件添加入口按钮:
                    ToolStripMenuItem pluginItem = new ToolStripMenuItem(pluginInfo.Name);
                    pluginItemList.Add(pluginItem);
                    pluginItem.Click += item.Response;
                    item.OnLoad();
                }
            }
            插件ToolStripMenuItem.DropDownItems.AddRange(pluginItemList.ToArray());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 加载插件
        /// </summary>
        private void LoadAllPlugins()
        {
            string[]            files         = Directory.GetFiles(Application.StartupPath + @"\plugins\");
            int                 i             = 0;
            PluginInfoAttribute typeAttribute = new PluginInfoAttribute();

            foreach (string file in files)                          //遍历指定路径下的文件名
            {
                string ext = file.Substring(file.LastIndexOf(".")); //检索文件类型
                if (ext != ".dll")                                  //查找.DLL文件(筛选1)
                {
                    continue;                                       //如果文件类型非.DLL执行下一轮循环
                }
                try {
                    Assembly tmp   = Assembly.LoadFile(file);                                                      //载入DLL,并实例化反射对象
                    Type[]   types = tmp.GetTypes();                                                               //获取模块里面的类
                    foreach (Type t in types)                                                                      //遍历模块里面的类
                    {
                        if (IsValidPlugin(t))                                                                      //如果包含接口"CSPluginKernel.IPlugin",说明插件适配与该主程序(筛选2)
                        {
                            plugins.Add(tmp.CreateInstance(t.FullName));                                           //将类实例化并添加到plugins动态数组里。
                            object[]            attbs     = t.GetCustomAttributes(typeAttribute.GetType(), false); //获取程序集“信息属性”,版本,作者等。
                            PluginInfoAttribute attribute = null;                                                  //定义信息属性对象,并指为空
                            foreach (object attb in attbs)                                                         //遍历信息属性集合
                            {
                                PluginInfoAttribute temP = attb as PluginInfoAttribute;                            //强制类型转换
                                if (temP != null)
                                {
                                    attribute = temP;    //这里temP等价于(PluginInfoAttribute)attb,
                                                         //另外在这时为对象初始化,attribute将不为null
                                    attribute.Index = i; //为Index属性赋值
                                    i++;
                                    break;
                                }
                            }
                            //如果读到“信息属性”,并实例化“信息属性”对象
                            if (attribute != null)
                            {
                                this.piProperties.Add(attribute);//将信息属性对象添加到piProperties动态数组
                            }
                            else
                            {
                                throw new Exception("未定义插件属性");//抛出异常
                            }
                        }
                    }
                } catch (Exception err) {
                    MessageBox.Show(err.Message);
                }
            }
            //遍历“信息属性”动态数组piProperties
            foreach (PluginInfoAttribute pia in piProperties)
            {
                ToolStripMenuItem ts = new ToolStripMenuItem(pia.Name + pia.Version);
                ts.Click += new EventHandler(RunPlugin); //为该菜单项注册单击事件
                toolStripDropDownButton1.DropDownItems.Add(ts);
                pia.Tag = ts;                            //将增加的信息菜单项添加到pia的附加信息里
            }
            //遍历“插件对象”动态数组
            foreach (IPlugin pi in plugins) //插件与主程序进行连接,如果成功则调用插件的OnLoad方法
            {
                if (pi.Connect((IfuncObject)this, (IvarObject)this))
                {
                    pi.OnLoad();
                }
                else
                {
                    MessageBox.Show("Can not connect plugin!");
                }
            }
        }
Ejemplo n.º 8
0
        private void LoadAllPlugins()
        {
            string[]            files         = Directory.GetFiles(Application.StartupPath + "\\plugins\\");
            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))
                        {
                            plugins.Add(tmp.CreateInstance(t.FullName));
                            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)
                            {
                                this.piProperties.Add(attribute);
                            }
                            else
                            {
                                throw new Exception("未定义插件属性");
                            }

                            if (ok)
                            {
                                break;
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            //随机产生颜色
            eMetroTileColor[] colors = Enum.GetValues(typeof(eMetroTileColor)) as eMetroTileColor[];
            Random            random = new Random();

            foreach (PluginInfoAttribute pia in piProperties)
            {
                //MenuItem tmp = menuItem6.MenuItems.Add(pia.Name + " " + pia.Version + " [ " + pia.Author + " ]");
                MetroTileItem MTI = new MetroTileItem();
                //随机赋值一个颜色
                MTI.TileColor = colors[random.Next(0, colors.Length)]; // eMetroTileColor.DarkGreen;
                MTI.Name      = pia.Name;
                MTI.TitleText = pia.Name;                              // +pia.Author + pia.Version;
                //MTI.Text = pia.Author;//
                MTI.Symbol             = "☺";
                MTI.TitleTextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
                MTI.Click += new EventHandler(RunPlugin);
                pia.Tag    = MTI;
                this.itemContainer1.SubItems.Add(MTI);
            }

            foreach (IPlugin pi in plugins)
            {
                if (pi.Connect((IApplicationObject)this) == ConnectionResult.Connection_Success)
                {
                    pi.OnLoad();
                }
                else
                {
                    MessageBox.Show("Can not connect plugin!");
                }
            }
        }
Ejemplo n.º 9
0
        private void LoadAllPlugins()
        {
            string[]            files         = Directory.GetFiles(Application.StartupPath + "\\plugins\\");
            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))
                        {
                            plugins.Add(tmp.CreateInstance(t.FullName));
                            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)
                            {
                                this.piProperties.Add(attribute);
                            }
                            else
                            {
                                throw new Exception("未定义插件属性");
                            }

                            if (ok)
                            {
                                break;
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            foreach (PluginInfoAttribute pia in piProperties)
            {
                pia.Tag = AddContextMenu(pia.Name, 插件ToolStripMenuItem.DropDownItems, new EventHandler(MenuClicked));
            }

            foreach (IPlugin pi in plugins)
            {
                if (pi.Connect((IApplicationObject)this) == ConnectionResult.Connection_Success)
                {
                    pi.OnLoad();
                }
                else
                {
                    MessageBox.Show("Can not connect plugin!");
                }
            }
        }
Ejemplo n.º 10
0
        public int LoadPlugs()
        {
            Plugs.Clear();
            PlugsInfo.Clear();

            if (!Directory.Exists(Environment.CurrentDirectory + @"\Plugins"))
            {
                return(0);
            }

            string[]            files         = Directory.GetFiles(Environment.CurrentDirectory + @"\Plugins");
            PluginInfoAttribute typeAttribute = new PluginInfoAttribute();

            foreach (var item in files)
            {
                string ext = Path.GetExtension(item);
                if (ext != ".dll")
                {
                    continue;
                }

                try
                {
                    Assembly tmp   = Assembly.UnsafeLoadFrom(item);
                    Type[]   types = tmp.GetTypes();
                    // 遍历实例获得实现接口的类
                    foreach (Type t in types)
                    {
                        if (t.GetInterface(interType.Name) != null)
                        {
                            T plug = (T)tmp.CreateInstance(t.FullName);
                            Plugs.Add(plug);

                            object[]            attbs     = t.GetCustomAttributes(typeAttribute.GetType(), false);
                            PluginInfoAttribute attribute = null;
                            foreach (object attb in attbs)
                            {
                                if (attb is PluginInfoAttribute)
                                {
                                    attribute = (PluginInfoAttribute)attb;
                                    break;
                                }
                            }

                            if (attribute != null)
                            {
                                PlugsInfo.Add(attribute);
                            }

                            CallBack();
                        }
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            }

            return(Plugs.Count);
        }
Ejemplo n.º 11
0
    // FIXME: should we use a thread to run each plugin?
    // FIXME: We should handle the case when there is a plugin that cannot
    // be loaded (Notifiying the user and so).
    // FIXME: We should verify that Plugin Names are unique and the plugin is
    // not duplicated in userdir and globaldir

    /*
     * Try to load as plugins all the assemblies found in globalPluginsDir.
     * The PluginInfo attribute must be present into the assembly.
     * If PluginInfo is not found the plugin is not loaded.
     */
    public void LoadPlugins()
    {
        if (enabled)
        {
            ArrayList files = new ArrayList();
            files.AddRange(Directory.GetFiles(globalPluginsDir));
            files.AddRange(Directory.GetFiles(userPluginsDir));
            foreach (string assemblie in files)
            {
#if DEBUG_PLUGINS
                Console.WriteLine("Trying to load assembly {0}", assemblie);
#endif
                try {
                    Assembly asm   = Assembly.LoadFrom(assemblie);
                    object[] attrs = asm.GetCustomAttributes(typeof(PluginInfoAttribute), false);
                    if (attrs.Length == 0)
                    {
#if DEBUG_PLUGINS
                        Console.WriteLine("WARNING: Plugin info not found in {0}",
                                          assemblie);
                        Console.WriteLine("Disabling plugin.");
#endif
                        // Assembly does not have PluginInfo, skip...
                        continue;
                    }
                    PluginInfoAttribute pinfo = attrs[0] as PluginInfoAttribute;
#if DEBUG_PLUGINS
                    Console.WriteLine("Info loaded");
#endif
                    IPlugin plugin = asm.CreateInstance(pinfo.Class) as IPlugin;
                    switch (pinfo.Type)
                    {
                    case PluginType.Active:
                        AppContext.ABus.AddMember(plugin);
                        break;

                    case PluginType.Passive:
                        AppContext.EBus.AddMember(plugin);
                        break;

                    case PluginType.Standard:
                        AppContext.EBus.AddMember(plugin);
                        AppContext.ABus.AddMember(plugin);
                        break;

                    default:
                        break;
                    }
                    pluginList.Add(plugin);
#if DEBUG_PLUGINS
                    Console.WriteLine("INFO: Plugin loaded succesfully.\nClass: {0}\nName: {1}",
                                      pinfo.Class,
                                      pinfo.Name);
#endif
                } catch (Exception e) {
#if DEBUG_PLUGINS
                    Console.WriteLine("ERROR: Could not load assembly");
                    Console.WriteLine(e.Message);
#endif
                }
            }
        }
    }