Example #1
0
        public Assembly Load(IPlugin plugin)
        {
            if (!Path.IsPathRooted(plugin.Path))
            {
                plugin.Path = Path.Combine(this._pluginDir, plugin.Name, plugin.Path);
            }

            if (!(plugin is ProxyPlugin))
            {
                plugin = new ProxyPlugin(plugin);
            }

            var proxy = (ProxyPlugin)this._store.AddOrUpdate(plugin.Name, plugin, (k, p) =>
            {
                if (p.Version != plugin.Version)
                {
                    return(plugin);
                }
                else
                {
                    return(p);
                }
            });

            return(proxy.Register());
        }
Example #2
0
        protected virtual IPlugin Read(string dir)
        {
            IPlugin plugin      = null;
            var     packageFile = Path.Combine(dir, Package);

            if (!File.Exists(packageFile))
            {
                return(plugin);
            }

            var json = File.ReadAllText(packageFile);

            var proxyPlugin = new ProxyPlugin(json.ToObject <Plugin>());

            if (proxyPlugin?.Check() ?? false)
            {
                if (proxyPlugin.Status)
                {
                    var assembly = this.Load(proxyPlugin);

                    var pluginType = assembly.GetTypes()
                                     .FirstOrDefault(
                        s => this._plutinType.IsAssignableFrom(s));
                    if (pluginType != null)
                    {
                        var tPlugin = (IPlugin)Activator.CreateInstance(pluginType);
                        proxyPlugin.Extension(tPlugin);
                    }
                }
                plugin = proxyPlugin;
            }

            return(plugin);
        }
        public bool LoadAssembly(PluginInfo pinfo, ProxyFrame proxy)
        {
            try
            {
                pinfo.Modules.Clear();
                bool     started  = false;
                Assembly assembly = Assembly.LoadFile(System.IO.Path.GetFullPath(pinfo.Path));
                foreach (Type t in assembly.GetTypes())
                {
                    if (t.IsSubclassOf(typeof(ProxyPlugin)))
                    {
                        ConstructorInfo info = t.GetConstructor(new Type[] { typeof(ProxyFrame) });
                        if (info == null)
                        {
                            OpenMetaverse.Logger.Log(string.Format("No suitable contructor for {0} in {1}", t.ToString(), pinfo.Name), OpenMetaverse.Helpers.LogLevel.Warning);
                        }
                        else
                        {
                            ProxyPlugin plugin = (ProxyPlugin)info.Invoke(new object[] { proxy });
                            plugin.Init();
                            pinfo.Modules.Add(t.ToString());
                            started = true;
                        }
                    }
                }

                if (started)
                {
                    OpenMetaverse.Logger.Log(string.Format("Loaded {0} plugins from {1}: {2}", pinfo.Modules.Count.ToString(), pinfo.Name, string.Join(", ", pinfo.Modules.ToArray())), OpenMetaverse.Helpers.LogLevel.Info);
                }
                else
                {
                    OpenMetaverse.Logger.Log("Found no plugins in " + pinfo.Name, OpenMetaverse.Helpers.LogLevel.Warning);
                }

                return(started);
            }
            catch (Exception e)
            {
                OpenMetaverse.Logger.Log("Failed loading plugins from " + pinfo.Path + ": " + e.Message, OpenMetaverse.Helpers.LogLevel.Error);
            }

            return(false);
        }
        public void LoadPlugin(string name)
        {
            Assembly assembly = Assembly.LoadFile(Path.GetFullPath(name));

            foreach (Type t in assembly.GetTypes())
            {
                try
                {
                    if (t.IsSubclassOf(typeof(ProxyPlugin)))
                    {
                        ConstructorInfo info   = t.GetConstructor(new Type[] { typeof(ProxyFrame) });
                        ProxyPlugin     plugin = (ProxyPlugin)info.Invoke(new object[] { _Frame });
                        plugin.Init();
                        listView1.Items.Add(new ListViewItem(new [] { assembly.ManifestModule.Name, Path.GetFullPath(name) }));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
Example #5
0
        public void LoadPlugin(string name)
        {
            Assembly assembly = Assembly.LoadFile(Path.GetFullPath(name));

            //assembly.g
            try
            {
                foreach (Type t in assembly.GetTypes())
                {
                    try
                    {
                        if (t.IsSubclassOf(typeof(ProxyPlugin)))
                        {
                            ConstructorInfo info   = t.GetConstructor(new Type[] { typeof(ProxyFrame) });
                            ProxyPlugin     plugin = (ProxyPlugin)info.Invoke(new object[] { _Frame });
                            plugin.Init();
                            listView1.Items.Add(new ListViewItem(new[] { assembly.ManifestModule.Name, Path.GetFullPath(name) }));
                        }
                    }
                    catch (ReflectionTypeLoadException e)
                    {
                        String info = e.ToString() + "\n";
                        foreach (Exception ee in e.LoaderExceptions)
                        {
                            MessageBox.Show(ee.ToString());
                            info += ee.ToString() + "\n";
                        }
                        MessageBox.Show(info);
                        Console.WriteLine(e.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Example #6
0
        /// <summary>Loads the specified plugin for the specified train.</summary>
        /// <param name="pluginFile">The file to the plugin.</param>
        /// <param name="trainFolder">The train folder.</param>
        /// <returns>Whether the plugin was loaded successfully.</returns>
        public bool LoadPlugin(string pluginFile, string trainFolder)
        {
            string pluginTitle = System.IO.Path.GetFileName(pluginFile);

            if (!System.IO.File.Exists(pluginFile))
            {
                TrainManagerBase.currentHost.AddMessage(MessageType.Error, true, "The train plugin " + pluginTitle + " could not be found.");
                return(false);
            }

            /*
             * Unload plugin if already loaded.
             * */
            if (Plugin != null)
            {
                UnloadPlugin();
            }

            /*
             * Prepare initialization data for the plugin.
             * */

            InitializationModes mode = (InitializationModes)TrainManagerBase.CurrentOptions.TrainStart;

            /*
             * Check if the plugin is a .NET plugin.
             * */
            Assembly assembly;

            try
            {
                assembly = Assembly.LoadFile(pluginFile);
            }
            catch (BadImageFormatException)
            {
                assembly = null;
                try
                {
                    AssemblyName myAssembly = AssemblyName.GetAssemblyName(pluginFile);
                    if (IntPtr.Size != 4 && myAssembly.ProcessorArchitecture == ProcessorArchitecture.X86)
                    {
                        TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " can only be used with the 32-bit version of OpenBVE");
                        return(false);
                    }
                }
                catch
                {
                    //ignored
                }
            }
            catch (Exception ex)
            {
                TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " could not be loaded due to the following exception: " + ex.Message);
                return(false);
            }

            if (assembly != null)
            {
                Type[] types;
                try
                {
                    types = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    foreach (Exception e in ex.LoaderExceptions)
                    {
                        TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " raised an exception on loading: " + e.Message);
                    }

                    return(false);
                }

                foreach (Type type in types)
                {
                    if (typeof(IRuntime).IsAssignableFrom(type))
                    {
                        if (type.FullName == null)
                        {
                            //Should never happen, but static code inspection suggests that it's possible....
                            throw new InvalidOperationException();
                        }

                        IRuntime api = assembly.CreateInstance(type.FullName) as IRuntime;
                        Plugin = new NetPlugin(pluginFile, trainFolder, api, this);
                        if (Plugin.Load(vehicleSpecs(), mode))
                        {
                            return(true);
                        }
                        else
                        {
                            Plugin = null;
                            return(false);
                        }
                    }
                }

                TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " does not export a train interface and therefore cannot be used with openBVE.");
                return(false);
            }

            /*
             * Check if the plugin is a Win32 plugin.
             *
             */
            try
            {
                if (!Win32Plugin.CheckHeader(pluginFile))
                {
                    TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " is of an unsupported binary format and therefore cannot be used with openBVE.");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " could not be read due to the following reason: " + ex.Message);
                return(false);
            }

            if (TrainManagerBase.currentHost.Platform != HostPlatform.MicrosoftWindows | IntPtr.Size != 4)
            {
                if (TrainManagerBase.currentHost.Platform == HostPlatform.MicrosoftWindows && IntPtr.Size != 4)
                {
                    //We can't load the plugin directly on x64 Windows, so use the proxy interface
                    Plugin = new ProxyPlugin(pluginFile, this);
                    if (Plugin.Load(vehicleSpecs(), mode))
                    {
                        return(true);
                    }

                    Plugin = null;
                    TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " failed to load.");
                    return(false);
                }

                //WINE doesn't seem to like the WCF proxy :(
                TrainManagerBase.currentHost.AddMessage(MessageType.Warning, false, "The train plugin " + pluginTitle + " can only be used on Microsoft Windows or compatible.");
                return(false);
            }

            if (TrainManagerBase.currentHost.Platform == HostPlatform.MicrosoftWindows && !System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\AtsPluginProxy.dll"))
            {
                TrainManagerBase.currentHost.AddMessage(MessageType.Warning, false, "AtsPluginProxy.dll is missing or corrupt- Please reinstall.");
                return(false);
            }

            Plugin = new Win32Plugin(pluginFile, this);
            if (Plugin.Load(vehicleSpecs(), mode))
            {
                return(true);
            }
            else
            {
                Plugin = null;
                TrainManagerBase.currentHost.AddMessage(MessageType.Error, false, "The train plugin " + pluginTitle + " does not export a train interface and therefore cannot be used with openBVE.");
                return(false);
            }
        }