From 4494320238804bde0b4483b8284be1c313ec945f Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 6 Jun 2021 14:56:43 -0400 Subject: [PATCH] Corrects the macOS Swift side of things. --- .../Documents/MachineDocument.swift | 88 ++++++------------- .../Mac/Clock Signal/Machine/CSMachine.h | 4 +- .../Mac/Clock Signal/Machine/CSMachine.mm | 3 +- .../Mac/Clock Signal/Machine/CSROMFetcher.mm | 3 +- 4 files changed, 35 insertions(+), 63 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index 71a02ced9..983988958 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -144,11 +144,15 @@ class MachineDocument: actionLock.lock() drawLock.lock() + let missingROMs = NSMutableString() if let machine = CSMachine(analyser: analysis, missingROMs: missingROMs) { + setRomRequesterIsVisible(false) + self.machine = machine machine.setVolume(userDefaultsVolume()) setupMachineOutput() } else { + self.missingROMs = missingROMs as String requestRoms() } @@ -409,23 +413,38 @@ class MachineDocument: @IBOutlet var romReceiverErrorField: NSTextField? @IBOutlet var romReceiverView: CSROMReceiverView? private var romRequestBaseText = "" + + private func setRomRequesterIsVisible(_ visible : Bool) { + if self.romRequesterPanel!.isVisible == visible { + return + } + + if visible { + self.windowControllers[0].window?.beginSheet(self.romRequesterPanel!, completionHandler: nil) + } else { + self.windowControllers[0].window?.endSheet(self.romRequesterPanel!) + } + } + func requestRoms() { // Don't act yet if there's no window controller yet. if self.windowControllers.count == 0 { return } - // Load the ROM requester dialogue. - Bundle.main.loadNibNamed("ROMRequester", owner: self, topLevelObjects: nil) - self.romReceiverView!.delegate = self - self.romRequestBaseText = romRequesterText!.stringValue - romReceiverErrorField?.alphaValue = 0.0 + // Load the ROM requester dialogue if it's not already loaded. + if self.romRequesterPanel == nil { + Bundle.main.loadNibNamed("ROMRequester", owner: self, topLevelObjects: nil) + self.romReceiverView!.delegate = self + self.romRequestBaseText = romRequesterText!.stringValue + romReceiverErrorField?.alphaValue = 0.0 + } // Populate the current absentee list. populateMissingRomList() // Show the thing. - self.windowControllers[0].window?.beginSheet(self.romRequesterPanel!, completionHandler: nil) + setRomRequesterIsVisible(true) } @IBAction func cancelRequestROMs(_ sender: NSButton?) { @@ -440,59 +459,10 @@ class MachineDocument: // Test whether the file identified matches any of the currently missing ROMs. // If so then remove that ROM from the missing list and update the request screen. // If no ROMs are still missing, start the machine. - do { - let fileData = try Data(contentsOf: URL) - var didInstallRom = false - - // Try to match by size first, CRC second. Accept that some ROMs may have - // some additional appended data. Arbitrarily allow them to be up to 10kb - // too large. -/* var index = 0 - for missingROM in self.missingROMs { - if fileData.count >= missingROM.size && fileData.count < missingROM.size + 10*1024 { - // Trim to size. - let trimmedData = fileData[0 ..< missingROM.size] - - // Get CRC. - if missingROM.crc32s.contains( (trimmedData as NSData).crc32 ) { - // This ROM matches; copy it into the application library, - // strike it from the missing ROM list and decide how to - // proceed. - let fileManager = FileManager.default - let targetPath = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] - .appendingPathComponent("ROMImages") - .appendingPathComponent(missingROM.machineName) - let targetFile = targetPath - .appendingPathComponent(missingROM.fileName) - - do { - try fileManager.createDirectory(atPath: targetPath.path, withIntermediateDirectories: true, attributes: nil) - try trimmedData.write(to: targetFile) - } catch let error { - showRomReceiverError(error: "Couldn't write to application support directory: \(error)") - } - - self.missingROMs.remove(at: index) - didInstallRom = true - break - } - } - - index = index + 1 - }*/ - - if didInstallRom { - if self.missingROMs.count == 0 { - self.windowControllers[0].window?.endSheet(self.romRequesterPanel!) - configureAs(self.machineDescription!) - } else { - populateMissingRomList() - } - } else { - showRomReceiverError(error: "Didn't recognise contents of \(URL.lastPathComponent)") - } - } catch let error { - showRomReceiverError(error: "Couldn't read file at \(URL.absoluteString): \(error)") + if CSMachine.attemptInstallROM(URL) { + configureAs(self.machineDescription!) + } else { + showRomReceiverError(error: "Didn't recognise contents of \(URL.lastPathComponent)") } } diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine.h b/OSBindings/Mac/Clock Signal/Machine/CSMachine.h index 9bc37dfad..7654899ed 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSMachine.h +++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine.h @@ -39,7 +39,7 @@ typedef NS_ENUM(NSInteger, CSMachineKeyboardInputMode) { @interface CSMachine : NSObject -+ (BOOL)attemptInstallROM:(NSURL *)url; ++ (BOOL)attemptInstallROM:(nonnull NSURL *)url; - (nonnull instancetype)init NS_UNAVAILABLE; @@ -50,7 +50,7 @@ typedef NS_ENUM(NSInteger, CSMachineKeyboardInputMode) { @param missingROMs An array that is filled with a list of ROMs that the machine requested but which were not found; populated only if this `init` has failed. */ -- (nullable instancetype)initWithAnalyser:(nonnull CSStaticAnalyser *)result missingROMs:(nullable inout NSString *)missingROMs NS_DESIGNATED_INITIALIZER; +- (nullable instancetype)initWithAnalyser:(nonnull CSStaticAnalyser *)result missingROMs:(nullable inout NSMutableString *)missingROMs NS_DESIGNATED_INITIALIZER; - (float)idealSamplingRateFromRange:(NSRange)range; @property (readonly, getter=isStereo) BOOL stereo; diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm index e81d63451..81b8f18ee 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm +++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm @@ -104,7 +104,7 @@ struct ActivityObserver: public Activity::Observer { NSMutableArray *_inputEvents; } -- (instancetype)initWithAnalyser:(CSStaticAnalyser *)result missingROMs:(inout NSString *)missingROMs { +- (instancetype)initWithAnalyser:(CSStaticAnalyser *)result missingROMs:(inout NSMutableString *)missingROMs { self = [super init]; if(self) { _analyser = result; @@ -113,6 +113,7 @@ struct ActivityObserver: public Activity::Observer { ROM::Request missing_roms; _machine.reset(Machine::MachineForTargets(_analyser.targets, CSROMFetcher(&missing_roms), error)); if(!_machine) { + [missingROMs appendFormat:@"Who told you?"]; /* for(const auto &missing_rom : missing_roms) { CSMissingROM *rom = [[CSMissingROM alloc] init]; diff --git a/OSBindings/Mac/Clock Signal/Machine/CSROMFetcher.mm b/OSBindings/Mac/Clock Signal/Machine/CSROMFetcher.mm index a48561a6b..556596015 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSROMFetcher.mm +++ b/OSBindings/Mac/Clock Signal/Machine/CSROMFetcher.mm @@ -65,7 +65,8 @@ BOOL CSInstallROM(NSURL *url) { // Copy the data to its destination and report success. NSURL *const targetURL = [urlsFor(*target_description, target_description->file_names[0]) firstObject]; - [data writeToURL:targetURL atomically:YES]; + [[NSFileManager defaultManager] createDirectoryAtPath:targetURL.URLByDeletingLastPathComponent.path withIntermediateDirectories:YES attributes:nil error:nil]; + [data writeToURL:targetURL atomically:NO]; return YES; }