mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 08:12:53 +01:00
Move export/import functions to a javascript library
This commit is contained in:
82
emscripten/deps.js
Normal file
82
emscripten/deps.js
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
var EmscriptenDeps = {
|
||||
ExportPersistantData: () =>
|
||||
{
|
||||
if (!window.JSZip)
|
||||
{
|
||||
alert("JSZip library not found. Aborting");
|
||||
return;
|
||||
}
|
||||
const zipFolder = (folder) =>
|
||||
{
|
||||
let zip = new JSZip();
|
||||
const processFolder = (name) => {
|
||||
let contents;
|
||||
try {
|
||||
contents = Module.FS.readdir(name);
|
||||
} catch(e) {
|
||||
return;
|
||||
}
|
||||
contents.forEach((entry) => {
|
||||
if ([".", ".."].includes(entry)) return;
|
||||
try {
|
||||
Module.FS.readFile(name + entry);
|
||||
processFile(name + entry);
|
||||
} catch(e) {
|
||||
processFolder(name + entry + "/");
|
||||
}
|
||||
})
|
||||
}
|
||||
const processFile = (name) => {
|
||||
zip.file(name, Module.FS.readFile(name));
|
||||
}
|
||||
processFolder(folder);
|
||||
return zip;
|
||||
}
|
||||
const zip = zipFolder("/persistant/");
|
||||
zip.generateAsync({type: "blob"}).then(blob => {
|
||||
const a = document.createElement("a");
|
||||
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = "OpenRCT2-emscripten.zip";
|
||||
a.click();
|
||||
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
|
||||
})
|
||||
},
|
||||
ImportPersistantData: () =>
|
||||
{
|
||||
if (!window.JSZip)
|
||||
{
|
||||
alert("JSZip library not found. Aborting");
|
||||
return;
|
||||
}
|
||||
if (!confirm("Are you sure? This will wipe all current data.")) return;
|
||||
alert("Select a zip file");
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.addEventListener("change", async (e) => {
|
||||
let zip = new JSZip();
|
||||
try {
|
||||
zip = await zip.loadAsync(e.target.files[0]);
|
||||
} catch(e) {
|
||||
alert("Not a zip file!");
|
||||
return;
|
||||
}
|
||||
await clearDatabase("/persistant/");
|
||||
for (const k in zip.files) {
|
||||
const entry = zip.files[k];
|
||||
if (entry.dir) {
|
||||
try {
|
||||
Module.FS.mkdir("/"+k);
|
||||
} catch(e) {}
|
||||
} else {
|
||||
Module.FS.writeFile("/"+k, await entry.async("uint8array"));
|
||||
}
|
||||
}
|
||||
console.log("Database restored");
|
||||
})
|
||||
input.click();
|
||||
}
|
||||
};
|
||||
|
||||
mergeInto(LibraryManager.library, EmscriptenDeps);
|
||||
Reference in New Issue
Block a user