bsnes/icarus/core/super-famicom.cpp

73 lines
3.0 KiB
C++
Raw Normal View History

2015-08-21 11:29:53 +00:00
auto Icarus::superFamicomManifest(const string& location) -> string {
vector<uint8_t> buffer;
auto files = directory::files(location, "*.rom");
concatenate(buffer, {location, "program.rom"});
concatenate(buffer, {location, "data.rom" });
for(auto& file : files.match("*.boot.rom" )) concatenate(buffer, {location, file});
for(auto& file : files.match("*.program.rom")) concatenate(buffer, {location, file});
for(auto& file : files.match("*.data.rom" )) concatenate(buffer, {location, file});
return superFamicomManifest(buffer, location);
}
auto Icarus::superFamicomManifest(vector<uint8_t>& buffer, const string& location) -> string {
SuperFamicomCartridge cartridge{buffer.data(), buffer.size()};
if(auto markup = cartridge.markup) {
markup.append("\n");
markup.append("information\n");
markup.append(" sha256: ", Hash::SHA256(buffer.data(), buffer.size()).digest(), "\n");
markup.append(" title: ", location.prefixname(), "\n");
markup.append(" note: ", "heuristically generated by icarus\n");
return markup;
}
return "";
}
auto Icarus::superFamicomImport(vector<uint8_t>& buffer, const string& location) -> bool {
auto name = location.prefixname();
auto source = location.pathname();
string target{settings.libraryPath, "Super Famicom/", name, ".sfc/"};
//if(directory::exists(target)) return failure("game already exists");
SuperFamicomCartridge cartridge{buffer.data(), buffer.size()};
auto markup = cartridge.markup;
if(!markup) return failure("does not appear to be a valid image");
vector<Markup::Node> roms;
auto document = BML::unserialize(markup);
superFamicomImportScanManifest(roms, document);
for(auto rom : roms) {
auto name = rom["name"].text();
auto size = rom["size"].decimal();
if(name == "program.rom" || name == "data.rom" || cartridge.firmware_appended) continue;
if(file::size({source, name}) != size) return failure({"firmware (", name, ") missing or invalid"});
}
markup.append("\n");
markup.append("information\n");
markup.append(" title: ", name, "\n");
markup.append(" note: ", "heuristically generated by icarus\n");
if(!directory::create(target)) return failure("library path unwritable");
if(settings.createManifests) file::write({target, "manifest.bml"}, markup);
unsigned offset = (buffer.size() & 0x7fff) == 512 ? 512 : 0; //skip header if present
for(auto rom : roms) {
auto name = rom["name"].text();
auto size = rom["size"].decimal();
if(name == "program.rom" || name == "data.rom" || cartridge.firmware_appended) {
file::write({target, name}, buffer.data() + offset, size);
offset += size;
} else {
auto firmware = file::read({source, name});
file::write({target, name}, firmware);
}
}
return success();
}
auto Icarus::superFamicomImportScanManifest(vector<Markup::Node>& roms, Markup::Node node) -> void {
if(node.name() == "rom") roms.append(node);
for(auto leaf : node) superFamicomImportScanManifest(roms, leaf);
}