2019-06-10 15:03:47 -07:00
|
|
|
//
|
|
|
|
|
// InstallAppOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
2019-06-21 11:20:03 -07:00
|
|
|
// Created by Riley Testut on 6/19/19.
|
2019-06-10 15:03:47 -07:00
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import Network
|
|
|
|
|
|
|
|
|
|
import AltKit
|
2019-06-21 11:20:03 -07:00
|
|
|
import Roxas
|
2019-06-10 15:03:47 -07:00
|
|
|
|
|
|
|
|
@objc(InstallAppOperation)
|
|
|
|
|
class InstallAppOperation: ResultOperation<Void>
|
|
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
let context: AppOperationContext
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
init(context: AppOperationContext)
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
self.context = context
|
|
|
|
|
|
2019-06-10 15:03:47 -07:00
|
|
|
super.init()
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
self.progress.totalUnitCount = 100
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
if let error = self.context.error
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
guard
|
|
|
|
|
let installedApp = self.context.installedApp,
|
|
|
|
|
let connection = self.context.connection,
|
|
|
|
|
let server = self.context.group.server
|
|
|
|
|
else { return self.finish(.failure(OperationError.invalidParameters)) }
|
|
|
|
|
|
|
|
|
|
installedApp.managedObjectContext?.perform {
|
|
|
|
|
print("Installing app:", installedApp.app.identifier)
|
|
|
|
|
self.context.group.beginInstallationHandler?(installedApp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let request = BeginInstallationRequest()
|
|
|
|
|
server.send(request, via: connection) { (result) in
|
2019-06-10 15:03:47 -07:00
|
|
|
switch result
|
|
|
|
|
{
|
|
|
|
|
case .failure(let error): self.finish(.failure(error))
|
2019-06-21 11:20:03 -07:00
|
|
|
case .success:
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
self.receive(from: connection, server: server) { (result) in
|
2019-07-19 16:42:40 -07:00
|
|
|
switch result
|
|
|
|
|
{
|
|
|
|
|
case .success:
|
|
|
|
|
installedApp.managedObjectContext?.performAndWait {
|
|
|
|
|
installedApp.refreshedDate = Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure: break
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
self.finish(result)
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
func receive(from connection: NWConnection, server: Server, completionHandler: @escaping (Result<Void, Error>) -> Void)
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
server.receive(ServerResponse.self, from: connection) { (result) in
|
|
|
|
|
do
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
let response = try result.get()
|
|
|
|
|
print(response)
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
if let error = response.error
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
self.finish(.failure(error))
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
2019-06-21 11:20:03 -07:00
|
|
|
else if response.progress == 1.0
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-07-19 16:42:40 -07:00
|
|
|
self.progress.completedUnitCount = self.progress.totalUnitCount
|
2019-06-21 11:20:03 -07:00
|
|
|
self.finish(.success(()))
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
2019-06-21 11:20:03 -07:00
|
|
|
else
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
self.progress.completedUnitCount = Int64(response.progress * 100)
|
|
|
|
|
self.receive(from: connection, server: server, completionHandler: completionHandler)
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
completionHandler(.failure(ALTServerError(error)))
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|