From dcb9e34d59a6b57afae2c1da0966459a157c9750 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 16 Nov 2019 00:34:27 +0200 Subject: [PATCH 1/5] alphabet settings --- Wox.Infrastructure/Alphabet.cs | 27 ++++++++++++++++++--- Wox.Infrastructure/UserSettings/Settings.cs | 5 ++++ Wox/App.xaml.cs | 3 ++- Wox/Languages/en.xaml | 1 + Wox/SettingWindow.xaml | 3 +++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Wox.Infrastructure/Alphabet.cs b/Wox.Infrastructure/Alphabet.cs index 157d1c9f1f..91979bf526 100644 --- a/Wox.Infrastructure/Alphabet.cs +++ b/Wox.Infrastructure/Alphabet.cs @@ -6,6 +6,7 @@ using hyjiacan.util.p4n; using hyjiacan.util.p4n.format; using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage; +using Wox.Infrastructure.UserSettings; namespace Wox.Infrastructure { @@ -14,9 +15,11 @@ namespace Wox.Infrastructure private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat(); private static ConcurrentDictionary PinyinCache; private static BinaryStorage> _pinyinStorage; + private static Settings _settings; - public static void Initialize() + public static void Initialize(Settings settings) { + _settings = settings; Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () => @@ -34,12 +37,20 @@ namespace Wox.Infrastructure _pinyinStorage.Save(PinyinCache); } + private static string[] EmptyStringArray = new string[0]; + private static string[][] Empty2DStringArray = new string[0][]; + /// /// replace chinese character with pinyin, non chinese character won't be modified /// should be word or sentence, instead of single character. e.g. 微软 /// public static string[] Pinyin(string word) { + if (!_settings.ShouldUsePinyin) + { + return EmptyStringArray; + } + var pinyin = word.Select(c => { var pinyins = PinyinHelper.toHanyuPinyinStringArray(c); @@ -57,7 +68,7 @@ namespace Wox.Infrastructure /// public static string[][] PinyinComination(string characters) { - if (!string.IsNullOrEmpty(characters)) + if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters)) { if (!PinyinCache.ContainsKey(characters)) { @@ -89,7 +100,7 @@ namespace Wox.Infrastructure } else { - return new string[][] { }; + return Empty2DStringArray; } } @@ -101,6 +112,11 @@ namespace Wox.Infrastructure public static bool ContainsChinese(string word) { + if (!_settings.ShouldUsePinyin) + { + return false; + } + var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray) .Any(p => p != null); return chinese; @@ -108,6 +124,11 @@ namespace Wox.Infrastructure private static string[] Combination(string[] array1, string[] array2) { + if (!_settings.ShouldUsePinyin) + { + return EmptyStringArray; + } + var combination = ( from a1 in array1 from a2 in array2 diff --git a/Wox.Infrastructure/UserSettings/Settings.cs b/Wox.Infrastructure/UserSettings/Settings.cs index 75f4393575..7371ea5d05 100644 --- a/Wox.Infrastructure/UserSettings/Settings.cs +++ b/Wox.Infrastructure/UserSettings/Settings.cs @@ -21,6 +21,11 @@ namespace Wox.Infrastructure.UserSettings public string ResultFontWeight { get; set; } public string ResultFontStretch { get; set; } + /// + /// when false Alphabet static service will always return empty results + /// + public bool ShouldUsePinyin { get; set; } = true; + private string _querySearchPrecision { get; set; } = StringMatcher.SearchPrecisionScore.Regular.ToString(); public string QuerySearchPrecision { diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index b66549b38a..0ec1614bf9 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -49,11 +49,12 @@ namespace Wox RegisterDispatcherUnhandledException(); ImageLoader.Initialize(); - Alphabet.Initialize(); _settingsVM = new SettingWindowViewModel(); _settings = _settingsVM.Settings; + Alphabet.Initialize(_settings); + StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision; PluginManager.LoadPlugins(_settings.PluginSettings); diff --git a/Wox/Languages/en.xaml b/Wox/Languages/en.xaml index ae8041121c..dca044dc11 100644 --- a/Wox/Languages/en.xaml +++ b/Wox/Languages/en.xaml @@ -34,6 +34,7 @@ Hide Wox on startup Hide tray icon Query Search Precision + Should Use Pinyin Plugin diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index 86d6a345a9..9a5146c7ee 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -55,6 +55,9 @@ Checked="OnAutoStartupChecked" Unchecked="OnAutoStartupUncheck"> + + + Date: Sat, 16 Nov 2019 00:57:30 +0200 Subject: [PATCH 2/5] fixed broken log --- .../Logger/ProgramLogger.cs | 59 +++++++++++-------- Plugins/Wox.Plugin.Program/Programs/UWP.cs | 2 +- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs b/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs index c8a104b101..aaf816e418 100644 --- a/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs +++ b/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs @@ -40,21 +40,11 @@ namespace Wox.Plugin.Program.Logger LogManager.Configuration = configuration; } - /// - /// Please follow exception format: |class name|calling method name|loading program path|user friendly message that explains the error - /// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue - /// [MethodImpl(MethodImplOptions.Synchronized)] - internal static void LogException(string message, Exception e) + internal static void LogException(string classname, string callingMethodName, string loadingProgramPath, + string interpretationMessage, Exception e) { - //Index 0 is always empty. - var parts = message.Split('|'); - var classname = parts[1]; - var callingMethodName = parts[2]; - var loadingProgramPath = parts[3]; - var interpretationMessage = parts[4]; - - Debug.WriteLine($"ERROR{message}"); + Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}"); var logger = LogManager.GetLogger(""); @@ -78,16 +68,16 @@ namespace Wox.Plugin.Program.Logger calledMethod = string.IsNullOrEmpty(calledMethod) ? "Not available" : calledMethod; logger.Error($"\nException full name: {e.GetType().FullName}" - + $"\nError status: {errorStatus}" - + $"\nClass name: {classname}" - + $"\nCalling method: {callingMethodName}" - + $"\nProgram path: {loadingProgramPath}" - + $"\nInnerException number: {innerExceptionNumber}" - + $"\nException message: {e.Message}" - + $"\nException error type: HResult {e.HResult}" - + $"\nException thrown in called method: {calledMethod}" - + $"\nPossible interpretation of the error: {interpretationMessage}" - + $"\nPossible resolution: {possibleResolution}"); + + $"\nError status: {errorStatus}" + + $"\nClass name: {classname}" + + $"\nCalling method: {callingMethodName}" + + $"\nProgram path: {loadingProgramPath}" + + $"\nInnerException number: {innerExceptionNumber}" + + $"\nException message: {e.Message}" + + $"\nException error type: HResult {e.HResult}" + + $"\nException thrown in called method: {calledMethod}" + + $"\nPossible interpretation of the error: {interpretationMessage}" + + $"\nPossible resolution: {possibleResolution}"); innerExceptionNumber++; e = e.InnerException; @@ -96,6 +86,29 @@ namespace Wox.Plugin.Program.Logger logger.Error("------------- END Wox.Plugin.Program exception -------------"); } + /// + /// Please follow exception format: |class name|calling method name|loading program path|user friendly message that explains the error + /// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue + /// + [MethodImpl(MethodImplOptions.Synchronized)] + internal static void LogException(string message, Exception e) + { + //Index 0 is always empty. + var parts = message.Split('|'); + if (parts.Length < 4) + { + var logger = LogManager.GetLogger(""); + logger.Error(e, $"fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}"); + } + + var classname = parts[1]; + var callingMethodName = parts[2]; + var loadingProgramPath = parts[3]; + var interpretationMessage = parts[4]; + + LogException(classname, callingMethodName, loadingProgramPath, interpretationMessage, e); + } + private static bool IsKnownWinProgramError(Exception e, string callingMethodName) { if (e.TargetSite?.Name == "GetDescription" && callingMethodName == "LnkProgram") diff --git a/Plugins/Wox.Plugin.Program/Programs/UWP.cs b/Plugins/Wox.Plugin.Program/Programs/UWP.cs index d511888ab0..d4a0596657 100644 --- a/Plugins/Wox.Plugin.Program/Programs/UWP.cs +++ b/Plugins/Wox.Plugin.Program/Programs/UWP.cs @@ -205,7 +205,7 @@ namespace Wox.Plugin.Program.Programs } catch (Exception e) { - ProgramLogger.LogException("|UWP|CurrentUserPackages|An unexpected error occured and " + ProgramLogger.LogException("UWP" ,"CurrentUserPackages", $"id","An unexpected error occured and " + $"unable to verify if package is valid", e); return false; } From 9d98d26cc7e6813e8736bec5ea609c85b58fff75 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 16 Nov 2019 01:28:07 +0200 Subject: [PATCH 3/5] added log into Alphabet service since it can induce a large perf hit --- Wox.Infrastructure/Alphabet.cs | 6 ++++++ Wox.Infrastructure/StringMatcher.cs | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Wox.Infrastructure/Alphabet.cs b/Wox.Infrastructure/Alphabet.cs index 91979bf526..d7fae882cb 100644 --- a/Wox.Infrastructure/Alphabet.cs +++ b/Wox.Infrastructure/Alphabet.cs @@ -117,6 +117,12 @@ namespace Wox.Infrastructure return false; } + if (word.Length > 40) + { + Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {word}"); + return false; + } + var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray) .Any(p => p != null); return chinese; diff --git a/Wox.Infrastructure/StringMatcher.cs b/Wox.Infrastructure/StringMatcher.cs index 9c49b4119b..5b82b18918 100644 --- a/Wox.Infrastructure/StringMatcher.cs +++ b/Wox.Infrastructure/StringMatcher.cs @@ -131,12 +131,6 @@ namespace Wox.Infrastructure { if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target)) { - if(source.Length > 40) - { - Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {source}"); - return 0; - } - if (Alphabet.ContainsChinese(source)) { var combination = Alphabet.PinyinComination(source); From 8446c9563d126f8b31b9d97b066bb9661504091a Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Sat, 16 Nov 2019 02:37:01 +0200 Subject: [PATCH 4/5] fixed lock issue --- Plugins/Wox.Plugin.Program/Main.cs | 10 ++++++---- Plugins/Wox.Plugin.Program/Programs/UWP.cs | 7 +++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index 0a6f37a4a0..009dd84fbd 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -90,9 +90,10 @@ namespace Wox.Plugin.Program public static void IndexWin32Programs() { + var win32S = Win32.All(_settings); lock (IndexLock) { - _win32s = Win32.All(_settings); + _win32s = win32S; } } @@ -101,17 +102,18 @@ namespace Wox.Plugin.Program var windows10 = new Version(10, 0); var support = Environment.OSVersion.Version.Major >= windows10.Major; + var applications = support ? UWP.All() : new UWP.Application[] { }; lock (IndexLock) { - _uwps = support ? UWP.All() : new UWP.Application[] { }; + _uwps = applications; } } public static void IndexPrograms() { - var t1 = Task.Run(() => { IndexWin32Programs(); }); + var t1 = Task.Run(IndexWin32Programs); - var t2 = Task.Run(() => { IndexUWPPrograms(); }); + var t2 = Task.Run(IndexUWPPrograms); Task.WaitAll(t1, t2); diff --git a/Plugins/Wox.Plugin.Program/Programs/UWP.cs b/Plugins/Wox.Plugin.Program/Programs/UWP.cs index 60f978b9dc..7a980640f0 100644 --- a/Plugins/Wox.Plugin.Program/Programs/UWP.cs +++ b/Plugins/Wox.Plugin.Program/Programs/UWP.cs @@ -165,9 +165,9 @@ namespace Wox.Plugin.Program.Programs } #endif #if DEBUG //make developer aware and implement handling - catch(Exception e) + catch { - throw e; + throw; } #endif return u.Apps; @@ -230,8 +230,7 @@ namespace Wox.Plugin.Program.Programs public override bool Equals(object obj) { - var uwp = obj as UWP; - if (uwp != null) + if (obj is UWP uwp) { return FamilyName.Equals(uwp.FamilyName); } From 9a2a88a703fc631c956bce7c85fcf177ddb9f8d9 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 17 Nov 2019 18:06:25 +1100 Subject: [PATCH 5/5] Fix build fail from task reindex run --- Plugins/Wox.Plugin.Program/Main.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index 009dd84fbd..e165a10826 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -111,9 +111,9 @@ namespace Wox.Plugin.Program public static void IndexPrograms() { - var t1 = Task.Run(IndexWin32Programs); + var t1 = Task.Run(()=>IndexWin32Programs()); - var t2 = Task.Run(IndexUWPPrograms); + var t2 = Task.Run(()=>IndexUWPPrograms()); Task.WaitAll(t1, t2);