2018-01-01 21:04:13 +00:00
|
|
|
//
|
|
|
|
// CSROMFetcher.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 01/01/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-01-01 21:04:13 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#include "CSROMFetcher.hpp"
|
|
|
|
|
|
|
|
#import "NSBundle+DataResource.h"
|
|
|
|
#import "NSData+StdVector.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2021-06-05 02:24:31 +00:00
|
|
|
ROMMachine::ROMFetcher CSROMFetcher(ROM::Request *missing) {
|
|
|
|
return [missing] (const ROM::Request &roms) -> ROM::Map {
|
2019-07-21 03:04:46 +00:00
|
|
|
NSArray<NSURL *> *const supportURLs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
|
|
|
|
|
2021-06-05 02:24:31 +00:00
|
|
|
ROM::Map results;
|
|
|
|
for(const auto &description: roms.all_descriptions()) {
|
|
|
|
for(const auto &file_name: description.file_names) {
|
|
|
|
NSData *fileData;
|
|
|
|
NSString *const subdirectory = [@"ROMImages/" stringByAppendingString:[NSString stringWithUTF8String:description.machine_name.c_str()]];
|
|
|
|
|
|
|
|
// Check for this file first within the application support directories.
|
|
|
|
for(NSURL *supportURL in supportURLs) {
|
|
|
|
NSURL *const fullURL = [[supportURL URLByAppendingPathComponent:subdirectory]
|
|
|
|
URLByAppendingPathComponent:[NSString stringWithUTF8String:file_name.c_str()]];
|
|
|
|
fileData = [NSData dataWithContentsOfURL:fullURL];
|
|
|
|
if(fileData) break;
|
|
|
|
}
|
2018-01-01 21:04:13 +00:00
|
|
|
|
2021-06-05 02:24:31 +00:00
|
|
|
// Failing that, check inside the application bundle.
|
|
|
|
if(!fileData) {
|
|
|
|
fileData = [[NSBundle mainBundle]
|
|
|
|
dataForResource:[NSString stringWithUTF8String:file_name.c_str()]
|
|
|
|
withExtension:nil
|
|
|
|
subdirectory:subdirectory];
|
|
|
|
}
|
2019-07-21 22:41:00 +00:00
|
|
|
|
2021-06-05 02:24:31 +00:00
|
|
|
// Store an appropriate result.
|
|
|
|
if(fileData) {
|
|
|
|
results[description.name] = fileData.stdVector8;
|
2019-07-21 22:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-01 21:04:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 02:24:31 +00:00
|
|
|
// TODO: sever all found ROMs from roms and store to missing, if provided.
|
|
|
|
|
2018-01-01 21:04:13 +00:00
|
|
|
return results;
|
2018-01-25 23:28:19 +00:00
|
|
|
};
|
2018-01-01 21:04:13 +00:00
|
|
|
}
|