Example #1
0
        /// <summary>
        /// Function to load our useless image codec plug in.
        /// </summary>
        /// <returns><b>true</b> if successful, <b>false</b> if not.</returns>
        private bool LoadCodec()
        {
            const string pluginName = "Gorgon.Graphics.Example.TvImageCodecPlugIn";

            _pluginCache = new GorgonMefPlugInCache(GorgonApplication.Log);

            // Load our plug in.
            _pluginCache.LoadPlugInAssemblies(GorgonApplication.StartupPath.FullName, "TVImageCodec.dll");

            // Activate the plugin service.
            IGorgonPlugInService pluginService = new GorgonMefPlugInService(_pluginCache);

            // Find the plugin.
            GorgonImageCodecPlugIn plugIn = pluginService.GetPlugIn <GorgonImageCodecPlugIn>(pluginName);

            if ((plugIn == null) || (plugIn.Codecs.Count == 0))
            {
                return(false);
            }

            // Normally you would enumerate the plug ins, but in this case we know there's only one.
            _customCodec = plugIn.CreateCodec(plugIn.Codecs[0].Name);

            return(_customCodec != null);
        }
Example #2
0
        /// <summary>
        /// Function to load the Gorgon pack file provider plugin.
        /// </summary>
        /// <returns>The file system provider.</returns>
        private IGorgonFileSystemProvider LoadGorPackProvider()
        {
            // The Gorgon packed file provider plug in dll.
            const string gorPackDll = "Gorgon.FileSystem.GorPack.dll";
            // The name of the Gorgon packed file plugin.
            const string gorPackPlugInName = "Gorgon.IO.GorPack.GorPackProvider";

            // Like the zip file example, we'll just create the plugin infrastructure, grab the provider object
            // and get rid of the plugin stuff since we won't need it again.
            _assemblyCache = new GorgonMefPlugInCache(GorgonApplication.Log);
            _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, gorPackDll);

            var plugIns = new GorgonMefPlugInService(_assemblyCache);

            return(plugIns.GetPlugIn <GorgonFileSystemProvider>(gorPackPlugInName));
        }
Example #3
0
        private static void Main(string[] args)
        {
            _log = new GorgonLog("Example 004", "Tape_Worm", typeof(Program).Assembly.GetName().Version);

            // Set up the assembly cache.
            // We'll need the assemblies loaded into this object in order to load our plugin types.
            var pluginCache = new GorgonMefPlugInCache(_log);

            // Create our plugin service.
            // This takes the cache of assemblies we just created.
            IGorgonPlugInService pluginService = new GorgonMefPlugInService(pluginCache);

            try
            {
                Console.Title           = "Gorgon Example #4 - PlugIns.";
                Console.ForegroundColor = ConsoleColor.White;

                Console.WriteLine("This is an example to show how to create and use custom plugins.");
                Console.WriteLine("The plugin interface in Gorgon is quite flexible and gives the developer");
                Console.WriteLine("the ability to allow extensions to their own applications.\n");

                Console.ResetColor();

                pluginCache.LoadPlugInAssemblies(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]).FormatDirectory(Path.DirectorySeparatorChar), "Example004.*PlugIn.dll");
                Console.WriteLine("{0} plugin assemblies found.", pluginCache.PlugInAssemblies.Count);

                if (pluginCache.PlugInAssemblies.Count == 0)
                {
                    return;
                }

                // Our text writer plugin interfaces.
                IList <TextColorWriter> writers = new List <TextColorWriter>();

                // Create our plugin instances, we'll limit to 9 entries just for giggles.
                TextColorPlugIn[] plugins = (from pluginName in pluginService.GetPlugInNames()
                                             let plugin = pluginService.GetPlugIn <TextColorPlugIn>(pluginName)
                                                          where plugin != null
                                                          select plugin).ToArray();

                // Display a list of the available plugins.
                Console.WriteLine("\n{0} Plug-ins loaded:\n", plugins.Length);
                for (int i = 0; i < plugins.Length; i++)
                {
                    // Here's where we make use of our description.
                    Console.WriteLine("{0}. {1} ({2})", i + 1, plugins[i].Description, plugins[i].GetType().FullName);

                    // Create the text writer interface and add it to the list.
                    writers.Add(plugins[i].CreateWriter());
                }

                Console.Write("0. Quit\n\nSelect a plugin:  ");

                // Loop until we quit.
                while (true)
                {
                    if (!Console.KeyAvailable)
                    {
                        continue;
                    }

                    Console.ResetColor();

                    // Remember our cursor coordinates.
                    int cursorX = Console.CursorLeft; // Cursor position.
                    int cursorY = Console.CursorTop;

                    ConsoleKeyInfo keyValue = Console.ReadKey(false);

                    if (char.IsNumber(keyValue.KeyChar))
                    {
                        if (keyValue.KeyChar == '0')
                        {
                            break;
                        }

                        // Move to the next line and clear the previous line of text.
                        Console.WriteLine();
                        Console.Write(new string(' ', Console.BufferWidth - 1));
                        Console.CursorLeft = 0;

                        // Call our text color writer to print the text in the plugin color.
                        int writerIndex = keyValue.KeyChar - '0';
                        writers[writerIndex - 1].WriteString($"You pressed #{writerIndex}.");
                    }

                    Console.CursorTop  = cursorY;
                    Console.CursorLeft = cursorX;
                }
            }
            catch (Exception ex)
            {
                ex.Catch(_ =>
                {
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Exception:\n{0}\n\nStack Trace:{1}", ex.Message, ex.StackTrace);
                },
                         _log);
                Console.ResetColor();
#if DEBUG
                Console.ReadKey(true);
#endif
            }
            finally
            {
                // Always call dispose so we can unload our temporary application domain.
                //pluginAssemblies.Dispose();
                pluginCache.Dispose();

                _log.LogEnd();
            }
        }