Example #1
0
        public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
        {
            var plugins = new List<PluginPair>();
            var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);

            foreach (var metadata in metadatas)
            {
                var milliseconds = Stopwatch.Debug($"C# plugin constructor init: {metadata.Name}", () =>
                {

            #if DEBUG
                    var assembly = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
                    var types = assembly.GetTypes();
                    var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
                    var plugin = (IPlugin)Activator.CreateInstance(type);
            #else
                    Assembly assembly;
                    try
                    {
                        assembly = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
                    }
                    catch (Exception e)
                    {
                        Log.Exception(new WoxPluginException(metadata.Name, "Couldn't load assembly", e));
                        return;
                    }
                    var types = assembly.GetTypes();
                    Type type;
                    try
                    {
                        type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
                    }
                    catch (InvalidOperationException e)
                    {
                        Log.Exception(new WoxPluginException(metadata.Name, "Can't find class implement IPlugin", e));
                        return;
                    }
                    IPlugin plugin;
                    try
                    {
                        plugin = (IPlugin)Activator.CreateInstance(type);
                    }
                    catch (Exception e)
                    {
                        Log.Exception(new WoxPluginException(metadata.Name, "Can't create instance", e));
                        return;
                    }
            #endif
                    PluginPair pair = new PluginPair
                    {
                        Plugin = plugin,
                        Metadata = metadata
                    };
                    plugins.Add(pair);
                });
                metadata.InitTime += milliseconds;

            }
            return plugins;
        }
Example #2
0
 public ActionKeywords(string pluginId)
 {
     InitializeComponent();
     _plugin = PluginManager.GetPluginForId(pluginId);
     if (_plugin == null)
     {
         MessageBox.Show(InternationalizationManager.Instance.GetTranslation("cannotFindSpecifiedPlugin"));
         Close();
     }
 }
Example #3
0
 public ActionKeywords(string pluginId, Settings settings)
 {
     InitializeComponent();
     _plugin = PluginManager.GetPluginForId(pluginId);
     _settings = settings;
     if (_plugin == null)
     {
         MessageBox.Show(_translater.GetTranslation("cannotFindSpecifiedPlugin"));
         Close();
     }
 }
Example #4
0
        public override List<PluginPair> LoadPlugin()
        {
            List<PluginPair> plugins = new List<PluginPair>();

            List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
            foreach (PluginMetadata metadata in metadatas)
            {
                try
                {
                    Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
                    List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
                    if (types.Count == 0)
                    {
                        Log.Warn(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
                            metadata.Name));
                        continue;
                    }

                    foreach (Type type in types)
                    {
                        PluginPair pair = new PluginPair()
                        {
                            Plugin = Activator.CreateInstance(type) as IPlugin,
                            Metadata = metadata
                        };

                        var sys = pair.Plugin as BaseSystemPlugin;
                        if (sys != null)
                        {
                            sys.PluginDirectory = metadata.PluginDirecotry;
                        }

                        plugins.Add(pair);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
            #if (DEBUG)
                    {
                        throw;
                    }
            #endif
                }

            }

            return plugins;
        }
Example #5
0
        public override List<PluginPair> LoadPlugin()
        {
            List<PluginPair> plugins = new List<PluginPair>();
            List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.Python.ToUpper()).ToList();
            foreach (PluginMetadata metadata in metadatas)
            {
                PythonPluginWrapper python = new PythonPluginWrapper(metadata);
                PluginPair pair = new PluginPair()
                {
                    Plugin = python,
                    Metadata = metadata
                };
                plugins.Add(pair);
            }

            return plugins;
        }
Example #6
0
        private void SwitchPythonEnv(PluginPair thirdPlugin)
        {
            if (currentPythonModulePath != thirdPlugin.Metadata.PluginDirecotry)
            {
                currentPythonModulePath = thirdPlugin.Metadata.PluginDirecotry;

                if (GIL != IntPtr.Zero)
                {
                    Runtime.PyEval_RestoreThread(GIL);
                    PythonEngine.Shutdown();
                }
                PythonEngine.Initialize();
                IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
                IntPtr sysDotPath = Runtime.PySys_GetObject("path");
                Runtime.PyList_Append(sysDotPath, pyStrPtr);
                GIL = PythonEngine.BeginAllowThreads();
            }
        }
Example #7
0
        public IEnumerable<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
        {
            var plugins = new List<PluginPair>();
            List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();

            foreach (PluginMetadata metadata in CSharpPluginMetadatas)
            {
                try
                {
                    Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
                    List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract &&  o.GetInterfaces().Contains(typeof(IPlugin))).ToList();
                    if (types.Count == 0)
                    {
                        Log.Warn(string.Format("Couldn't load plugin {0}: didn't find the class that implement IPlugin", metadata.Name));
                        continue;
                    }

                    foreach (Type type in types)
                    {
                        PluginPair pair = new PluginPair()
                        {
                            Plugin = Activator.CreateInstance(type) as IPlugin,
                            Metadata = metadata
                        };

                        plugins.Add(pair);
                    }
                }
                catch (System.Exception e)
                {
                    Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
            #if (DEBUG)
                    {
                        throw;
                    }
            #endif
                }
            }

            return plugins;
        }
Example #8
0
 internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
 {
     var pluginI18n = pluginPair.Plugin as IPluginI18n;
     if (pluginI18n == null) return;
     try
     {
         pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle();
         pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription();
     }
     catch (Exception e)
     {
         var woxPluginException = new WoxPluginException(pluginPair.Metadata.Name, "Update Plugin metadata translation failed:", e);
         Log.Error(woxPluginException);
     }
 }
Example #9
0
 internal static void ExecutePluginQuery(PluginPair pair, Query query)
 {
     try
     {
         Stopwatch sw = new Stopwatch();
         sw.Start();
         List<Result> results = pair.Plugin.Query(query) ?? new List<Result>();
         results.ForEach(o =>
         {
             o.PluginID = pair.Metadata.ID;
         });
         sw.Stop();
         DebugHelper.WriteLine(string.Format("Plugin query: {0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds));
         pair.QueryCount += 1;
         if (pair.QueryCount == 1)
         {
             pair.AvgQueryTime = sw.ElapsedMilliseconds;
         }
         else
         {
             pair.AvgQueryTime = (pair.AvgQueryTime + sw.ElapsedMilliseconds) / 2;
         }
         API.PushResults(query, pair.Metadata, results);
     }
     catch (System.Exception e)
     {
         throw new WoxPluginException(pair.Metadata.Name, e);
     }
 }
Example #10
0
 internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
 {
     var pluginI18n = pluginPair.Plugin as IPluginI18n;
     if (pluginI18n == null) return;
     try
     {
         pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle();
         pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription();
     }
     catch (System.Exception e)
     {
         Log.Warn("Update Plugin metadata translation failed:" + e.Message);
     #if (DEBUG)
         {
             throw;
         }
     #endif
     }
 }