[HTML5] Export/Import save data

Hi guys,


i'm making my own game with RPG Maker MV but i have an question.


Is that a plugin exist to export and Import the savedata in a file ?


Exemple: i have became the game and i save my progression but i want to continue the game in other computer or smartphone.


Because if my gamers use ccleaner or update her browser they lost her save ^^


Thanks for your answer 😉


Regards
I made this a while ago, so you may try if it suits your needs: http://pastebin.com/pGkW3Ssv


It's not a full plugin, though, since it was mainly used for debugging.
Thanks for your help and for understand me (because i'm french and i don't speak english like you 😛)


But i am an newbie on JS ^^ why use it ? i copy/past in on an js file and i add it in the plugin folder ?


And why you don't make an official plugin for this ? Because make an HTML5 game but can't keep the save is weird 😛


Regards and thanks very much for you help, i test it in the evening
I'll see what i can do. It's been a while, since i wrote a proper plugin, so that might be a good opportunity to remove some of the rust 😀
I have read your help in the file header but i'm lost 😛


Have you an exemple for use it ? You say it


To export a savefile from local web storage to a file, call the following function:
  IAVRA.SAVEMANAGER.exportWebStorage(savefileId);


and for import use it IAVRA.SAVEMANAGER.importWebStorage(savefileId);


but where can i add this for add an sub menu into my game for launch it 😛


Regards
As i said, it's not a real plugin, so there are no visual elements, only JavaScript functions. I made a new version, that's not quite finished:


/*:
* @plugindesc v0.01 Offers a number of functions to export saves to files and import them again.
* @author Iavra
*/

(function($, undefined) {
"use strict";

/**
* Loads a given file, parses its content as json and passes it to a given callback. Throws an exception, if the
* file can't be loaded.
*/
var _loadFile = function(url, callback) {
var request = new XMLHttpRequest();
request.overrideMimeType('application/json');
request.open('GET', url);
request.onload = function() { callback(JsonEx.parse(LZString.decompressFromBase64(request.responseText))); };
request.onerror = function() { throw new Error('Couldn\'t load the file \'' + url + '\'.'); };
request.send();
};

/**
* Creates a download with the given data under the given filename. JavaScript can't write directly to the file
* system, unless we are using Node.js, which can't be guaranteed.
*/
var _saveFile = function(filename, data) {
var element = document.createElement('a');
var encoded = encodeURIComponent(LZString.compressToBase64(JsonEx.stringify(data)));
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encoded);
element.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
element.dispatchEvent(event);
} else { element.click(); }
};

//=============================================================================
// IAVRA.SAVE
//=============================================================================

$.SAVE = {
export: function(id) {
var info = DataManager.loadSavefileInfo(id), data = StorageManager.load(id);
if(!info || !data) { throw new Error('Couldn\'t find the savefile \'' + id + '\'.'); }
_saveFile('%1.save'.format(id.padZero(3)), {info: info, data: data});
},
import: function(id, url) {
var globalInfo = DataManager.loadGlobalInfo();
_loadFile(url, function(data) {
globalInfo[id] = data.info;
DataManager.saveGlobalInfo(globalInfo);
StorageManager.save(id, data.data);
})
}
}

})(this.IAVRA || (this.IAVRA = {}));


It offers 2 functions:


IAVRA.SAVE.export(id);
IAVRA.SAVE.import(id, url);


The first one exports the given save to a file and opens a download window. The second one takes the url to such a file and imports it under the given savefile id.


That said, the export currently only really works in web mode (game opened in browser) and not in local mode (testplay in MV, for example), since i can't get the download to work.
Yes i understand,


If you can't export and import if the game is not in an http server it's normal, beacause it's use the webstorage and the webstorage work with an domaine (Exemple: http://localhost) but not work with file://index.html


Okay if i undertsand it i copy past this code in js.file i add this on the index.html with src and i call the function with my form choice (button etc...) ?


i follow you if you continue it 😉


Regards
Hey everyone,

I wanted to touch base on why I revived the forum thread – I was in need of a particular plugin that wasn't updated and wasn't exactly user-friendly for those not well-versed in plugin coding. So, I took it upon myself to modify the plugin, aiming to make it more accessible for everyone.

I even ran tests on itch.io using the Opera browser, and it seems to function flawlessly. I can't quite explain why it works, but it does! I hope this modified version can benefit others just as much as it helped me.
JavaScript:
//=============================================================================
// syth_save_in_and_out
//=============================================================================
/*:
 * @plugindesc Allows importing and exporting save files for browser game versions.
 * @help This plugin provides functionality to import and export save files for browser game versions.
 * Possible plugin script calls:
 * - sythG.SAVE.export();
 * - sythG.SAVE.import();
 *
 *
 * @author IAVRA, sythG edit
 * @version 1.0
 *
 * @help syth_in_and_out
 * ----------------------------------------------------------------------------
 * Info:
 * Original plugin by IAVRA, edited by sythG.
 *
 * This plugin allows importing and exporting save files for browser game versions.
 *
 *
 * Possible plugin script calls:
 * sythG.SAVE.export();
 * this will open a prompt where you input the number of your save file (slot1 = 1, slot2 = 2)...
 * sythG.SAVE.import();
 * this will open the filebrowser, select the save file you import. it will overwrite the slot with the file name number!
 * ----------------------------------------------------------------------------
 
 */

(function($, undefined) {
    "use strict";

    // Original code by IAVRA
    // sythG edit  

    var _loadFile = function(url, callback) {
        var request = new XMLHttpRequest();
        request.overrideMimeType('application/json');
        request.open('GET', url);
        request.onload = function() { callback(JsonEx.parse(LZString.decompressFromBase64(request.responseText))); };
        request.onerror = function() { throw new Error('Couldn\'t load the file \'' + url + '\'.'); };
        request.send();
    };

    var _saveFile = function(filename, data) {
        var element = document.createElement('a');
        var encoded = encodeURIComponent(LZString.compressToBase64(JsonEx.stringify(data)));
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encoded);
        element.setAttribute('download', filename);
        if (document.createEvent) {
            var event = document.createEvent('MouseEvents');
            event.initEvent('click', true, true);
            element.dispatchEvent(event);
        } else { element.click(); }
    };

    //=============================================================================
    // sythG.SAVE
    //=============================================================================

    $.SAVE = {
        export: function() {
            var id = window.prompt('Please enter the ID to export:');
            if (id === null || id === '') return;
            var info = DataManager.loadSavefileInfo(id),
                data = StorageManager.load(id);
            if (!info || !data) {
                window.alert('The selected save slot ID does not exist.');
                return;
            }
            _saveFile('XM_%1.json'.format(id), { info: info, data: data });
        },
        import: function() {
            var input = document.createElement('input');
            input.type = 'file';
            input.accept = '.json';
            input.style.display = 'none';

            input.onchange = function(e) {
                var file = e.target.files[0];
                var reader = new FileReader();

                reader.onload = function(event) {
                    var url = event.target.result;
                    var fileName = file.name;
                    var slotId = fileName.match(/\d+/); // Extract the number from the file name
                    if (!slotId) {
                        window.alert('Invalid file name. Please use the correct format (e.g., XXX.json)');
                        return;
                    }
                    slotId = parseInt(slotId[0], 10); // Convert extracted number to an integer
                    var globalInfo = DataManager.loadGlobalInfo();
                    _loadFile(url, function(data) {
                        globalInfo[slotId] = data.info; // Set the imported data to the extracted slot ID
                        DataManager.saveGlobalInfo(globalInfo);
                        StorageManager.save(slotId, data.data);
                        window.alert('Import successful. Please restart the game for changes to take effect.');
                    });
                };

                reader.readAsDataURL(file);
            };

            document.body.appendChild(input);
            input.click();
            document.body.removeChild(input);
        }
    };

})(this.sythG || (this.sythG = {}));

what it does now is it makes everything more userfriendly,

Script Calls:

sythG.SAVE.export();

- when you call this a window pop up asking the player to select what save slot he want to export. just type in the number
1 for slot 1, 2 for slot 2... it then downloads/saves the file to your pc. as XM_(id).json
XM because thats the name for my project. if you want it to be saved with different name you can just change this part
of the code
JavaScript:
_saveFile('XM_%1.json'.format(id), { info: info, data: data });
replace the XM_ with whatever you need.

sythG.SAVE.import();

-This will open the file browser, and you should select an exported save file. It will then import that save file, overwriting the save slot with the corresponding ID (if the file name is XM_2.json, it will use slot 2). After importing, you'll need to instruct your players to restart the game.
If you have any questions I can't help you because I changed the whole thing with Chatgpt. I truly have no idea what i did.

This feature can also be utilized for non-HTML game versions, enabling players to import save files from older iterations of your project without the need for manual copy-pasting.
Necro-post notice: @sythmanG

Original thread: https://forums.rpgmakerweb.com/threads/html5-export-import-save-data.60506/