From cc5d36fbac0947a90045025608eb9f696269fea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Fri, 5 Jul 2024 23:05:01 +0200 Subject: [PATCH 1/4] Possible fixes for broken encoding --- src/UniGetUI.Core.Tools/Tools.cs | 3 ++- .../WinGet.cs | 19 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/UniGetUI.Core.Tools/Tools.cs b/src/UniGetUI.Core.Tools/Tools.cs index 90a30350f..9ad60aab3 100644 --- a/src/UniGetUI.Core.Tools/Tools.cs +++ b/src/UniGetUI.Core.Tools/Tools.cs @@ -90,7 +90,8 @@ public static async Task> Which(string command) RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, - StandardOutputEncoding = System.Text.Encoding.UTF8 + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), } }; process.Start(); diff --git a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs index 51a136f24..a6d2f523f 100644 --- a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs +++ b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs @@ -119,16 +119,16 @@ protected override async Task GetAvailableUpdates_UnSafe() { StartInfo = new ProcessStartInfo() { - FileName = PowerShellPath, - Arguments = PowerShellPromptArgs, + FileName = "cmd.exe", + Arguments = "/C " + PowerShellPath + " " + PowerShellPromptArgs, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, - StandardOutputEncoding = System.Text.Encoding.UTF8, + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), StandardInputEncoding = new UTF8Encoding(false), - StandardErrorEncoding = System.Text.Encoding.UTF8, + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), } }; @@ -196,21 +196,20 @@ function Print-WinGetPackage { protected override async Task GetInstalledPackages_UnSafe() { List Packages = []; - Process p = new() { StartInfo = new ProcessStartInfo() - { - FileName = PowerShellPath, - Arguments = PowerShellPromptArgs, + { + FileName = "cmd.exe", + Arguments = "/C " + PowerShellPath + " " + PowerShellPromptArgs, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, - StandardOutputEncoding = System.Text.Encoding.UTF8, - StandardErrorEncoding = System.Text.Encoding.UTF8, + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), StandardInputEncoding = new UTF8Encoding(false), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), } }; From 3cb9ef427e13ead1d964c0d65874585a220c1666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Fri, 5 Jul 2024 23:19:04 +0200 Subject: [PATCH 2/4] Try zero value for getting default chpc --- src/UniGetUI.Core.Data/CoreData.cs | 2 ++ .../WinGet.cs | 17 +++++++++-------- .../WinGetHelpers.cs | 11 ++++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/UniGetUI.Core.Data/CoreData.cs b/src/UniGetUI.Core.Data/CoreData.cs index e5c707a80..1db3de78b 100644 --- a/src/UniGetUI.Core.Data/CoreData.cs +++ b/src/UniGetUI.Core.Data/CoreData.cs @@ -6,6 +6,8 @@ namespace UniGetUI.Core.Data { public static class CoreData { + public const int CODE_PAGE = 0; + public const string VersionName = "3.1.0"; // Do not modify this line, use file scripts/apply_versions.py public const double VersionNumber = 3.1; // Do not modify this line, use file scripts/apply_versions.py diff --git a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs index a6d2f523f..6729c1b64 100644 --- a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs +++ b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using UniGetUI.Core.Data; using UniGetUI.Core.Logging; using UniGetUI.Core.Tools; using UniGetUI.PackageEngine.Classes.Manager.Classes; @@ -24,9 +25,9 @@ public class WinGet : PackageManager private LocalWingetSource GOGSource { get; set; } private LocalWingetSource MicrosoftStoreSource { get; set; } - private readonly string PowerShellPath; - private readonly string PowerShellPromptArgs; - private readonly string PowerShellInlineArgs; + public readonly string PowerShellPath; + public readonly string PowerShellPromptArgs; + public readonly string PowerShellInlineArgs; public WinGet() : base() { @@ -114,7 +115,7 @@ protected override async Task FindPackages_UnSafe(string query) protected override async Task GetAvailableUpdates_UnSafe() { List Packages = []; - + Process p = new() { StartInfo = new ProcessStartInfo() @@ -126,9 +127,9 @@ protected override async Task GetAvailableUpdates_UnSafe() RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, - StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), StandardInputEncoding = new UTF8Encoding(false), - StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), } }; @@ -207,9 +208,9 @@ protected override async Task GetInstalledPackages_UnSafe() RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, - StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), StandardInputEncoding = new UTF8Encoding(false), - StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), } }; diff --git a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGetHelpers.cs b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGetHelpers.cs index 9beedc3a8..448e34748 100644 --- a/src/UniGetUI.PackageEngine.Managers.WinGet/WinGetHelpers.cs +++ b/src/UniGetUI.PackageEngine.Managers.WinGet/WinGetHelpers.cs @@ -1,5 +1,6 @@ using Microsoft.Management.Deployment; using System.Diagnostics; +using System.Text; using System.Text.RegularExpressions; using UniGetUI.Core.Data; using UniGetUI.Core.Logging; @@ -425,13 +426,16 @@ public async Task FindPackages_UnSafe(WinGet ManagerInstance, string { StartInfo = new ProcessStartInfo() { - FileName = "powershell.exe", + FileName = "cmd.exe", + Arguments = "/C " + ManagerInstance.PowerShellPath + " " + ManagerInstance.PowerShellPromptArgs, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, - StandardOutputEncoding = System.Text.Encoding.UTF8 + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), + StandardInputEncoding = new UTF8Encoding(false), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), } }; @@ -440,7 +444,8 @@ public async Task FindPackages_UnSafe(WinGet ManagerInstance, string ManagerClasses.Classes.ProcessTaskLogger logger = ManagerInstance.TaskLogger.CreateNew(LoggableTaskType.FindPackages, p); string command = """ - Set-ExecutionPolicy Bypass -Scope Process -Force + Write-Output (Get-Module -Name Microsoft.WinGet.Client).Version + Import-Module Microsoft.WinGet.Client function Print-WinGetPackage { param ( [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Name, From 27227b141c0a52707173250b5082e6ed41bebb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Sat, 6 Jul 2024 00:11:58 +0200 Subject: [PATCH 3/4] Create a dynamic PageCode loader --- src/UniGetUI.Core.Data/CoreData.cs | 29 ++++++++++++++++++++++++++++- src/UniGetUI.Core.Tools/Tools.cs | 4 ++-- src/UniGetUI/EntryPoint.cs | 18 ++++++++++-------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/UniGetUI.Core.Data/CoreData.cs b/src/UniGetUI.Core.Data/CoreData.cs index 1db3de78b..ec3b60002 100644 --- a/src/UniGetUI.Core.Data/CoreData.cs +++ b/src/UniGetUI.Core.Data/CoreData.cs @@ -1,13 +1,40 @@ using System.Diagnostics; using System.Net; +using System.Runtime.InteropServices; using UniGetUI.Core.Logging; namespace UniGetUI.Core.Data { public static class CoreData { - public const int CODE_PAGE = 0; + private static int? __code_page = null; + public static int CODE_PAGE { get => __code_page ??= GetCodePage(); } + private static int GetCodePage() + { + try + { + Process p = new Process() + { + StartInfo = new ProcessStartInfo() + { + FileName = "chcp.com", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + p.Start(); + string contents = p.StandardOutput.ReadToEnd(); + return int.Parse(contents.Split(':')[^1].Trim()); + } + catch (Exception e) + { + Logger.Error(e); + return 0; + } + } + public const string VersionName = "3.1.0"; // Do not modify this line, use file scripts/apply_versions.py public const double VersionNumber = 3.1; // Do not modify this line, use file scripts/apply_versions.py diff --git a/src/UniGetUI.Core.Tools/Tools.cs b/src/UniGetUI.Core.Tools/Tools.cs index 9ad60aab3..2e1b1b1fd 100644 --- a/src/UniGetUI.Core.Tools/Tools.cs +++ b/src/UniGetUI.Core.Tools/Tools.cs @@ -90,8 +90,8 @@ public static async Task> Which(string command) RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, - StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), - StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(437), + StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), + StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE), } }; process.Start(); diff --git a/src/UniGetUI/EntryPoint.cs b/src/UniGetUI/EntryPoint.cs index 3a8ec62c3..ee1e353a4 100644 --- a/src/UniGetUI/EntryPoint.cs +++ b/src/UniGetUI/EntryPoint.cs @@ -47,17 +47,19 @@ static async Task AsyncMain(string[] args) { try { - string textart = @" - __ __ _ ______ __ __ ______ - / / / /___ (_) ____/__ / /_/ / / / _/ - / / / / __ \/ / / __/ _ \/ __/ / / // / -/ /_/ / / / / / /_/ / __/ /_/ /_/ // / -\____/_/ /_/_/\____/\___/\__/\____/___/ - Welcome to UniGetUI Version " + CoreData.VersionName; + string textart = $""" + __ __ _ ______ __ __ ______ + / / / /___ (_) ____/__ / /_/ / / / _/ + / / / / __ \/ / / __/ _ \/ __/ / / // / + / /_/ / / / / / /_/ / __/ /_/ /_/ // / + \____/_/ /_/_/\____/\___/\__/\____/___/ + Welcome to UniGetUI Version {CoreData.VersionName} + """; Logger.ImportantInfo(textart); Logger.ImportantInfo(" "); - Logger.ImportantInfo("Version Code: " + CoreData.VersionNumber.ToString()); + Logger.ImportantInfo($"Version Code: {CoreData.VersionNumber}"); + Logger.ImportantInfo($"Encoding Code Page set to {CoreData.CODE_PAGE}"); // WinRT single-instance fancy stuff WinRT.ComWrappersSupport.InitializeComWrappers(); From 264d4e18f7eca8636d7c7c83ca3ac1d4fc615351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Sat, 6 Jul 2024 00:14:54 +0200 Subject: [PATCH 4/4] Update CoreData.cs --- src/UniGetUI.Core.Data/CoreData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UniGetUI.Core.Data/CoreData.cs b/src/UniGetUI.Core.Data/CoreData.cs index ec3b60002..9f5fe51d0 100644 --- a/src/UniGetUI.Core.Data/CoreData.cs +++ b/src/UniGetUI.Core.Data/CoreData.cs @@ -7,7 +7,7 @@ namespace UniGetUI.Core.Data { public static class CoreData { - private static int? __code_page = null; + private static int? __code_page; public static int CODE_PAGE { get => __code_page ??= GetCodePage(); } private static int GetCodePage()