Path: blob/main/misc/emulator/gba/user_scripts/IodineGBASavesGlueCode.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 ImportSaveCallback(name) {11try {12var save = findValue("SAVE_" + name);13if (save != null) {14writeRedTemporaryText("Loaded save.");15return base64ToArray(save);16}17}18catch (error) {19alert("Could not read save: " + error.message);20}21return null;22}23function ExportSave() {24Iodine.exportSave();25}26function ExportSaveCallback(name, save) {27if (name != "") {28try {29setValue("SAVE_" + name, arrayToBase64(save));30}31catch (error) {32alert("Could not store save: " + error.message);33}34}35}36function registerSaveHandlers() {37Iodine.attachSaveExportHandler(ExportSaveCallback);38Iodine.attachSaveImportHandler(ImportSaveCallback);39}40//Wrapper for localStorage getItem, so that data can be retrieved in various types.41function findValue(key) {42try {43if (window.localStorage.getItem(key) != null) {44return JSON.parse(window.localStorage.getItem(key));45}46}47catch (error) {48//An older Gecko 1.8.1/1.9.0 method of storage (Deprecated due to the obvious security hole):49if (window.globalStorage[location.hostname].getItem(key) != null) {50return JSON.parse(window.globalStorage[location.hostname].getItem(key));51}52}53return null;54}55//Wrapper for localStorage setItem, so that data can be set in various types.56function setValue(key, value) {57try {58window.localStorage.setItem(key, JSON.stringify(value));59}60catch (error) {61//An older Gecko 1.8.1/1.9.0 method of storage (Deprecated due to the obvious security hole):62window.globalStorage[location.hostname].setItem(key, JSON.stringify(value));63}64}65//Wrapper for localStorage removeItem, so that data can be set in various types.66function deleteValue(key) {67try {68window.localStorage.removeItem(key);69}70catch (error) {71//An older Gecko 1.8.1/1.9.0 method of storage (Deprecated due to the obvious security hole):72window.globalStorage[location.hostname].removeItem(key);73}74}7576