Fix broken base64 function

This commit is contained in:
Varun Patil 2020-10-19 12:04:42 +05:30
parent 25d33d11d1
commit 2c08238259
2 changed files with 24 additions and 2 deletions

20
extra/js/drive.js Normal file
View File

@ -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;
}

View File

@ -3,8 +3,10 @@
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="manifest" href="manifest.webmanifest">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<script src="js/localforage.min.js"></script>
<script src="js/drive.js"></script>
<script src="gameasync/mapping.js"></script>
<title>MKXP</title>
@ -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);
});
});