132 lines
5.9 KiB
Swift
132 lines
5.9 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Foundation
|
|
|
|
// File references only. Guest writes and ROM contents never enter preferences.
|
|
struct DiskAssignments: Codable, Equatable, Sendable {
|
|
struct Drive: Codable, Equatable, Sendable {
|
|
var url: URL
|
|
var writeProtected: Bool
|
|
var temporary: Bool = false
|
|
init(url: URL, writeProtected: Bool, temporary: Bool = false) {
|
|
self.url = url; self.writeProtected = writeProtected; self.temporary = temporary
|
|
}
|
|
private enum CodingKeys: String, CodingKey { case url, writeProtected, temporary }
|
|
init(from decoder: Decoder) throws {
|
|
let values = try decoder.container(keyedBy: CodingKeys.self)
|
|
url = try values.decode(URL.self, forKey: .url)
|
|
writeProtected = try values.decode(Bool.self, forKey: .writeProtected)
|
|
temporary = try values.decodeIfPresent(Bool.self, forKey: .temporary) ?? false
|
|
}
|
|
}
|
|
var hardDisks: HardDiskAssignments?
|
|
var cartridges: CartridgeAssignments?
|
|
var version = 1
|
|
var rom: URL?
|
|
var drives: [Drive?] = Array(repeating: nil, count: 4)
|
|
|
|
func validate() throws {
|
|
try cartridges?.validate()
|
|
try hardDisks?.validate()
|
|
guard version == 1, drives.count == 4,
|
|
rom != nil || drives.allSatisfy({ $0 == nil }),
|
|
([rom] + drives.map { $0?.url }).compactMap({ $0 }).allSatisfy({
|
|
$0.isFileURL && $0.host.map { $0.isEmpty || $0 == "localhost" } != false && $0.path.hasPrefix("/")
|
|
}) else { throw DiskAssignmentStore.Failure.invalid }
|
|
}
|
|
}
|
|
|
|
struct DiskAssignmentStore: Sendable {
|
|
let url: URL
|
|
static var standard: Self {
|
|
Self(url: AppIdentity.supportDirectory
|
|
.appendingPathComponent("DiskAssignments.json"))
|
|
}
|
|
enum Failure: LocalizedError {
|
|
case invalid
|
|
var errorDescription: String? { "The remembered disk settings are damaged or use an unsupported version. Select the disk files again." }
|
|
}
|
|
func load() throws -> DiskAssignments {
|
|
let file: FileHandle
|
|
do { file = try FileHandle(forReadingFrom: url) }
|
|
catch let error as CocoaError where error.code == .fileReadNoSuchFile || error.code == .fileNoSuchFile {
|
|
return DiskAssignments()
|
|
}
|
|
defer { try? file.close() }
|
|
let bytes = try file.read(upToCount: 65537) ?? Data()
|
|
guard bytes.count <= 65536 else { throw Failure.invalid }
|
|
let value = try JSONDecoder().decode(DiskAssignments.self, from: bytes)
|
|
try value.validate()
|
|
return value
|
|
}
|
|
func save(_ value: DiskAssignments) throws {
|
|
try value.validate()
|
|
let bytes = try JSONEncoder().encode(value)
|
|
guard bytes.count <= 65536 else { throw Failure.invalid }
|
|
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
|
|
try bytes.write(to: url, options: .atomic)
|
|
}
|
|
}
|
|
|
|
// Stored in the same atomic preferences file as the disk setup. Slot zero is
|
|
// the single-cartridge configuration; slots 1...4 survive switching modes.
|
|
struct CartridgeAssignments: Codable, Equatable, Sendable {
|
|
enum Kind: Int, Codable, Sendable { case empty = 0, rom = 1, floppy = 2, hardDisk = 3, ramDisk = 4, orchestra90 = 5, speechSound = 6, sdc = 7 }
|
|
struct Slot: Codable, Equatable, Sendable {
|
|
var kind: Kind = .empty
|
|
var url: URL?
|
|
var speechROMURL: URL? // SSC speech chip; url holds its PIC firmware.
|
|
var sdcFolderURL: URL? // Local folder representing the SD card; never copied into a profile.
|
|
var sdcReadOnly: Bool? // Older profiles default to protected storage.
|
|
}
|
|
var multiPak = false
|
|
var mpiConfigured = false
|
|
var physicalSlot = 4
|
|
var slots = Array(repeating: Slot(), count: 5)
|
|
private static func localReference(_ url: URL?) -> Bool {
|
|
guard let url else { return true }
|
|
guard url.isFileURL, url.path.hasPrefix("/"), url.query == nil, url.fragment == nil else { return false }
|
|
return url.host == nil || url.host == "" || url.host == "localhost"
|
|
}
|
|
private static func validSlot(_ slot: Slot) -> Bool {
|
|
if slot.kind == .sdc {
|
|
guard slot.speechROMURL == nil else { return false }
|
|
return localReference(slot.url) && localReference(slot.sdcFolderURL)
|
|
}
|
|
guard slot.sdcFolderURL == nil, slot.sdcReadOnly == nil else { return false }
|
|
if slot.kind == .speechSound { return localReference(slot.url) && localReference(slot.speechROMURL) }
|
|
guard slot.speechROMURL == nil else { return false }
|
|
if slot.kind == .orchestra90 { return localReference(slot.url) }
|
|
if slot.kind == .rom { return slot.url != nil && localReference(slot.url) }
|
|
return slot.url == nil
|
|
}
|
|
func validate() throws {
|
|
guard slots.count == 5, (1...4).contains(physicalSlot), slots.allSatisfy(Self.validSlot) else {
|
|
throw DiskAssignmentStore.Failure.invalid
|
|
}
|
|
for kind: Kind in [.floppy, .hardDisk, .ramDisk, .orchestra90, .speechSound, .sdc] {
|
|
guard slots.dropFirst().filter({ $0.kind == kind }).count <= 1 else {
|
|
throw DiskAssignmentStore.Failure.invalid
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct HardDiskAssignments: Codable, Equatable, Sendable {
|
|
var drives: [DiskAssignments.Drive?] = Array(repeating: nil, count: 2)
|
|
var clockEnabled = true
|
|
var clockReadOnly = true
|
|
func validate() throws {
|
|
guard drives.count == 2 else { throw DiskAssignmentStore.Failure.invalid }
|
|
for entry in drives {
|
|
guard let drive = entry else { continue }
|
|
let url: URL = drive.url
|
|
guard url.isFileURL, url.path.hasPrefix("/") else {
|
|
throw DiskAssignmentStore.Failure.invalid
|
|
}
|
|
if let host = url.host, !host.isEmpty, host != "localhost" {
|
|
throw DiskAssignmentStore.Failure.invalid
|
|
}
|
|
}
|
|
}
|
|
}
|