Example #1
0
        /// <summary>
        /// Function to load the file system provider plug ins.
        /// </summary>
        /// <param name="pluginCache">The MEF plug in cache used to load the file system plug ins.</param>
        /// <param name="pluginDir">The plug in directory.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="pluginCache"/>, or the <paramref name="pluginDir"/> parameter is <b>null</b>.</exception>
        public void LoadProviders(GorgonMefPlugInCache pluginCache, DirectoryInfo pluginDir)
        {
            if (pluginCache == null)
            {
                throw new ArgumentNullException(nameof(pluginCache));
            }

            if (pluginDir == null)
            {
                throw new ArgumentNullException(nameof(pluginDir));
            }

            IReadOnlyList <PlugInAssemblyState> assemblies = pluginCache.ValidateAndLoadAssemblies(pluginDir.GetFiles("*.dll"), Program.Log);

            if (assemblies.Count > 0)
            {
                foreach (PlugInAssemblyState record in assemblies.Where(item => !item.IsAssemblyLoaded && item.IsManaged))
                {
                    _disabled[Path.GetFileName(record.Path)] = new DisabledPlugIn(DisabledReasonCode.Error, Path.GetFileName(record.Path), record.LoadFailureReason, record.Path);
                }
            }

            IGorgonPlugInService plugins = new GorgonMefPlugInService(pluginCache);
            IReadOnlyList <GorgonFileSystemProvider> readers = plugins.GetPlugIns <GorgonFileSystemProvider>();
            IReadOnlyList <FileWriterPlugIn>         writers = plugins.GetPlugIns <FileWriterPlugIn>();

            // Get readers.
            foreach (IGorgonFileSystemProvider reader in readers)
            {
                try
                {
                    Program.Log.Print($"Creating file system reader plug in '{reader.Name}'...", LoggingLevel.Simple);
                    _readers[reader.Name] = reader;
                }
                catch (Exception ex)
                {
                    Program.Log.Print($"ERROR: Cannot create file system reader plug in '{reader.Name}'.", LoggingLevel.Simple);
                    Program.Log.LogException(ex);

                    _disabled[reader.Name] = new DisabledPlugIn(DisabledReasonCode.Error, reader.Name, string.Format(Resources.GOREDIT_DISABLE_FILE_PROVIDER_EXCEPTION, ex.Message), reader.ProviderPath);
                }
            }

            // Get writers
            foreach (FileWriterPlugIn writer in writers)
            {
                IReadOnlyList <string> disabled = writer.IsPlugInAvailable();

                try
                {
                    Program.Log.Print($"Creating file system writer plug in '{writer.Name}'...", LoggingLevel.Simple);

                    if (disabled.Count != 0)
                    {
                        Program.Log.Print($"WARNING: The file system writer plug in '{writer.Name}' is disabled:", LoggingLevel.Simple);
                        foreach (string reason in disabled)
                        {
                            Program.Log.Print($"WARNING: {reason}", LoggingLevel.Verbose);
                        }

                        _disabled[writer.Name] = new DisabledPlugIn(DisabledReasonCode.ValidationError, writer.Name, string.Join("\n", disabled), writer.PlugInPath);
                        continue;
                    }

                    writer.AssignCommonServices(_commonServices);
                    _writers[writer.GetType().FullName] = writer;
                }
                catch (Exception ex)
                {
                    Program.Log.Print($"ERROR: Cannot create file system writer plug in '{writer.Name}'.", LoggingLevel.Simple);
                    Program.Log.LogException(ex);

                    _disabled[writer.Name] = new DisabledPlugIn(DisabledReasonCode.Error, writer.Name, string.Format(Resources.GOREDIT_DISABLE_FILE_PROVIDER_EXCEPTION, ex.Message), writer.PlugInPath);
                }
            }
        }