Skip to content

Commit

Permalink
Full rewrite using appkit instead of swift ui
Browse files Browse the repository at this point in the history
now supports installing multiple vms
  • Loading branch information
* committed Oct 12, 2024
1 parent 080c419 commit 19e12c1
Show file tree
Hide file tree
Showing 54 changed files with 3,123 additions and 2,234 deletions.
501 changes: 78 additions & 423 deletions virtualOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

This file was deleted.

40 changes: 10 additions & 30 deletions virtualOS.xcodeproj/xcshareddata/xcschemes/virtualOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1400"
version = "1.3">
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand All @@ -14,7 +15,7 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00989C5F27E2340C0048776B"
BlueprintIdentifier = "015228542CB27BC100209934"
BuildableName = "virtualOS.app"
BlueprintName = "virtualOS"
ReferencedContainer = "container:virtualOS.xcodeproj">
Expand All @@ -26,32 +27,11 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00989C7027E2340D0048776B"
BuildableName = "virtualOSTests.xctest"
BlueprintName = "virtualOSTests"
ReferencedContainer = "container:virtualOS.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00989C7A27E2340D0048776B"
BuildableName = "virtualOSUITests.xctest"
BlueprintName = "virtualOSUITests"
ReferencedContainer = "container:virtualOS.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Release"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -64,7 +44,7 @@
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00989C5F27E2340C0048776B"
BlueprintIdentifier = "015228542CB27BC100209934"
BuildableName = "virtualOS.app"
BlueprintName = "virtualOS"
ReferencedContainer = "container:virtualOS.xcodeproj">
Expand All @@ -81,7 +61,7 @@
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00989C5F27E2340C0048776B"
BlueprintIdentifier = "015228542CB27BC100209934"
BuildableName = "virtualOS.app"
BlueprintName = "virtualOS"
ReferencedContainer = "container:virtualOS.xcodeproj">
Expand Down
1 change: 1 addition & 0 deletions virtualOS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xcuserstate
26 changes: 26 additions & 0 deletions virtualOS/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// AppDelegate.swift
// virtualOS
//
// Created by Jahn Bertsch.
//

import Cocoa

@main
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}


}

18 changes: 18 additions & 0 deletions virtualOS/Extension/OperatingSystemVersion+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// OsVersion.swift
// virtualOS
//
// Created by Jahn Bertsch
//

import Virtualization

#if arch(arm64)

extension VZMacOSRestoreImage {
var operatingSystemVersionString: String {
return "macOS \(operatingSystemVersion.majorVersion).\(operatingSystemVersion.minorVersion).\(operatingSystemVersion.patchVersion) (Build \(buildVersion))"
}
}

#endif
36 changes: 14 additions & 22 deletions virtualOS/Extension/URL+Paths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,26 @@ extension URL {
static let bundleName = "virtualOS.bundle/"
static let defaultVmBundlePath = basePath + "/\(bundleName)"

static var vmBundleURL: URL {
return URL(fileURLWithPath: vmBundlePath)
static var baseURL: URL {
return URL(fileURLWithPath: basePath)
}
static var diskImageURL: URL {
return URL(fileURLWithPath: vmBundlePath + "/Disk.img")
static var vmBundleURL: URL {
return URL(fileURLWithPath: defaultVmBundlePath)
}
static var auxiliaryStorageURL: URL {
return URL(fileURLWithPath: vmBundlePath + "/AuxiliaryStorage")
var auxiliaryStorageURL: URL {
return self.appending(path: "AuxiliaryStorage")
}
static var machineIdentifierURL: URL {
return URL(fileURLWithPath: vmBundlePath + "/MachineIdentifier")
var hardwareModelURL: URL {
return self.appending(path: "HardwareModel")
}
static var hardwareModelURL: URL {
return URL(fileURLWithPath: vmBundlePath + "/HardwareModel")
var diskImageURL: URL {
return self.appending(path: "Disk.img")
}
static var parametersURL: URL {
return URL(fileURLWithPath: vmBundlePath + "/Parameters.txt")
var machineIdentifierURL: URL {
return self.appending(path: "MachineIdentifier")
}

static var vmBundlePath: String {
if let hardDiskDirectoryBookmarkData = UserDefaults.standard.hardDiskDirectoryBookmarkData,
let hardDiskDirectoryURL = Bookmark.startAccess(data: hardDiskDirectoryBookmarkData, forType: .hardDisk)
{
let vmBundlePath = hardDiskDirectoryURL.appendingPathComponent(bundleName).path
return vmBundlePath
} else {
return URL.defaultVmBundlePath
}
var parametersURL: URL {
return self.appending(path: "Parameters.txt")
}
}

4 changes: 2 additions & 2 deletions virtualOS/Extension/UserDefaults+Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// UserDefaults+Settings.swift
// virtualOS
//
// Created by Jahn Bertsch on 03.07.22.
// Created by Jahn Bertsch.
//

import Foundation
Expand All @@ -16,7 +16,7 @@ extension UserDefaults {
if object(forKey: Self.diskSizeKey) != nil {
return integer(forKey: Self.diskSizeKey)
}
return 60 // default value
return 30 // default value
}
set {
set(newValue, forKey: Self.diskSizeKey)
Expand Down
8 changes: 0 additions & 8 deletions virtualOS/Info.plist

This file was deleted.

18 changes: 0 additions & 18 deletions virtualOS/Model/ApplicationDelegate.swift

This file was deleted.

49 changes: 16 additions & 33 deletions virtualOS/Model/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,38 @@
//

import Foundation
import OSLog

struct Bookmark {
enum BookmarkType {
case hardDisk
case sharedFolder
}

fileprivate static var accessedURLs: [BookmarkType: URL] = [:]
fileprivate static let logger = Logger.init(subsystem: "com.github.virtualOS", category: "log")
fileprivate static var currentlyAccessedUrl: URL?

static func createBookmarkData(fromUrl url: URL) -> Data? {
if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, relativeTo: nil) {
do {
let bookmarkData = try url.bookmarkData(options: .withSecurityScope, relativeTo: nil)
return bookmarkData
} catch let error {
Self.logger.log(level: .default, "error creating bookmark: \(error.localizedDescription)")
}
return nil
}

static func startAccess(data: Data?, forType key: BookmarkType) -> URL? {
static func startAccess(bookmarkData: Data?) -> URL? {
if let currentlyAccessedUrl = Self.currentlyAccessedUrl {
// stop previous access
currentlyAccessedUrl.stopAccessingSecurityScopedResource()
}

var bookmarkDataIsStale = false
if let bookmarkData = data,
if let bookmarkData,
let bookmarkURL = try? URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &bookmarkDataIsStale),
!bookmarkDataIsStale
{
// stop accessing previous resource
if let previousURL = accessedURLs[key],
previousURL != bookmarkURL
{
previousURL.stopAccessingSecurityScopedResource()
}

if accessedURLs[key] != bookmarkURL {
// resource not already accessed, start access
_ = bookmarkURL.startAccessingSecurityScopedResource()
accessedURLs[key] = bookmarkURL
}
_ = bookmarkURL.startAccessingSecurityScopedResource()
Self.currentlyAccessedUrl = bookmarkURL
return bookmarkURL
}

return nil
}

static func stopAccess(url: URL, forKey key: BookmarkType) {
url.stopAccessingSecurityScopedResource()
Self.accessedURLs[key] = nil
}

static func stopAllAccess() {
for (_, accessedURL) in accessedURLs {
accessedURL.stopAccessingSecurityScopedResource()
}
Self.accessedURLs = [:]
}
}
18 changes: 18 additions & 0 deletions virtualOS/Model/Bundle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Bundle.swift
// virtualOS
//
// Created by Jahn Bertsch.
//

import Foundation

struct VMBundle: Identifiable, Hashable {
var id: String {
return url.absoluteString
}
var url: URL
var name: String {
return url.lastPathComponent.replacingOccurrences(of: ".bundle", with: "")
}
}
14 changes: 14 additions & 0 deletions virtualOS/Model/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Constants.swift
// virtualOS
//
// Created by Jahn Bertsch
//

import AppKit

struct Constants {
static let restoreImageNameLatest = "latest"
static let selectedRestoreImage = "selectedRestoreImage"
static let restoreImageNameSelectedNotification = Notification.Name("restoreImageSelected")
}
56 changes: 56 additions & 0 deletions virtualOS/Model/FileModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// FileModel.swift
// virtualOS
//
// Created by Jahn Bertsch.
//

import Foundation

struct FileModel {
var bundleExists: Bool {
return FileManager.default.fileExists(atPath: URL.vmBundleURL.path())
}

var restoreImageExists: Bool {
return FileManager.default.fileExists(atPath: URL.restoreImageURL.path)
}

var sharedFolderExists: Bool {
// if let hardDiskDirectoryBookmarkData = Bookmark.startAccess(data: virtualMac.parameters.sharedFolder, forType: .hardDisk) {
// var isDirectory = ObjCBool(false)
// if FileManager.default.fileExists(atPath: hardDiskDirectoryBookmarkData.path, isDirectory: &isDirectory),
// isDirectory.boolValue == true
// {
// return true
// }
// }
return false
}

func getVMBundles() -> [VMBundle] {
var result: [VMBundle] = []
if let urls = try? FileManager.default.contentsOfDirectory(at: URL.baseURL, includingPropertiesForKeys: nil, options: []) {
for url in urls {
if url.lastPathComponent.hasSuffix("bundle") {
result.append(VMBundle(url: url))
}
}
}
return result
}

func getRestoreImages() -> [String] {
var result: [String] = []

if let urls = try? FileManager.default.contentsOfDirectory(at: URL.baseURL, includingPropertiesForKeys: nil, options: []) {
for url in urls {
if url.lastPathComponent.hasSuffix("ipsw") {
result.append(url.lastPathComponent)
}
}
}
return result
}

}
Loading

0 comments on commit 19e12c1

Please sign in to comment.