diff --git a/build_all.sh b/build_all.sh index 704f826..0c14041 100755 --- a/build_all.sh +++ b/build_all.sh @@ -29,3 +29,6 @@ emcc --clear-cache --clear-ports ./pull_minetest.sh ./build_fsroot.sh ./build_minetest.sh + +# Finished product +./build_www.sh diff --git a/build_minetest.sh b/build_minetest.sh index d0d2c66..b188428 100755 --- a/build_minetest.sh +++ b/build_minetest.sh @@ -62,33 +62,3 @@ if ! $INCREMENTAL; then fi emmake make - -echo "Installing into www/" -rm -rf "$WWW_DIR" -mkdir "$WWW_DIR" - -FILES="minetest.js minetest.wasm minetest.worker.js" - -for I in $FILES; do - cp src/"$I" "$WWW_DIR" -done - -if [ -f src/minetest.wasm.map ]; then - cp src/minetest.wasm.map "$WWW_DIR" -fi - -cp "$BASE_DIR/static/index.html" "$WWW_DIR" -cp "$BASE_DIR/static/launcher.js" "$WWW_DIR" -cp "$BASE_DIR/static/.htaccess" "$WWW_DIR" - -cp "$BUILD_DIR/fsroot.tar.zst" "$WWW_DIR/base.pack" - -echo "DONE" - -popd -popd - -# Optional step to deploy to webserver -if [ -f deploy.sh ]; then - ./deploy.sh -fi diff --git a/build_www.sh b/build_www.sh new file mode 100755 index 0000000..74b3cf1 --- /dev/null +++ b/build_www.sh @@ -0,0 +1,55 @@ +#!/bin/bash -eux + +source common.sh + +# Generate a random hash for this release +# This is used as a prefix for cache invalidation +SEEDFILE="/tmp/minetest_build_uuid_seed" +dd status=none if=/dev/urandom bs=64 count=1 > "$SEEDFILE" +md5sum -b "$SEEDFILE" > "$SEEDFILE".hash +RELEASE_UUID=`cut -b -12 "$SEEDFILE".hash` + +RELEASE_DIR="$WWW_DIR/$RELEASE_UUID" +PACKS_DIR="$RELEASE_DIR/packs" + +echo "Installing release $RELEASE_UUID into www/" +rm -rf "$WWW_DIR" +mkdir "$WWW_DIR" +mkdir "$RELEASE_DIR" +mkdir "$PACKS_DIR" + +# Copy emscripten generated files +pushd "$BUILD_DIR/minetest/src" +EMSCRIPTEN_FILES="minetest.js minetest.wasm minetest.worker.js" +for I in $EMSCRIPTEN_FILES; do + cp "$I" "$RELEASE_DIR" +done +if [ -f minetest.wasm.map ]; then + cp minetest.wasm.map "$RELEASE_DIR" +fi +popd + +apply_substitutions() { + local srcfile="$1" + local dstfile="$2" + sed "s/%__RELEASE_UUID__%/$RELEASE_UUID/g" "$srcfile" > "$dstfile" +} + +# Copy static files, replacing $RELEASE_UUID with the id +pushd "$BASE_DIR/static" +apply_substitutions htaccess_toplevel "$WWW_DIR"/.htaccess +apply_substitutions index.html "$WWW_DIR"/index.html +apply_substitutions htaccess_release "$RELEASE_DIR"/.htaccess +apply_substitutions launcher.js "$RELEASE_DIR"/launcher.js +popd + +# Copy base file system pack +cp "$BUILD_DIR/fsroot.tar.zst" "$PACKS_DIR/base.pack" + +echo "DONE" + +# Optional script to customize deployment +# Use this to add extra data packs, deploy to webserver, etc +if [ -f deploy.sh ]; then + ./deploy.sh +fi diff --git a/incremental.sh b/incremental.sh index dfc1325..47366c0 100755 --- a/incremental.sh +++ b/incremental.sh @@ -1,4 +1,7 @@ #!/bin/bash -eux +# Incremental build for making changes to only minetest / irrlicht + export INCREMENTAL=true ./build_minetest.sh +./build_www.sh diff --git a/static/htaccess_release b/static/htaccess_release new file mode 100644 index 0000000..d600afd --- /dev/null +++ b/static/htaccess_release @@ -0,0 +1 @@ +Header set Cache-Control "public, max-age=31536000, immutable" diff --git a/static/.htaccess b/static/htaccess_toplevel similarity index 100% rename from static/.htaccess rename to static/htaccess_toplevel diff --git a/static/index.html b/static/index.html index b18384c..e926429 100644 --- a/static/index.html +++ b/static/index.html @@ -6,6 +6,6 @@ Minetest for the Web - + diff --git a/static/launcher.js b/static/launcher.js index f0d9bd9..d122565 100644 --- a/static/launcher.js +++ b/static/launcher.js @@ -1,3 +1,7 @@ +// These are relative paths +const RELEASE_DIR = '%__RELEASE_UUID__%'; // set by build_www.sh +const PACKS_DIR = RELEASE_DIR + '/packs'; + const rtCSS = ` body { font-family: arial; @@ -194,7 +198,7 @@ var pendingPacks = 0; function fetchPack(name) { pendingPacks += 1; const xhr = new XMLHttpRequest(); - xhr.open('GET', name + '.pack', true); + xhr.open('GET', PACKS_DIR + '/' + name + '.pack', true); xhr.responseType = 'arraybuffer'; xhr.onprogress = (event) => { console.log(`Fetched ${event.loaded} of ${event.total}`); @@ -481,6 +485,6 @@ fetchPacks(); // Start loading the wasm module const mtModuleScript = document.createElement("script"); mtModuleScript.type = "text/javascript"; -mtModuleScript.src = "minetest.js"; +mtModuleScript.src = RELEASE_DIR + "/minetest.js"; mtModuleScript.async = true; document.body.appendChild(mtModuleScript);