Store binary in IndexedDB
This commit is contained in:
		
							parent
							
								
									218b9515a7
								
							
						
					
					
						commit
						709bcdb5bc
					
				
					 1 changed files with 7 additions and 26 deletions
				
			
		| 
						 | 
					@ -1,24 +1,3 @@
 | 
				
			||||||
// 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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Canvas used for image generation
 | 
					// Canvas used for image generation
 | 
				
			||||||
var generationCanvas = document.createElement('canvas')
 | 
					var generationCanvas = document.createElement('canvas')
 | 
				
			||||||
window.fileAsyncCache = {};
 | 
					window.fileAsyncCache = {};
 | 
				
			||||||
| 
						 | 
					@ -109,8 +88,7 @@ window.loadFileAsync = function(fullPath, bitmap, callback) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
window.saveFile = function(filename) {
 | 
					window.saveFile = function(filename) {
 | 
				
			||||||
    const buf = FS.readFile('/game/' + filename);
 | 
					    const buf = FS.readFile('/game/' + filename);
 | 
				
			||||||
    const b64 = _bytesToBase64(buf);
 | 
					    localforage.setItem(namespace + filename, buf);
 | 
				
			||||||
    localforage.setItem(namespace + filename, b64);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    localforage.getItem(namespace, function(err, res) {
 | 
					    localforage.getItem(namespace, function(err, res) {
 | 
				
			||||||
        if (err || !res) res = {};
 | 
					        if (err || !res) res = {};
 | 
				
			||||||
| 
						 | 
					@ -118,7 +96,7 @@ window.saveFile = function(filename) {
 | 
				
			||||||
        localforage.setItem(namespace, res);
 | 
					        localforage.setItem(namespace, res);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (window.saveCloudFile || (()=>{}))(filename, buf, b64);
 | 
					    (window.saveCloudFile || (()=>{}))(filename, buf);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var loadFiles = function() {
 | 
					var loadFiles = function() {
 | 
				
			||||||
| 
						 | 
					@ -133,8 +111,11 @@ var loadFiles = function() {
 | 
				
			||||||
            localforage.getItem(namespace + key, (err, res) => {
 | 
					            localforage.getItem(namespace + key, (err, res) => {
 | 
				
			||||||
                if (err) return;
 | 
					                if (err) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                const buf = _base64ToBytes(res);
 | 
					                // Don't overwrite existing files
 | 
				
			||||||
                FS.writeFile('/game/' + key, buf);
 | 
					                const fname = '/game/' + key;
 | 
				
			||||||
 | 
					                if (FS.analyzePath(fname).exists) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                FS.writeFile(fname, res);
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue