mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:38:38 +00:00
When multiple PE images are passed with --extract, each image previously invoked extract() starting from index 0. Unnamed containers ( ame=None) in subsequent images collided on container_00, resulting in duplicate ame keys in the output index.json. Thread start_index=len(index) through multi-image extraction so stems and index entries remain unique, and use strerror on write failures.
412 lines
16 KiB
Python
412 lines
16 KiB
Python
"""Inventory the DXBC shader containers embedded in a PE image, outside `shaders.rpf`.
|
|
|
|
The package ships shader programs in two places: the game's own containers inside `shaders.rpf`, which
|
|
`tools/shader_carve.py` recovers, and containers embedded in DLLs. This tool scans any binary for the latter,
|
|
validates each container's chunk table, reads the `ILDN` module name, and reports what each container carries:
|
|
|
|
* `has_xbch`: the `DXIL` chunk's `XDAT` tag 0x0d starts with `XBCH`. This is the console program:
|
|
`tools/fssm_unpack.py` decompresses it to `FSSM` and the game's RDNA2 pipeline (disasm + `rdna2spirv.py`)
|
|
translates it, exactly like a shader from `shaders.rpf`. `docs/embedded_shaders.md` has the measurement.
|
|
* `has_fspx`: the `ACCC` chunk starts with `FSPX`. That is the FidelityFX AOT compute program, a different
|
|
format (`tools/fspx_unpack.py`, `docs/fspx.md`).
|
|
* `has_bitcode`: the `DXIL` chunk contains an LLVM bitcode magic (`BC C0 DE`). Some containers carry both the
|
|
bitcode and the console `XBCH` record; the console runtime uses the latter.
|
|
|
|
Usage:
|
|
python -I pe_shader_containers.py <image> [<image> ...] [--json FILE] [--extract DIR] [--verbose]
|
|
|
|
`--json` writes the per-container records (offset, size, sha256, chunk list, name, stage, flags). `--extract` writes
|
|
each container as `<module>.dxbc` plus an `index.json` whose entries the translator reads (with `stage` from
|
|
`PSV0`, defaulting to `"cs"` when absent), so the next step can be `fssm_unpack.py <DIR>`: nothing is extracted unless asked for, and extracted shaders are game
|
|
data that must stay in the user's work folder, never in the repository. Nothing here prints shader bytes.
|
|
`--json` is published through a staged temp file so a mid-write failure leaves any previous complete report.
|
|
|
|
For a native program packed in an SGD5 envelope, `offset` locates the envelope, `stored_size` includes its header,
|
|
and `size` and `sha256` describe the inflated DXBC. Plain containers keep their original ordering and offsets.
|
|
Standard library only; reads files, never runs them.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import stat
|
|
import struct
|
|
import sys
|
|
import tempfile
|
|
import zlib
|
|
|
|
# -I keeps the script directory off sys.path, so add it explicitly before importing the sibling module.
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from file_mode import published_mode # noqa: E402
|
|
from replace_retry import replace as replace_retry # noqa: E402
|
|
from report_paths import same_report_path # noqa: E402
|
|
|
|
DXBC = b"DXBC"
|
|
SGD5_PROGRAM = struct.pack("<I", 0xDDCE7)
|
|
XDAT = b"XDAT"
|
|
XBCH = b"XBCH"
|
|
FSPX = b"FSPX"
|
|
BITCODE = b"BC\xc0\xde"
|
|
MAX_CHUNKS = 64 # a container with more chunks is not one this parser accepts
|
|
NAME_LIMIT = 4096 # longer than any ILDN path seen; stops a malformed length from reading the image
|
|
SAFE_NAME = re.compile(r"[^A-Za-z0-9_.-]+")
|
|
|
|
|
|
class ContainerError(ValueError):
|
|
"""The bytes at this offset do not form a DXBC container this parser accepts."""
|
|
|
|
|
|
def u32(data: bytes, offset: int) -> int:
|
|
return struct.unpack_from("<I", data, offset)[0]
|
|
|
|
|
|
def parse_container(data: bytes, offset: int):
|
|
"""Return (total_size, chunks) for the container at `offset`, or raise ContainerError.
|
|
|
|
The package's containers differ from DXC's in one place only: the chunk table is a u32 offset array at
|
|
+32 (count at +28), and each chunk starts with its fourcc and a u32 size. `tools/fspx_unpack.parts()`
|
|
reads the same shape. The checks below reject the four-byte `DXBC` immediates and error strings that
|
|
occur in ordinary code.
|
|
"""
|
|
if data[offset : offset + 4] != DXBC:
|
|
raise ContainerError("no DXBC magic")
|
|
if offset + 32 > len(data):
|
|
raise ContainerError("truncated header")
|
|
total, count = u32(data, offset + 24), u32(data, offset + 28)
|
|
if not 32 < total <= len(data) - offset:
|
|
raise ContainerError(f"total size {total} does not fit the image")
|
|
if not 0 < count <= MAX_CHUNKS:
|
|
raise ContainerError(f"implausible chunk count {count}")
|
|
table_end = 32 + 4 * count
|
|
if table_end > total:
|
|
raise ContainerError("chunk table past the container")
|
|
chunks = []
|
|
for k in range(count):
|
|
start = u32(data, offset + 32 + 4 * k)
|
|
if not table_end <= start <= total - 8:
|
|
raise ContainerError(f"chunk {k} offset {start} outside the container")
|
|
fourcc = data[offset + start : offset + start + 4]
|
|
size = u32(data, offset + start + 4)
|
|
if start + 8 + size > total:
|
|
raise ContainerError(f"chunk {k} payload past the container")
|
|
if start % 4:
|
|
raise ContainerError(f"chunk {k} offset {start} not 4-byte aligned")
|
|
if not all(0x20 <= b < 0x7F for b in fourcc):
|
|
raise ContainerError(f"chunk {k} fourcc is not printable")
|
|
chunks.append((fourcc, start, size))
|
|
starts = [start for _, start, _ in chunks]
|
|
if starts != sorted(starts) or len(set(starts)) != len(starts):
|
|
raise ContainerError("chunk table is not sorted and unique")
|
|
if not chunks or max(start + 8 + size for _, start, size in chunks) != total:
|
|
raise ContainerError("the last chunk does not end at the container size")
|
|
return total, chunks
|
|
|
|
|
|
def chunk_payload(data: bytes, offset: int, chunk) -> bytes:
|
|
_, start, size = chunk
|
|
return data[offset + start + 8 : offset + start + 8 + size]
|
|
|
|
|
|
def module_name(ildn: bytes) -> str | None:
|
|
"""The module name in an `ILDN` part: `00 00 <u16 length> <PDB path>`; basename without `.pdb`."""
|
|
if len(ildn) < 4:
|
|
return None
|
|
length = struct.unpack_from("<H", ildn, 2)[0]
|
|
path = ildn[4 : 4 + min(length, NAME_LIMIT)].split(b"\0", 1)[0]
|
|
base = path.replace(b"\\", b"/").rsplit(b"/", 1)[-1].decode("latin-1")
|
|
return base[:-4] if base.lower().endswith(".pdb") else base or None
|
|
|
|
|
|
PSV0_STAGES = {0: "ps", 1: "vs", 2: "gs", 3: "hs", 4: "ds", 5: "cs", 13: "ms", 14: "as"}
|
|
|
|
|
|
def psv0_stage(psv0: bytes) -> str | None:
|
|
"""Return the shader stage from a `PSV0` chunk payload, or None if short/unknown."""
|
|
if len(psv0) < 29:
|
|
return None
|
|
info_size = u32(psv0, 0)
|
|
if info_size < 25 or 4 + 24 >= len(psv0):
|
|
return None
|
|
return PSV0_STAGES.get(psv0[4 + 24])
|
|
|
|
|
|
def xdat_tags(dxil: bytes) -> list[dict]:
|
|
"""The XDAT entries of a `DXIL` part, as {tag, size, magic} with the first four payload bytes as ASCII."""
|
|
start = dxil.find(XDAT)
|
|
if start < 0 or start + 12 > len(dxil):
|
|
return []
|
|
count, _header = struct.unpack_from("<II", dxil, start + 4)
|
|
if count > 256:
|
|
return []
|
|
out = []
|
|
for k in range(count):
|
|
at = start + 12 + 12 * k
|
|
if at + 12 > len(dxil):
|
|
break
|
|
tag, offset, size = struct.unpack_from("<III", dxil, at)
|
|
if offset + size > len(dxil):
|
|
continue
|
|
magic = dxil[offset : offset + 4]
|
|
out.append(
|
|
{
|
|
"tag": tag,
|
|
"size": size,
|
|
"magic": magic.decode("latin-1")
|
|
if all(0x20 <= b < 0x7F for b in magic)
|
|
else "",
|
|
}
|
|
)
|
|
return out
|
|
|
|
|
|
def inspect_raw(data: bytes, image: str) -> tuple[list[dict], list[int]]:
|
|
"""Every valid container in `data`, and the offsets of `DXBC` hits that failed validation."""
|
|
found, rejected = [], []
|
|
i = 0
|
|
while True:
|
|
i = data.find(DXBC, i)
|
|
if i < 0:
|
|
break
|
|
try:
|
|
total, chunks = parse_container(data, i)
|
|
except ContainerError:
|
|
rejected.append(i)
|
|
i += 4
|
|
continue
|
|
parts = {fourcc: (start, size) for fourcc, start, size in chunks}
|
|
dialect = b""
|
|
if b"DXIL" in parts:
|
|
dialect = chunk_payload(data, i, (b"DXIL",) + parts[b"DXIL"])
|
|
elif b"ACCC" in parts:
|
|
dialect = chunk_payload(data, i, (b"ACCC",) + parts[b"ACCC"])
|
|
tags = xdat_tags(dialect) if b"DXIL" in parts else []
|
|
stage = (
|
|
psv0_stage(chunk_payload(data, i, (b"PSV0",) + parts[b"PSV0"]))
|
|
if b"PSV0" in parts
|
|
else None
|
|
)
|
|
record = {
|
|
"image": image,
|
|
"offset": i,
|
|
"size": total,
|
|
"sha256": hashlib.sha256(data[i : i + total]).hexdigest(),
|
|
"name": module_name(chunk_payload(data, i, (b"ILDN",) + parts[b"ILDN"]))
|
|
if b"ILDN" in parts
|
|
else None,
|
|
"stage": stage,
|
|
"chunks": [
|
|
{"fourcc": fourcc.decode("latin-1"), "size": size}
|
|
for fourcc, _, size in chunks
|
|
],
|
|
"xdat_tags": tags,
|
|
"has_xbch": any(t["tag"] == 0x0D and t["magic"] == "XBCH" for t in tags),
|
|
"has_fspx": (b"ACCC" in parts and dialect[:4] == FSPX),
|
|
"has_bitcode": BITCODE in dialect,
|
|
}
|
|
found.append(record)
|
|
i += total
|
|
return found, rejected
|
|
|
|
|
|
def packed_dxbc(data: bytes, offset: int) -> bytes:
|
|
# SGD5 program envelope described by five-star-recomp (docs/shader-containers.md).
|
|
# Validate both stored sizes and the complete raw-deflate stream before inspecting its DXBC.
|
|
if offset < 4 or offset + 12 > len(data):
|
|
return b""
|
|
stored, size = struct.unpack_from("<II", data, offset + 4)
|
|
# Both stored sizes include the 12-byte envelope itself.
|
|
if (stored != u32(data, offset - 4) or not 12 < stored <= len(data) - offset
|
|
or stored > 64 << 20 or not 32 < size <= 64 << 20):
|
|
return b""
|
|
inflater = zlib.decompressobj(-15)
|
|
try:
|
|
dxbc = inflater.decompress(data[offset + 12:offset + stored], size + 1)
|
|
except zlib.error:
|
|
return b""
|
|
if (not inflater.eof or inflater.unused_data or len(dxbc) != size
|
|
or not dxbc.startswith(DXBC) or u32(dxbc, 24) != size):
|
|
return b""
|
|
return dxbc
|
|
|
|
|
|
def inspect(data: bytes, image: str) -> tuple[list[dict], list[int]]:
|
|
found, rejected = inspect_raw(data, image)
|
|
# Keep the plain containers' existing indices. Extract packed console programs only:
|
|
# desktop containers can use the same envelope but have no RDNA2 program to translate.
|
|
pos = 0
|
|
while (offset := data.find(SGD5_PROGRAM, pos)) >= 0:
|
|
pos = offset + len(SGD5_PROGRAM)
|
|
dxbc = packed_dxbc(data, offset)
|
|
if not dxbc:
|
|
continue
|
|
records, _ = inspect_raw(dxbc, image)
|
|
if records and records[0]["offset"] == 0 and records[0]["has_xbch"]:
|
|
record = records[0]
|
|
record.update(offset=offset, stored_size=u32(data, offset + 4))
|
|
found.append(record)
|
|
return found, rejected
|
|
|
|
|
|
def safe_stem(name: str | None, index: int) -> str:
|
|
stem = SAFE_NAME.sub("_", name or "").strip("._")
|
|
return stem or f"container_{index:02d}"
|
|
|
|
|
|
def extract(data: bytes, records: list[dict], directory: str, start_index: int = 0) -> list[dict]:
|
|
"""Write each container as `<module>.dxbc`; return the translator's index entries."""
|
|
os.makedirs(directory, exist_ok=True)
|
|
index = []
|
|
for k, record in enumerate(records, start=start_index):
|
|
stem = safe_stem(record["name"], k)
|
|
file = f"{stem}.dxbc"
|
|
target = os.path.join(directory, file)
|
|
offset = record["offset"]
|
|
content = (packed_dxbc(data, offset) if "stored_size" in record
|
|
else data[offset : offset + record["size"]])
|
|
n = k
|
|
while True:
|
|
try:
|
|
with open(target, "xb") as f:
|
|
f.write(content)
|
|
break
|
|
except FileExistsError:
|
|
file = f"{stem}_{n:02d}.dxbc"
|
|
target = os.path.join(directory, file)
|
|
n += 1
|
|
index.append({"name": stem, "file": file, "stage": record.get("stage") or "cs"})
|
|
return index
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
ap = argparse.ArgumentParser(
|
|
description="Inventory the DXBC shader containers embedded in PE images."
|
|
)
|
|
ap.add_argument("images", nargs="+", help="PE image(s) to scan")
|
|
ap.add_argument("--json", help="write the per-container records here")
|
|
ap.add_argument(
|
|
"--extract",
|
|
metavar="DIR",
|
|
help="write each container as <module>.dxbc plus index.json (game data: keep it out of the repo)",
|
|
)
|
|
ap.add_argument(
|
|
"--verbose", action="store_true", help="print one line per container"
|
|
)
|
|
a = ap.parse_args(argv)
|
|
|
|
if a.extract and os.path.exists(a.extract) and not os.path.isdir(a.extract):
|
|
ap.error(f"--extract {a.extract} is not a folder")
|
|
if a.extract:
|
|
index_path = os.path.join(a.extract, "index.json")
|
|
for image in a.images:
|
|
if same_report_path(index_path, image):
|
|
ap.error(f"--extract index {index_path} would overwrite the input image {image}")
|
|
if a.json:
|
|
if os.path.isdir(a.json):
|
|
ap.error(f"--json {a.json} is a folder, not a file")
|
|
for image in a.images:
|
|
if same_report_path(a.json, image):
|
|
ap.error(f"--json {a.json} would overwrite the input image {image}")
|
|
if a.extract and same_report_path(a.json, index_path):
|
|
ap.error(f"--json {a.json} would overwrite the extract index")
|
|
|
|
all_records, status, index = [], 0, []
|
|
for image in a.images:
|
|
try:
|
|
with open(image, "rb") as f:
|
|
data = f.read()
|
|
except OSError as e:
|
|
print(f"ERROR: cannot read {image}: {e.strerror or e}", file=sys.stderr)
|
|
status = 2
|
|
continue
|
|
records, rejected = inspect(data, image)
|
|
all_records.extend(records)
|
|
print(
|
|
f"{image}: {len(records)} container(s), {len(rejected)} rejected DXBC hit(s)"
|
|
)
|
|
if a.verbose:
|
|
for r in records:
|
|
name = r["name"] or "?"
|
|
flags = "".join(
|
|
c
|
|
for c, on in (
|
|
("X", r["has_xbch"]),
|
|
("F", r["has_fspx"]),
|
|
("B", r["has_bitcode"]),
|
|
)
|
|
if on
|
|
)
|
|
chunks = ",".join(c["fourcc"] for c in r["chunks"])
|
|
print(
|
|
f" {r['offset']:#x} {r['size']:>7} {flags:<3} {name:<28} {chunks}"
|
|
)
|
|
if a.extract:
|
|
try:
|
|
index.extend(extract(data, records, a.extract, start_index=len(index)))
|
|
except OSError as e:
|
|
print(f"ERROR: cannot write {a.extract}: {e.strerror or e}", file=sys.stderr)
|
|
return 2
|
|
|
|
if a.extract:
|
|
try:
|
|
with open(index_path, "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(index, f, indent=0)
|
|
except OSError as e:
|
|
print(f"ERROR: cannot write {index_path}: {e.strerror or e}", file=sys.stderr)
|
|
return 2
|
|
print(f" extracted {len(index)} file(s) to {a.extract}")
|
|
print(" these are game data: do not commit them", file=sys.stderr)
|
|
|
|
if a.json:
|
|
try:
|
|
write_json_report(a.json, all_records)
|
|
except OSError as e:
|
|
print(f"ERROR: cannot write {a.json}: {e.strerror or e}", file=sys.stderr)
|
|
return 2
|
|
print(f"wrote {a.json}")
|
|
return status
|
|
|
|
|
|
def write_json_report(path: str, report) -> None:
|
|
"""Publish a complete regular file, or write directly to a non-regular destination."""
|
|
os.makedirs(os.path.dirname(os.path.abspath(path)) or ".", exist_ok=True)
|
|
destination = os.path.realpath(path)
|
|
try:
|
|
mode = os.stat(destination).st_mode
|
|
except FileNotFoundError:
|
|
mode = None
|
|
if mode is not None and not stat.S_ISREG(mode):
|
|
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(report, f, indent=1)
|
|
return
|
|
if mode is not None:
|
|
# Retain the direct writer's permission check without truncating the report.
|
|
os.close(os.open(destination, os.O_WRONLY))
|
|
|
|
staged = tempfile.NamedTemporaryFile(
|
|
mode="w", encoding="utf-8", newline="\n", delete=False,
|
|
dir=os.path.dirname(destination) or ".", prefix=".pe_shader_containers-", suffix=".tmp",
|
|
)
|
|
try:
|
|
with staged:
|
|
json.dump(report, staged, indent=1)
|
|
if os.name != "nt":
|
|
os.chmod(staged.name, published_mode(destination))
|
|
replace_retry(staged.name, destination)
|
|
finally:
|
|
try:
|
|
os.unlink(staged.name)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
|
|
sys.stdout.reconfigure(errors="replace")
|
|
except (AttributeError, ValueError, OSError):
|
|
pass
|
|
sys.exit(main())
|