Skip to content

Load emscripten data via a zip file #24135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions distribution/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.4.22 (in development)
------------------------------------------------------------------------
- Change: [#23803] Lightning strikes and thunder happen at the same frequency independently of the game speed.
- Change: [#24135] Compress Emscripten js/wasm files.
- Fix: [#21919] Non-recolourable cars still show colour picker.
- Fix: [#23108] Missing pieces on Hypercoaster and Hyper-Twister, even with the ‘all drawable track pieces’ cheat enabled.
- Fix: [#24013] Failure to load a scenario preview image (minimap) could lead to an uncaught exception error message.
Expand Down
1 change: 0 additions & 1 deletion emscripten/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<title>OpenRCT2</title>
<script src="https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js" crossorigin=anonymous></script>
<script src="openrct2.js"></script>
<style>
* { padding: 0; margin: 0; }
canvas {
Expand Down
38 changes: 38 additions & 0 deletions emscripten/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,46 @@
if (!window.SharedArrayBuffer)
{
document.getElementById("loadingWebassembly").innerText = "Error! SharedArrayBuffer is not defined. This page required the CORP and COEP response headers.";
return;
}
if (!window.WebAssembly)
{
document.getElementById("loadingWebassembly").innerText = "Error! This page requires WebAssembly. Please upgrade your browser or enable WebAssembly support.";
return;
}

let assets;
try
{
let req = await fetch("openrct2.zip");
if (!req.ok) {
throw new Error("Response is not ok!")
}
let data = await req.blob();
let zip = new JSZip();
let contents = await zip.loadAsync(data);
assets = {
js: URL.createObjectURL(new Blob([await zip.file("openrct2.js").async("uint8array")], {type: 'application/json'})),
wasm: URL.createObjectURL(new Blob([await zip.file("openrct2.wasm").async("uint8array")], {type: 'application/wasm'}))
}
}
catch(e)
{
assets = null;
console.warn("Failed to fetch openrct2.zip. Will pull not-compressed files", e);
}

await new Promise(resolve => {
const script = document.createElement("script");
script.src = assets === null ? "openrct2.js" : assets.js;
script.addEventListener("load", resolve);
script.addEventListener("error", (e) => {
document.getElementById("loadingWebassembly").innerText = "Error loading openrct2.js!";
console.error(e);
});
document.body.appendChild(script);
})

window.Module = await window.OPENRCT2_WEB(
{
noInitialRun: true,
Expand All @@ -37,6 +71,10 @@
monitorRunDependencies: () => {},
locateFile: function(fileName)
{
if (assets !== null && fileName === "openrct2.wasm")
{
return assets.wasm;
}
console.log("loading", fileName);
return fileName;
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build-emscripten
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ mkdir -p www/
cd www/
cp -r ../openrct2.* ./
cp -r ../../emscripten/static/* ./
zip -r openrct2.zip ../openrct2.*
Loading