From 2dbf4109d92a684a94bbd365975391001afb420b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Sun, 19 Jan 2025 15:23:57 +0100 Subject: [PATCH 1/2] Add support for the app setings to be contained within the executable folder --- src/UniGetUI.Core.Data/CoreData.cs | 117 ++++++++++++++++------------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/src/UniGetUI.Core.Data/CoreData.cs b/src/UniGetUI.Core.Data/CoreData.cs index 29b916bb9..bfc50458e 100644 --- a/src/UniGetUI.Core.Data/CoreData.cs +++ b/src/UniGetUI.Core.Data/CoreData.cs @@ -10,42 +10,6 @@ public static class CoreData { private static int? __code_page; 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(); - string purifiedString = ""; - - foreach (var c in contents.Split(':')[^1].Trim()) - { - if (c >= '0' && c <= '9') - { - purifiedString += c; - } - } - - return int.Parse(purifiedString); - } - catch (Exception e) - { - Logger.Error(e); - return 0; - } - } - public const string VersionName = "3.1.6-beta1"; // Do not modify this line, use file scripts/apply_versions.py public const int BuildNumber = 76; // Do not modify this line, use file scripts/apply_versions.py @@ -63,6 +27,8 @@ public static HttpClientHandler GenericHttpClientParameters } } + private static bool? IS_PORTABLE; + /// /// The directory where all the user data is stored. The directory is automatically created if it does not exist. /// @@ -70,9 +36,21 @@ public static string UniGetUIDataDirectory { get { - string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui"); - string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI"); - return GetNewDataDirectoryOrMoveOld(old_path, new_path); + if (IS_PORTABLE is null) + IS_PORTABLE = File.Exists(Path.Join(UniGetUIExecutableDirectory, "ForceUniGetUIPortable")); + + if (IS_PORTABLE == true) + { + string path = Path.Join(UniGetUIExecutableDirectory, "Settings"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + return path; + } + else + { + string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui"); + string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI"); + return GetNewDataDirectoryOrMoveOld(old_path, new_path); + } } } @@ -84,11 +62,7 @@ public static string UniGetUIInstallationOptionsDirectory get { string path = Path.Join(UniGetUIDataDirectory, "InstallationOptions"); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - + if (!Directory.Exists(path)) Directory.CreateDirectory(path); return path; } } @@ -100,9 +74,9 @@ public static string UniGetUICacheDirectory_Data { get { - string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WingetUI", "CachedData"); - string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI", "CachedMetadata"); - return GetNewDataDirectoryOrMoveOld(old_path, new_path); + string path = Path.Join(UniGetUIDataDirectory, "CachedMetadata"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + return path; } } @@ -113,9 +87,9 @@ public static string UniGetUICacheDirectory_Icons { get { - string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WingetUI", "CachedIcons"); - string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI", "CachedMedia"); - return GetNewDataDirectoryOrMoveOld(old_path, new_path); + string path = Path.Join(UniGetUIDataDirectory, "CachedMedia"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + return path; } } @@ -126,9 +100,9 @@ public static string UniGetUICacheDirectory_Lang { get { - string old_dir = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WingetUI", "CachedLangFiles"); - string new_dir = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI", "CachedLanguageFiles"); - return GetNewDataDirectoryOrMoveOld(old_dir, new_dir); + string path= Path.Join(UniGetUIDataDirectory, "CachedLanguageFiles"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + return path; } } @@ -308,5 +282,42 @@ private static string GetNewDataDirectoryOrMoveOld(string old_path, string new_p TypeInfoResolverChain = { new DefaultJsonTypeInfoResolver() }, WriteIndented = true, }; + + + 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(); + string purifiedString = ""; + + foreach (var c in contents.Split(':')[^1].Trim()) + { + if (c >= '0' && c <= '9') + { + purifiedString += c; + } + } + + return int.Parse(purifiedString); + } + catch (Exception e) + { + Logger.Error(e); + return 0; + } + } + } } From 47ac186323198ce51aff0040cfdc4b2228603932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Sun, 19 Jan 2025 16:12:47 +0100 Subject: [PATCH 2/2] When set to portable mode, the installer will create the required flag so UniGetUI runs in portable mode --- InstallerExtras/ForceUniGetUIPortable | 0 UniGetUI.iss | 1 + src/UniGetUI/EntryPoint.cs | 1 + 3 files changed, 2 insertions(+) create mode 100644 InstallerExtras/ForceUniGetUIPortable diff --git a/InstallerExtras/ForceUniGetUIPortable b/InstallerExtras/ForceUniGetUIPortable new file mode 100644 index 000000000..e69de29bb diff --git a/UniGetUI.iss b/UniGetUI.iss index b8a603037..a572dd9b0 100644 --- a/UniGetUI.iss +++ b/UniGetUI.iss @@ -224,6 +224,7 @@ Source: "unigetui_bin\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion; Source: "unigetui_bin\*"; DestDir: "{app}"; Flags: createallsubdirs ignoreversion recursesubdirs; Source: "src\UniGetUI.PackageEngine.Managers.Chocolatey\choco-cli\*"; DestDir: "{userpf}\..\UniGetUI\Chocolatey"; Flags: createallsubdirs ignoreversion recursesubdirs uninsneveruninstall; Tasks: regularinstall\chocoinstall; Check: not CmdLineParamExists('/NoChocolatey'); Source: "InstallerExtras\EnsureWinGet.ps1"; DestDir: "{tmp}"; Flags: deleteafterinstall +Source: "InstallerExtras\ForceUniGetUIPortable"; DestDir: "{app}"; Tasks: portableinstall [Icons] diff --git a/src/UniGetUI/EntryPoint.cs b/src/UniGetUI/EntryPoint.cs index dbbc87e7b..52d390b8b 100644 --- a/src/UniGetUI/EntryPoint.cs +++ b/src/UniGetUI/EntryPoint.cs @@ -56,6 +56,7 @@ Welcome to UniGetUI Version {CoreData.VersionName} Logger.ImportantInfo(textart); Logger.ImportantInfo(" "); Logger.ImportantInfo($"Build {CoreData.BuildNumber}"); + Logger.ImportantInfo($"Data directory {CoreData.UniGetUIDataDirectory}"); Logger.ImportantInfo($"Encoding Code Page set to {CoreData.CODE_PAGE}"); // WinRT single-instance fancy stuff