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>
|
|
|
|
|
2019-07-21 22:41:00 +00:00
|
|
|
ROMMachine::ROMFetcher CSROMFetcher(std::vector<ROMMachine::ROM> *missing_roms) {
|
2019-07-23 01:14:21 +00:00
|
|
|
return [missing_roms] (const std::vector<ROMMachine::ROM> &roms) -> std::vector<std::unique_ptr<std::vector<std::uint8_t>>> {
|
2019-07-21 03:04:46 +00:00
|
|
|
NSArray<NSURL *> *const supportURLs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
|
|
|
|
|
2018-01-01 21:04:13 +00:00
|
|
|
std::vector<std::unique_ptr<std::vector<std::uint8_t>>> results;
|
2019-07-20 02:35:22 +00:00
|
|
|
for(const auto &rom: roms) {
|
2019-07-21 03:04:46 +00:00
|
|
|
NSData *fileData;
|
2019-07-23 01:14:21 +00:00
|
|
|
NSString *const subdirectory = [@"ROMImages/" stringByAppendingString:[NSString stringWithUTF8String:rom.machine_name.c_str()]];
|
2019-07-21 03:04:46 +00:00
|
|
|
|
|
|
|
// Check for this file first within the application support directories.
|
|
|
|
for(NSURL *supportURL in supportURLs) {
|
|
|
|
NSURL *const fullURL = [[supportURL URLByAppendingPathComponent:subdirectory]
|
|
|
|
URLByAppendingPathComponent:[NSString stringWithUTF8String:rom.file_name.c_str()]];
|
|
|
|
fileData = [NSData dataWithContentsOfURL:fullURL];
|
|
|
|
if(fileData) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failing that, check inside the application bundle.
|
|
|
|
if(!fileData) {
|
|
|
|
fileData = [[NSBundle mainBundle]
|
|
|
|
dataForResource:[NSString stringWithUTF8String:rom.file_name.c_str()]
|
|
|
|
withExtension:nil
|
|
|
|
subdirectory:subdirectory];
|
|
|
|
}
|
2018-01-01 21:04:13 +00:00
|
|
|
|
2019-07-21 22:41:00 +00:00
|
|
|
// Store an appropriate result, accumulating a list of the missing if requested.
|
|
|
|
if(!fileData) {
|
2018-01-01 21:04:13 +00:00
|
|
|
results.emplace_back(nullptr);
|
2019-07-21 22:41:00 +00:00
|
|
|
|
|
|
|
if(missing_roms) {
|
|
|
|
missing_roms->push_back(rom);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 21:04:13 +00:00
|
|
|
else {
|
|
|
|
std::unique_ptr<std::vector<std::uint8_t>> data(new std::vector<std::uint8_t>);
|
|
|
|
*data = fileData.stdVector8;
|
|
|
|
results.emplace_back(std::move(data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2018-01-25 23:28:19 +00:00
|
|
|
};
|
2018-01-01 21:04:13 +00:00
|
|
|
}
|