Save game to indexeddb
This commit is contained in:
		
							parent
							
								
									17517a5849
								
							
						
					
					
						commit
						ad9474a6e9
					
				
					 5 changed files with 65 additions and 1 deletions
				
			
		
							
								
								
									
										36
									
								
								shell.html
									
										
									
									
									
								
							
							
						
						
									
										36
									
								
								shell.html
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -3,6 +3,8 @@
 | 
			
		|||
  <head>
 | 
			
		||||
    <meta charset="utf-8">
 | 
			
		||||
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 | 
			
		||||
    <script src="js/localforage.min.js"></script>
 | 
			
		||||
 | 
			
		||||
    <title>MKXP</title>
 | 
			
		||||
    <style>
 | 
			
		||||
    	body {
 | 
			
		||||
| 
						 | 
				
			
			@ -100,9 +102,41 @@
 | 
			
		|||
    </div>
 | 
			
		||||
 | 
			
		||||
    <script type='text/javascript'>
 | 
			
		||||
      var namespace = 'kne';
 | 
			
		||||
      window.saveFile = function(filename) {
 | 
			
		||||
          const buf = FS.readFile('/game/' + filename);
 | 
			
		||||
          const b64 = btoa(String.fromCharCode.apply(null, buf));
 | 
			
		||||
          localforage.setItem(namespace + filename, b64);
 | 
			
		||||
 | 
			
		||||
          localforage.getItem(namespace, function(err, res) {
 | 
			
		||||
              if (err || !res) res = {};
 | 
			
		||||
              res[filename] = 1;
 | 
			
		||||
              localforage.setItem(namespace, res);
 | 
			
		||||
          });
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      var loadFiles = function() {
 | 
			
		||||
          localforage.getItem(namespace, function(err, res) {
 | 
			
		||||
              if (err || !res) return;
 | 
			
		||||
 | 
			
		||||
              const keys = Object.keys(res);
 | 
			
		||||
 | 
			
		||||
              console.log('Locally stored savefiles', keys);
 | 
			
		||||
 | 
			
		||||
              keys.forEach((key) => {
 | 
			
		||||
                  localforage.getItem(namespace + key, (err, res) => {
 | 
			
		||||
                      if (err) return;
 | 
			
		||||
 | 
			
		||||
                      const buf = new Uint8Array(atob(res).split('').map((c) => c.charCodeAt(0)));
 | 
			
		||||
                      FS.writeFile('/game/' + key, buf);
 | 
			
		||||
                  });
 | 
			
		||||
              });
 | 
			
		||||
          });
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      var Module = {
 | 
			
		||||
        preRun: [],
 | 
			
		||||
        postRun: [],
 | 
			
		||||
        postRun: [loadFiles],
 | 
			
		||||
        print: (function() {
 | 
			
		||||
          return function(text) {
 | 
			
		||||
            console.log(text);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue