Skip to content

Commit

Permalink
Implement Restart method, improve cancel detection on ProcessOperations
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Dec 26, 2024
1 parent 70289bb commit f7fa0a6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 42 deletions.
8 changes: 6 additions & 2 deletions src/UniGetUI.PackageEngine.Operations/AbstractOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public void Cancel()
case OperationStatus.Failed:
break;
case OperationStatus.Running:
Status = OperationStatus.Canceled;
CancelRequested?.Invoke(this, EventArgs.Empty);
Status = OperationStatus.Canceled;
break;
case OperationStatus.InQueue:
Status = OperationStatus.Canceled;
OperationQueue.Remove(this);
Status = OperationStatus.Canceled;
break;
Expand Down Expand Up @@ -195,7 +197,8 @@ public async Task MainThread()
{
Status = OperationStatus.Failed;
OperationFailed?.Invoke(this, EventArgs.Empty);
Line(Metadata.FailureMessage + " - " + CoreTools.Translate("Click here for more details"), LineType.StdERR);
Line(Metadata.FailureMessage, LineType.StdERR);
Line(Metadata.FailureMessage + " - " + CoreTools.Translate("Click here for more details"), LineType.Progress);
}
else if (result == OperationVeredict.Canceled)
{
Expand All @@ -214,7 +217,8 @@ public void SkipQueue()

public void Retry()
{
throw new NotImplementedException();
if (Status is OperationStatus.Running or OperationStatus.InQueue) return;
_ = MainThread();
}

protected abstract Task<OperationVeredict> PerformOperation();
Expand Down
4 changes: 2 additions & 2 deletions src/UniGetUI.PackageEngine.Operations/PackageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PackageOperation(
bool IgnoreParallelInstalls = false)
: this(package, InstallationOptions.FromPackage(package), role, IgnoreParallelInstalls) { }

protected sealed override async Task PrepareProcessStartInfo()
protected sealed override void PrepareProcessStartInfo()
{
Package.SetTag(PackageTag.OnQueue);
string operation_args = string.Join(" ", Package.Manager.OperationHelper.GetParameters(Package, Options, Role));
Expand All @@ -74,7 +74,7 @@ protected sealed override async Task PrepareProcessStartInfo()
{
if (Settings.Get("DoCacheAdminRights") || Settings.Get("DoCacheAdminRightsForBatches"))
{
await CoreTools.CacheUACForCurrentProcess();
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
}

process.StartInfo.FileName = CoreData.GSudoPath;
Expand Down
16 changes: 12 additions & 4 deletions src/UniGetUI.PackageEngine.Operations/ProcessOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ namespace UniGetUI.PackageOperations;
public abstract class AbstractProcessOperation : AbstractOperation
{
protected Process process { get; private set; }
private bool ProcessKilled;
protected AbstractProcessOperation(bool queue_enabled) : base(queue_enabled)
{
process = new();
CancelRequested += (_, _) => process.Kill();
OperationStarting += async (_, _) =>
CancelRequested += (_, _) =>
{
ProcessKilled = true;
process.Kill();
};
OperationStarting += (_, _) =>
{
process = new();
process.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -50,7 +55,7 @@ protected AbstractProcessOperation(bool queue_enabled) : base(queue_enabled)

Line(line, lineType);
};
await PrepareProcessStartInfo();
PrepareProcessStartInfo();
};
}

Expand All @@ -76,9 +81,12 @@ protected override async Task<OperationVeredict> PerformOperation()
Line($"End Time: \"{DateTime.Now}\"", LineType.OperationInfo);
Line($"Process return value: \"{process.ExitCode}\" (0x{process.ExitCode:X})", LineType.OperationInfo);

if (ProcessKilled)
return OperationVeredict.Canceled;

return await GetProcessVeredict(process.ExitCode, []);
}

protected abstract Task<OperationVeredict> GetProcessVeredict(int ReturnCode, string[] Output);
protected abstract Task PrepareProcessStartInfo();
protected abstract void PrepareProcessStartInfo();
}
8 changes: 4 additions & 4 deletions src/UniGetUI.PackageEngine.Operations/SourceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public class AddSourceOperation : SourceOperation
public AddSourceOperation(IManagerSource source) : base(source)
{ }

protected override async Task PrepareProcessStartInfo()
protected override void PrepareProcessStartInfo()
{
if (Source.Manager.Capabilities.Sources.MustBeInstalledAsAdmin)
{
if (Settings.Get("DoCacheAdminRights") || Settings.Get("DoCacheAdminRightsForBatches"))
{
await CoreTools.CacheUACForCurrentProcess();
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
}
process.StartInfo.FileName = CoreData.GSudoPath;
process.StartInfo.Arguments = $"\"{Source.Manager.Status.ExecutablePath}\" " + Source.Manager.Properties.ExecutableCallArgs + " " + string.Join(" ", Source.Manager.SourcesHelper.GetAddSourceParameters(Source));
Expand Down Expand Up @@ -80,13 +80,13 @@ public class RemoveSourceOperation : SourceOperation
public RemoveSourceOperation(IManagerSource source) : base(source)
{ }

protected override async Task PrepareProcessStartInfo()
protected override void PrepareProcessStartInfo()
{
if (Source.Manager.Capabilities.Sources.MustBeInstalledAsAdmin)
{
if (Settings.Get("DoCacheAdminRights") || Settings.Get("DoCacheAdminRightsForBatches"))
{
await CoreTools.CacheUACForCurrentProcess();
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
}
process.StartInfo.FileName = CoreData.GSudoPath;
process.StartInfo.Arguments = $"\"{Source.Manager.Status.ExecutablePath}\" " + Source.Manager.Properties.ExecutableCallArgs + " " + string.Join(" ", Source.Manager.SourcesHelper.GetRemoveSourceParameters(Source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,12 @@ public IEnumerable<string> GetParameters(
IInstallationOptions options,
OperationType operation)
{
try
{
var parameters = _getOperationParameters(package, options, operation);
Logger.Info(
$"Loaded operation parameters for package id={package.Id} on manager {Manager.Name} and operation {operation}: " +
string.Join(' ', parameters));
return parameters;
}
catch (Exception ex)
{
Logger.Error(
$"A fatal error ocurred while loading operation parameters for package id={package.Id} on manager {Manager.Name} and operation {operation}");
Logger.Error(ex);
return [];
}
var parameters = _getOperationParameters(package, options, operation);
Logger.Info(
$"Loaded operation parameters for package id={package.Id} on manager {Manager.Name} and operation {operation}: " +
string.Join(' ', parameters));
return parameters;

}

public OperationVeredict GetResult(
Expand All @@ -53,22 +44,13 @@ public OperationVeredict GetResult(
IEnumerable<string> processOutput,
int returnCode)
{
try
{
if (returnCode is 999 && processOutput.Last() == "Error: The operation was canceled by the user.")
{
Logger.Warn("Elevator [or GSudo] UAC prompt was canceled, not showing error message...");
return OperationVeredict.Canceled;
}

return _getOperationResult(package, operation, processOutput, returnCode);
}
catch (Exception ex)
if (returnCode is 999 && (!processOutput.Any() || processOutput.Last() == "Error: The operation was canceled by the user."))
{
Logger.Error(
$"A fatal error ocurred while loading operation parameters for package id={package.Id} on manager {Manager.Name} and operation {operation}");
Logger.Error(ex);
return OperationVeredict.Failure;
Logger.Warn("Elevator [or GSudo] UAC prompt was canceled, not showing error message...");
return OperationVeredict.Canceled;
}

return _getOperationResult(package, operation, processOutput, returnCode);
}
}
2 changes: 1 addition & 1 deletion src/UniGetUI/Controls/OperationWidgets/OperationControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace UniGetUI.Controls.OperationWidgets;
public class OperationControl: INotifyPropertyChanged
{
public AbstractOperation Operation;
private bool ErrorTooltipShown = false;
private bool ErrorTooltipShown;

public OperationControl(AbstractOperation operation)
{
Expand Down

0 comments on commit f7fa0a6

Please sign in to comment.