-
Notifications
You must be signed in to change notification settings - Fork 19
Attempt repair content on failed content installation #142
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
Assets/PatchKit Patcher/Scripts/AppUpdater/AppRepairer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading; | ||
using System.Collections.Generic; | ||
using PatchKit.Unity.Patcher.Cancellation; | ||
using PatchKit.Unity.Patcher.Debug; | ||
using PatchKit.Unity.Patcher.AppUpdater.Commands; | ||
using PatchKit.Unity.Patcher.AppUpdater.Status; | ||
using PatchKit.Api.Models.Main; | ||
|
||
namespace PatchKit.Unity.Patcher.AppUpdater | ||
{ | ||
public class AppRepairer | ||
{ | ||
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(AppRepairer)); | ||
|
||
public readonly AppUpdaterContext Context; | ||
|
||
// set to true if you wish to check file hashes | ||
public bool CheckHashes = false; | ||
|
||
// how many times process will repeat until it ultimately fails | ||
public int RepeatCount = 3; | ||
|
||
private UpdaterStatus _status; | ||
|
||
private AppUpdaterStrategyResolver _strategyResolver; | ||
|
||
private AppUpdaterCommandFactory _commandFactory; | ||
|
||
private int _lowestVersionWithContentId; | ||
|
||
|
||
public AppRepairer(AppUpdaterContext context, UpdaterStatus status) | ||
{ | ||
DebugLogger.LogConstructor(); | ||
|
||
Checks.ArgumentNotNull(context, "context"); | ||
|
||
Context = context; | ||
_status = status; | ||
|
||
_strategyResolver = new AppUpdaterStrategyResolver(_status); | ||
_commandFactory = new AppUpdaterCommandFactory(); | ||
} | ||
|
||
// returns true if data is valid (was valid from the start or successfull repair was performed) | ||
public bool Perform(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken) | ||
{ | ||
_lowestVersionWithContentId = Context.App.GetLowestVersionWithContentId(cancellationToken); | ||
|
||
for(int attempt = 1; attempt <= RepeatCount; ++attempt) | ||
{ | ||
DebugLogger.Log("Running integrity check, attempt " + attempt + " of " + RepeatCount); | ||
|
||
if (PerformInternal(cancellationToken)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
// retry count reached, let's check for the last time if data is ok, but without repairing | ||
int installedVersionId = Context.App.GetInstalledVersionId(); | ||
VersionIntegrity results = CheckIntegrity(cancellationToken, installedVersionId); | ||
var filesNeedFixing = FilesNeedFixing(results); | ||
|
||
if (filesNeedFixing.Count() == 0) | ||
{ | ||
DebugLogger.Log("No missing or invalid size files."); | ||
return true; | ||
} | ||
|
||
|
||
DebugLogger.LogError("Still have corrupted files after all fixing attempts"); | ||
return false; | ||
} | ||
|
||
// returns true if there was no integrity errors, false if there was and repair was performed | ||
private bool PerformInternal(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken) | ||
{ | ||
int installedVersionId = Context.App.GetInstalledVersionId(); | ||
|
||
VersionIntegrity results = CheckIntegrity(cancellationToken, installedVersionId); | ||
var filesNeedFixing = FilesNeedFixing(results); | ||
|
||
if (filesNeedFixing.Count() == 0) | ||
{ | ||
DebugLogger.Log("No missing or invalid size files."); | ||
return true; | ||
} | ||
|
||
// need to collect some data about the application to calculate the repair cost and make decisions | ||
|
||
int latestVersionId = Context.App.GetLatestVersionId(true, cancellationToken); | ||
|
||
AppContentSummary installedVersionContentSummary | ||
= Context.App.RemoteMetaData.GetContentSummary(installedVersionId, cancellationToken); | ||
|
||
AppContentSummary latestVersionContentSummary | ||
= Context.App.RemoteMetaData.GetContentSummary(latestVersionId, cancellationToken); | ||
|
||
bool isNewVersionAvailable = installedVersionId < latestVersionId; | ||
|
||
long contentSize = isNewVersionAvailable | ||
? latestVersionContentSummary.Size | ||
: installedVersionContentSummary.Size; | ||
|
||
double repairCost = CalculateRepairCost(installedVersionContentSummary, filesNeedFixing); | ||
|
||
|
||
if (_lowestVersionWithContentId > installedVersionId) | ||
{ | ||
DebugLogger.Log( | ||
"Repair is impossible because lowest version with content id is " | ||
+ _lowestVersionWithContentId + | ||
" and currently installed version id is " | ||
+ installedVersionId + | ||
". Uninstalling to prepare for content strategy."); | ||
|
||
IUninstallCommand uninstall = _commandFactory.CreateUninstallCommand(Context); | ||
uninstall.Prepare(_status, cancellationToken); | ||
uninstall.Execute(cancellationToken); | ||
} | ||
else if (repairCost < contentSize) | ||
{ | ||
DebugLogger.Log(string.Format("Repair cost {0} is smaller than content cost {1}, repairing...", repairCost, contentSize)); | ||
IAppUpdaterStrategy repairStrategy = _strategyResolver.Create(StrategyType.Repair, Context); | ||
repairStrategy.Update(cancellationToken); | ||
} | ||
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); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private VersionIntegrity CheckIntegrity( | ||
PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken, | ||
int installedVersionId | ||
) | ||
{ | ||
ICheckVersionIntegrityCommand checkIntegrity = _commandFactory | ||
.CreateCheckVersionIntegrityCommand( | ||
versionId: installedVersionId, | ||
context: Context, | ||
isCheckingHash: CheckHashes, | ||
isCheckingSize: true, | ||
cancellationToken: cancellationToken); | ||
|
||
checkIntegrity.Prepare(_status, cancellationToken); | ||
checkIntegrity.Execute(cancellationToken); | ||
|
||
return checkIntegrity.Results; | ||
} | ||
|
||
private IEnumerable<FileIntegrity> FilesNeedFixing(VersionIntegrity results) | ||
{ | ||
var missingFiles = results.Files.Where(f => f.Status == FileIntegrityStatus.MissingData); | ||
var invalidSizeFiles = results.Files.Where(f => f.Status == FileIntegrityStatus.InvalidSize); | ||
|
||
return missingFiles.Concat(invalidSizeFiles); | ||
} | ||
|
||
private long CalculateRepairCost(AppContentSummary contentSummary, IEnumerable<FileIntegrity> filesToRepair) | ||
{ | ||
return filesToRepair | ||
.Select(f => contentSummary.Files.FirstOrDefault(e => e.Path == f.FileName)) | ||
.Sum(f => f.Size); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/PatchKit Patcher/Scripts/AppUpdater/AppRepairer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.