1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-29 12:50:28 +00:00

Corrects the macOS Swift side of things.

This commit is contained in:
Thomas Harte 2021-06-06 14:56:43 -04:00
parent 5acd97c860
commit 4494320238
4 changed files with 35 additions and 63 deletions

View File

@ -144,11 +144,15 @@ class MachineDocument:
actionLock.lock() actionLock.lock()
drawLock.lock() drawLock.lock()
let missingROMs = NSMutableString()
if let machine = CSMachine(analyser: analysis, missingROMs: missingROMs) { if let machine = CSMachine(analyser: analysis, missingROMs: missingROMs) {
setRomRequesterIsVisible(false)
self.machine = machine self.machine = machine
machine.setVolume(userDefaultsVolume()) machine.setVolume(userDefaultsVolume())
setupMachineOutput() setupMachineOutput()
} else { } else {
self.missingROMs = missingROMs as String
requestRoms() requestRoms()
} }
@ -409,23 +413,38 @@ class MachineDocument:
@IBOutlet var romReceiverErrorField: NSTextField? @IBOutlet var romReceiverErrorField: NSTextField?
@IBOutlet var romReceiverView: CSROMReceiverView? @IBOutlet var romReceiverView: CSROMReceiverView?
private var romRequestBaseText = "" 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() { func requestRoms() {
// Don't act yet if there's no window controller yet. // Don't act yet if there's no window controller yet.
if self.windowControllers.count == 0 { if self.windowControllers.count == 0 {
return return
} }
// Load the ROM requester dialogue. // Load the ROM requester dialogue if it's not already loaded.
Bundle.main.loadNibNamed("ROMRequester", owner: self, topLevelObjects: nil) if self.romRequesterPanel == nil {
self.romReceiverView!.delegate = self Bundle.main.loadNibNamed("ROMRequester", owner: self, topLevelObjects: nil)
self.romRequestBaseText = romRequesterText!.stringValue self.romReceiverView!.delegate = self
romReceiverErrorField?.alphaValue = 0.0 self.romRequestBaseText = romRequesterText!.stringValue
romReceiverErrorField?.alphaValue = 0.0
}
// Populate the current absentee list. // Populate the current absentee list.
populateMissingRomList() populateMissingRomList()
// Show the thing. // Show the thing.
self.windowControllers[0].window?.beginSheet(self.romRequesterPanel!, completionHandler: nil) setRomRequesterIsVisible(true)
} }
@IBAction func cancelRequestROMs(_ sender: NSButton?) { @IBAction func cancelRequestROMs(_ sender: NSButton?) {
@ -440,59 +459,10 @@ class MachineDocument:
// Test whether the file identified matches any of the currently missing ROMs. // 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 so then remove that ROM from the missing list and update the request screen.
// If no ROMs are still missing, start the machine. // If no ROMs are still missing, start the machine.
do { if CSMachine.attemptInstallROM(URL) {
let fileData = try Data(contentsOf: URL) configureAs(self.machineDescription!)
var didInstallRom = false } else {
showRomReceiverError(error: "Didn't recognise contents of \(URL.lastPathComponent)")
// 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)")
} }
} }

View File

@ -39,7 +39,7 @@ typedef NS_ENUM(NSInteger, CSMachineKeyboardInputMode) {
@interface CSMachine : NSObject @interface CSMachine : NSObject
+ (BOOL)attemptInstallROM:(NSURL *)url; + (BOOL)attemptInstallROM:(nonnull NSURL *)url;
- (nonnull instancetype)init NS_UNAVAILABLE; - (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 @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. 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; - (float)idealSamplingRateFromRange:(NSRange)range;
@property (readonly, getter=isStereo) BOOL stereo; @property (readonly, getter=isStereo) BOOL stereo;

View File

@ -104,7 +104,7 @@ struct ActivityObserver: public Activity::Observer {
NSMutableArray<dispatch_block_t> *_inputEvents; NSMutableArray<dispatch_block_t> *_inputEvents;
} }
- (instancetype)initWithAnalyser:(CSStaticAnalyser *)result missingROMs:(inout NSString *)missingROMs { - (instancetype)initWithAnalyser:(CSStaticAnalyser *)result missingROMs:(inout NSMutableString *)missingROMs {
self = [super init]; self = [super init];
if(self) { if(self) {
_analyser = result; _analyser = result;
@ -113,6 +113,7 @@ struct ActivityObserver: public Activity::Observer {
ROM::Request missing_roms; ROM::Request missing_roms;
_machine.reset(Machine::MachineForTargets(_analyser.targets, CSROMFetcher(&missing_roms), error)); _machine.reset(Machine::MachineForTargets(_analyser.targets, CSROMFetcher(&missing_roms), error));
if(!_machine) { if(!_machine) {
[missingROMs appendFormat:@"Who told you?"];
/* for(const auto &missing_rom : missing_roms) { /* for(const auto &missing_rom : missing_roms) {
CSMissingROM *rom = [[CSMissingROM alloc] init]; CSMissingROM *rom = [[CSMissingROM alloc] init];

View File

@ -65,7 +65,8 @@ BOOL CSInstallROM(NSURL *url) {
// Copy the data to its destination and report success. // Copy the data to its destination and report success.
NSURL *const targetURL = [urlsFor(*target_description, target_description->file_names[0]) firstObject]; 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; return YES;
} }