Ejemplo n.º 1
0
        /// <summary>
        /// The function that is load the plugin what can use
        /// </summary>
        /// <param name="fileSystem">The instance of FileSystem class</param>
        public static void LoadPlugins(FileSystem.FileSystem fileSystem)
        {
            if (fileSystem == null)
            {
                return;
            }
            FileSystem = fileSystem;
            string PluginsFolder = FileSystem.GetDataFolder("InputDevicePlugins");

            if (!System.IO.Directory.Exists(PluginsFolder))
            {
                return;
            }
            string[] PluginFiles = System.IO.Directory.GetFiles(PluginsFolder, "*.dll");
            foreach (var File in PluginFiles)
            {
                Assembly Plugin;
                try
                {
                    Plugin = Assembly.LoadFrom(File);
                }
                catch
                {
                    continue;
                }
                Type[] Types;
                try
                {
                    Types = Plugin.GetTypes();
                }
                catch
                {
                    continue;
                }
                foreach (var Type in Types)
                {
                    if (typeof(IInputDevice).IsAssignableFrom(Type))
                    {
                        if (Type.FullName == null)
                        {
                            continue;
                        }
                        AvailablePluginInfos.Add(new PluginInfo(Plugin));
                        AvailablePlugins.Add(Plugin.CreateInstance(Type.FullName) as IInputDevice);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }