diff --git a/extra/js/drive.js b/extra/js/drive.js new file mode 100644 index 0000000..54391c3 --- /dev/null +++ b/extra/js/drive.js @@ -0,0 +1,20 @@ +// https://stackoverflow.com/a/9458996 +function _bytesToBase64(bytes) { + var binary = ''; + var len = bytes.byteLength; + for (var i = 0; i < len; i++) { + binary += String.fromCharCode(bytes[i]); + } + return window.btoa(binary); +} + +// https://stackoverflow.com/a/21797381 +function _base64ToBytes(base64) { + var binary_string = window.atob(base64); + var len = binary_string.length; + var bytes = new Uint8Array(len); + for (var i = 0; i < len; i++) { + bytes[i] = binary_string.charCodeAt(i); + } + return bytes; +} diff --git a/extra/shell.html b/extra/shell.html index 6876732..ff790a8 100644 --- a/extra/shell.html +++ b/extra/shell.html @@ -3,8 +3,10 @@ + + MKXP @@ -176,7 +178,7 @@ window.saveFile = function(filename) { const buf = FS.readFile('/game/' + filename); - const b64 = btoa(String.fromCharCode.apply(null, buf)); + const b64 = _bytesToBase64(buf); localforage.setItem(namespace + filename, b64); localforage.getItem(namespace, function(err, res) { @@ -198,7 +200,7 @@ localforage.getItem(namespace + key, (err, res) => { if (err) return; - const buf = new Uint8Array(atob(res).split('').map((c) => c.charCodeAt(0))); + const buf = _base64ToBytes(res); FS.writeFile('/game/' + key, buf); }); });