This guide demonstrates how to modify .html
and .js
files after exporting a Godot game to enable Brotli-Wasm decompression for game files.
These instructions are only for the non-auto Brotli decompression example.
If you want to try browser-based auto-decompression (where the browser handles decompression automatically), simply upload the .br
files to your platform, and the user's browser will handle the rest. For this exmaple, check the auto_browser_decompress_example folder.
To jump right in, follow these steps:
-
Go to brotli_export folder.
-
Start a local server to test:
python -m http.server 8000
Visit http://localhost:8000 in your browser.
-
Game engine must start with *.br files.
For detailed instructions, see below. Example images are provided in the changes_in_*
folders, and a complete demo is available in the brotli_export
folder.
-
Add
type="module
to the<script>
tag:<script type="module" src="{project_name}.js"></script>
-
Add
type="module
to the inline<script>
tag:<script type="module"></script>
-
Insert
await load_b();
before the engine creation line:<script> await load_b(); // Your engine initialization code </script>
For reference, see the images in the changes_in_html
folder.
-
Add the Brotli loader function: At the top of your
.js
file, include:var brotli = null; window.load_b = async function () { brotli = await import("./brotli.js").then(m => m.default); };
-
Modify the
loadFetch
function: Add this at the beginning of the function:if (file === "index.pck") { file += ".br"; }
-
Update WebAssembly instantiation: Replace this block:
if (typeof (WebAssembly.instantiateStreaming) !== 'undefined') { // Replace this }
With:
var decompressed = await brotli.decompress(new Uint8Array(await r.arrayBuffer())); WebAssembly.instantiateStreaming(Promise.resolve(new Response(decompressed, { 'headers': [['content-type', 'application/wasm']] })), imports).then(done);
-
Update preloader path: Find this line:
loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true);
Add
.br
:loadPromise = preloader.loadPromise(`${loadPath}.wasm.br`, size, true);
-
Replace the block in
Promise
function: Locate this block:return new Promise(async function (resolve, reject) { for (const file of preloader.preloadedFiles) { me.rtenv['copyToFS'](file.path, file.buffer); } });
Replace it with:
for (const file of preloader.preloadedFiles) { var b = await file.buffer.arrayBuffer(); me.rtenv['copyToFS'](file.path, b); }
For more clarity, refer to the changes_in_js
folder.
After applying the changes:
-
Run the Python compression script:
python compress.py
-
Remove the original
.wasm
and.pck
files. -
Start the local server:
python -m http.server 8000
Test the changes at http://localhost:8000.
If you encounter any issues, inspect the demo files and refer to the images provided in the changes_in_*
folders.
Enjoy a more efficient, Brotli-compressed game loading experience!