コード例 #1
0
        private void LoadPlugins()
        {
            foreach (string pluginFile in availablePlugins)
            {
                Assembly pluginAssembly = null;
                try
                {
                    Log.Debug("PluginsNew: loadPlugins {0}", pluginFile);
                    pluginAssembly = Assembly.LoadFrom(pluginFile);
                }
                catch (BadImageFormatException)
                {
                    Log.Warn("PluginsNew: {0} has a bad image format", pluginFile);
                }

                if (pluginAssembly != null)
                {
                    try
                    {
                        Type[]        exportedTypes   = pluginAssembly.GetExportedTypes();
                        List <object> NonSetupWindows = new List <object>();

                        foreach (Type type in exportedTypes)
                        {
                            bool isPlugin    = (type.GetInterface("MediaPortal.GUI.Library.ISetupForm") != null);
                            bool isGuiWindow = ((type.IsClass) && (type.IsSubclassOf(typeof(GUIWindow))));

                            // an abstract class cannot be instanciated
                            if (type.IsAbstract)
                            {
                                continue;
                            }

                            bool isIncompatible = !CompatibilityManager.IsPluginCompatible(type);
                            if (isIncompatible)
                            {
                                Log.Warn(
                                    "Plugin Manager: Plugin {0} is incompatible with the current MediaPortal version! (File: {1})",
                                    type.FullName, pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1));
                            }

                            // Try to locate the interface we're interested in
                            if (isPlugin || isGuiWindow)
                            {
                                // Create instance of the current type
                                object pluginObject;
                                try
                                {
                                    pluginObject = Activator.CreateInstance(type);
                                }
                                catch (TargetInvocationException)
                                {
                                    MessageBox.Show(
                                        string.Format(
                                            "An error occured while loading the plugin {0}.\n\nIt's incompatible with the current MediaPortal version and won't be loaded.",
                                            type.FullName
                                            ), "Plugin Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    Log.Warn(
                                        "Plugin Manager: Plugin {0} is incompatible with the current MediaPortal version! (File: {1})",
                                        type.FullName, pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1));
                                    continue;
                                }

                                if (isPlugin)
                                {
                                    ISetupForm      pluginForm = pluginObject as ISetupForm;
                                    IExternalPlayer extPlayer  = pluginObject as IExternalPlayer;
                                    IShowPlugin     showPlugin = pluginObject as IShowPlugin;

                                    if (pluginForm != null)
                                    {
                                        ItemTag tag = new ItemTag();
                                        tag.SetupForm = pluginForm;
                                        tag.DllName   = pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1);
                                        tag.WindowId  = pluginForm.GetWindowId();

                                        if (isGuiWindow)
                                        {
                                            GUIWindow win = (GUIWindow)pluginObject;
                                            if (tag.WindowId == win.GetID)
                                            {
                                                tag.Type      = win.GetType().ToString();
                                                tag.IsProcess = false;
                                                tag.IsWindow  = true;
                                            }
                                        }
                                        else if (extPlayer != null)
                                        {
                                            tag.IsExternalPlayer = true;
                                        }
                                        else
                                        {
                                            tag.IsProcess = true;
                                        }

                                        if (showPlugin != null)
                                        {
                                            tag.ShowDefaultHome = showPlugin.ShowDefaultHome();
                                        }

                                        tag.IsIncompatible = isIncompatible;

                                        LoadPluginImages(type, tag);
                                        loadedPlugins.Add(tag);
                                    }
                                }
                                else
                                {
                                    NonSetupWindows.Add(pluginObject);
                                }
                            }
                        }
                        // Filter plugins from e.g. dialogs or other windows.
                        foreach (GUIWindow win in NonSetupWindows)
                        {
                            foreach (ItemTag tag in loadedPlugins)
                            {
                                if (tag.WindowId == win.GetID)
                                {
                                    tag.Type      = win.GetType().ToString();
                                    tag.IsProcess = false;
                                    tag.IsWindow  = true;
                                    Log.Debug(
                                        "PluginsNew: {0}, window plugin, does not implement \"ISetupForm\" and \"GUIWindow\" in the same class",
                                        tag.Type);
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            string.Format(
                                "An error occured while loading the plugin file {0}.\n\nIt's broken or incompatible with the current MediaPortal version and won't be loaded.",
                                pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1)), "Plugin Manager", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        Log.Warn(
                            "PluginManager: Plugin file {0} is broken or incompatible with the current MediaPortal version and won't be loaded!",
                            pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1));
                        Log.Error("PluginManager: Exception: {0}", ex);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Load available window plugins in a list
        ///
        /// Copied and modified from MediaPortal config: PluginsNew.cs
        /// </summary>
        private void LoadPlugins()
        {
            try
            {
                foreach (string pluginFile in availablePlugins)
                {
                    Assembly pluginAssembly = null;
                    try
                    {
                        pluginAssembly = Assembly.LoadFrom(pluginFile);
                    }
                    catch (BadImageFormatException)
                    {
                        Log.Warn("[WifiRemote Setup] {0} has a bad image format", pluginFile);
                    }

                    if (pluginAssembly != null)
                    {
                        try
                        {
                            Type[] exportedTypes = pluginAssembly.GetExportedTypes();

                            foreach (Type type in exportedTypes)
                            {
                                bool isPlugin    = (type.GetInterface("MediaPortal.GUI.Library.ISetupForm") != null);
                                bool isGuiWindow = ((type.IsClass) && (type.IsSubclassOf(typeof(GUIWindow))));

                                // an abstract class cannot be instanciated
                                if (type.IsAbstract)
                                {
                                    continue;
                                }

                                // Try to locate the interface we're interested in
                                if (isPlugin || isGuiWindow)
                                {
                                    // Create instance of the current type
                                    object pluginObject;
                                    try
                                    {
                                        pluginObject = Activator.CreateInstance(type);
                                    }
                                    catch (TargetInvocationException)
                                    {
                                        // Plugin is incompatible with current MediaPortal
                                        Log.Error("[WifiRemote Setup] Plugin " + pluginFile + " incompatible");
                                        continue;
                                    }

                                    if (isPlugin)
                                    {
                                        ISetupForm pluginForm = pluginObject as ISetupForm;

                                        if (pluginForm != null)
                                        {
                                            ItemTag tag = new ItemTag();
                                            tag.SetupForm = pluginForm;
                                            tag.DllName   = pluginFile.Substring(pluginFile.LastIndexOf(@"\") + 1);
                                            tag.WindowId  = pluginForm.GetWindowId();

                                            if (isGuiWindow)
                                            {
                                                GUIWindow win = (GUIWindow)pluginObject;
                                                if (tag.WindowId == win.GetID)
                                                {
                                                    tag.Type = win.GetType().ToString();
                                                }
                                            }

                                            LoadPluginImages(type, tag);
                                            plugins.Add(tag);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            // plugin broken or incompatible
                            Log.Error("[WifiRemote Setup] Exception: {0}", ex);
                        }
                    }
                }
            }
            catch (Exception excep)
            {
                Log.Error("[WifiRemote Setup] Error loading plugins: " + excep.Message);
            }
        }