コード例 #1
0
ファイル: CLI.cs プロジェクト: codacy-badger/Byt3
        /// <summary>
        /// Tries to create a chain collection from a name
        /// </summary>
        /// <param name="asm">The containing assembly</param>
        /// <param name="name">The name of the collection(or "*" for any collection)</param>
        /// <param name="collection">The out parameter containing the created collection</param>
        /// <returns>The success state</returns>
        private static bool TryCreateChainCollection(Assembly asm, string name, out IChainCollection collection)
        {
            Type[] types = asm.GetTypes();
            if (name == "*")
            {
                Type tt = types.FirstOrDefault(x => x.GetInterfaces().Contains(typeof(IChainCollection)));
                if (tt != null)
                {
                    collection = (IChainCollection)Activator.CreateInstance(tt);
                }
                collection = null;
            }
            else
            {
                collection = types.Where(x => x.GetInterfaces().Contains(typeof(IChainCollection)))
                             .Select(x => (IChainCollection)Activator.CreateInstance(x)).FirstOrDefault(x => x.Name == name);
            }

            return(collection != null);
        }
コード例 #2
0
ファイル: CLI.cs プロジェクト: codacy-badger/ext-pp
        /// <summary>
        /// Creates the Plugin chain based on a argument in ChainSyntax.
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="noCollection"></param>
        /// <returns></returns>
        private IEnumerable <AbstractPlugin> CreatePluginChain(string[] arg, bool noCollection)
        {
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL3, "Creating Plugin Chain...");
            List <AbstractPlugin> ret = new List <AbstractPlugin>();

            string[] names = null;
            string   path  = "";

            foreach (var plugin in arg)
            {
                ParseChainSyntax(plugin, out path, out names);


                if (File.Exists(path))
                {
                    Assembly asm          = Assembly.LoadFile(Path.GetFullPath(path));
                    Type[]   types        = asm.GetTypes();
                    bool     isCollection = names != null && names.Length == 1 && names[0].StartsWith('(') &&
                                            names[0].EndsWith(')');
                    if ((names == null && !noCollection) || isCollection)
                    {
                        if (names == null)
                        {
                            Type t = types.FirstOrDefault(x => x.GetInterfaces().Contains(typeof(IChainCollection)));
                            if (t != null)
                            {
                                List <AbstractPlugin> r = ((IChainCollection)Activator.CreateInstance(t)).GetChain()
                                                          .Select(x => (AbstractPlugin)Activator.CreateInstance(x)).ToList();
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Creating Chain Collection with Plugins: {0}", r.Select(x => x.GetType().Name).Unpack(", "));
                                ret.AddRange(r);
                            }
                        }
                        else
                        {
                            names[0] = names[0].Trim('(', ')');
                            this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Searching Chain Collection: {0}", names[0]);

                            IChainCollection coll = types.Where(x => x.GetInterfaces().Contains(typeof(IChainCollection)))
                                                    .Select(x => (IChainCollection)Activator.CreateInstance(x)).FirstOrDefault(x => x.GetName() == names[0]);

                            if (coll != null)
                            {
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Found Chain Collection: {0}", names[0]);
                                List <AbstractPlugin> r = coll.GetChain()
                                                          .Select(x => (AbstractPlugin)Activator.CreateInstance(x)).ToList();
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Creating Chain Collection with Plugins: {0}", r.Select(x => x.GetType().Name).Unpack(", "));
                                ret.AddRange(r);
                            }
                        }
                    }
                    else
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL4, "Loading {0} in file {1}", names == null ? "all plugins" : names.Unpack(", "), path);

                        if (names == null)
                        {
                            ret.AddRange(_pluginManager.FromFile(path));
                        }
                        else
                        {
                            List <AbstractPlugin> plugins = _pluginManager.FromFile(path);
                            for (int i = 0; i < names.Length; i++)
                            {
                                for (int j = 0; j < plugins.Count; j++)
                                {
                                    if (plugins[j].Prefix.Contains(names[i]))
                                    {
                                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Creating instance of: {0}", plugins[j].GetType().Name);
                                        ret.Add(plugins[j]);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "Could not load file: {0}", path);
                }
            }

            return(ret);
        }