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.