/// <summary> /// Function to load in the gaming device driver plug ins. /// </summary> /// <returns>A list of gaming device driver plug ins.</returns> private static IReadOnlyList <IGorgonGamingDeviceDriver> GetGamingDeviceDrivers() { GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.InputPlugInPath); // Access our plugin cache. _pluginCache = new GorgonMefPlugInCache(GorgonApplication.Log); // Get the files from the plugin directory. // The plugin directory can be changed in the configuration file // to point at wherever you'd like. If a {0} place holder is // in the path, it will be replaced with whatever the build // configuration is set to (i.e. DEBUG or RELEASE). _pluginCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.Input.*.dll"); if (_pluginCache.PlugInAssemblies.Count == 0) { return(Array.Empty <IGorgonGamingDeviceDriver>()); } // Create our plugin service. IGorgonPlugInService pluginService = new GorgonMefPlugInService(_pluginCache); // Create our input service factory. var factory = new GorgonGamingDeviceDriverFactory(pluginService, GorgonApplication.Log); // Retrieve the list of driver plug ins from the input service factory. return(factory.LoadAllDrivers()); }
/// <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); }
/// <summary> /// Function to load the file system providers. /// </summary> /// <returns>The number of file system provider plug ins.</returns> private static int LoadFileSystemProviders() { _pluginAssemblies.LoadPlugInAssemblies(PlugInPath, "Gorgon.FileSystem.*"); // Get the file system provider factory so we can retrieve our newly loaded providers. IGorgonFileSystemProviderFactory providerFactory = new GorgonFileSystemProviderFactory(_pluginService, _log); // Get all the providers. // We could limit this to a single provider, or to a single plugin assembly if we choose. But for // this example, we'll get everything we've got. _providers = providerFactory.CreateProviders(); return(_providers.Count); }
/// <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)); }
/// <summary> /// Function to load the zip file provider plugin. /// </summary> /// <returns><b>true</b> if successfully loaded, <b>false</b> if not.</returns> private static bool LoadZipProviderPlugIn() { var zipProviderFile = new FileInfo(Path.Combine(PlugInPath.FormatDirectory(Path.DirectorySeparatorChar), "Gorgon.FileSystem.Zip.dll")); // Check to see if the file exists. if (!zipProviderFile.Exists) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Could not find the plugin assembly file:\n'{0}'.", zipProviderFile.FullName); Console.ResetColor(); #if DEBUG Console.ReadKey(); #endif return(false); } // Load the plugin assembly. _pluginAssemblies.LoadPlugInAssemblies(zipProviderFile.Directory?.FullName, zipProviderFile.Name); // Create our file system provider factory so we can retrieve the zip file provider. var providerFactory = new GorgonFileSystemProviderFactory(_pluginService, _log); // Get our zip file provider. GorgonFileSystemProvider provider; try { provider = providerFactory.CreateProvider(PlugInName); } catch (GorgonException gEx) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(gEx.Message); Console.ResetColor(); #if DEBUG Console.ReadKey(); #endif return(false); } _fileSystem = new GorgonFileSystem(provider, _log); Console.WriteLine("\nThe zip file file system provider was loaded successfully."); return(true); }
/// <summary> /// Raises the <see cref="E:System.Windows.Forms.Form.Load" /> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.PlugInLocation); // Create the assembly cache. _assemblies = new GorgonMefPlugInCache(GorgonApplication.Log); _assemblies.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.Input.XInput.dll"); // Create the plug services. IGorgonPlugInService pluginService = new GorgonMefPlugInService(_assemblies); // Create the gaming device driver factory. var factory = new GorgonGamingDeviceDriverFactory(pluginService, GorgonApplication.Log); // Create our factory. _driver = factory.LoadDriver("Gorgon.Input.XInput.GorgonXInputDriver"); _stickPosition = new PointF[3]; _sprayStates = new SprayCan[3]; // Get our list of active controllers. UpdateActiveControllers(); // Get the graphics interface for our panel. _surface = new DrawingSurface(panelDisplay); // Set up our idle loop. GorgonApplication.IdleMethod += Idle; // Launch a background task to check for connected devices. Task.Run(CheckForConnectedDevices); } catch (Exception ex) { // We do this here instead of just calling the dialog because this // function will send the exception to the Gorgon log file. GorgonExample.HandleException(ex); GorgonApplication.Quit(); } }
/// <summary> /// Function to load the zip file system provider. /// </summary> private void LoadZipFileSystemProvider() { // Name of our zip provider plugin. const string zipProviderPlugInName = "Gorgon.IO.Zip.ZipProvider"; // We can load the objects we need and discard the plugin system after. // This works because we keep the references to the objects that our // plugin creates, even after the plugin is gone. using (var pluginAssemblies = new GorgonMefPlugInCache(Program.Log)) { pluginAssemblies.LoadPlugInAssemblies(Program.PlugInPath, "Gorgon.FileSystem.Zip.DLL"); var providerFactory = new GorgonFileSystemProviderFactory( new GorgonMefPlugInService(pluginAssemblies), Program.Log); _fileSystem = new GorgonFileSystem(providerFactory.CreateProvider(zipProviderPlugInName), Program.Log); } }
/// <summary> /// Function to initialize the application. /// </summary> private static async Task InitializeAsync(FormMain window) { try { GorgonExample.ResourceBaseDirectory = new DirectoryInfo(Settings.Default.ResourceLocation); GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.PlugInLocation); // Load our packed file system plug in. window.UpdateStatus("Loading plugins..."); IGorgonPlugInService plugIns = await Task.Run(() => { _assemblyCache = new GorgonMefPlugInCache(GorgonApplication.Log); _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.FileSystem.GorPack.dll"); return(new GorgonMefPlugInService(_assemblyCache)); }); window.UpdateStatus("Initializing graphics..."); // Retrieve the list of video adapters. We can do this on a background thread because there's no interaction between other threads and the // underlying D3D backend yet. IReadOnlyList <IGorgonVideoAdapterInfo> videoDevices = await Task.Run(() => GorgonGraphics.EnumerateAdapters(log: GorgonApplication.Log)); if (videoDevices.Count == 0) { throw new GorgonException(GorgonResult.CannotCreate, "Gorgon requires at least a Direct3D 11.4 capable video device.\nThere is no suitable device installed on the system."); } // Find the best video device. _graphics = new GorgonGraphics(videoDevices.OrderByDescending(item => item.FeatureSet).First()); _screen = new GorgonSwapChain(_graphics, window, new GorgonSwapChainInfo("Gorgon2D Space Scene Example") { Width = Settings.Default.Resolution.Width, Height = Settings.Default.Resolution.Height, Format = BufferFormat.R8G8B8A8_UNorm }); // Create a secondary render target for our scene. We use 16 bit floating point for the effect fidelity. // We'll lock our resolution to 1920x1080 (pretty common resolution for most people). _mainRtv = GorgonRenderTarget2DView.CreateRenderTarget(_graphics, new GorgonTexture2DInfo("Main RTV") { Width = (int)_baseResolution.X, Height = (int)_baseResolution.Y, Format = BufferFormat.R16G16B16A16_Float, Binding = TextureBinding.ShaderResource }); _mainSrv = _mainRtv.GetShaderResourceView(); _mainRtvAspect = _mainRtv.Width < _mainRtv.Height ? new DX.Vector2(1, (float)_mainRtv.Height / _mainRtv.Width) : new DX.Vector2((float)_mainRtv.Width / _mainRtv.Height, 1); // Initialize the renderer so that we are able to draw stuff. _renderer = new Gorgon2D(_graphics); // Set up our raw input. _input = new GorgonRawInput(window, GorgonApplication.Log); _keyboard = new GorgonRawKeyboard(); _keyboard.KeyUp += Keyboard_KeyUp; _input.RegisterDevice(_keyboard); GorgonExample.LoadResources(_graphics); // Now for the fun stuff, load our asset resources. We can load this data by mounting a directory (which I did while developing), or use a packed file. // // The resource manager will hold all the data we need for the scene. Including 3D meshes, post processing effects, etc... _resources = new ResourceManagement(_renderer, plugIns); _resources.Load(Path.Combine(GorgonExample.GetResourcePath(@"FileSystems").FullName, "SpaceScene.gorPack")); window.UpdateStatus("Loading resources..."); await _resources.LoadResourcesAsync(); SetupScene(); // Build up a font to use for rendering any GUI text. _helpFont = GorgonExample.Fonts.GetFont(new GorgonFontInfo("Segoe UI", 10.0f, FontHeightMode.Points, "Segoe UI 10pt") { OutlineSize = 2, Characters = (Resources.Instructions + "S:1234567890x").Distinct().ToArray(), FontStyle = FontStyle.Bold, AntiAliasingMode = FontAntiAliasMode.AntiAlias, OutlineColor1 = GorgonColor.Black, OutlineColor2 = GorgonColor.Black }); _textSprite = new GorgonTextSprite(_helpFont) { Position = new DX.Vector2(0, 64), DrawMode = TextDrawMode.OutlinedGlyphs, Color = GorgonColor.YellowPure }; GorgonExample.ShowStatistics = true; // Set the idle here. We don't want to try and render until we're done loading. GorgonApplication.IdleMethod = Idle; } finally { GorgonExample.EndInit(); } }
/// <summary> /// Raises the <see cref="E:System.Windows.Forms.Form.Load"></see> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.PlugInLocation); // Load the assembly. _assemblyCache = new GorgonMefPlugInCache(GorgonApplication.Log); // Create the plugin service. IGorgonPlugInService plugInService = new GorgonMefPlugInService(_assemblyCache); // Create the factory to retrieve gaming device drivers. var factory = new GorgonGamingDeviceDriverFactory(plugInService); // Create the raw input interface. _input = new GorgonRawInput(this, GorgonApplication.Log); // Get available gaming device driver plug ins. _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.Input.DirectInput.dll"); _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.Input.XInput.dll"); _drivers = factory.LoadAllDrivers(); _joystickList = new List <IGorgonGamingDevice>(); // Get all gaming devices from the drivers. foreach (IGorgonGamingDeviceDriver driver in _drivers) { IReadOnlyList <IGorgonGamingDeviceInfo> infoList = driver.EnumerateGamingDevices(true); foreach (IGorgonGamingDeviceInfo info in infoList) { IGorgonGamingDevice device = driver.CreateGamingDevice(info); // Turn off dead zones for this example. foreach (GorgonGamingDeviceAxis axis in device.Axis) { axis.DeadZone = GorgonRange.Empty; } _joystickList.Add(device); } } // Create mouse. _mouse = new GorgonRawMouse(); // Create the graphics interface. ClientSize = Settings.Default.Resolution; IReadOnlyList <IGorgonVideoAdapterInfo> adapters = GorgonGraphics.EnumerateAdapters(); _graphics = new GorgonGraphics(adapters[0], log: GorgonApplication.Log); _screen = new GorgonSwapChain(_graphics, this, new GorgonSwapChainInfo("INeedYourInput Swapchain") { Width = Settings.Default.Resolution.Width, Height = Settings.Default.Resolution.Height, Format = BufferFormat.R8G8B8A8_UNorm }); _graphics.SetRenderTarget(_screen.RenderTargetView); if (!Settings.Default.IsWindowed) { _screen.EnterFullScreen(); } // For the backup image. Used to make it as large as the monitor that we're on. var currentScreen = Screen.FromHandle(Handle); // Relocate the window to the center of the screen. Location = new Point(currentScreen.Bounds.Left + (currentScreen.WorkingArea.Width / 2) - (ClientSize.Width / 2), currentScreen.Bounds.Top + (currentScreen.WorkingArea.Height / 2) - (ClientSize.Height / 2)); // Create the 2D renderer. _2D = new Gorgon2D(_graphics); // Create the text font. var fontFactory = new GorgonFontFactory(_graphics); _font = fontFactory.GetFont(new GorgonFontInfo("Arial", 9.0f, FontHeightMode.Points, "Arial 9pt") { FontStyle = FontStyle.Bold, AntiAliasingMode = FontAntiAliasMode.AntiAlias }); // Create text sprite. _messageSprite = new GorgonTextSprite(_font, "Using mouse and keyboard (Windows Forms).") { Color = Color.Black }; // Create a back buffer. _backBuffer = GorgonRenderTarget2DView.CreateRenderTarget(_graphics, new GorgonTexture2DInfo("Backbuffer storage") { Width = _screen.Width, Height = _screen.Height, Format = _screen.Format }); _backBuffer.Clear(Color.White); _backBufferView = _backBuffer.GetShaderResourceView(); // Clear our backup image to white to match our primary screen. using (IGorgonImage image = new GorgonImage(new GorgonImageInfo(ImageType.Image2D, _screen.Format) { Width = _screen.Width, Height = _screen.Height, Format = _screen.Format })) { image.Buffers[0].Fill(0xff); _backupImage = image.ToTexture2D(_graphics, new GorgonTexture2DLoadOptions { Binding = TextureBinding.None, Usage = ResourceUsage.Staging }); } // Set gorgon events. _screen.BeforeSwapChainResized += BeforeSwapChainResized; _screen.AfterSwapChainResized += AfterSwapChainResized; // Enable the mouse. Cursor = Cursors.Cross; _mouse.MouseButtonDown += MouseInput; _mouse.MouseMove += MouseInput; _mouse.MouseWheelMove += (sender, args) => { _radius += args.WheelDelta.Sign(); if (_radius < 2.0f) { _radius = 2.0f; } if (_radius > 10.0f) { _radius = 10.0f; } }; // Set the mouse position. _mouse.Position = new Point(ClientSize.Width / 2, ClientSize.Height / 2); _noBlending = _blendBuilder.BlendState(GorgonBlendState.NoBlending) .Build(); _inverted = _blendBuilder.BlendState(GorgonBlendState.Inverted) .Build(); // Set up blending states for our pen. var blendStateBuilder = new GorgonBlendStateBuilder(); _currentBlend = _drawModulatedBlend = _blendBuilder.BlendState(blendStateBuilder .ResetTo(GorgonBlendState.Default) .DestinationBlend(alpha: Blend.One) .Build()) .Build(); _drawAdditiveBlend = _blendBuilder.BlendState(blendStateBuilder .ResetTo(GorgonBlendState.Additive) .DestinationBlend(alpha: Blend.One) .Build()) .Build(); _drawNoBlend = _blendBuilder.BlendState(blendStateBuilder .ResetTo(GorgonBlendState.NoBlending) .DestinationBlend(alpha: Blend.One) .Build()) .Build(); GorgonApplication.IdleMethod = Gorgon_Idle; } catch (ReflectionTypeLoadException refEx) { string refErr = string.Join("\n", refEx.LoaderExceptions.Select(item => item.Message)); GorgonDialogs.ErrorBox(this, refErr); } catch (Exception ex) { GorgonExample.HandleException(ex); GorgonApplication.Quit(); } }
/// <summary> /// Function to initialize the application. /// </summary> /// <returns>The main window for the application.</returns> private static FormMain Initialize() { GorgonExample.ResourceBaseDirectory = new DirectoryInfo(Settings.Default.ResourceLocation); GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.PlugInLocation); FormMain window = GorgonExample.Initialize(new DX.Size2(Settings.Default.Resolution.Width, Settings.Default.Resolution.Height), "Depth"); try { IReadOnlyList <IGorgonVideoAdapterInfo> videoDevices = GorgonGraphics.EnumerateAdapters(log: GorgonApplication.Log); if (videoDevices.Count == 0) { throw new GorgonException(GorgonResult.CannotCreate, "Gorgon requires at least a Direct3D 11.4 capable video device.\nThere is no suitable device installed on the system."); } // Find the best video device. _graphics = new GorgonGraphics(videoDevices.OrderByDescending(item => item.FeatureSet).First()); _screen = new GorgonSwapChain(_graphics, window, new GorgonSwapChainInfo("Gorgon2D Depth Buffer Example") { Width = Settings.Default.Resolution.Width, Height = Settings.Default.Resolution.Height, Format = BufferFormat.R8G8B8A8_UNorm }); _depthBuffer = GorgonDepthStencil2DView.CreateDepthStencil(_graphics, new GorgonTexture2DInfo(_screen.RenderTargetView) { Binding = TextureBinding.DepthStencil, Format = BufferFormat.D24_UNorm_S8_UInt }); // Tell the graphics API that we want to render to the "screen" swap chain. _graphics.SetRenderTarget(_screen.RenderTargetView, _depthBuffer); // Initialize the renderer so that we are able to draw stuff. _renderer = new Gorgon2D(_graphics); GorgonExample.LoadResources(_graphics); // Load our packed file system plug in. _assemblyCache = new GorgonMefPlugInCache(GorgonApplication.Log); _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.FileSystem.GorPack.dll"); IGorgonPlugInService plugIns = new GorgonMefPlugInService(_assemblyCache); // Load the file system containing our application data (sprites, images, etc...) IGorgonFileSystemProviderFactory providerFactory = new GorgonFileSystemProviderFactory(plugIns, GorgonApplication.Log); IGorgonFileSystemProvider provider = providerFactory.CreateProvider("Gorgon.IO.GorPack.GorPackProvider"); IGorgonFileSystem fileSystem = new GorgonFileSystem(provider, GorgonApplication.Log); // We can load the editor file system directly. // This is handy for switching a production environment where your data may be stored // as a compressed file, and a development environment where your data consists of loose // files. // fileSystem.Mount(@"D:\unpak\scratch\DeepAsAPuddle.gorPack\fs\"); // For now though, we'll load the packed file. fileSystem.Mount(Path.Combine(GorgonExample.GetResourcePath(@"FileSystems").FullName, "Depth.gorPack")); // Get our sprites. These make up the frames of animation for our Guy. // If and when there's an animation editor, we'll only need to create a single sprite and load the animation. IGorgonVirtualFile[] spriteFiles = fileSystem.FindFiles("/Sprites/", "*", true).ToArray(); // Load our sprite data (any associated textures will be loaded as well). Dictionary <string, GorgonSprite> sprites = new Dictionary <string, GorgonSprite>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < spriteFiles.Length; i++) { IGorgonVirtualFile file = spriteFiles[i]; (GorgonSprite sprite, GorgonTexture2D texture) = fileSystem.LoadSprite(_renderer, file.FullPath); // The LoadSprite extension method will automatically find and load your associated texture if you're using // a Gorgon editor file system. So it's important that you leep track of your textures, disposing of just // the associated GorgonTexture2DView won't cut it here, so you'll need to dispose the actual texture resource // when you're done with it. if (!_textures.Contains(texture)) { _textures.Add(texture); } // At super duper resolution, the example graphics would be really hard to see, so we'll scale them up. sprite.Scale = new DX.Vector2((_screen.Width / (_screen.Height / 2)) * 2.0f); sprites[file.Name] = sprite; } _snowTile = sprites["Snow"]; _snowTile.Depth = 0.5f; _icicle = sprites["Icicle"]; _icicle.Depth = 0.2f; _guySprite = sprites["Guy_Up_0"]; _guySprite.Depth = 0.1f; _guyPosition = new DX.Vector2(_screen.Width / 2 + _guySprite.ScaledSize.Width * 1.25f, _screen.Height / 2 + _guySprite.ScaledSize.Height); BuildAnimations(sprites); } finally { GorgonExample.EndInit(); } return(window); }
/// <summary>Function to load all the specified plug in assemblies.</summary> /// <param name="pluginCache">The plugin cache that will hold the plug in assembies.</param> /// <param name="pluginAssemblyFiles">The list of plug in assembly paths to load.</param> /// <param name="log">The application logging interface.</param> /// <returns>A list of <see cref="PlugInAssemblyState"/> objects for each plug in assembly loaded.</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="pluginAssemblyFiles" /> parameter is <b>null</b></exception> public static IReadOnlyList <PlugInAssemblyState> ValidateAndLoadAssemblies(this GorgonMefPlugInCache pluginCache, IEnumerable <FileInfo> pluginAssemblyFiles, IGorgonLog log) { if (pluginAssemblyFiles == null) { throw new ArgumentNullException(nameof(pluginAssemblyFiles)); } var records = new List <PlugInAssemblyState>(); // We use this to determine whether the plug in can be loaded into the current platform. AssemblyPlatformType currentPlatform = IntPtr.Size == 8 ? AssemblyPlatformType.x64 : AssemblyPlatformType.x86; foreach (FileInfo file in pluginAssemblyFiles) { // If we've already got the assembly loaded into this cache, then there's no need to try and load it. if (pluginCache.PlugInAssemblies?.Any(item => string.Equals(item, file.FullName, StringComparison.OrdinalIgnoreCase)) ?? false) { continue; } if (!file.Exists) { log.Print($"[ERROR] Plug in '{file.FullName}' was not found.", LoggingLevel.Verbose); records.Add(new PlugInAssemblyState(file.FullName, Resources.GOREDIT_PLUGIN_LOAD_FAIL_NOT_FOUND, false)); continue; } (bool isManaged, AssemblyPlatformType platformType) = GorgonMefPlugInCache.IsManagedAssembly(file.FullName); if ((!isManaged) || (platformType == AssemblyPlatformType.Unknown)) { log.Print($"[WARNING] Skipping '{file.FullName}'. Not a valid .NET assembly.", LoggingLevel.Verbose); records.Add(new PlugInAssemblyState(file.FullName, string.Format(Resources.GOREDIT_PLUGIN_LOAD_FAIL_NOT_DOT_NET, file.Name), false)); continue; } // Ensure that our platform type matches (AnyCPU is exempt and will always run, and DLLs don't allow Prefer 32 bit). if ((currentPlatform == AssemblyPlatformType.x86) && (platformType == AssemblyPlatformType.x64)) { log.Print($"[ERROR] Cannot load assembly '{file.FullName}', currently executing in an x86 environment, but the assembly is x64.", LoggingLevel.Simple); records.Add(new PlugInAssemblyState(file.FullName, string.Format(Resources.GOREDIT_PLUGIN_LOAD_FAIL_PLATFORM_MISMATCH, file.Name, platformType, currentPlatform), true)); continue; } if ((currentPlatform == AssemblyPlatformType.x64) && (platformType == AssemblyPlatformType.x86)) { log.Print($"[ERROR] Cannot load assembly '{file.FullName}', currently executing in an x64 environment, but the assembly is x86.", LoggingLevel.Simple); records.Add(new PlugInAssemblyState(file.FullName, string.Format(Resources.GOREDIT_PLUGIN_LOAD_FAIL_PLATFORM_MISMATCH, file.Name, platformType, currentPlatform), true)); continue; } try { log.Print($"Loading plug in assembly '{file.FullName}'...", LoggingLevel.Simple); pluginCache.LoadPlugInAssemblies(file.DirectoryName, file.Name); records.Add(new PlugInAssemblyState(file.FullName, string.Empty, true)); } catch (Exception ex) { log.Print($"ERROR: Cannot load plug in assembly '{file.FullName}'.", LoggingLevel.Simple); log.LogException(ex); records.Add(new PlugInAssemblyState(file.FullName, string.Format(Resources.GOREDIT_PLUGIN_LOAD_FAIL_EXCEPTION, file.Name, ex.Message), true)); } } return(records); }
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(); } }