#!/usr/bin/env bash
# Compila o Switch Hook (watabou, 2016, 7DRL; codigo e arte GPL-3.0; github.com/watabou/switch-hook) para a web
# (HTML5) com Haxe 4.3.6, Lime 8.1.2 e OpenFL 9.3.3 (as ferramentas pinadas do Plankter, ver build-plankter.sh) e
# o msignal 1.2.5 (MIT, lib.haxe.org). O ndll do Lime so existe para x86_64: em Apple Silicon roda sob Rosetta.
#
# Mudancas (o fonte GPL publicado e o repositorio do commit; o que mudamos e este script mais switch-hook/web.patch):
# - switch-hook/web.patch: o OpenFL 9 nao tem mais o openfl.display.Tilesheet, que o jogo usa para desenhar o mapa,
# a vida e a area de ataque; entra uma classe com a mesma interface (utils/Tilesheet.hx) que desenha cada tile como
# um retangulo com preenchimento de bitmap; e sai o link para o Twitter do autor (tela de titulo);
# - uma Content-Security-Policy no index.html (nenhuma conexao fora desta origem);
# - a versao do cache de assets, um numero aleatorio que o Lime grava no SwitchHook.js, vira 1 (build reprodutivel).
#
# src/FredGames.Bundler/Builds/build-switch-hook.sh
#
# Saida: artifacts/build/switch-hook-a1e5f9f/{web.tar.gz,switch-hook-a1e5f9f.tar.gz,web.patch,
# third-party-licenses.txt}; os dois tar saem iguais a cada execucao.
set -euo pipefail
NAME=switch-hook
COMMIT=a1e5f9f14bb687abbf35dfd4ec27613464a2f7d3
SHA256=fcdea1e72a93c3e2d3a67ce54dde69af4b6f80cbb07ec8b0013b8311cc880058
MSIGNAL_SHA256=8b9255d114b1f4a490851e5604b351ca059ae7991809f36cac48aad25de9ce3e
HAXE=4.3.6
NEKO=2.4.0
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(git -C "$here" rev-parse --show-toplevel 2>/dev/null || pwd)"
out="$root/artifacts/build/$NAME-${COMMIT:0:7}"
cache="$root/artifacts/build/cache"
tools="$root/artifacts/tools"
libs="$tools/haxelib-plankter"
py="$tools/venv/bin/python"
mkdir -p "$cache"
fetch() {
if [ ! -f "$cache/$2" ]; then
curl -fsSL --retry 4 "$1" -o "$cache/$2.partial" && mv "$cache/$2.partial" "$cache/$2"
fi
echo "$3 $cache/$2" | shasum -a 256 -c -
}
# Haxe, Neko, Lime e OpenFL pinados: os mesmos do Plankter.
bash "$here/build-plankter.sh" --install
fetch "https://github.com/watabou/switch-hook/archive/$COMMIT.tar.gz" "$NAME-${COMMIT:0:7}.tar.gz" "$SHA256"
fetch "https://lib.haxe.org/files/3.0/msignal-1,2,5.zip" "msignal-1,2,5.zip" "$MSIGNAL_SHA256"
rm -rf "$out"
mkdir -p "$out/tree" "$out/web" "$out/haxelib" "$out/bin" "$out/home"
tar -xzf "$cache/$NAME-${COMMIT:0:7}.tar.gz" -C "$out/tree" --strip-components 1
tree="$out/tree"
# Fonte publicado: o repositorio do commit, sem mudanca, em tar deterministico (ordem, dono e data fixos).
TZ=UTC find "$tree" -exec touch -t 202001010000 {} +
( cd "$tree" && find . -type f | sed 's|^\./||' | LC_ALL=C sort > "$out/files.txt" )
( cd "$tree" && COPYFILE_DISABLE=1 tar --format=ustar --uid 0 --gid 0 --uname root --gname root -n \
-cf - -T "$out/files.txt" ) | gzip -n -9 > "$out/$NAME-${COMMIT:0:7}.tar.gz"
( cd "$tree" && patch -p1 --no-backup-if-mismatch < "$here/switch-hook/web.patch" > "$out/patch.log" )
cp "$here/switch-hook/web.patch" "$out/"
# Repositorio de bibliotecas desta compilacao: links para o Lime e o OpenFL, mais o msignal.
for lib in lime openfl; do ln -s "$libs/$lib" "$out/haxelib/$lib"; done
mkdir -p "$out/haxelib/msignal/1,2,5"
unzip -q "$cache/msignal-1,2,5.zip" -d "$out/haxelib/msignal/1,2,5"
printf '%s' "1.2.5" > "$out/haxelib/msignal/.current"
# O haxelib e o neko sao chamados pelo Lime por um shell (/bin/sh), que perde o DYLD_*: dois wrappers resolvem.
for b in haxelib neko; do
real="$tools/haxe-$HAXE/$b"; [ "$b" = neko ] && real="$tools/neko-$NEKO/neko"
printf '#!/bin/sh\nexport DYLD_FALLBACK_LIBRARY_PATH=%s\nexec %s "$@"\n' "$tools/neko-$NEKO" "$real" > "$out/bin/$b"
chmod +x "$out/bin/$b"
done
export NEKOPATH="$tools/neko-$NEKO" DYLD_FALLBACK_LIBRARY_PATH="$tools/neko-$NEKO" HAXE_STD_PATH="$tools/haxe-$HAXE/std"
export PATH="$out/bin:$tools/haxe-$HAXE:$PATH" HAXEPATH="$tools/haxe-$HAXE" HAXELIB_PATH="$out/haxelib"
export HOME="$out/home" TZ=UTC LC_ALL=C
cd "$tree"
arch -x86_64 /bin/bash -c 'haxelib run lime build html5 -release' > "$out/build.log" 2>&1 || { tail -30 "$out/build.log" >&2; exit 1; }
bin="$tree/Export/html5/bin"
test -f "$bin/SwitchHook.js"
"$py" -I - "$bin/index.html" <<'PY'
import sys, pathlib
p = pathlib.Path(sys.argv[1]); t = p.read_text()
csp = ('\n\t')
assert t.count('') == 1
t = t.replace('', '\n\t' + csp.rstrip('\t'), 1)
p.write_text(t)
PY
"$py" -I - "$bin/SwitchHook.js" <<'PY'
import sys, re, pathlib
p = pathlib.Path(sys.argv[1]); t = p.read_text()
t, n = re.subn(r'(this\.image = new haxe_ds_StringMap\(\);\n\tthis\.version = )\d+;', r'\g<1>1;', t)
assert n == 1, n
p.write_text(t)
PY
rmdir "$bin/lib" 2>/dev/null || true
cp -R "$bin/." "$out/web/"
TZ=UTC find "$out/web" -exec touch -t 202001010000 {} +
( cd "$out" && find web -type f | LC_ALL=C sort > "$out/web-files.txt" )
( cd "$out" && COPYFILE_DISABLE=1 tar --format=ustar --uid 0 --gid 0 --uname root --gname root -n \
-cf - -T "$out/web-files.txt" ) | gzip -n -9 > "$out/web.tar.gz"
# Licencas do que a compilacao embute no jogo (Haxe std, Lime, OpenFL, msignal e as bibliotecas JS do Lime).
{
echo "Switch Hook - third-party licences of the code compiled into the game (SwitchHook.js)"
echo "======================================================================================"
echo
echo "Built with Haxe $HAXE, Lime 8.1.2, OpenFL 9.3.3 and msignal 1.2.5 (see src/build-switch-hook.sh)."
echo "Every library below is under the MIT licence; texts follow."
echo
echo "--- Haxe standard library ---"
sed -n '/^Copyright (C)2005/,/^DEALINGS IN THE SOFTWARE\./p' "$tools/haxe-$HAXE/LICENSE.txt"
for l in lime:8,1,2 openfl:9,3,3; do
echo; echo "--- ${l%%:*} ${l##*:} (LICENSE.md) ---"
cat "$libs/${l%%:*}/${l##*:}/LICENSE.md"
done
echo; echo "--- msignal 1.2.5 (licence header of msignal/Signal.hx; the package has no LICENSE file) ---"
sed -n '/^Copyright/,/^\*\//p' "$out/haxelib/msignal/1,2,5/msignal/Signal.hx" | sed '$d'
echo; echo "--- Lime NOTICE.md (the JavaScript libraries below are bundled in the HTML5 output) ---"
sed -n '/howler.js 2.1.1/,/stats.js r16/p' "$libs/lime/8,1,2/NOTICE.md"
echo "(the output carries howler.js v2.2.4: Copyright (c) 2013-2020 James Simpson and GoldFire Studios, Inc., MIT;"
echo " FileSaver.js: Copyright (c) 2016 Eli Grey, MIT; pako 1.0.2: Copyright (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin,"
echo " MIT, with zlib parts Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler, zlib licence. The MIT licence"
echo " text is the one above; the notices travel inside SwitchHook.js.)"
} > "$out/third-party-licenses.txt"
ls -l "$out/web" "$out"/*.tar.gz