// --- functions --- /// <summary>Loads all non-runtime plugins.</summary> /// <returns>Whether loading all plugins was successful.</returns> internal static bool LoadPlugins() { UnloadPlugins(); string folder = Program.FileSystem.GetDataFolder("Plugins"); string[] files = { }; try { files = Directory.GetFiles(folder); } catch { // ignored } List <ContentLoadingPlugin> list = new List <ContentLoadingPlugin>(); StringBuilder builder = new StringBuilder(); foreach (string file in files) { if (file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { #if !DEBUG try { #endif ContentLoadingPlugin plugin = new ContentLoadingPlugin(file); Assembly assembly; Type[] types; try { assembly = Assembly.LoadFile(file); types = assembly.GetTypes(); } catch { builder.Append("Plugin ").Append(Path.GetFileName(file)).AppendLine(" is not a .Net assembly."); continue; } bool iTexture = false; bool iObject = false; bool iRuntime = false; foreach (Type type in types) { if (type.FullName == null) { continue; } if (type.IsSubclassOf(typeof(TextureInterface))) { iTexture = true; } if (type.IsSubclassOf(typeof(SoundInterface))) { plugin.Sound = (SoundInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(ObjectInterface))) { iObject = true; } if (typeof(OpenBveApi.Runtime.IRuntime).IsAssignableFrom(type)) { iRuntime = true; } } if (plugin.Sound != null) { plugin.Load(Program.CurrentHost, Program.FileSystem, new Interface.Options()); list.Add(plugin); } else if (!iTexture && !iObject && !iRuntime) { builder.Append("Plugin ").Append(Path.GetFileName(file)).AppendLine(" does not implement compatible interfaces."); builder.AppendLine(); } #if !DEBUG } catch (Exception ex) { builder.Append("Could not load plugin ").Append(Path.GetFileName(file)).AppendLine(":").AppendLine(ex.Message); builder.AppendLine(); } #endif } } Program.CurrentHost.Plugins = list.ToArray(); if (Program.CurrentHost.Plugins.Length == 0) { MessageBox.Show($@"No available texture & sound loader plugins were found.{Environment.NewLine} Please re-download openBVE.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } string message = builder.ToString().Trim(new char[] { }); if (message.Length != 0) { return(MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button2) == DialogResult.OK); } return(true); }
/// <summary>Loads all non-runtime plugins.</summary> /// <returns>Whether loading all plugins was successful.</returns> internal static bool LoadPlugins() { UnloadPlugins(); string folder = Program.FileSystem.GetDataFolder("Plugins"); string[] files = { }; try { files = Directory.GetFiles(folder); } catch { } List <ContentLoadingPlugin> list = new List <ContentLoadingPlugin>(); StringBuilder builder = new StringBuilder(); foreach (string file in files) { if (file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { #if !DEBUG try { #endif ContentLoadingPlugin plugin = new ContentLoadingPlugin(file); Assembly assembly; Type[] types; try { assembly = Assembly.LoadFile(file); types = assembly.GetTypes(); } #pragma warning disable 168 catch (Exception ex) #pragma warning restore 168 { #if !DEBUG if ((ex is ReflectionTypeLoadException)) { /* * This is actually a .Net assembly, it just failed to load a reference * Probably built against a newer API version. */ builder.Append("Plugin ").Append(Path.GetFileName(file)).AppendLine(" failed to load. \n \n Please check that you are using the most recent version of OpenBVE."); } else { builder.Append("Plugin ").Append(Path.GetFileName(file)).AppendLine(" is not a .Net assembly."); } #endif continue; } bool iruntime = false; foreach (Type type in types) { if (type.FullName == null) { continue; } if (type.IsSubclassOf(typeof(OpenBveApi.Textures.TextureInterface))) { plugin.Texture = (OpenBveApi.Textures.TextureInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(OpenBveApi.Sounds.SoundInterface))) { plugin.Sound = (OpenBveApi.Sounds.SoundInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(OpenBveApi.Objects.ObjectInterface))) { plugin.Object = (OpenBveApi.Objects.ObjectInterface)assembly.CreateInstance(type.FullName); } if (typeof(OpenBveApi.Runtime.IRuntime).IsAssignableFrom(type)) { iruntime = true; } if (type.IsSubclassOf(typeof(OpenBveApi.Routes.RouteInterface))) { plugin.Route = (OpenBveApi.Routes.RouteInterface)assembly.CreateInstance(type.FullName); } } if (plugin.Texture != null | plugin.Sound != null | plugin.Object != null | plugin.Route != null) { plugin.Load(Program.CurrentHost, Program.FileSystem, Interface.CurrentOptions); list.Add(plugin); } else if (!iruntime) { builder.Append("Plugin ").Append(Path.GetFileName(file)).AppendLine(" does not implement compatible interfaces."); builder.AppendLine(); } #if !DEBUG } catch (Exception ex) { builder.Append("Could not load plugin ").Append(Path.GetFileName(file)).AppendLine(":").AppendLine(ex.Message); builder.AppendLine(); } #endif } } Program.CurrentHost.Plugins = list.ToArray(); if (Program.CurrentHost.Plugins.Length == 0) { MessageBox.Show("No available texture & sound loader plugins were found." + Environment.NewLine + " Please re-download openBVE.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } string message = builder.ToString().Trim(new char[] { }); if (message.Length != 0) { return(MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button2) == DialogResult.OK); } else { return(true); } }
/// <summary>Loads all non-runtime plugins.</summary> /// <returns>Whether loading all plugins was successful.</returns> public bool LoadPlugins(FileSystem.FileSystem fileSystem, BaseOptions currentOptions, out string errorMessage, object TrainManagerReference = null, object RendererReference = null) { UnloadPlugins(out errorMessage); string folder = fileSystem.GetDataFolder("Plugins"); string[] files = {}; try { files = Directory.GetFiles(folder); } catch { // ignored } List <ContentLoadingPlugin> list = new List <ContentLoadingPlugin>(); StringBuilder builder = new StringBuilder(); foreach (string file in files) { if (file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { try { ContentLoadingPlugin plugin = new ContentLoadingPlugin(file); Assembly assembly; Type[] types; try { assembly = Assembly.LoadFile(file); types = assembly.GetTypes(); } catch (Exception ex) { if ((ex is ReflectionTypeLoadException)) { /* * This is actually a .Net assembly, it just failed to load a reference * Probably built against a newer API version. */ builder.Append("Plugin ").Append(System.IO.Path.GetFileName(file)).AppendLine(" failed to load. \n \n Please check that you are using the most recent version of OpenBVE."); } else { builder.Append("Plugin ").Append(System.IO.Path.GetFileName(file)).AppendLine(" is not a .Net assembly."); } continue; } bool iruntime = false; foreach (Type type in types) { if (type.FullName == null) { continue; } if (type.IsSubclassOf(typeof(Textures.TextureInterface))) { plugin.Texture = (Textures.TextureInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(Sounds.SoundInterface))) { plugin.Sound = (Sounds.SoundInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(Objects.ObjectInterface))) { plugin.Object = (Objects.ObjectInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(Routes.RouteInterface))) { plugin.Route = (Routes.RouteInterface)assembly.CreateInstance(type.FullName); } if (type.IsSubclassOf(typeof(Trains.TrainInterface))) { plugin.Train = (Trains.TrainInterface)assembly.CreateInstance(type.FullName); } if (typeof(Runtime.IRuntime).IsAssignableFrom(type)) { iruntime = true; } } if (plugin.Texture != null | plugin.Sound != null | plugin.Object != null | plugin.Route != null | plugin.Train != null) { plugin.Load(this, fileSystem, currentOptions, TrainManagerReference, RendererReference); list.Add(plugin); } else if (!iruntime) { builder.Append("Plugin ").Append(System.IO.Path.GetFileName(file)).AppendLine(" does not implement compatible interfaces."); builder.AppendLine(); } } catch (Exception ex) { builder.Append("Could not load plugin ").Append(System.IO.Path.GetFileName(file)).AppendLine(":").AppendLine(ex.Message); builder.AppendLine(); } } } Plugins = list.ToArray(); if (Plugins.Length == 0) { errorMessage = "No available content loading plugins were found." + Environment.NewLine + " Please re-download openBVE."; return(false); } errorMessage = builder.ToString().Trim(new char[] { }); if (errorMessage.Length != 0) { return(false); } return(true); }