Commit 2f67a6d3 by Sarkizz

调整热更新逻辑

parent 3a10ef30
......@@ -56,10 +56,10 @@ open class MRUpdateManager: NSObject {
public class HotUpdateFileInfo {
public var fileName: String
public var downloadURL: String
public var fileHash: String?
public var downloadURL: URL
public var fileHash: String
init(fileName: String, downloadURL: String, fileHash: String? = nil) {
public init(fileName: String, downloadURL: URL, fileHash: String) {
self.fileName = fileName
self.downloadURL = downloadURL
self.fileHash = fileHash
......@@ -105,13 +105,13 @@ open class MRUpdateManager: NSObject {
open func resetFilesFolder() {
asyncIfNeeded {
if let bundleWWW = self.bundleWWWURL {
if self.settings.sandboxSrc.isURLExists(true) {
MRFiles.syncDelete(self.settings.sandboxSrc)
} else {
if !self.settings.sandboxSrc.isURLExists(true) {
self.settings.sandboxSrc.syncCreate()
}
bundleWWW.syncCopy(to: self.settings.sandboxSrc)
self.sandboxUpdateURL.syncDelete()
self.sandboxBackupURL.syncDelete()
}
}
}
......@@ -129,7 +129,7 @@ extension MRUpdateManager {
}
}
public func checkUpdate(update: @escaping (_ data: Any, _ complate: (_ isUpdated: Bool, _ error: Error?) -> Void) -> Void) {
public func checkUpdate(update: @escaping (_ data: Any, _ complate: @escaping (_ isUpdated: Bool, _ error: Error?) -> Void) -> Void) {
if _status == .updating {
return
}
......@@ -167,7 +167,7 @@ extension MRUpdateManager {
break
case .silence:
let operations = {
self.silenceUpdate { (error) in
self.updateFromLocal { (error) in
self.asyncInMain {
complate?(self, error)
}
......@@ -181,6 +181,27 @@ extension MRUpdateManager {
break
}
}
public func updateFromLocal(_ complate:((_ error: Error?) -> Void)?) {
sandboxBackupURL.syncDelete()
if let error = sandboxWWWURL.syncMoveItems(to: sandboxBackupURL) {
complate?(error)
} else {
if let error = sandboxUpdateURL.syncMoveItems(to: sandboxWWWURL) {
//rollback
sandboxBackupURL.syncMoveItems(to: sandboxWWWURL)
complate?(error)
} else {
complate?(nil)
}
}
}
public func recoverFromBundleIfFileIncomplete(_ file: URL) {
if !checkFilesIntegrity(file) {
resetFilesFolder()
}
}
}
extension MRUpdateManager {
......@@ -251,29 +272,29 @@ extension MRUpdateManager {
let p = Float(i)/Float(infos.count)
let fileURL = updateURL.appendingPathComponent(info.fileName)
if let hash = info.fileHash {
//Step 2
if fileURL.isURLExists(), self.isSameHash(with: fileURL, hash: hash) {
if fileURL.isURLExists(), self.isSameHash(with: fileURL, hash: info.fileHash) {
progressBlock(p)
continue
}
//Step 3
let fileURLInWWW = wwwURL.appendingPathComponent(info.fileName)
if fileURLInWWW.isURLExists(), self.isSameHash(with: fileURLInWWW, hash: hash) {
if fileURLInWWW.isURLExists(),
self.isSameHash(with: fileURLInWWW, hash: info.fileHash) {
self.createFolderIfNeeded(with: info.fileName, in: updateURL)
let distURL = fileURL.deletingLastPathComponent()
if fileURLInWWW.syncCopy(to: distURL) == nil, self.isSameHash(with: fileURL, hash: hash) {
if fileURLInWWW.syncCopy(to: distURL) == nil,
self.isSameHash(with: fileURL, hash: info.fileHash) {
progressBlock(p)
continue
}
}
}
fileURL.syncDelete()
if let url = URL(string: info.downloadURL),
let data = try? Data(contentsOf: url), data.syncWrite(to: fileURL) == nil {
if let data = try? Data(contentsOf: info.downloadURL),
data.syncWrite(to: fileURL) == nil {
progressBlock(p)
} else {
isUpdateError = true
......@@ -286,27 +307,6 @@ extension MRUpdateManager {
}
extension MRUpdateManager {
private func silenceUpdate(_ complate: ((_ error: Error?) -> Void)?) {
updateFromLocal(complate)
}
private func updateFromLocal(_ complate:((_ error: Error?) -> Void)?) {
sandboxBackupURL.syncDelete()
if let error = sandboxWWWURL.syncMoveItems(to: sandboxBackupURL) {
complate?(error)
} else {
if let error = sandboxUpdateURL.syncMoveItems(to: sandboxWWWURL) {
//rollback
sandboxBackupURL.syncMoveItems(to: sandboxWWWURL)
complate?(error)
} else {
complate?(nil)
}
}
}
}
extension MRUpdateManager {
private func asyncIfNeeded(_ operation: @escaping () -> Void) {
if let queue = fileQueue {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment