Path: blob/main/misc/emulator/gba/user_scripts/IodineGBAROMLoadGlueCode.js
28515 views
"use strict";1/*2Copyright (C) 2012-2013 Grant Galitz34Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:56The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.78THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.9*/10function attachBIOS(BIOS) {11try {12Iodine.attachBIOS(new Uint8Array(BIOS));13}14catch (error) {15Iodine.attachBIOS(BIOS);16}17}18function attachROM(ROM) {19try {20Iodine.attachROM(new Uint8Array(ROM));21}22catch (error) {23Iodine.attachROM(ROM);24}25}26function fileLoadShimCode(files, ROMHandler) {27if (typeof files != "undefined") {28if (files.length >= 1) {29//Gecko 1.9.2+ (Standard Method)30try {31var binaryHandle = new FileReader();32binaryHandle.onloadend = function () {33ROMHandler(this.result);34}35binaryHandle.readAsArrayBuffer(files[files.length - 1]);36}37catch (error) {38try {39var result = files[files.length - 1].getAsBinary();40var resultConverted = [];41for (var index = 0; index < result.length; ++index) {42resultConverted[index] = result.charCodeAt(index) & 0xFF;43}44ROMHandler(resultConverted);45}46catch (error) {47alert("Could not load the processed ROM file!");48}49}50}51}52}53function fileLoadBIOS() {54fileLoadShimCode(this.files, attachBIOS);55}56function fileLoadROM() {57fileLoadShimCode(this.files, attachROM);58}59function downloadFile(fileName, registrationHandler) {60var ajax = new XMLHttpRequest();61ajax.onload = registrationHandler;62ajax.open("GET", "./" + fileName, true);63ajax.responseType = "arraybuffer";64ajax.overrideMimeType("text/plain; charset=x-user-defined");65ajax.send(null);66}67function processDownload(parentObj, attachHandler) {68try {69attachHandler(new Uint8Array(parentObj.response));70}71catch (error) {72var data = parentObj.responseText;73var length = data.length;74var dataArray = [];75for (var index = 0; index < length; index++) {76dataArray[index] = data.charCodeAt(index) & 0xFF;77}78attachHandler(dataArray);79}80}8182