#!/usr/bin/env bash
# PlayAgain's patch series on top of the fork's core (Remenby31/wasm-dolphin 50f0c362,
# Dolphin e22551ea + its 150 locked patches). Not a repository: patch files, applied in order.
#   0001  vendor/dolphin: InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.{h,cpp}
#         (four CI devices "PlayAgain/0..3/Wii Remote", the shared block and the pa_wiimote_* C API),
#         the backend's registration (ControllerInterface.cpp), its CMake entry, and the
#         per-update connect hook (BTEmu.cpp). All under __EMSCRIPTEN__/EMSCRIPTEN.
#   0002  the fork: dolphin_web_core.cpp calls ciface::PlayAgain::InitializeWiimotes() after the
#         pads; upstream-discio-worker.js adds the block's address to its "direct-pad" message.
#   0101  the dcbx-loop invalidation rule the same with the JIT on and off (R5); -dolphin + -fork;
#         ?dcbxskip=legacy restores the old rule.
#   0102  harness only: frame-locked replay (pad and Wii Remote scripts keyed to fields since a
#         state load) and digests (validationPaReplay), guest memory peek (validationPaPeek).
#   0103  MEM2 inline (Wii): compiled blocks, region fragments and the uncompiled blocks' C++ paths;
#         stores through the cached 0x9 view only; the oracles' MEM2 states (--mem2). ?mem2inline=0.
#   0104  the direct RunWasmBlock dispatch arm, compiled in and switched at run time. ?directdispatch=0.
#   0105  rotate byte swaps in block modules, block-cache key v2. ?blockbswap=0.
#   0106  harness only: wasm blocks only for block starts in a range (validationPaJitRange,
#         replay.mjs --jitrange), for bisecting a JIT-on/JIT-off digest difference.
#   0107  J2 first step (fork scripts only): a Wii disc gets a region cache key and its compilation
#         plan from guest RAM (src/aot-ram.js) when the disc plan fails or is empty; a function that runs
#         on into the next one (the _savegpr/_restgpr ladders) takes the words up to the end of the
#         flow, and functions holding rfi/sc stay with the JIT. Needs ?aot=1.
#         Harness: src/pa-j2.json install/function filters for bisecting.
#   0108  the game's own save in and out of the user directory (a raw memory card for a GameCube
#         game, a Wii title's data files; worker messages paSaveFiles / paPutSaveFiles), and the
#         emulation speed changed while a game runs (SetEmulationSpeed, worker message paSetSpeed).
# Rule: a file ending in -dolphin.patch applies to vendor/dolphin, every other one to the fork.
# Usage: apply.sh [checkout]   (after build.sh steps 1-4 have synced and patched vendor/dolphin)
#        then: node tools/build-core-repro.mjs --no-sync --allow-dirty --jobs 8
set -euo pipefail
HERE=$(cd "$(dirname "$0")" && pwd)
SRC=${1:-$HOME/.cache/playagain/dolphin-web/wasm-dolphin}
for p in "$HERE"/patches/[0-9][0-9][0-9][0-9]-*.patch; do
  case "$p" in
    *-dolphin.patch) git -C "$SRC/vendor/dolphin" apply "$p" ;;
    *) git -C "$SRC" apply "$p" ;;
  esac
  echo "applied $(basename "$p")"
done
echo "build with: (cd $SRC && node tools/build-core-repro.mjs --no-sync --allow-dirty --jobs 8)"
