Skip to content

Commit

Permalink
Merge pull request #3255 from marticliment/improve-winget-error-detec…
Browse files Browse the repository at this point in the history
…tion

Fix WinGet getting corrupted on devices with non-admin accounts
  • Loading branch information
marticliment authored Feb 4, 2025
2 parents 0ec1513 + 5f96734 commit 5ba1a32
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/UniGetUI.Core.Data/CoreData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static string UniGetUIExecutableFile
}
}

public static string GSudoPath = "";
public static string ElevatorPath = "";

/// <summary>
/// This method will return the most appropriate data directory.
Expand Down
12 changes: 7 additions & 5 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using UniGetUI.Core.Data;
using UniGetUI.Core.Language;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;

namespace UniGetUI.Core.Tools
{
Expand Down Expand Up @@ -419,7 +420,7 @@ public static async Task CacheUACForCurrentProcess()
{
StartInfo = new ProcessStartInfo
{
FileName = CoreData.GSudoPath,
FileName = CoreData.ElevatorPath,
Arguments = "cache on --pid " + Environment.ProcessId + " -d 1",
UseShellExecute = false,
RedirectStandardOutput = true,
Expand All @@ -443,7 +444,7 @@ public static async Task ResetUACForCurrentProcess()
{
StartInfo = new ProcessStartInfo
{
FileName = CoreData.GSudoPath,
FileName = CoreData.ElevatorPath,
Arguments = "cache off --pid " + Environment.ProcessId,
UseShellExecute = false,
RedirectStandardOutput = true,
Expand Down Expand Up @@ -558,9 +559,10 @@ public static async Task WaitForInternetConnection()

public static async Task _waitForInternetConnection()
{
Logger.Debug(
"Checking for internet connectivity. Pinging google.com, microsoft.com, couldflare.com and marticliment.com");
string[] hosts = ["google.com", "microsoft.com", "cloudflare.com", "marticliment.com"];
if (Settings.Get("DisableWaitForInternetConnection")) return;

Logger.Debug("Checking for internet connectivity. Pinging google.com, microsoft.com, couldflare.com and marticliment.com");
string[] hosts = ["google.com", "microsoft.com", "cloudflare.com", "github.com"];
while (true)
{
foreach (var host in hosts)
Expand Down
21 changes: 15 additions & 6 deletions src/UniGetUI.PackageEngine.Operations/PackageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UniGetUI.PackageEngine.Classes.Packages.Classes;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.Managers.WingetManager;
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUI.PackageEngine.PackageLoader;
using UniGetUI.PackageOperations;
Expand Down Expand Up @@ -59,7 +60,7 @@ private bool RequiresAdminRights()
{
return Package.OverridenOptions.RunAsAdministrator is true
|| Options.RunAsAdministrator
|| (Settings.Get("AlwaysElevate" + Package.Manager.Name) && !Package.OverridenOptions.RunAsAdministrator is false);
|| (Settings.Get("AlwaysElevate" + Package.Manager.Name) && Package.OverridenOptions.RunAsAdministrator != false);
}

protected override void ApplyRetryAction(string retryMode)
Expand Down Expand Up @@ -98,19 +99,27 @@ protected sealed override void PrepareProcessStartInfo()
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
}

process.StartInfo.FileName = CoreData.GSudoPath;
process.StartInfo.Arguments =
$"\"{Package.Manager.Status.ExecutablePath}\" {Package.Manager.Properties.ExecutableCallArgs} {operation_args}";
if (Package.Manager is WinGet)
{
string WinGetTemp = Path.Join(Path.GetTempPath(), "UniGetUI", "ElevatedWinGetTemp");
process.StartInfo.Environment["TEMP"] = WinGetTemp;
process.StartInfo.Environment["TMP"] = WinGetTemp;
}
process.StartInfo.FileName = CoreData.ElevatorPath;
process.StartInfo.Arguments = $"\"{Package.Manager.Status.ExecutablePath}\" {Package.Manager.Properties.ExecutableCallArgs} {operation_args}";
}
else
{
process.StartInfo.FileName = Package.Manager.Status.ExecutablePath;
process.StartInfo.Arguments = $"{Package.Manager.Properties.ExecutableCallArgs} {operation_args}";
}
ApplyCapabilities(admin,

ApplyCapabilities(
admin,
Options.InteractiveInstallation,
(Options.SkipHashCheck && Role is not OperationType.Uninstall),
Package.OverridenOptions.Scope ?? Options.InstallationScope);
Package.OverridenOptions.Scope ?? Options.InstallationScope
);
}

protected sealed override Task<OperationVeredict> GetProcessVeredict(int ReturnCode, string[] Output)
Expand Down
4 changes: 2 additions & 2 deletions src/UniGetUI.PackageEngine.Operations/SourceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override void PrepareProcessStartInfo()
}

admin = true;
process.StartInfo.FileName = CoreData.GSudoPath;
process.StartInfo.FileName = CoreData.ElevatorPath;
process.StartInfo.Arguments = $"\"{Source.Manager.Status.ExecutablePath}\" " + Source.Manager.Properties.ExecutableCallArgs + " " + string.Join(" ", Source.Manager.SourcesHelper.GetAddSourceParameters(Source));
}
else
Expand Down Expand Up @@ -108,7 +108,7 @@ protected override void PrepareProcessStartInfo()
{
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
}
process.StartInfo.FileName = CoreData.GSudoPath;
process.StartInfo.FileName = CoreData.ElevatorPath;
process.StartInfo.Arguments = $"\"{Source.Manager.Status.ExecutablePath}\" " + Source.Manager.Properties.ExecutableCallArgs + " " + string.Join(" ", Source.Manager.SourcesHelper.GetRemoveSourceParameters(Source));

}
Expand Down
3 changes: 2 additions & 1 deletion src/UniGetUI.PackageEngine.PackageEngine/PEInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using UniGetUI.PackageEngine.Managers.VcpkgManager;
using UniGetUI.PackageEngine.PackageLoader;
using System.Collections.ObjectModel;
using UniGetUI.Core.Tools;
using UniGetUI.PackageOperations;

namespace UniGetUI.PackageEngine
Expand Down Expand Up @@ -64,8 +65,8 @@ public static void Initialize()
Logger.Warn("Timeout: Not all package managers have finished initializing.");
}

_ = UpgradablePackagesLoader.ReloadPackages();
_ = InstalledPackagesLoader.ReloadPackages();
_ = UpgradablePackagesLoader.ReloadPackages();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Interfaces;

namespace UniGetUI.PackageEngine.PackageLoader
Expand Down Expand Up @@ -48,11 +49,18 @@ public List<IPackage> Packages
private readonly bool ALLOW_MULTIPLE_PACKAGE_VERSIONS;
private readonly bool DISABLE_RELOAD;
private readonly bool PACKAGES_CHECKED_BY_DEFAULT;
private readonly bool REQUIRES_INTERNET;
protected string LOADER_IDENTIFIER;
private int LoadOperationIdentifier;
protected IEnumerable<IPackageManager> Managers { get; private set; }

public AbstractPackageLoader(IEnumerable<IPackageManager> managers, string identifier, bool AllowMultiplePackageVersions = false, bool DisableReload = false, bool CheckedBydefault = false)
public AbstractPackageLoader(
IEnumerable<IPackageManager> managers,
string identifier,
bool AllowMultiplePackageVersions,
bool DisableReload,
bool CheckedBydefault,
bool RequiresInternet)
{
Managers = managers;
PackageReference = new ConcurrentDictionary<long, IPackage>();
Expand All @@ -63,6 +71,7 @@ public AbstractPackageLoader(IEnumerable<IPackageManager> managers, string ident
ALLOW_MULTIPLE_PACKAGE_VERSIONS = AllowMultiplePackageVersions;
LOADER_IDENTIFIER = identifier;
ALLOW_MULTIPLE_PACKAGE_VERSIONS = AllowMultiplePackageVersions;
REQUIRES_INTERNET = RequiresInternet;
}

/// <summary>
Expand Down Expand Up @@ -108,6 +117,11 @@ public virtual async Task ReloadPackages()
IsLoading = true;
StartedLoading?.Invoke(this, EventArgs.Empty);

if (REQUIRES_INTERNET)
{
await CoreTools.WaitForInternetConnection();
}

List<Task<IEnumerable<IPackage>>> tasks = new();

foreach (IPackageManager manager in Managers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public class DiscoverablePackagesLoader : AbstractPackageLoader
private string QUERY_TEXT = string.Empty;

public DiscoverablePackagesLoader(IEnumerable<IPackageManager> managers)
: base(managers, "DISCOVERABLE_PACKAGES", AllowMultiplePackageVersions: false, CheckedBydefault: false)
: base(managers,
identifier: "DISCOVERABLE_PACKAGES",
AllowMultiplePackageVersions: false,
DisableReload: false,
CheckedBydefault: false,
RequiresInternet: true)
{
Instance = this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ public class InstalledPackagesLoader : AbstractPackageLoader
public static InstalledPackagesLoader Instance = null!;

public InstalledPackagesLoader(IEnumerable<IPackageManager> managers)
: base(managers, "INSTALLED_PACKAGES", AllowMultiplePackageVersions: true, CheckedBydefault: false)
: base(
managers,
identifier: "INSTALLED_PACKAGES",
AllowMultiplePackageVersions: true,
DisableReload: false,
CheckedBydefault: false,
RequiresInternet: true)
{
Instance = this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public class PackageBundlesLoader : AbstractPackageLoader
public static PackageBundlesLoader Instance = null!;

public PackageBundlesLoader(IEnumerable<IPackageManager> managers)
: base(managers, "PACKAGE_BUNDLES", AllowMultiplePackageVersions: true, DisableReload: true, CheckedBydefault: false)
: base(managers,
identifier: "PACKAGE_BUNDLES",
AllowMultiplePackageVersions: true,
DisableReload: true,
CheckedBydefault: false,
RequiresInternet: false)
{
Instance = this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public class UpgradablePackagesLoader : AbstractPackageLoader
public ConcurrentDictionary<string, IPackage> IgnoredPackages = new();

public UpgradablePackagesLoader(IEnumerable<IPackageManager> managers)
: base(managers, "DISCOVERABLE_PACKAGES", AllowMultiplePackageVersions: false, CheckedBydefault: !Settings.Get("DisableSelectingUpdatesByDefault"))
: base(managers,
identifier: "UPGRADABLE_PACKAGES",
AllowMultiplePackageVersions: false,
DisableReload: false,
CheckedBydefault: !Settings.Get("DisableSelectingUpdatesByDefault"),
RequiresInternet: true)
{
Instance = this;
FinishedLoading += (_, _) => StartAutoCheckTimeout();
Expand Down
10 changes: 5 additions & 5 deletions src/UniGetUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,26 @@ private static async void LoadGSudo()
if (!DEBUG && !Settings.Get("DisableUniGetUIElevator"))
{
Logger.ImportantInfo("Using built-in UniGetUI Elevator");
CoreData.GSudoPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "UniGetUI Elevator.exe");
CoreData.ElevatorPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "UniGetUI Elevator.exe");
}
else if (Settings.Get("UseUserGSudo"))
{
var(found, gsudo_path) = await CoreTools.WhichAsync("gsudo.exe");
if (found)
{
Logger.Info($"Using System GSudo at {gsudo_path}");
CoreData.GSudoPath = gsudo_path;
CoreData.ElevatorPath = gsudo_path;
}
else
{
Logger.Error("System GSudo enabled but not found!");
CoreData.GSudoPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "gsudo.exe");
CoreData.ElevatorPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "gsudo.exe");
}
}
else
{
Logger.Warn($"Using bundled GSudo at {CoreData.GSudoPath} since UniGetUI Elevator is not available!");
CoreData.GSudoPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "gsudo.exe");
Logger.Warn($"Using bundled GSudo at {CoreData.ElevatorPath} since UniGetUI Elevator is not available!");
CoreData.ElevatorPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "gsudo.exe");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/UniGetUI/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@
SettingName="DisableLangAutoUpdater"
StateChanged="DisableDownloadingNewTranslations_StateChanged"
/>
<widgets:CheckboxCard
x:Name="DisableWaitForInternetConnection"
Text="Wait for the device to be connected to the internet before attempting to do tasks that require internet connectivity."
SettingName="DisableWaitForInternetConnection"
StateChanged="DisableDownloadingNewTranslations_StateChanged"
/>
<widgets:TextboxCard
Text="Use a custom icon and screenshot database URL"
Placeholder="Leave empty for default"
Expand Down
6 changes: 3 additions & 3 deletions test_publish.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off
rmdir /q /s src\UniGetUI\bin\x64\Release\net8.0-windows10.0.22621.0\win-x64\publish\
rmdir /q /s src\UniGetUI\bin\x64\Release\net8.0-windows10.0.26100.0\win-x64\publish\
dotnet publish src/UniGetUI/UniGetUI.csproj /noLogo /property:Configuration=Release /property:Platform=x64 -v m
%signcommand% "src\UniGetUI\bin\x64\Release\net8.0-windows10.0.22621.0\win-x64\publish\UniGetUI.exe"
src\UniGetUI\bin\x64\Release\net8.0-windows10.0.22621.0\win-x64\publish\UniGetUI.exe
%signcommand% "src\UniGetUI\bin\x64\Release\net8.0-windows10.0.26100.0\win-x64\publish\UniGetUI.exe"
src\UniGetUI\bin\x64\Release\net8.0-windows10.0.26100.0\win-x64\publish\UniGetUI.exe
pause

0 comments on commit 5ba1a32

Please sign in to comment.