Made Add ROMs accept a menu file, improve error handling for load menu rom, small code style tweaks

This commit is contained in:
Evan Barger 2020-07-09 20:42:57 -04:00
parent 3a1c821c26
commit cf4b8f1218
3 changed files with 38 additions and 18 deletions

View File

@ -75,7 +75,7 @@
</td>
<td>
<button v-on:click="triggerAddMenuLabel" class="upload" type="button">
<label for="menuFileInput" ref="addMenuLabel" v-on:click="stopPropagation">Add From GBNP ROM</label>
<label for="menuFileInput" ref="addMenuLabel" v-on:click="stopPropagation">Load GBNP ROM</label>
</button>
<input style="display: none" id="menuFileInput" type="file" v-on:change="addMenu" accept=".gb,.gbc">
</td>
@ -105,7 +105,7 @@
<input style="display: none" id="romFileInput" type="file" v-on:change="addROM" :disabled="!menu.present() || romOverflow" accept=".gb,.gbc" multiple>
<button v-on:click="triggerAddMenuLabel" class="upload" type="button">
<label for="menuFileInput" ref="addMenuLabel" v-on:click="stopPropagation">Add From GBNP ROM</label>
<label for="menuFileInput" ref="addMenuLabel" v-on:click="stopPropagation">Load GBNP ROM</label>
</button>
<input style="display: none" id="menuFileInput" type="file" v-on:change="addMenu" accept=".gb,.gbc">
</div>

View File

@ -101,7 +101,8 @@ let app = new Vue({
addMenu: function(e) {
let fileReader = new FileReader()
fileReader.onload = () => {
this.roms = this.processor.parseMenuData(fileReader.result, this.fontIndex);
const parsedRoms = this.processor.parseMenuData(fileReader.result, this.fontIndex);
if (parsedRoms.length > 0) { this.roms = parsedRoms; }
}
fileReader.readAsArrayBuffer(e.target.files[0]);
@ -112,8 +113,13 @@ let app = new Vue({
for (let i = 0; i < files.length; i++) {
let fileReader = new FileReader();
fileReader.onload = () => {
const rom = new ROM(fileReader.result, this.fontIndex)
if (!rom.bad) { this.roms.push(rom); }
const rom = new ROM(fileReader.result, this.fontIndex);
if (rom.isMenu()) {
const parsedRoms = this.processor.parseMenuData(fileReader.result, this.fontIndex);
parsedRoms.forEach((rom) => this.roms.push(rom));
} else if (!rom.bad) {
this.roms.push(rom);
}
}
fileReader.readAsArrayBuffer(files[i]);
}
@ -158,15 +164,15 @@ let app = new Vue({
rom.updateMenuText(val, this.fontIndex);
}, 500);
},
stopPropagation: function(e) { e.stopImmediatePropagation(); },
triggerAddMenuLabel: function(e) { this.$refs.addMenuLabel.click(); },
triggerAddRomLabel: function(e) { this.$refs.addRomLabel.click(); },
preventDefault: function(e) { e.preventDefault(); },
dropFile: function(e) {
this.addROM(null, e.dataTransfer.files);
e.target.classList.remove('over')
e.preventDefault();
}
},
triggerAddMenuLabel: function(e) { this.$refs.addMenuLabel.click(); },
triggerAddRomLabel: function(e) { this.$refs.addRomLabel.click(); },
stopPropagation: function(e) { e.stopImmediatePropagation(); },
preventDefault: function(e) { e.preventDefault(); },
}
});

View File

@ -12,13 +12,14 @@ const BITMAP_PREVIEW_BYTES = [
[0xBB, 0xBB, 0xBB, 0xFF], // light grey
[0x66, 0x66, 0x66, 0xFF], // dark grey
[0x00, 0x00, 0x00, 0xFF] // black
]
];
const FONTS = [
{ style: 'normal 8px Gameboy', y: 7 },
{ style: 'normal 8px PokemonGB', y: 7 },
{ style: 'normal 8px Nokia', y: 7 },
{ style: 'normal 16px Gamer', y: 7 }
]
];
const MENU_TITLE_CHECK = 'NP M-MENU';
class Menu {
constructor() {
@ -76,7 +77,9 @@ class ROM {
let paddedFile = new FileSeeker(this.arrayBuffer);
if (file.size() > this.arrayBuffer.byteLength) {
alert(`Error with ${this.title}!\nROM header size is smaller than the file size! Did you load a menu by mistake?`)
if (!this.isMenu()) {
alert(`Error with ${this.title}!\nROM header size is smaller than the file size!`);
}
this.bad = true;
return;
} else {
@ -113,6 +116,10 @@ class ROM {
return Math.trunc(Math.pow(4, this.ramByte - 1)) * 2;
}
isMenu() {
return this.title.includes(MENU_TITLE_CHECK);
}
updateMenuText(text, fontIndex) {
this.menuText = text;
this.updateBitmap(fontIndex);
@ -442,9 +449,16 @@ class Processor {
}
parseMenuData(menuBuffer, fontIndex) {
this.roms = [];
const roms = [];
const menuFile = new FileSeeker(menuBuffer);
menuFile.seek(0x134);
const title = String.fromCharCode(...menuFile.read(0xF)).replace(/\0/g, '');
if (!title.includes(MENU_TITLE_CHECK)) {
alert(`${title} does not appear to be a menu ROM!\nDid you select a regular ROM by mistake?`);
return [];
}
let romSizes = [];
for (let i = 0; i < 7; i++) {
const indexPosition = 0x1C200 + i * 512
@ -462,17 +476,17 @@ class Processor {
for (let i = 0; i < romSizes.length; i++) {
const romData = menuFile.read(romSizes[i] * 128 * 1024);
const romBuffer = (new Uint8Array(romData)).buffer;
this.roms.push(new ROM(romBuffer, fontIndex))
roms.push(new ROM(romBuffer, fontIndex))
}
} catch(e) {
console.log(e)
alert("Failed to parse roms!");
alert("Failed to parse roms from menu file!");
}
} else {
alert("No roms detected!")
alert("No roms detected in menu file!")
}
return this.roms;
return roms;
}
}