Skip to content

Commit

Permalink
Handle an edge case where WinGet COM would crash
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Dec 28, 2024
1 parent fa8fa5d commit 6e26d61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.InteropServices;
using Microsoft.Management.Deployment;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
Expand Down Expand Up @@ -86,26 +87,35 @@ protected override IEnumerable<string> _getOperationParameters(IPackage package,
});
}

var installOptions = NativePackageHandler.GetInstallationOptions(package, operation);
if (installOptions?.ElevationRequirement is ElevationRequirement.ElevationRequired
or ElevationRequirement.ElevatesSelf)
try
{
package.OverridenOptions.RunAsAdministrator = true;
var installOptions = NativePackageHandler.GetInstallationOptions(package, operation);
if (installOptions?.ElevationRequirement is ElevationRequirement.ElevationRequired
or ElevationRequirement.ElevatesSelf)
{
package.OverridenOptions.RunAsAdministrator = true;
}
else if (installOptions?.ElevationRequirement is ElevationRequirement.ElevationProhibited)
{
if (CoreTools.IsAdministrator())
throw new UnauthorizedAccessException(
CoreTools.Translate("This package cannot be installed from an elevated context.")
+ CoreTools.Translate("Please run UniGetUI as a regular user and try again."));

if (options.RunAsAdministrator)
throw new UnauthorizedAccessException(
CoreTools.Translate("This package cannot be installed from an elevated context.")
+ CoreTools.Translate("Please check the installation options for this package and try again"));
package.OverridenOptions.RunAsAdministrator = false;
}
}
else if (installOptions?.ElevationRequirement is ElevationRequirement.ElevationProhibited)
catch (Exception ex)
{
if (CoreTools.IsAdministrator())
throw new UnauthorizedAccessException(
CoreTools.Translate("This package cannot be installed from an elevated context.")
+ CoreTools.Translate("Please run UniGetUI as a regular user and try again."));

if (options.RunAsAdministrator)
throw new UnauthorizedAccessException(
CoreTools.Translate("This package cannot be installed from an elevated context.")
+ CoreTools.Translate("Please check the installation options for this package and try again"));
package.OverridenOptions.RunAsAdministrator = false;
Logger.Error("Recovered from fatal WinGet exception:");
Logger.Error(ex);
}


return parameters;
}

Expand Down
22 changes: 18 additions & 4 deletions src/UniGetUI/Pages/DialogPages/DialogHelper_Operations.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Windows.UI;
using ExternalLibraries.Clipboard;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -68,7 +69,7 @@ public static async Task ShowOperationFailedDialog(AbstractOperation operation,

ScrollViewer ScrollView = new()
{
MaxHeight = 500,
MaxHeight = MainApp.Instance.MainWindow.NavigationPage.ActualHeight > 800? 500: 300,
MaxWidth = 800,
CornerRadius = new CornerRadius(6),
Background = (Brush)Application.Current.Resources["ApplicationPageBackgroundThemeBrush"],
Expand Down Expand Up @@ -105,7 +106,10 @@ public static async Task ShowOperationFailedDialog(AbstractOperation operation,
Grid.SetRow(headerContent, 0);
Grid.SetRow(ScrollView, 1);

var CloseButton = new Button() { Content = CoreTools.Translate("Close"), HorizontalAlignment = HorizontalAlignment.Stretch };
var CloseButton = new Button()
{
Content = CoreTools.Translate("Close"), HorizontalAlignment = HorizontalAlignment.Stretch, Height = 30,
};
CloseButton.Click += (_, _) =>
{
dialog.Hide();
Expand All @@ -115,7 +119,12 @@ public static async Task ShowOperationFailedDialog(AbstractOperation operation,
var retryOptions = opControl.GetRetryOptions(() => dialog.Hide());
if (retryOptions.Any())
{
SplitButton RetryButton = new SplitButton() { Content = CoreTools.Translate("Retry"), HorizontalAlignment = HorizontalAlignment.Stretch };
SplitButton RetryButton = new SplitButton()
{
Content = CoreTools.Translate("Retry"),
HorizontalAlignment = HorizontalAlignment.Stretch,
Height = 30,
};
RetryButton.Click += (_, _) =>
{
operation.Retry(AbstractOperation.RetryMode.Retry);
Expand All @@ -132,7 +141,12 @@ public static async Task ShowOperationFailedDialog(AbstractOperation operation,
}
else
{
var RetryButton = new Button() { Content = CoreTools.Translate("Retry"), HorizontalAlignment = HorizontalAlignment.Stretch};
var RetryButton = new Button()
{
Content = CoreTools.Translate("Retry"),
HorizontalAlignment = HorizontalAlignment.Stretch,
Height = 30,
};
RetryButton.Click += (_, _) =>
{
operation.Retry(AbstractOperation.RetryMode.Retry);
Expand Down

0 comments on commit 6e26d61

Please sign in to comment.