From 76008022e78e8b251043fa7503409df7d610ce5a Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 8 May 2020 11:43:34 -0700 Subject: [PATCH] Redownloads missing cached apps when refreshing or updating --- AltStore/Managing Apps/AppManager.swift | 15 ++++++++++++--- .../FetchProvisioningProfilesOperation.swift | 3 ++- AltStore/Operations/RefreshAppOperation.swift | 7 ++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/AltStore/Managing Apps/AppManager.swift b/AltStore/Managing Apps/AppManager.swift index 536c2d1a..614c8ce3 100644 --- a/AltStore/Managing Apps/AppManager.swift +++ b/AltStore/Managing Apps/AppManager.swift @@ -498,8 +498,16 @@ private extension AppManager group.beginInstallationHandler?(installedApp) } + var downloadingApp = app + + if let installedApp = app as? InstalledApp, let storeApp = installedApp.storeApp, !FileManager.default.fileExists(atPath: installedApp.fileURL.path) + { + // Cached app has been deleted, so we need to redownload it. + downloadingApp = storeApp + } + /* Download */ - let downloadOperation = DownloadAppOperation(app: app, context: context) + let downloadOperation = DownloadAppOperation(app: downloadingApp, context: context) downloadOperation.resultHandler = { (result) in switch result { @@ -628,8 +636,9 @@ private extension AppManager case .success(let installedApp): completionHandler(.success(installedApp)) - case .failure(ALTServerError.unknownRequest): - // Fall back to installation if AltServer doesn't support newer provisioning profile requests. + case .failure(ALTServerError.unknownRequest), .failure(OperationError.appNotFound): + // Fall back to installation if AltServer doesn't support newer provisioning profile requests, + // OR if the cached app could not be found and we may need to redownload it. app.managedObjectContext?.performAndWait { // Must performAndWait to ensure we add operations before we return. let installProgress = self._install(app, operation: operation, group: group) { (result) in completionHandler(result) diff --git a/AltStore/Operations/FetchProvisioningProfilesOperation.swift b/AltStore/Operations/FetchProvisioningProfilesOperation.swift index 416b1edf..dfffd645 100644 --- a/AltStore/Operations/FetchProvisioningProfilesOperation.swift +++ b/AltStore/Operations/FetchProvisioningProfilesOperation.swift @@ -38,11 +38,12 @@ class FetchProvisioningProfilesOperation: ResultOperation<[String: ALTProvisioni } guard - let app = self.context.app, let team = self.context.team, let session = self.context.session else { return self.finish(.failure(OperationError.invalidParameters)) } + guard let app = self.context.app else { return self.finish(.failure(OperationError.appNotFound)) } + self.progress.totalUnitCount = Int64(1 + app.appExtensions.count) self.prepareProvisioningProfile(for: app, parentApp: nil, team: team, session: session) { (result) in diff --git a/AltStore/Operations/RefreshAppOperation.swift b/AltStore/Operations/RefreshAppOperation.swift index 8461fba7..9391df54 100644 --- a/AltStore/Operations/RefreshAppOperation.swift +++ b/AltStore/Operations/RefreshAppOperation.swift @@ -40,12 +40,9 @@ class RefreshAppOperation: ResultOperation throw error } - guard - let server = self.context.server, - let app = self.context.app, - let profiles = self.context.provisioningProfiles - else { throw OperationError.invalidParameters } + guard let server = self.context.server, let profiles = self.context.provisioningProfiles else { throw OperationError.invalidParameters } + guard let app = self.context.app else { throw OperationError.appNotFound } guard let udid = Bundle.main.object(forInfoDictionaryKey: Bundle.Info.deviceID) as? String else { throw OperationError.unknownUDID } ServerManager.shared.connect(to: server) { (result) in