Skip to content

Commit c0fc29b

Browse files
authored
Merge pull request #144 from patchkit-net/bugfix/v3.17.x.x/content-repair
Fix previously refactored AppRepairer
2 parents c0f34f9 + 5dc4f2f commit c0fc29b

File tree

7 files changed

+41
-11
lines changed

7 files changed

+41
-11
lines changed

Assets/PatchKit Patcher/Scripts/AppUpdater/AppRepairer.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ AppContentSummary latestVersionContentSummary
104104
bool isNewVersionAvailable = installedVersionId < latestVersionId;
105105

106106
long contentSize = isNewVersionAvailable
107-
? latestVersionContentSummary.Size
108-
: installedVersionContentSummary.Size;
107+
? latestVersionContentSummary.Files.Sum(f => f.Size)
108+
: installedVersionContentSummary.Files.Sum(f => f.Size);
109109

110110
double repairCost = CalculateRepairCost(installedVersionContentSummary, filesNeedFixing);
111111

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

122-
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
123-
uninstall.Prepare(_status, cancellationToken);
124-
uninstall.Execute(cancellationToken);
122+
ReinstallContent(cancellationToken);
125123
}
126124
else if (repairCost < contentSize)
127125
{
@@ -131,10 +129,8 @@ AppContentSummary latestVersionContentSummary
131129
}
132130
else
133131
{
134-
DebugLogger.Log("Content cost is smaller than repair. Uninstalling to prepare for content strategy.");
135-
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
136-
uninstall.Prepare(_status, cancellationToken);
137-
uninstall.Execute(cancellationToken);
132+
DebugLogger.Log(string.Format("Content cost {0} is smaller than repair {1}. Reinstalling.", contentSize, repairCost));
133+
ReinstallContent(cancellationToken);
138134
}
139135

140136
return false;
@@ -173,5 +169,17 @@ private long CalculateRepairCost(AppContentSummary contentSummary, IEnumerable<F
173169
.Select(f => contentSummary.Files.FirstOrDefault(e => e.Path == f.FileName))
174170
.Sum(f => f.Size);
175171
}
172+
173+
private void ReinstallContent(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken)
174+
{
175+
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context);
176+
uninstall.Prepare(_status, cancellationToken);
177+
uninstall.Execute(cancellationToken);
178+
179+
// not catching any exceptions here, because exception during content installation in this place should be fatal
180+
var contentStrategy = new AppUpdaterContentStrategy(Context, _status);
181+
contentStrategy.RepairOnError = false; // do not attempt to repair content to not cause a loop
182+
contentStrategy.Update(cancellationToken);
183+
}
176184
}
177185
}

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterContentStrategy.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class AppUpdaterContentStrategy: IAppUpdaterStrategy
1717

1818
private bool _updateHasBeenCalled;
1919

20+
public bool RepairOnError { get; set; }
21+
2022
public AppUpdaterContentStrategy(AppUpdaterContext context, UpdaterStatus status)
2123
{
2224
Checks.ArgumentNotNull(context, "context");
@@ -25,6 +27,9 @@ public AppUpdaterContentStrategy(AppUpdaterContext context, UpdaterStatus status
2527

2628
_context = context;
2729
_status = status;
30+
31+
// defaults
32+
RepairOnError = true;
2833
}
2934

3035
public StrategyType GetStrategyType()
@@ -103,7 +108,7 @@ public void Update(CancellationToken cancellationToken)
103108

104109
installContent.Execute(cancellationToken);
105110

106-
if (installContent.NeedRepair)
111+
if (installContent.NeedRepair && RepairOnError)
107112
{
108113
DebugLogger.Log("Content installed with errors, requesting repair");
109114

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterDiffStrategy.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public struct Context<CommandType>
3131

3232
private bool _updateHasBeenCalled;
3333

34+
// not used
35+
public bool RepairOnError { get; set; }
36+
3437
public AppUpdaterDiffStrategy(AppUpdaterContext context, UpdaterStatus status)
3538
{
3639

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterEmptyStrategy.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public class AppUpdaterEmptyStrategy: IAppUpdaterStrategy
88
{
99
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(AppUpdaterStrategyResolver));
1010

11+
// not used
12+
public bool RepairOnError { get; set; }
13+
1114
public StrategyType GetStrategyType()
1215
{
1316
return StrategyType.Empty;

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterRepairAndDiffStrategy.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class AppUpdaterRepairAndDiffStrategy: IAppUpdaterStrategy
1717
private readonly IAppUpdaterStrategy _repairStrategy;
1818
private readonly IAppUpdaterStrategy _diffStrategy;
1919

20+
// not used
21+
public bool RepairOnError { get; set; }
22+
2023
public AppUpdaterRepairAndDiffStrategy(AppUpdaterContext context, UpdaterStatus status)
2124
{
2225
Assert.IsNotNull(context, "Context is null");

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterRepairStrategy.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class AppUpdaterRepairStrategy: IAppUpdaterStrategy
2222

2323
private readonly ILogger _logger;
2424

25+
// not used
26+
public bool RepairOnError { get; set; }
27+
2528
public AppUpdaterRepairStrategy(AppUpdaterContext context, UpdaterStatus status)
2629
{
2730
Assert.IsNotNull(context, "Context is null");

Assets/PatchKit Patcher/Scripts/AppUpdater/IAppUpdaterStrategy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public enum StrategyType
1515

1616
public interface IAppUpdaterStrategy
1717
{
18+
// if set to true, attempt to repair by using AppRepairer on error.
19+
// Makes sense to set it to false from ongoing repair process.
20+
// default: true
21+
bool RepairOnError { get; set; }
22+
1823
void Update(CancellationToken cancellationToken);
1924

2025
StrategyType GetStrategyType();

0 commit comments

Comments
 (0)