mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-22 10:21:57 -05:00
Move export/import functions to a javascript library
This commit is contained in:
parent
44d3925408
commit
89859a40f9
5 changed files with 90 additions and 72 deletions
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);
|
|
@ -39,49 +39,6 @@
|
|||
{
|
||||
console.log("loading", fileName);
|
||||
return fileName;
|
||||
},
|
||||
funcs: {
|
||||
export: () =>
|
||||
{
|
||||
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);
|
||||
})
|
||||
},
|
||||
import: () =>
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -247,31 +204,6 @@ async function clearDatabase(dir) {
|
|||
processFolder(dir);
|
||||
await new Promise(res => Module.FS.syncfs(false, res));
|
||||
}
|
||||
function 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;
|
||||
}
|
||||
function fileExists(path) {
|
||||
try {
|
||||
Module.FS.readFile(path);
|
||||
|
|
|
@ -18,7 +18,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "Emscripten")
|
|||
endif ()
|
||||
set(SHARED_FLAGS "-fexceptions")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS} ${SHARED_FLAGS}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMSCRIPTEN_LDFLAGS} --bind ${SHARED_FLAGS} -s EXPORTED_FUNCTIONS=_GetVersion,_main")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMSCRIPTEN_LDFLAGS} --bind ${SHARED_FLAGS} -s EXPORTED_FUNCTIONS=_GetVersion,_main --js-library ${ROOT_DIR}/emscripten/deps.js")
|
||||
find_package(SpeexDSP REQUIRED)
|
||||
elseif (MSVC)
|
||||
find_package(SDL2 REQUIRED)
|
||||
|
|
|
@ -48,6 +48,10 @@
|
|||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
extern "C" {
|
||||
extern void ExportPersistantData();
|
||||
extern void ImportPersistantData();
|
||||
}
|
||||
#endif
|
||||
|
||||
using namespace OpenRCT2;
|
||||
|
@ -1987,10 +1991,10 @@ namespace OpenRCT2::Ui::Windows
|
|||
break;
|
||||
#ifdef __EMSCRIPTEN__
|
||||
case WIDX_EXPORT_EMSCRIPTEN_DATA:
|
||||
MAIN_THREAD_EM_ASM({ Module.funcs.export(); });
|
||||
ExportPersistantData();
|
||||
break;
|
||||
case WIDX_IMPORT_EMSCRIPTEN_DATA:
|
||||
MAIN_THREAD_EM_ASM({ Module.funcs.import(); });
|
||||
ImportPersistantData();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -122,7 +122,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "Emscripten")
|
|||
endif ()
|
||||
set(SHARED_FLAGS "-fexceptions")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS} ${SHARED_FLAGS}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMSCRIPTEN_LDFLAGS} --bind ${SHARED_FLAGS}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMSCRIPTEN_LDFLAGS} --bind ${SHARED_FLAGS} --js-library ${ROOT_DIR}/emscripten/deps.js")
|
||||
find_package(SpeexDSP REQUIRED)
|
||||
elseif (MSVC)
|
||||
find_package(png 1.6 REQUIRED)
|
||||
|
|
Loading…
Reference in a new issue