Files
neoLegacy-mac/extract-ui-as3.sh
2026-07-13 00:00:40 -05:00

294 lines
9.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Decompile ActionScript from every distinct Windows64 UI SWF.
# Output: as3/<swf-name>/scripts/**/*.as plus differing basename variants.
set -euo pipefail
GAME_DIR="${GAME_DIR:-$HOME/neoLegacy}"
OUT_DIR="${OUT_DIR:-$HOME/neoLegacy-mac/as3}"
FFDEC_JAR="${FFDEC_JAR:-$HOME/neoLegacy-mac/ffdec/ffdec.jar}"
JOBS="${JOBS:-8}"
LOG_FILE="${LOG_FILE:-${OUT_DIR}.ffdec.log}"
ALLOW_SHRINK="${ALLOW_SHRINK:-0}"
COMMON_MEDIA="$GAME_DIR/Minecraft.Client/Common/Media"
WINDOWS_MEDIA="$GAME_DIR/Minecraft.Client/Windows64Media/Media"
if ! command -v java >/dev/null 2>&1; then
echo "java is required" >&2
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "python3 is required" >&2
exit 1
fi
if [[ ! -r "$FFDEC_JAR" ]]; then
echo "FFDec jar is not readable: $FFDEC_JAR" >&2
exit 1
fi
if [[ ! -d "$COMMON_MEDIA" || ! -d "$WINDOWS_MEDIA" ]]; then
echo "neoLegacy media roots were not found under: $GAME_DIR" >&2
exit 1
fi
if [[ ! "$JOBS" =~ ^[1-9][0-9]*$ ]]; then
echo "JOBS must be a positive integer: $JOBS" >&2
exit 1
fi
if [[ "$ALLOW_SHRINK" != 0 && "$ALLOW_SHRINK" != 1 ]]; then
echo "ALLOW_SHRINK must be 0 or 1: $ALLOW_SHRINK" >&2
exit 1
fi
mkdir -p "$(dirname "$OUT_DIR")" "$(dirname "$LOG_FILE")"
lock_dir="${OUT_DIR}.lock"
lock_acquired=0
candidates=""
ordered=""
selected=""
jobs_file=""
stage_dir=""
backup_dir="${OUT_DIR}.old.$$"
worker_pid=""
terminate_workers() {
local attempt
if [[ -z "$worker_pid" ]] || ! kill -0 "$worker_pid" 2>/dev/null; then
worker_pid=""
return
fi
kill -TERM -- "-$worker_pid" 2>/dev/null || true
wait "$worker_pid" 2>/dev/null || true
for attempt in {1..20}; do
kill -0 -- "-$worker_pid" 2>/dev/null || break
sleep 0.05
done
kill -KILL -- "-$worker_pid" 2>/dev/null || true
worker_pid=""
}
cleanup() {
terminate_workers
if [[ -e "$backup_dir" ]]; then
rm -rf "$OUT_DIR"
mv "$backup_dir" "$OUT_DIR" || true
fi
[[ -z "$candidates" ]] || rm -f "$candidates"
[[ -z "$ordered" ]] || rm -f "$ordered"
[[ -z "$selected" ]] || rm -f "$selected"
[[ -z "$jobs_file" ]] || rm -f "$jobs_file"
if [[ -n "$stage_dir" && -d "$stage_dir" ]]; then
rm -rf "$stage_dir"
fi
if [[ "$lock_acquired" -eq 1 ]]; then
rm -rf "$lock_dir"
fi
}
trap cleanup EXIT
trap 'terminate_workers; exit 130' HUP INT TERM
if ! mkdir "$lock_dir" 2>/dev/null; then
owner="$(cat "$lock_dir/pid" 2>/dev/null || true)"
if [[ "$owner" =~ ^[0-9]+$ ]] && kill -0 "$owner" 2>/dev/null; then
echo "another extraction is already running (pid $owner)" >&2
exit 1
fi
rm -rf "$lock_dir"
if ! mkdir "$lock_dir" 2>/dev/null; then
echo "could not acquire extraction lock: $lock_dir" >&2
exit 1
fi
fi
printf '%s\n' "$$" >"$lock_dir/pid"
lock_acquired=1
: >"$LOG_FILE"
candidates="$(mktemp)"
ordered="$(mktemp)"
selected="$(mktemp)"
jobs_file="$(mktemp)"
stage_dir="$(mktemp -d "${OUT_DIR}.tmp.XXXXXX")"
scan_root() {
local root="$1"
local priority="$2"
local swf
local signature
local valid
local hash
while IFS= read -r -d '' swf; do
signature="$(dd if="$swf" bs=3 count=1 2>/dev/null || true)"
valid=1
case "$signature" in
FWS|CWS|ZWS) valid=0 ;;
esac
hash="$(shasum -a 256 "$swf" | awk '{print $1}')"
printf '%s\t%s\t%s\t%s\t%s\n' \
"$(basename "$swf")" "$valid" "$priority" "$hash" "$swf" >>"$candidates"
done < <(find "$root" -type f -name '*.swf' -print0)
}
scan_root "$COMMON_MEDIA" 0
scan_root "$WINDOWS_MEDIA" 1
input_count="$(wc -l <"$candidates" | tr -d ' ')"
if [[ "$input_count" -eq 0 ]]; then
echo "no SWFs found under: $GAME_DIR" >&2
exit 1
fi
LC_ALL=C sort -t $'\t' -k1,1 -k2,2n -k3,3n -k5,5 "$candidates" >"$ordered"
awk -F '\t' '!seen[$1]++' "$ordered" >"$selected"
while IFS=$'\t' read -r name valid priority hash swf; do
if [[ "$valid" -ne 0 ]]; then
printf 'FAILED: %s (no valid SWF exists for this basename)\n' "$swf" >>"$LOG_FILE"
fi
done <"$selected"
failures="$(grep -c '^FAILED:' "$LOG_FILE" || true)"
if [[ "$failures" -ne 0 ]]; then
printf '%s distinct inputs are not valid SWFs; see %s\n' "$failures" "$LOG_FILE" >&2
exit 1
fi
variants=0
duplicates=0
jobs=0
while IFS=$'\t' read -r name valid priority hash swf; do
canonical="$(awk -F '\t' -v name="$name" '$1 == name {print; exit}' "$selected")"
IFS=$'\t' read -r canonical_name canonical_valid canonical_priority canonical_hash canonical_swf <<<"$canonical"
if [[ "$valid" -ne 0 ]]; then
duplicates="$((duplicates + 1))"
printf 'IGNORED_INVALID: %s\n' "$swf" >>"$LOG_FILE"
continue
fi
if [[ "$swf" == "$canonical_swf" ]]; then
dest_rel="${name%.swf}"
elif [[ "$hash" == "$canonical_hash" ]]; then
duplicates="$((duplicates + 1))"
printf 'IGNORED_IDENTICAL: %s\n' "$swf" >>"$LOG_FILE"
continue
elif [[ "$swf" == "$COMMON_MEDIA/"* ]]; then
rel="${swf#"$COMMON_MEDIA"/}"
dest_rel="_variants/common/${rel%.swf}"
variants="$((variants + 1))"
else
rel="${swf#"$WINDOWS_MEDIA"/}"
dest_rel="_variants/windows/${rel%.swf}"
variants="$((variants + 1))"
fi
printf '%s\0%s\0' "$swf" "$dest_rel" >>"$jobs_file"
printf '%s\t%s\t%s\n' "$dest_rel" "$hash" "$swf" >>"$stage_dir/.sources.tsv"
jobs="$((jobs + 1))"
done <"$ordered"
export ALLOW_SHRINK FFDEC_JAR LOG_FILE OUT_DIR stage_dir
extract_one() {
local swf="$1"
local dest_rel="$2"
local dest="$stage_dir/$dest_rel"
local previous="$OUT_DIR/$dest_rel"
local diagnostics
local old_count
local new_count
diagnostics="$(mktemp)"
mkdir -p "$dest"
if ! java -jar "$FFDEC_JAR" -export script "$dest" "$swf" >"$diagnostics" 2>&1; then
printf 'FAILED: %s\n' "$swf" >>"$LOG_FILE"
sed 's/^/ /' "$diagnostics" >>"$LOG_FILE"
rm -f "$diagnostics"
return 1
fi
old_count=0
if [[ -d "$previous" ]]; then
old_count="$(find "$previous" -name '*.as' | wc -l | tr -d ' ')"
fi
new_count="$(find "$dest" -name '*.as' | wc -l | tr -d ' ')"
if [[ "$ALLOW_SHRINK" -eq 0 && "$new_count" -lt "$old_count" ]]; then
printf 'FAILED: %s produced %s scripts; previous output had %s\n' \
"$swf" "$new_count" "$old_count" >>"$LOG_FILE"
sed 's/^/ /' "$diagnostics" >>"$LOG_FILE"
rm -f "$diagnostics"
return 1
fi
if [[ "$new_count" -eq 0 ]]; then
printf 'EMPTY: %s\n' "$swf" >>"$LOG_FILE"
fi
rm -f "$diagnostics"
}
export -f extract_one
python3 -c 'import os, sys; os.setsid(); os.execvp("xargs", ("xargs", "-0", "-n2", "-P", sys.argv[1], "bash", "-c", "extract_one \"$0\" \"$1\""))' \
"$JOBS" <"$jobs_file" &
worker_pid="$!"
worker_status=0
wait "$worker_pid" || worker_status="$?"
worker_pid=""
if [[ "$worker_status" -ne 0 ]]; then
failures="$(grep -c '^FAILED:' "$LOG_FILE" || true)"
printf '%s SWFs failed to decompile; see %s\n' "$failures" "$LOG_FILE" >&2
exit 1
fi
scripts="$(find "$stage_dir" -name '*.as' | wc -l | tr -d ' ')"
empty="$(grep -c '^EMPTY:' "$LOG_FILE" || true)"
canonical_count="$(wc -l <"$selected" | tr -d ' ')"
if [[ "$canonical_count" -eq 0 || "$scripts" -eq 0 ]]; then
echo "FFDec produced no ActionScript output" >&2
exit 1
fi
if [[ "$ALLOW_SHRINK" -eq 0 && -d "$OUT_DIR" ]]; then
old_files="$stage_dir/.old-files"
new_files="$stage_dir/.new-files"
missing_files="$stage_dir/.missing-files"
(cd "$OUT_DIR" && find . -name '*.as' -print | LC_ALL=C sort) >"$old_files"
(cd "$stage_dir" && find . -name '*.as' -print | LC_ALL=C sort) >"$new_files"
comm -23 "$old_files" "$new_files" >"$missing_files"
if [[ -s "$missing_files" ]]; then
printf 'FAILED: new output is missing files from the previous extraction\n' >>"$LOG_FILE"
awk 'NR <= 20 { print " " $0 }' "$missing_files" >>"$LOG_FILE"
rm -f "$old_files" "$new_files" "$missing_files"
printf 'new output shrank; see %s (set ALLOW_SHRINK=1 to accept)\n' "$LOG_FILE" >&2
exit 1
fi
rm -f "$old_files" "$new_files" "$missing_files"
if [[ -f "$OUT_DIR/.sources.tsv" ]]; then
old_destinations="$stage_dir/.old-destinations"
new_destinations="$stage_dir/.new-destinations"
missing_destinations="$stage_dir/.missing-destinations"
cut -f1 "$OUT_DIR/.sources.tsv" | LC_ALL=C sort >"$old_destinations"
cut -f1 "$stage_dir/.sources.tsv" | LC_ALL=C sort >"$new_destinations"
comm -23 "$old_destinations" "$new_destinations" >"$missing_destinations"
if [[ -s "$missing_destinations" ]]; then
printf 'FAILED: new output omits prior SWF destinations\n' >>"$LOG_FILE"
awk 'NR <= 20 { print " " $0 }' "$missing_destinations" >>"$LOG_FILE"
rm -f "$old_destinations" "$new_destinations" "$missing_destinations"
printf 'SWF destination set shrank; see %s (set ALLOW_SHRINK=1 to accept)\n' "$LOG_FILE" >&2
exit 1
fi
rm -f "$old_destinations" "$new_destinations" "$missing_destinations"
fi
fi
if [[ -e "$OUT_DIR" ]]; then
mv "$OUT_DIR" "$backup_dir"
fi
if ! mv "$stage_dir" "$OUT_DIR"; then
exit 1
fi
stage_dir=""
old_backup="$backup_dir"
backup_dir=""
rm -rf "$old_backup"
printf 'done: %s ActionScript files from %s canonical SWFs + %s distinct variants; ' \
"$scripts" "$canonical_count" "$variants"
printf '%s duplicate/invalid inputs ignored, %s scriptless SWFs\n' "$duplicates" "$empty"