Iavra

Iavra Save Manager

Dec 16th, 2015 (edited)
2,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Stores a savefile in local storage or extracts it to a file. Can be used to move savefiles.
  3.  * @author Iavra
  4.  *
  5.  * @help
  6.  * To export a savefile from local web storage to a file, call the following function:
  7.  * IAVRA.SAVEMANAGER.exportWebStorage(savefileId);
  8.  *
  9.  * To import a savefile from a file to local web storage, call the following function (make sure, that the file actually
  10.  * exists in your "save" folder):
  11.  * IAVRA.SAVEMANAGER.importWebStorage(savefileId);
  12.  *
  13.  * When importing a savefile, the global save will automatically be imported, if possible.
  14.  */
  15. var IAVRA = IAVRA || {};
  16.  
  17. (function($) {
  18.     "use strict";
  19.    
  20.     var _download = function(filename, data) {
  21.         var element = document.createElement('a');
  22.         element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data));
  23.         element.setAttribute('download', filename);
  24.         element.style.display = 'none';
  25.         document.body.appendChild(element);
  26.         element.click();
  27.         document.body.removeChild(element);
  28.     };
  29.    
  30.     var _upload = function(filename, callback) {
  31.         var request = new XMLHttpRequest();
  32.         request.open('GET', filename);
  33.         request.onload = function() { callback(request.responseText); };
  34.         request.onerror = function() { throw new Error('The file named ' + filename + ' does not exist.'); };
  35.         request.send();
  36.     };
  37.    
  38.     $.SAVEMANAGER = {
  39.    
  40.         exportWebStorage: function(savefileId) {
  41.             var data = StorageManager.loadFromWebStorage(savefileId);
  42.             if(!data) { throw new Error('The savefile with id ' + savefileId + ' could not be found in web storage.'); }
  43.             _download(StorageManager.localFilePath(savefileId).replace(/^.*[\\\/]/, ''), LZString.compressToBase64(data));
  44.         },
  45.        
  46.         importWebStorage: function(savefileId) {
  47.             _upload(StorageManager.localFilePath(savefileId), function(data) {
  48.                 StorageManager.saveToWebStorage(savefileId, LZString.decompressFromBase64(data));
  49.                 if(parseInt(savefileId) > 0) {
  50.                     _upload(StorageManager.localFilePath(0), function(data) {
  51.                         var globalInfo = JsonEx.parse(StorageManager.loadFromWebStorage(0)) || [];
  52.                         var savefileInfo = ((JsonEx.parse(LZString.decompressFromBase64(data)) || [])[savefileId]) || DataManager.makeSavefileInfo();
  53.                         globalInfo[savefileId] = savefileInfo;
  54.                         StorageManager.saveToWebStorage(0, JsonEx.stringify(globalInfo));
  55.                     });
  56.                 }
  57.             });
  58.         }
  59.        
  60.     };
  61.    
  62. })(IAVRA);
Add Comment
Please, Sign In to add comment