mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-03 11:39:29 +08:00
52 lines
1.7 KiB
Swift
52 lines
1.7 KiB
Swift
import Foundation
|
|
import UniformTypeIdentifiers
|
|
|
|
public extension UTType {
|
|
static let virtualBuddySavedState = UTType(
|
|
exportedAs: "codes.rambo.VirtualBuddy.SavedState",
|
|
conformingTo: .bundle
|
|
)
|
|
}
|
|
|
|
public struct VBSavedStateMetadata: Identifiable, Hashable, Codable {
|
|
public var id: String
|
|
public var date: Date
|
|
}
|
|
|
|
public extension VBVirtualMachine {
|
|
|
|
#error("TODO: Saved state should be saved to library in _SavedState directory, VMs will need a uuid so that saved states can be associated with a VM without breaking when VMs are renamed")
|
|
func savedStateDirectoryCreatingIfNeeded() throws -> URL {
|
|
try savedStateDirectoryURL.creatingDirectoryIfNeeded()
|
|
}
|
|
|
|
func createSavedStatePackageURL() throws -> URL {
|
|
let baseURL = try savedStateDirectoryCreatingIfNeeded()
|
|
let suffix = DateFormatter.savedStateFileName.string(from: .now)
|
|
let url = baseURL.appendingPathComponent("Save-\(suffix)", conformingTo: .virtualBuddySavedState)
|
|
return try url.creatingDirectoryIfNeeded()
|
|
}
|
|
|
|
func savedStateDataFileURL(in packageURL: URL) -> URL {
|
|
packageURL.appendingPathComponent("State")
|
|
}
|
|
|
|
func savedStateMetadataFileURL(in packageURL: URL) -> URL {
|
|
packageURL.appendingPathComponent("Info", conformingTo: .propertyList)
|
|
}
|
|
|
|
static func virtualMachineURL(forSavedStatePackageURL url: URL) -> URL {
|
|
url.deletingLastPathComponent().deletingLastPathComponent()
|
|
}
|
|
|
|
}
|
|
|
|
private extension DateFormatter {
|
|
static let savedStateFileName: DateFormatter = {
|
|
let f = DateFormatter()
|
|
f.calendar = .init(identifier: .gregorian)
|
|
f.dateFormat = "yyyy-MM-dd_HH;mm;ss"
|
|
return f
|
|
}()
|
|
}
|