コード例 #1
0
        private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
        {
            string         configPath = Path.Combine(pluginDirectory, PluginConfigName);
            PluginMetadata metadata;

            if (!File.Exists(configPath))
            {
                Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
                return(null);
            }

            try {
                metadata                 = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginType      = PluginType.ThirdParty;
                metadata.PluginDirectory = pluginDirectory;
            }
            catch (Exception) {
                string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
                Log.Warn(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, metadata.Language);
                Log.Warn(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }
            if (!File.Exists(metadata.ExecuteFilePath))
            {
                string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, metadata.ExecuteFilePath);
                Log.Warn(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }

            var customizedPluginConfig =
                UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID);
            if (customizedPluginConfig != null && !string.IsNullOrEmpty(customizedPluginConfig.Actionword))
            {
                metadata.ActionKeyword = customizedPluginConfig.Actionword;
            }

            return(metadata);
        }
コード例 #2
0
        private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
        {
            string         configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
            PluginMetadata metadata;

            if (!File.Exists(configPath))
            {
                return(null);
            }

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
            }
            catch (Exception e)
            {
                Log.Exception($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: invalid json format", e);
                return(null);
            }

            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                Log.Error($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: invalid language {metadata.Language}");
                return(null);
            }
            if (!File.Exists(metadata.ExecuteFilePath))
            {
                Log.Error($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: file {metadata.ExecuteFilePath} doesn't exist");
                return(null);
            }

            return(metadata);
        }
コード例 #3
0
        public static IEnumerable <PluginPair> DotNetPlugins(List <PluginMetadata> source)
        {
            var plugins   = new List <PluginPair>();
            var metadatas = source.Where(o => AllowedLanguage.IsDotNet(o.Language));

            foreach (var metadata in metadatas)
            {
                var milliseconds = Stopwatch.Debug($"|PluginsLoader.DotNetPlugins|Constructor init cost for {metadata.Name}", () =>
                {
#if DEBUG
                    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(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 = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
                    }
                    catch (Exception e)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|Couldn't load assembly for {metadata.Name}", 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($"|PluginsLoader.DotNetPlugins|Can't find class implement IPlugin for <{metadata.Name}>", e);
                        return;
                    }
                    IPlugin plugin;
                    try
                    {
                        plugin = (IPlugin)Activator.CreateInstance(type);
                    }
                    catch (Exception e)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|Can't create instance for <{metadata.Name}>", e);
                        return;
                    }
#endif
                    PluginPair pair = new PluginPair
                    {
                        Plugin   = plugin,
                        Metadata = metadata
                    };
                    plugins.Add(pair);
                });
                metadata.InitTime += milliseconds;
            }
            return(plugins);
        }
コード例 #4
0
ファイル: BasePluginLoader.cs プロジェクト: sdjc/Wox
        private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
        {
            string         configPath = Path.Combine(pluginDirectory, PluginConfigName);
            PluginMetadata metadata;

            if (!File.Exists(configPath))
            {
                Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
                return(null);
            }

            try
            {
                metadata                 = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginType      = PluginType.ThirdParty;
                metadata.PluginDirecotry = pluginDirectory;
            }
            catch (Exception)
            {
                string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
                Log.Error(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
                                             metadata.Language);
                Log.Error(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }
            if (!File.Exists(metadata.ExecuteFilePath))
            {
                string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
                                             metadata.ExecuteFilePath);
                Log.Error(error);
#if (DEBUG)
                {
                    throw new WoxException(error);
                }
#endif
                return(null);
            }

            return(metadata);
        }
コード例 #5
0
ファイル: PluginConfig.cs プロジェクト: pengzhxyz/Wox
        private static PluginMetadata GetPluginMetadata(string pluginDirectory)
        {
            string configPath = Path.Combine(pluginDirectory, pluginConfigName);

            if (!File.Exists(configPath))
            {
                Log.Warn($"parse plugin {configPath} failed: didn't find config file.");
                return(null);
            }

            PluginMetadata metadata;

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
                // for plugins which doesn't has ActionKeywords key
                metadata.ActionKeywords = metadata.ActionKeywords ?? new List <string> {
                    metadata.ActionKeyword
                };
                // for plugin still use old ActionKeyword
                metadata.ActionKeyword = metadata.ActionKeywords?[0];
            }
            catch (System.Exception e)
            {
                string msg = $"Parse plugin config {configPath} failed: json format is not valid";
                Log.Error(new WoxException(msg));
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                string msg = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
                Log.Error(new WoxException(msg));
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                string msg = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
                Log.Error(new WoxException(msg));
                return(null);
            }

            //replace action keyword if user customized it.
            var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID);

            if (customizedPluginConfig?.ActionKeywords?.Count > 0)
            {
                metadata.ActionKeywords = customizedPluginConfig.ActionKeywords;
                metadata.ActionKeyword  = customizedPluginConfig.ActionKeywords[0];
            }

            return(metadata);
        }
コード例 #6
0
ファイル: PluginInstaller.cs プロジェクト: whoisjim/PowerToys
        private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
        {
            string         configPath = Path.Combine(pluginDirectory, "plugin.json");
            PluginMetadata metadata;

            if (!File.Exists(configPath))
            {
                return(null);
            }

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
            }
            catch (Exception e)
            {
                string error = $"Parse plugin config {configPath} failed: json format is not valid";
                Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
                {
                    throw new Exception(error);
                }
#else
                return(null);
#endif
            }

            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                string error = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
#if DEBUG
                {
                    throw new Exception(error);
                }
#else
                return(null);
#endif
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                string error = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
#if DEBUG
                {
                    throw new Exception(error);
                }
#else
                return(null);
#endif
            }

            return(metadata);
        }
コード例 #7
0
        private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
        {
            var            configPath = Path.Combine(pluginDirectory, PluginConfig.PluginConfigName);
            PluginMetadata metadata;

            if (!File.Exists(configPath))
            {
                return(null);
            }

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
            }
            catch (Exception)
            {
                var error = $"Parse plugin config {configPath} failed: json format is not valid";
#if (DEBUG)
                {
                    throw new Exception(error);
                }
#endif
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                var error = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
#if (DEBUG)
                {
                    throw new Exception(error);
                }
#endif
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                var error = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
#if (DEBUG)
                {
                    throw new Exception(error);
                }
#endif
                return(null);
            }

            return(metadata);
        }
コード例 #8
0
        private static PluginMetadata GetPluginMetadata(string pluginDirectory)
        {
            string configPath = Path.Combine(pluginDirectory, pluginConfigName);

            if (!File.Exists(configPath))
            {
                Log.Warn($"parse plugin {configPath} failed: didn't find config file.");
                return(null);
            }

            PluginMetadata metadata;

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
                // for plugins which doesn't has ActionKeywords key
                metadata.ActionKeywords = metadata.ActionKeywords ?? new List <string> {
                    metadata.ActionKeyword
                };
                // for plugin still use old ActionKeyword
                metadata.ActionKeyword = metadata.ActionKeywords?[0];
            }
            catch (Exception e)
            {
                string msg = $"Parse plugin config {configPath} failed: json format is not valid";
                Log.Error(new WoxException(msg));
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                string msg = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
                Log.Error(new WoxException(msg));
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                string msg = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
                Log.Error(new WoxException(msg));
                return(null);
            }

            return(metadata);
        }
コード例 #9
0
ファイル: PluginConfig.cs プロジェクト: soyfrien/PowerToys
        private static PluginMetadata GetPluginMetadata(string pluginDirectory)
        {
            string configPath = Path.Combine(pluginDirectory, PluginConfigName);

            if (!File.Exists(configPath))
            {
                Log.Error($"Didn't find config file <{configPath}>", MethodBase.GetCurrentMethod().DeclaringType);

                return(null);
            }

            PluginMetadata metadata;

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;

                // for plugins which doesn't has ActionKeywords key
                metadata.ActionKeywords = metadata.ActionKeywords ?? new List <string> {
                    metadata.ActionKeyword
                };

                // for plugin still use old ActionKeyword
                metadata.ActionKeyword = metadata.ActionKeywords?[0];
            }
            catch (Exception e)
            {
                Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e, MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>", MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for config <{configPath}", MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            return(metadata);
        }
コード例 #10
0
        private static PluginMetadata GetPluginMetadata(string pluginDirectory)
        {
            string configPath = Path.Combine(pluginDirectory, PluginConfigName);

            if (!File.Exists(configPath))
            {
                Logger.WoxError($"Didn't find config file <{configPath}>");
                return(null);
            }

            PluginMetadata metadata;

            try
            {
                metadata = JsonConvert.DeserializeObject <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
                // for plugins which doesn't has ActionKeywords key
                metadata.ActionKeywords = metadata.ActionKeywords ?? new List <string> {
                    metadata.ActionKeyword
                };
                // for plugin still use old ActionKeyword
                metadata.ActionKeyword = metadata.ActionKeywords?[0];
            }
            catch (Exception e)
            {
                e.Data.Add(nameof(configPath), configPath);
                e.Data.Add(nameof(pluginDirectory), pluginDirectory);
                Logger.WoxError($"invalid json for config <{configPath}>", e);
                return(null);
            }


            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                Logger.WoxError($"Invalid language <{metadata.Language}> for config <{configPath}>");
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                Logger.WoxError($"execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
                return(null);
            }

            return(metadata);
        }
コード例 #11
0
        private static PluginMetadata GetPluginMetadata(string pluginDirectory)
        {
            string configPath = Path.Combine(pluginDirectory, PluginConfigName);

            if (!File.Exists(configPath))
            {
                Log.Error($"Didn't find config file <{configPath}>", MethodBase.GetCurrentMethod().DeclaringType);

                return(null);
            }

            PluginMetadata metadata;

            try
            {
                metadata = JsonSerializer.Deserialize <PluginMetadata>(File.ReadAllText(configPath));
                metadata.PluginDirectory = pluginDirectory;
            }
            catch (Exception e)
            {
                Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e, MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            if (!AllowedLanguage.IsAllowed(metadata.Language))
            {
                Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>", MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            if (string.IsNullOrEmpty(metadata.IcoPathDark) || string.IsNullOrEmpty(metadata.IcoPathLight))
            {
                Log.Error($"|PluginConfig.GetPluginMetadata|couldn't get icon information for config <{configPath}>", MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            if (!File.Exists(metadata.ExecuteFilePath))
            {
                Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for config <{configPath}", MethodBase.GetCurrentMethod().DeclaringType);
                return(null);
            }

            return(metadata);
        }
コード例 #12
0
        public static IEnumerable <PluginPair> DotNetPlugins(List <PluginMetadata> source)
        {
            var erroredPlugins = new List <string>();

            var plugins   = new List <PluginPair>();
            var metadatas = source.Where(o => AllowedLanguage.IsDotNet(o.Language));

            foreach (var metadata in metadatas)
            {
                var milliseconds = Stopwatch.Debug(
                    $"|PluginsLoader.DotNetPlugins|Constructor init cost for {metadata.Name}", () =>
                {
#if DEBUG
                    var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
                    var assembly       = assemblyLoader.LoadAssemblyAndDependencies();
                    var type           = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly, typeof(IPlugin),
                                                                                       typeof(IAsyncPlugin));

                    var plugin = Activator.CreateInstance(type);
#else
                    Assembly assembly = null;
                    object plugin     = null;

                    try
                    {
                        var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
                        assembly           = assemblyLoader.LoadAssemblyAndDependencies();

                        var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly, typeof(IPlugin),
                                                                                 typeof(IAsyncPlugin));

                        plugin = Activator.CreateInstance(type);
                    }
                    catch (Exception e) when(assembly == null)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|Couldn't load assembly for the plugin: {metadata.Name}", e);
                    }
                    catch (InvalidOperationException e)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
                    }
                    catch (ReflectionTypeLoadException e)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
                    }
                    catch (Exception e)
                    {
                        Log.Exception($"|PluginsLoader.DotNetPlugins|The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
                    }

                    if (plugin == null)
                    {
                        erroredPlugins.Add(metadata.Name);
                        return;
                    }
#endif
                    plugins.Add(new PluginPair
                    {
                        Plugin   = plugin,
                        Metadata = metadata
                    });
                });
                metadata.InitTime += milliseconds;
            }

            if (erroredPlugins.Count > 0)
            {
                var errorPluginString = String.Join(Environment.NewLine, erroredPlugins);

                var errorMessage = "The following "
                                   + (erroredPlugins.Count > 1 ? "plugins have " : "plugin has ")
                                   + "errored and cannot be loaded:";

                Task.Run(() =>
                {
                    MessageBox.Show($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
                                    $"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
                                    $"Please refer to the logs for more information", "",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                });
            }

            return(plugins);
        }
コード例 #13
0
ファイル: Person.cs プロジェクト: JulietteR/TD-.Net
        public Person NextPerson(AllowedLanguage langage)
        {
            var person = new Person();

            var maleFirstName = new List<string>();
            var femaleFirstName = new List<string>();
            var lastName = new List<string>();
            var civilityLong = new List<string>();
            var civilityShort = new List<string>();

            switch (langage)
            {
                case AllowedLanguage.ENGLISH:
                    maleFirstName = EnglishMaleFirstName;
                    femaleFirstName = EnglishFemaleFirstName;
                    lastName = EnglishLastName;
                    civilityLong = EnglishCivilityLong;
                    civilityShort = EnglishCivilityShort;
                    break;

                case AllowedLanguage.FRENCH:
                    maleFirstName = FrenchMaleFirstName;
                    femaleFirstName = FrenchFemaleFirstName;
                    lastName = FrenchLastName;
                    civilityLong = FrenchCivilityLong;
                    civilityShort = FrenchCivilityShort;
                    break;
            }

            // on determine le genre
            var i = Next(0, Enum.GetValues(typeof(Gender)).Length);

            person.Gender = (Gender)i;
            person.CivilityLong = civilityLong[i];
            person.CivilityShort = civilityShort[i];

            switch (person.Gender)
            {
                case Gender.MALE:
                    i = Next(0, maleFirstName.Count - 1);
                    person.FirstName = maleFirstName[i];
                    break;

                case Gender.FEMALE:
                    i = Next(0, femaleFirstName.Count - 1);
                    person.FirstName = femaleFirstName[i];
                    break;

            }

            i = Next(0, lastName.Count - 1);
            person.LastName = lastName[i];

            return person;
        }