2
0

VMWizardOSMacView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. import Virtualization
  18. @available(macOS 12, *)
  19. struct VMWizardOSMacView: View {
  20. @ObservedObject var wizardState: VMWizardState
  21. @State private var isFileImporterPresented = false
  22. var body: some View {
  23. VMWizardContent("macOS") {
  24. Section {
  25. Text("To install macOS, you need to download a recovery IPSW. If you do not select an existing IPSW, the latest macOS IPSW will be downloaded from Apple.")
  26. Spacer()
  27. Text("Drag and drop IPSW file here").foregroundColor(.secondary)
  28. Spacer()
  29. #if arch(arm64)
  30. if let selected = wizardState.macRecoveryIpswURL {
  31. Text(selected.lastPathComponent)
  32. .font(.caption)
  33. }
  34. FileBrowseField(url: $wizardState.macRecoveryIpswURL, isFileImporterPresented: $isFileImporterPresented)
  35. #endif
  36. if wizardState.isBusy {
  37. Spinner(size: .large)
  38. }
  39. Spacer()
  40. } header: {
  41. Text("Import IPSW")
  42. }
  43. }
  44. .fileImporter(isPresented: $isFileImporterPresented, allowedContentTypes: [.ipsw], onCompletion: processIpsw)
  45. .onDrop(of: [.fileURL], delegate: self)
  46. .onAppear {
  47. wizardState.bootDevice = .none
  48. }
  49. }
  50. private func processIpsw(_ result: Result<URL, Error>) {
  51. wizardState.busyWorkAsync {
  52. #if arch(arm64)
  53. let url = try result.get()
  54. let scopedAccess = url.startAccessingSecurityScopedResource()
  55. defer {
  56. if scopedAccess {
  57. url.stopAccessingSecurityScopedResource()
  58. }
  59. }
  60. let image = try await VZMacOSRestoreImage.image(from: url)
  61. guard let model = image.mostFeaturefulSupportedConfiguration?.hardwareModel else {
  62. throw NSLocalizedString("Your machine does not support running this IPSW.", comment: "VMWizardOSMacView")
  63. }
  64. await MainActor.run {
  65. wizardState.macPlatform = UTMAppleConfigurationMacPlatform(newHardware: model)
  66. wizardState.macRecoveryIpswURL = url
  67. wizardState.macPlatformVersion = image.buildVersion.integerPrefix()
  68. wizardState.bootImageURL = nil
  69. wizardState.next()
  70. }
  71. #else
  72. throw NSLocalizedString("macOS guests are only supported on ARM64 devices.", comment: "VMWizardOSMacView")
  73. #endif
  74. }
  75. }
  76. }
  77. @available(macOS 12, *)
  78. extension VMWizardOSMacView: DropDelegate {
  79. func validateDrop(info: DropInfo) -> Bool {
  80. urlFrom(info: info) != nil
  81. }
  82. func performDrop(info: DropInfo) -> Bool {
  83. guard let url = urlFrom(info: info) else { return false }
  84. processIpsw(.success(url))
  85. return true
  86. }
  87. private func urlFrom(info: DropInfo) -> URL? {
  88. let providers = info.itemProviders(for: [.fileURL])
  89. guard providers.count == 1,
  90. let first = providers.first
  91. else { return nil }
  92. var validURL: URL?
  93. let group = DispatchGroup()
  94. group.enter()
  95. _ = first.loadObject(ofClass: URL.self) { url, _ in
  96. if url?.pathExtension == "ipsw" {
  97. validURL = url
  98. }
  99. group.leave()
  100. }
  101. group.wait()
  102. return validURL
  103. }
  104. }
  105. @available(macOS 12, *)
  106. struct VMWizardOSMacView_Previews: PreviewProvider {
  107. @StateObject static var wizardState = VMWizardState()
  108. static var previews: some View {
  109. VMWizardOSMacView(wizardState: wizardState)
  110. }
  111. }