// Summary:
        //     Load extensions which are located in the bin\extensions\ directory.
        public void loadExtensions()
        {
            var      allAssemblies  = AppDomain.CurrentDomain.GetAssemblies();
            Assembly exeAssembly    = Assembly.GetExecutingAssembly();
            string   exeLocation    = exeAssembly.Location;
            string   exePath        = Path.GetDirectoryName(exeLocation);
            string   extensionsPath = exePath + "\\extensions";

            if (!Directory.Exists(extensionsPath))
            {
                return;
            }

            // try to load *.dll in bin\extensions\
            var files = Directory.EnumerateFiles(extensionsPath, "*.dll",
                                                 SearchOption.TopDirectoryOnly);

            foreach (string file in files)
            {
                // skip the assembly that has been loaded
                string shortName = Path.GetFileName(file);
                if (allAssemblies.Any(x => x.ManifestModule.Name == shortName))
                {
                    continue;
                }

                // Assembly.LoadFile doesn't resolve dependencies,
                // so don't use Assembly.LoadFile
                Assembly assembly = Assembly.LoadFrom(file);
                if (assembly != null)
                {
                    _loadedExtensions.Add(assembly);
                }
            }

            // call init() in extensions
            foreach (Assembly assembly in _loadedExtensions)
            {
                // call init() function in the loaded assembly
                var types = from type in assembly.GetTypes()
                            where type.IsSubclassOf(typeof(iS3.Core.Extensions))
                            select type;
                foreach (var type in types)
                {
                    object obj = Activator.CreateInstance(type);
                    iS3.Core.Extensions extension = obj as iS3.Core.Extensions;
                    if (extension == null)
                    {
                        continue;
                    }
                    string msg = extension.init();
                    output(msg);
                }
            }
        }
        // Summary:
        //     Load tools which are located in the bin\tools\ directory.
        public void loadToolboxes()
        {
            var      allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            Assembly exeAssembly   = Assembly.GetExecutingAssembly();
            string   exeLocation   = exeAssembly.Location;
            string   exePath       = Path.GetDirectoryName(exeLocation);
            string   toolsPath     = exePath + "\\tools";

            if (!Directory.Exists(toolsPath))
            {
                return;
            }

            // try to load *.dll in bin\tools\
            var files = Directory.EnumerateFiles(toolsPath, "*.dll",
                                                 SearchOption.TopDirectoryOnly);

            foreach (string file in files)
            {
                string shortName = Path.GetFileName(file);
                if (allAssemblies.Any(x => x.ManifestModule.Name == shortName))
                {
                    continue;
                }

                // Assembly.LoadFile doesn't resolve dependencies,
                // so don't use Assembly.LoadFile
                Assembly assembly = Assembly.LoadFrom(file);
                if (assembly != null)
                {
                    _loadedToolboxes.Add(assembly);
                }
            }

            foreach (Assembly assembly in _loadedToolboxes)
            {
                // call init() function in the loaded assembly
                var types = from type in assembly.GetTypes()
                            where type.IsSubclassOf(typeof(iS3.Core.Extensions))
                            select type;
                foreach (var type in types)
                {
                    object obj = Activator.CreateInstance(type);
                    iS3.Core.Extensions extension = obj as iS3.Core.Extensions;
                    if (extension == null)
                    {
                        continue;
                    }
                    string msg = extension.init();
                    output(msg);
                }

                // call tools.treeItems() can add it to ToolsPanel
                types = from type in assembly.GetTypes()
                        where type.IsSubclassOf(typeof(Tools))
                        select type;
                foreach (var type in types)
                {
                    object obj   = Activator.CreateInstance(type);
                    Tools  tools = obj as Tools;
                    IEnumerable <ToolTreeItem> treeitems = tools.treeItems();
                    if (treeitems == null)
                    {
                        continue;
                    }
                    foreach (var item in treeitems)
                    {
                        ToolsPanel.toolboxesTree.add(item);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public List <iS3MenuItem> LoadExtension()
        {
            var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            //Assembly exeAssembly = Assembly.GetExecutingAssembly();
            string exeLocation    = System.IO.Directory.GetCurrentDirectory();
            string extensionsPath = exeLocation + "\\extensions";

            if (!Directory.Exists(extensionsPath))
            {
                return(null);
            }

            // try to load *.dll in bin\extensions\
            var files = Directory.EnumerateFiles(extensionsPath, "*.dll",
                                                 SearchOption.TopDirectoryOnly);
            List <iS3MenuItem> menuTreeItems = new List <iS3MenuItem>();

            foreach (string file in files)
            {
                // skip the assembly that has been loaded
                string shortName = Path.GetFileName(file);
                if (allAssemblies.Any(x => x.ManifestModule.Name == shortName))
                {
                    continue;
                }

                // Assembly.LoadFile doesn't resolve dependencies,
                // so don't use Assembly.LoadFile
                Assembly assembly = Assembly.LoadFrom(file);
                if (assembly != null)
                {
                    _loadedExtensions.Add(assembly);
                }
            }

            // call init() in extensions
            foreach (Assembly assembly in _loadedExtensions)
            {
                // call init() function in the loaded assembly
                var types = from type in assembly.GetTypes()
                            where type.IsSubclassOf(typeof(Extensions))
                            select type;
                foreach (var type in types)
                {
                    object     obj       = Activator.CreateInstance(type);
                    Extensions extension = obj as Extensions;
                    if (extension == null)
                    {
                        continue;
                    }
                    string msg = extension.init();
                    _mainFrame.output(msg);
                    // add menu
                    if (extension.menuItems() != null)
                    {
                        List <iS3MenuItem> menuitems = extension.menuItems().ToList();
                        menuTreeItems.AddRange(menuitems);
                    }
                }
                //save assembly by the dll name
                //
                if (assembly.FullName.Split(',').Length > 0)
                {
                    assemblyDict[assembly.FullName.Split(',')[0]] = assembly;
                }
            }
            return(menuTreeItems);
        }