示例#1
0
        public static void LoadWindowPlugin(string strFile)
        {
            if (!IsPlugInEnabled(strFile))
            {
                return;
            }

            Log.Info("  Load plugins from : {0}", strFile);
            try
            {
                Assembly assem = Assembly.LoadFrom(strFile);
                if (assem != null)
                {
                    Log.Info("  File Version : {0}", FileVersionInfo.GetVersionInfo(strFile).ProductVersion);

                    Type[] types = assem.GetExportedTypes();
                    if (types.Any(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(GUIWindow))) && !CompatibilityManager.IsPluginCompatible(assem))
                    {
                        Log.Error(
                            "PluginManager: {0} is tagged as incompatible with the current MediaPortal version and won't be loaded!",
                            assem.FullName);
                        _incompatibilities.Add(assem);
                    }
                    else
                    {
                        //MarkPluginAsCompatible(assem);

                        Type[] foundInterfaces = null;

                        foreach (Type t in types)
                        {
                            try
                            {
                                if (t.IsClass)
                                {
                                    if (t.IsAbstract)
                                    {
                                        continue;
                                    }

                                    Object newObj = null;
                                    if (t.IsSubclassOf(typeof(GUIWindow)))
                                    {
                                        try
                                        {
                                            if (!CompatibilityManager.IsPluginCompatible(t))
                                            {
                                                Log.Error(
                                                    "PluginManager: {0} is tagged as incompatible with the current MediaPortal version and won't be loaded!",
                                                    t.FullName);
                                                _incompatibilities.Add(t);
                                                continue;
                                            }

                                            newObj = (object)Activator.CreateInstance(t);
                                            GUIWindow win = (GUIWindow)newObj;

                                            if (win.GetID >= 0 && IsWindowPlugInEnabled(win.GetType().ToString()))
                                            {
                                                try
                                                {
                                                    win.Init();
                                                    _guiPlugins.Add(win);
                                                }
                                                catch (Exception ex)
                                                {
                                                    Log.Error("Error initializing window:{0} {1} {2} {3}", win.ToString(), ex.Message, ex.Source,
                                                              ex.StackTrace);
                                                }
                                                GUIWindowManager.Add(ref win);
                                            }
                                            //else Log.Info("  plugin:{0} not enabled",win.GetType().ToString());
                                        }
                                        catch (Exception guiWindowsException)
                                        {
                                            Log.Error("Exception while loading GUIWindows instances: {0}", t.FullName);
                                            Log.Error(guiWindowsException.Message);
                                            Log.Error(guiWindowsException.StackTrace);
                                        }
                                    }

                                    // If we get to this point, the plugin has loaded successfully
                                    // Mark it as compatible.
                                    //MarkPluginAsCompatible(t);

                                    TypeFilter myFilter2 = new TypeFilter(MyInterfaceFilter);
                                    try
                                    {
                                        foundInterfaces = t.FindInterfaces(myFilter2, "MediaPortal.GUI.Library.ISetupForm");
                                        if (foundInterfaces.Length > 0)
                                        {
                                            if (newObj == null)
                                            {
                                                newObj = (object)Activator.CreateInstance(t);
                                            }
                                            ISetupForm setup = (ISetupForm)newObj;
                                            if (!PluginEntryExists(setup.PluginName()))
                                            {
                                                Log.Info("PluginManager:  {0} {1} not found in Mediaportal.xml so adding it now",
                                                         setup.PluginName(), t.Assembly.ManifestModule.Name);
                                                AddPluginEntry(setup.PluginName(), t.Assembly.ManifestModule.Name);
                                            }
                                            if (IsPluginNameEnabled(setup.PluginName()))
                                            {
                                                _setupForms.Add(setup);
                                            }
                                        }
                                    }
                                    catch (Exception iSetupFormException)
                                    {
                                        Log.Error("Exception while loading ISetupForm instances: {0}", t.FullName);
                                        Log.Error(iSetupFormException.Message);
                                        Log.Error(iSetupFormException.StackTrace);
                                    }

                                    try
                                    {
                                        foundInterfaces = t.FindInterfaces(myFilter2, "MediaPortal.GUI.Library.IWakeable");
                                        if (foundInterfaces.Length > 0)
                                        {
                                            if (newObj == null)
                                            {
                                                newObj = (object)Activator.CreateInstance(t);
                                            }
                                            IWakeable setup = (IWakeable)newObj;
                                            if (PluginEntryExists(setup.PluginName()) && IsPluginNameEnabled(setup.PluginName()))
                                            {
                                                _wakeables.Add(setup);
                                            }
                                        }
                                    }
                                    catch (Exception iWakeableException)
                                    {
                                        Log.Error("Exception while loading IWakeable instances: {0}", t.FullName);
                                        Log.Error(iWakeableException.Message);
                                        Log.Error(iWakeableException.StackTrace);
                                    }
                                }
                            }
                            catch (NullReferenceException) {}
                        }
                    }
                }
            }
            catch (BadImageFormatException) { }
            catch (Exception ex)
            {
                Log.Info(
                    "PluginManager: Plugin file {0} is broken or incompatible with the current MediaPortal version and won't be loaded!",
                    strFile.Substring(strFile.LastIndexOf(@"\") + 1));
                Log.Info("PluginManager: Exception: {0}", ex);
            }
        }
示例#2
0
        private static void LoadPlugin(string strFile)
        {
            if (!IsPlugInEnabled(strFile))
            {
                return;
            }

            Type[] foundInterfaces = null;

            Log.Info("  Load plugins from : {0}", strFile);
            try
            {
                Assembly assem = Assembly.LoadFrom(strFile);
                if (assem != null)
                {
                    Log.Info("  File Version : {0}", FileVersionInfo.GetVersionInfo(strFile).ProductVersion);

                    Type[]     types     = assem.GetExportedTypes();
                    TypeFilter myFilter2 = new TypeFilter(MyInterfaceFilter);

                    if (types.Any(t => t.IsClass && !t.IsAbstract && typeof(IPlugin).IsAssignableFrom(t)) &&
                        !CompatibilityManager.IsPluginCompatible(assem))
                    {
                        Log.Error(
                            "PluginManager: {0} is tagged as incompatible with the current MediaPortal version and won't be loaded!", assem.FullName);
                        _incompatibilities.Add(assem);
                    }
                    else
                    {
                        foreach (Type t in types)
                        {
                            try
                            {
                                if (t.IsClass)
                                {
                                    if (t.IsAbstract)
                                    {
                                        continue;
                                    }

                                    Object  newObj = null;
                                    IPlugin plugin = null;
                                    try
                                    {
                                        foundInterfaces = t.FindInterfaces(myFilter2, "MediaPortal.GUI.Library.IPlugin");
                                        if (foundInterfaces.Length > 0)
                                        {
                                            if (!CompatibilityManager.IsPluginCompatible(t))
                                            {
                                                Log.Error(
                                                    "PluginManager: {0} is tagged as incompatible with the current MediaPortal version and won't be loaded!",
                                                    t.FullName);
                                                _incompatibilities.Add(t);
                                                continue;
                                            }

                                            newObj = (object)Activator.CreateInstance(t);
                                            plugin = (IPlugin)newObj;
                                        }
                                    }
                                    catch (TargetInvocationException ex)
                                    {
                                        Log.Error(ex);
                                        Log.Error(
                                            "PluginManager: {0} is incompatible with the current MediaPortal version and won't be loaded!",
                                            t.FullName);
                                        continue;
                                    }
                                    catch (Exception iPluginException)
                                    {
                                        Log.Error("Exception while loading IPlugin instances: {0}", t.FullName);
                                        Log.Error(iPluginException.ToString());
                                        Log.Error(iPluginException.Message);
                                        Log.Error(iPluginException.StackTrace);
                                    }
                                    if (plugin == null)
                                    {
                                        continue;
                                    }

                                    // If we get to this point, the plugin has loaded successfully
                                    // Mark it as compatible.
                                    //MarkPluginAsCompatible(t);

                                    try
                                    {
                                        foundInterfaces = t.FindInterfaces(myFilter2, "MediaPortal.GUI.Library.ISetupForm");
                                        if (foundInterfaces.Length > 0)
                                        {
                                            if (newObj == null)
                                            {
                                                newObj = (object)Activator.CreateInstance(t);
                                            }
                                            ISetupForm setup = (ISetupForm)newObj;
                                            // don't activate plugins that have NO entry at all in
                                            // MediaPortal.xml
                                            if (!PluginEntryExists(setup.PluginName()))
                                            {
                                                Log.Info("PluginManager:  {0} {1} not found in Mediaportal.xml so adding it now",
                                                         setup.PluginName(), t.Assembly.ManifestModule.Name);
                                                AddPluginEntry(setup.PluginName(), t.Assembly.ManifestModule.Name);
                                                MPSettings.Instance.SetValueAsBool("home", setup.PluginName(), false);
                                                MPSettings.Instance.SetValueAsBool("myplugins", setup.PluginName(), true);
                                                MPSettings.Instance.SetValueAsBool("pluginswindows", t.ToString(), true);
                                            }
                                            if (IsPluginNameEnabled(setup.PluginName()))
                                            {
                                                _setupForms.Add(setup);
                                                _nonGuiPlugins.Add(plugin);
                                            }
                                        }
                                        else
                                        {
                                            //IPlugin without ISetupForm, adding anyway
                                            if (!PluginEntryExists(t.Name))
                                            {
                                                AddPluginEntry(t.Name, t.Assembly.ManifestModule.Name);
                                            }
                                            _nonGuiPlugins.Add(plugin);
                                        }
                                    }
                                    catch (Exception iSetupFormException)
                                    {
                                        Log.Error("Exception while loading ISetupForm instances: {0}", t.FullName);
                                        Log.Error(iSetupFormException.Message);
                                        Log.Error(iSetupFormException.StackTrace);
                                    }

                                    try
                                    {
                                        foundInterfaces = t.FindInterfaces(myFilter2, "MediaPortal.GUI.Library.IWakeable");
                                        if (foundInterfaces.Length > 0)
                                        {
                                            if (newObj == null)
                                            {
                                                newObj = (object)Activator.CreateInstance(t);
                                            }
                                            IWakeable setup = (IWakeable)newObj;
                                            if (PluginEntryExists(setup.PluginName()) && IsPluginNameEnabled(setup.PluginName()))
                                            {
                                                _wakeables.Add(setup);
                                            }
                                        }
                                    }
                                    catch (Exception iWakeableException)
                                    {
                                        Log.Error("Exception while loading IWakeable instances: {0}", t.FullName);
                                        Log.Error(iWakeableException.Message);
                                        Log.Error(iWakeableException.StackTrace);
                                    }
                                }
                            }
                            catch (NullReferenceException) {}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Info(
                    "PluginManager: Plugin file {0} is broken or incompatible with the current MediaPortal version and won't be loaded!",
                    strFile.Substring(strFile.LastIndexOf(@"\") + 1));
                Log.Info("PluginManager: Exception: {0}", ex);
            }
        }