|
3 | 3 | using System.Linq; |
4 | 4 | using System.Linq.Expressions; |
5 | 5 | using System.Threading; |
| 6 | +using System.Collections.Generic; |
6 | 7 | using PatchKit.Unity.Patcher.Cancellation; |
7 | 8 | using PatchKit.Unity.Patcher.Debug; |
8 | 9 | using PatchKit.Unity.Patcher.AppUpdater.Commands; |
@@ -71,43 +72,46 @@ AppContentSummary latestVersionContentSummary |
71 | 72 | checkIntegrity.Prepare(_status); |
72 | 73 | checkIntegrity.Execute(cancellationToken); |
73 | 74 |
|
74 | | - int missingFilesCount = checkIntegrity.Results.Files |
75 | | - .Select(f => f.Status == FileIntegrityStatus.MissingData) |
76 | | - .Count(); |
| 75 | + var missingFiles = checkIntegrity.Results.Files |
| 76 | + .Where(f => f.Status == FileIntegrityStatus.MissingData); |
| 77 | + |
| 78 | + int missingFilesCount = missingFiles.Count(); |
77 | 79 |
|
78 | | - int invalidSizeFilesCount = checkIntegrity.Results.Files |
79 | | - .Select(f => f.Status == FileIntegrityStatus.InvalidSize) |
80 | | - .Count(); |
| 80 | + var invalidSizeFiles = checkIntegrity.Results.Files |
| 81 | + .Where(f => f.Status == FileIntegrityStatus.InvalidSize); |
| 82 | + |
| 83 | + int invalidSizeFilesCount = invalidSizeFiles.Count(); |
81 | 84 |
|
82 | 85 | if (missingFilesCount + invalidSizeFilesCount == 0) |
83 | 86 | { |
84 | 87 | DebugLogger.Log("No missing or invalid size files."); |
85 | 88 | return; |
86 | 89 | } |
87 | 90 |
|
88 | | - var repairStrategy = new AppUpdaterRepairAndDiffStrategy(Context, _status, performDiff: false); |
89 | | - |
90 | | - double repairCost = (missingFilesCount + invalidSizeFilesCount) * 2; |
91 | | - if (isNewVersionAvailable) |
92 | | - { |
93 | | - repairCost *= latestVersionContentSummary.Chunks.Size; |
94 | | - } |
95 | | - else |
96 | | - { |
97 | | - repairCost *= installedVersionContentSummary.Chunks.Size; |
98 | | - } |
| 91 | + double repairCost = CalculateRepairCost(installedVersionContentSummary, missingFiles.Concat(invalidSizeFiles)); |
99 | 92 |
|
100 | 93 | if (repairCost < contentSize) |
101 | 94 | { |
102 | 95 | DebugLogger.Log(string.Format("Repair cost {0} is smaller than content cost {1}, repairing...", repairCost, contentSize)); |
| 96 | + IAppUpdaterStrategy repairStrategy = _strategyResolver.Create(StrategyType.RepairAndDiff, Context); |
103 | 97 | repairStrategy.Update(cancellationToken); |
104 | 98 | } |
105 | 99 | else |
106 | 100 | { |
107 | | - DebugLogger.Log("Content cost is smaller than repair."); |
| 101 | + DebugLogger.Log("Content cost is smaller than repair. Uninstalling to prepare for content strategy."); |
| 102 | + IUninstallCommand uninstall = commandFactory.CreateUninstallCommand(Context); |
| 103 | + uninstall.Prepare(_status); |
| 104 | + uninstall.Execute(cancellationToken); |
108 | 105 | } |
109 | 106 | } |
110 | 107 |
|
| 108 | + private long CalculateRepairCost(AppContentSummary contentSummary, IEnumerable<FileIntegrity> filesToRepair) |
| 109 | + { |
| 110 | + return filesToRepair |
| 111 | + .Select(f => contentSummary.Files.FirstOrDefault(e => e.Path == f.FileName)) |
| 112 | + .Sum(f => f.Size); |
| 113 | + } |
| 114 | + |
111 | 115 | public void Update(CancellationToken cancellationToken) |
112 | 116 | { |
113 | 117 | Assert.MethodCalledOnlyOnce(ref _updateHasBeenCalled, "Update"); |
|
0 commit comments