Skip to content

Fix previously refactored AppRepairer #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions Assets/PatchKit Patcher/Scripts/AppUpdater/AppRepairer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ AppContentSummary latestVersionContentSummary
bool isNewVersionAvailable = installedVersionId < latestVersionId;

long contentSize = isNewVersionAvailable
? latestVersionContentSummary.Size
: installedVersionContentSummary.Size;
? latestVersionContentSummary.Files.Sum(f => f.Size)
: installedVersionContentSummary.Files.Sum(f => f.Size);

double repairCost = CalculateRepairCost(installedVersionContentSummary, filesNeedFixing);

Expand All @@ -117,11 +117,9 @@ AppContentSummary latestVersionContentSummary
+ _lowestVersionWithContentId +
" and currently installed version id is "
+ installedVersionId +
". Uninstalling to prepare for content strategy.");
". Reinstalling.");

IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
uninstall.Prepare(_status, cancellationToken);
uninstall.Execute(cancellationToken);
ReinstallContent(cancellationToken);
}
else if (repairCost < contentSize)
{
Expand All @@ -131,10 +129,8 @@ AppContentSummary latestVersionContentSummary
}
else
{
DebugLogger.Log("Content cost is smaller than repair. Uninstalling to prepare for content strategy.");
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
uninstall.Prepare(_status, cancellationToken);
uninstall.Execute(cancellationToken);
DebugLogger.Log(string.Format("Content cost {0} is smaller than repair {1}. Reinstalling.", contentSize, repairCost));
ReinstallContent(cancellationToken);
}

return false;
Expand Down Expand Up @@ -173,5 +169,17 @@ private long CalculateRepairCost(AppContentSummary contentSummary, IEnumerable<F
.Select(f => contentSummary.Files.FirstOrDefault(e => e.Path == f.FileName))
.Sum(f => f.Size);
}

private void ReinstallContent(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken)
{
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
uninstall.Prepare(_status, cancellationToken);
uninstall.Execute(cancellationToken);

// not catching any exceptions here, because exception during content installation in this place should be fatal
var contentStrategy = new AppUpdaterContentStrategy(Context, _status);
contentStrategy.RepairOnError = false; // do not attempt to repair content to not cause a loop
contentStrategy.Update(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class AppUpdaterContentStrategy: IAppUpdaterStrategy

private bool _updateHasBeenCalled;

public bool RepairOnError { get; set; }

public AppUpdaterContentStrategy(AppUpdaterContext context, UpdaterStatus status)
{
Checks.ArgumentNotNull(context, "context");
Expand All @@ -25,6 +27,9 @@ public AppUpdaterContentStrategy(AppUpdaterContext context, UpdaterStatus status

_context = context;
_status = status;

// defaults
RepairOnError = true;
}

public StrategyType GetStrategyType()
Expand Down Expand Up @@ -103,7 +108,7 @@ public void Update(CancellationToken cancellationToken)

installContent.Execute(cancellationToken);

if (installContent.NeedRepair)
if (installContent.NeedRepair && RepairOnError)
{
DebugLogger.Log("Content installed with errors, requesting repair");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public struct Context<CommandType>

private bool _updateHasBeenCalled;

// not used
public bool RepairOnError { get; set; }

public AppUpdaterDiffStrategy(AppUpdaterContext context, UpdaterStatus status)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class AppUpdaterEmptyStrategy: IAppUpdaterStrategy
{
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(AppUpdaterStrategyResolver));

// not used
public bool RepairOnError { get; set; }

public StrategyType GetStrategyType()
{
return StrategyType.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class AppUpdaterRepairAndDiffStrategy: IAppUpdaterStrategy
private readonly IAppUpdaterStrategy _repairStrategy;
private readonly IAppUpdaterStrategy _diffStrategy;

// not used
public bool RepairOnError { get; set; }

public AppUpdaterRepairAndDiffStrategy(AppUpdaterContext context, UpdaterStatus status)
{
Assert.IsNotNull(context, "Context is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class AppUpdaterRepairStrategy: IAppUpdaterStrategy

private readonly ILogger _logger;

// not used
public bool RepairOnError { get; set; }

public AppUpdaterRepairStrategy(AppUpdaterContext context, UpdaterStatus status)
{
Assert.IsNotNull(context, "Context is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public enum StrategyType

public interface IAppUpdaterStrategy
{
// if set to true, attempt to repair by using AppRepairer on error.
// Makes sense to set it to false from ongoing repair process.
// default: true
bool RepairOnError { get; set; }

void Update(CancellationToken cancellationToken);

StrategyType GetStrategyType();
Expand Down