Esempio n. 1
0
        /// <summary>
        ///		Scans for plugin files in the current directory.
        /// </summary>
        ///<param name="folder"></param>
        ///<returns></returns>
        protected IList <ObjectCreator> ScanForPlugins(string folder)
        {
            var pluginFactories = new List <ObjectCreator>();

#if NET_40 && !(XBOX || XBOX360 || WINDOWS_PHONE)
            this.SatisfyImports(folder);
            foreach (var plugin in plugins)
            {
                pluginFactories.Add(new ObjectCreator(plugin.GetType()));
                Debug.WriteLine(String.Format("MEF IPlugin: {0}.", plugin));
            }
#elif !(WINDOWS_PHONE)
            if (Directory.Exists(folder))
            {
                var files = Directory.GetFiles(folder);
                //var assemblyName = Assembly.GetExecutingAssembly().GetName().Name + ".dll";

                foreach (var file in files)
                {
                    var currentFile = Path.GetFileName(file);

                    if (Path.GetExtension(file) != ".dll" /*|| currentFile == assemblyName */)
                    {
                        continue;
                    }
                    var fullPath = Path.GetFullPath(file);

                    if (!_isValidModule(fullPath))
                    {
                        Debug.WriteLine(String.Format("Skipped {0} [Not managed]", fullPath));
                        continue;
                    }

                    var loader = new DynamicLoader(fullPath);

                    pluginFactories.AddRange(loader.Find(typeof(IPlugin)));
                }
            }
#endif
            return(pluginFactories);
        }
Esempio n. 2
0
        /// <summary>
        ///     Internal constructor.  This class cannot be instantiated externally.
        /// </summary>
        internal PlatformManager()
        {
            // First look in current Executing assembly for a PlatformManager
            if (instance == null)
            {
                var platformMgr = new DynamicLoader();
                var platforms   = platformMgr.Find(typeof(IPlatformManager));
                if (platforms.Count != 0)
                {
                    instance = platformMgr.Find(typeof(IPlatformManager))[0].CreateInstance <IPlatformManager>();
                }
            }

#if NET_40 && !(XBOX || XBOX360 || WINDOWS_PHONE)
            if (instance == null)
            {
                this.SatisfyImports(".");
                if (platforms != null && platforms.Count() != 0)
                {
                    instance = platforms.First();
                    System.Diagnostics.Debug.WriteLine(String.Format("MEF IPlatformManager: {0}.", instance));
                }
            }
#endif

#if !(SILVERLIGHT || WINDOWS_PHONE || XBOX || XBOX360 || NETFX_CORE)
            // Then look in loaded assemblies
            if (instance == null)
            {
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                for (var index = 0; index < assemblies.Length && instance == null; index++)
                {
                    //TODO: NRSC Added: Deal with Dynamic Assemblies not having a Location
                    //if (assemblies[index].IsDynamic)
                    //    continue;
                    try
                    {
                        var platformMgr = new DynamicLoader(assemblies[index]);
                        var platforms   = platformMgr.Find(typeof(IPlatformManager));
                        if (platforms.Count != 0)
                        {
                            instance = platformMgr.Find(typeof(IPlatformManager))[0].CreateInstance <IPlatformManager>();
                        }
                    }
                    catch (Exception)
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("Failed to load assembly: {0}.", assemblies[index].FullName));
                    }
                }
            }

            // Then look in external assemblies
            if (instance == null)
            {
                // find and load a platform manager assembly
                var cwd = Assembly.GetExecutingAssembly().CodeBase;
                var uri = new Uri(cwd);
                if (uri.IsFile)
                {
                    cwd = Path.GetDirectoryName(uri.LocalPath);
                }
                var files = Directory.GetFiles(cwd, "Axiom.Platforms.*.dll").ToArray();
                var file  = "";

                // make sure there is 1 platform manager available
                if (files.Length == 0)
                {
                    throw new PluginException("A PlatformManager was not found in the execution path, and is required.");
                }
                else
                {
                    var isWindows = IsWindowsOS;
                    var platform  = IsWindowsOS ? "Windows" : "Linux";

                    if (files.Length == 1)
                    {
                        file = files[0];
                    }
                    else
                    {
                        for (var i = 0; i < files.Length; i++)
                        {
                            if ((files[i].IndexOf(platform) != -1) == true)
                            {
                                file = files[i];
                            }
                        }
                    }

                    System.Diagnostics.Debug.WriteLine(String.Format("Selected the PlatformManager contained in {0}.", file));
                }

                var path = Path.Combine(System.IO.Directory.GetCurrentDirectory(), file);

                var platformMgr = new DynamicLoader(path);
                var platforms   = platformMgr.Find(typeof(IPlatformManager));
                if (platforms.Count != 0)
                {
                    instance = platformMgr.Find(typeof(IPlatformManager))[0].CreateInstance <IPlatformManager>();
                }
            }
#endif

            // All else fails, yell loudly
            if (instance == null)
            {
                throw new PluginException("The available Platform assembly did not contain any subclasses of PlatformManager, which is required.");
            }
        }