Terrain tiles are read through MHDR, MCIN and MCRF, as 1.12.1 reads them (#2988)

benilla found a tile's chunks by walking record sizes, so a Noggit export whose MCNK sizes count their header lost every chunk after the first. 1.12.1 reads the tile through MHDR's offsets (`0x6c2010`) and each chunk from its MCIN entry's file offset (`0x6c299d`), never an MCNK's size. benilla now does too.

Two more reads follow the reference. With MCNK flag 0x8000 an alpha map decodes whole at 64×64 (`0x6b0a6b`, `0x6b0ac0`), and without it at 63×63 with the edge copied. Doodads and WMOs are made only from the chunks' MCRF refs (`0x6b00f0`), so an entry no chunk references never appears. The static item and terrain shaders now sample in uniform control flow, which WGSL requires, and a lint holds them to it.

Every chunk of 2,380 install tiles parses identically before and after. Each fix has a test that fails without it.

Fixes #2981
Fixes #16
Fixes #21
Fixes #613

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Sam Sung
2026-11-04 12:20:02 +07:00
committed by GitHub
parent bd9235f919
commit e63ff908a2
7 changed files with 618 additions and 92 deletions
+2 -2
View File
@@ -19,8 +19,8 @@ const W: usize = 64;
impl CombinedAlphaMap {
/// Decode `chunk`'s alpha layers. `has_big_alpha` makes uncompressed layers 8-bit, and
/// `fix_alpha` fills the last row and column from their neighbours (a 63×63 source); vanilla
/// is `false`, `true`.
/// `fix_alpha` fills the last row and column from their neighbours (a 63×63 source): vanilla
/// is `false`, and `true` unless the chunk sets [`crate::MCNK_ALPHA_64`] (`0x6b0a60`).
pub fn new(chunk: &McnkChunk, has_big_alpha: bool, fix_alpha: bool) -> Self {
let mut map = vec![0u8; W * W * 4];
for px in map.as_chunks_mut::<4>().0 {
+366 -60
View File
@@ -1,9 +1,13 @@
//! A reader for 1.12.1's monolithic root ADTs: IFF chunks (a reversed magic, a `u32` size, the
//! A reader for 1.12.1's monolithic root ADTs: IFF records (a reversed magic, a `u32` size, the
//! payload) holding the asset names, the placements and 256 `MCNK` terrain chunks.
//!
//! The tile is read the way the reference reads it (`0x6c2010`): never by walking record sizes,
//! but through `MHDR`'s offsets to the top-level records and `MCIN`'s entries to the `MCNK`s.
use std::collections::HashMap;
use std::io::Cursor;
use benilla_bytes::{chunks, ByteExt};
use benilla_bytes::ByteExt;
/// A terrain vertex normal: three signed bytes in WoW's X, Y, Z, the order of the file
/// (`CMapChunk::LoadNormals`, `0x6aff10`).
@@ -131,6 +135,13 @@ pub struct MclqChunk {
/// Searing Gorge's. The reference's header walk copies it to its chunk flag `0x40` (`0x6af5f0`).
pub const MCNK_IMPASSABLE: u32 = 0x2;
/// MCNK header flag bit 15: the chunk's alpha maps are stored whole, 64 × 64. Clear, a map holds 63
/// useful values per 64-value row and the decoder copies column 62 into 63 and row 62 into 63
/// (`0x6b0a60` picks `0x6b0ac0` on the bit, else `0x6b0bf0`). That holds at `shadowLevel` 0; with
/// the global `0xc63284` non-zero the dispatcher takes the 32 × 32 decoders `0x6b0b30` and
/// `0x6b0c80` instead (`cmp eax,2` at `0x6b0a72` is always 2, from its one caller `0x6b02d9`).
pub const MCNK_ALPHA_64: u32 = 0x8000;
/// The MCNK header fields the renderer reads. The names mislead: `pred_tex` and `no_effect_doodad`
/// are the two halves of the predominant-texture map at `+0x40`, and `unknown_8bytes` is the real
/// noEffectDoodad at `+0x50`; the consumer reassembles them.
@@ -153,6 +164,11 @@ impl McnkHeader {
pub fn impassable(&self) -> bool {
self.flags & MCNK_IMPASSABLE != 0
}
/// Whether the raw header sets [`MCNK_ALPHA_64`]: the alpha maps need no edge copy.
pub fn alpha_maps_whole(&self) -> bool {
self.flags & MCNK_ALPHA_64 != 0
}
}
/// One MCNK terrain chunk.
@@ -166,6 +182,12 @@ pub struct McnkChunk {
pub shadow: Option<McshChunk>,
/// The MCLQ liquid blocks in disk order: none on a dry chunk, two at a river mouth.
pub liquids: Vec<MclqChunk>,
/// MCRF's first part: indices into the tile's MDDF, the doodads that overlap this chunk. The
/// count is the header's `+0x10`, not the record's size (`0x6af68e`).
pub doodad_refs: Vec<u32>,
/// MCRF's second part, after the doodad refs: indices into MODF, the count the header's
/// `+0x38`.
pub object_refs: Vec<u32>,
}
/// MDDF: a placed M2 doodad (36 bytes).
@@ -199,8 +221,17 @@ pub struct RootAdt {
pub textures: Vec<String>,
pub models: Vec<String>,
pub wmos: Vec<String>,
/// The MDDF entries some chunk's MCRF references, in MDDF order, one per unique id. The
/// reference makes a doodad only from a chunk's ref (`0x6b00f0` → `0x694bc0`), and nothing else
/// reads MDDF, so an unreferenced entry never exists. Its dedup is global, in a hash by unique
/// id (`0xca7dc0`, `0x694c08`): the first def made wins, in chunk load order. Here the dedup is
/// per tile, first reference in MCIN order; the streamer's by-uid refcount does the rest.
pub doodad_placements: Vec<DoodadPlacement>,
/// The MODF entries some chunk's MCRF references, by the same rule (`0x6b00f0` → `0x695650`,
/// hash `0xca7d98`, `0x695698`).
pub wmo_placements: Vec<WmoPlacement>,
/// The tile's chunks in MCIN order (row · 16 + column). An entry the file cannot hold an MCNK
/// at, offset 0 included, is skipped: the reference does not check and would read garbage.
pub mcnk_chunks: Vec<McnkChunk>,
}
@@ -238,42 +269,82 @@ fn cstring_at(blob: &[u8], offset: usize) -> String {
String::from_utf8_lossy(&blob[offset.min(blob.len())..end]).into_owned()
}
/// The data of the top-level record `offset` bytes from MHDR's data (`0x6c2010` forms each table
/// as `lea [offset + mhdr + 8]`), cut to the record's own size and the file's end: the reference
/// reads MTEX's, MDDF's and MODF's size and indexes the rest unbounded. A record that does not fit
/// the file, or an offset of 0, is empty.
fn mhdr_record(b: &[u8], mhdr: usize, offset: u32) -> &[u8] {
if offset == 0 {
return &[];
}
let record = mhdr + offset as usize;
let (Some(size), Some(rest)) = (b.u32_at(record + 4), b.get(record + 8..)) else {
return &[];
};
&rest[..(size as usize).min(rest.len())]
}
/// Parse a vanilla monolithic ADT; the cursor's position is ignored.
///
/// The reference finds the records without a size walk (`0x6c2010`): MHDR's data starts at
/// `16 + the version record's size` (`0x6c2021`), its offsets from there name MCIN, MTEX, MMDX,
/// MMID, MWMO, MWID, MDDF and MODF (`+0x04` to `+0x20`), and no tag is read. Chunk row · 16 +
/// column is the MCNK at its MCIN entry's file offset (`0x6c2880`, `0x6c299d`); the entry's size is
/// never read and its third dword is the client's own.
pub fn parse_adt(cursor: &mut Cursor<&[u8]>) -> Result<ParsedAdt> {
let b: &[u8] = cursor.get_ref();
let mut textures = Vec::new();
let mut mmdx = Vec::new();
let mut mmid: Vec<u32> = Vec::new();
let mut mwmo = Vec::new();
let mut mwid: Vec<u32> = Vec::new();
let mut doodad_placements = Vec::new();
let mut wmo_placements = Vec::new();
let mut mcnk_chunks = Vec::new();
let version_size = b.u32_at(4).ok_or(Error::Truncated("MVER"))? as usize;
let mhdr = 16 + version_size;
let field = |at: usize| b.u32_at(mhdr + at).ok_or(Error::Truncated("MHDR"));
let (mcin, mtex, mmdx, mmid) = (field(0x04)?, field(0x08)?, field(0x0c)?, field(0x10)?);
let (mwmo, mwid, mddf, modf) = (field(0x14)?, field(0x18)?, field(0x1c)?, field(0x20)?);
let table = |offset| mhdr_record(b, mhdr, offset);
for (magic, data) in chunks(b) {
match &magic {
b"XETM" => textures = split_strings(data), // MTEX
b"XDMM" => mmdx = data.to_vec(), // MMDX (blob)
b"DIMM" => mmid = read_u32_list(data, "MMID")?, // MMID (offsets)
b"OMWM" => mwmo = data.to_vec(), // MWMO (blob)
b"DIWM" => mwid = read_u32_list(data, "MWID")?, // MWID (offsets)
b"FDDM" => doodad_placements = read_mddf(data)?, // MDDF
b"FDOM" => wmo_placements = read_modf(data)?, // MODF
b"KNCM" => mcnk_chunks.push(read_mcnk(data)?), // MCNK
_ => {}
let textures = split_strings(table(mtex));
let mmdx = table(mmdx);
let mmid = read_u32_list(table(mmid), "MMID")?;
let mwmo = table(mwmo);
let mwid = read_u32_list(table(mwid), "MWID")?;
let doodad_entries = read_mddf(table(mddf))?;
let wmo_entries = read_modf(table(modf))?;
// MCIN's data, 256 sixteen-byte entries; its own size is not read.
let mcin_data = (mcin != 0)
.then(|| b.get(mhdr + mcin as usize + 8..))
.flatten()
.unwrap_or(&[]);
let mut mcnk_chunks = Vec::new();
for entry in mcin_data.as_chunks::<16>().0.iter().take(256) {
// The entry's first dword is the MCNK record's file offset (`0x6c299a`). The reference
// does not check it (an offset of 0 would read the file's start as an MCNK); a malformed
// entry that no MCNK header fits at is skipped here, the rest of the tile kept.
let record = entry.u32_at(0).ok_or(Error::Truncated("MCIN"))? as usize;
if record == 0 {
continue;
}
if let Some(chunk) = b.get(record + 8..).and_then(|d| read_mcnk(d).ok()) {
mcnk_chunks.push(chunk);
}
}
// MDDF/MODF `name_id` indexes the MMID/MWID offset tables.
let models = mmid
.iter()
.map(|&o| cstring_at(&mmdx, o as usize))
.collect();
let wmos = mwid
.iter()
.map(|&o| cstring_at(&mwmo, o as usize))
.collect();
let models = mmid.iter().map(|&o| cstring_at(mmdx, o as usize)).collect();
let wmos = mwid.iter().map(|&o| cstring_at(mwmo, o as usize)).collect();
// The placements are the ones a chunk refers to (`0x6b00f0`), one def per unique id.
let doodad_placements = referenced(
&doodad_entries,
|p| p.unique_id,
&mcnk_chunks,
|c| &c.doodad_refs,
);
let wmo_placements = referenced(
&wmo_entries,
|p| p.unique_id,
&mcnk_chunks,
|c| &c.object_refs,
);
Ok(ParsedAdt::Root(Box::new(RootAdt {
textures,
@@ -285,6 +356,27 @@ pub fn parse_adt(cursor: &mut Cursor<&[u8]>) -> Result<ParsedAdt> {
})))
}
/// The `entries` some chunk's `refs` name (an index out of range names none), in entry order, one
/// per unique id: the first reference in MCIN order wins. The client's table is global and its
/// first def made wins, in chunk load order (`0x694bc0`, `0x695650`); with this per-tile pick, the
/// streamer's by-uid refcount gives the same.
fn referenced<T: Clone>(
entries: &[T],
unique_id: impl Fn(&T) -> u32,
chunks: &[McnkChunk],
refs: impl Fn(&McnkChunk) -> &[u32],
) -> Vec<T> {
let mut first: HashMap<u32, usize> = HashMap::new();
for &i in chunks.iter().flat_map(&refs) {
if let Some(e) = entries.get(i as usize) {
first.entry(unique_id(e)).or_insert(i as usize);
}
}
let mut picked: Vec<usize> = first.into_values().collect();
picked.sort_unstable();
picked.into_iter().map(|i| entries[i].clone()).collect()
}
/// Split a NUL-separated, NUL-terminated string blob, dropping empties.
fn split_strings(blob: &[u8]) -> Vec<String> {
blob.split(|&c| c == 0)
@@ -392,6 +484,8 @@ fn read_mcnk(data: &[u8]) -> Result<McnkChunk> {
alpha: None,
shadow: None,
liquids: Vec::new(),
doodad_refs: Vec::new(),
object_refs: Vec::new(),
};
// Sub-chunks sit at the header's offsets, not in sequence (AhnQiraj's are not contiguous).
@@ -494,6 +588,26 @@ fn read_mcnk(data: &[u8]) -> Result<McnkChunk> {
chunk.liquids = read_mclq_blocks(slice(p, size_liquid), chunk.header.flags);
}
// MCRF: `+0x10` doodad refs then `+0x38` object refs, one dword each, from the offset at `+0x20`
// (`0x6af9a7` reads `+0x20` into `+0xf28`; `0x6af68b`, `0x6af68e`). The counts are the header's; the record's own size is not read.
let doodad_count = h.u32_at(0x10).ok_or(Error::Truncated("MCNK header"))? as usize;
let object_count = h.u32_at(0x38).ok_or(Error::Truncated("MCNK header"))? as usize;
if doodad_count + object_count > 0 {
if let Some((p, _)) = locate(
h.u32_at(0x20).ok_or(Error::Truncated("MCNK header"))?,
b"FRCM",
) {
let refs = data[p..]
.as_chunks::<4>()
.0
.iter()
.map(|r| u32::from_le_bytes(*r));
let mut refs = refs.take(doodad_count + object_count);
chunk.doodad_refs = refs.by_ref().take(doodad_count).collect();
chunk.object_refs = refs.collect();
}
}
Ok(chunk)
}
@@ -562,13 +676,92 @@ mod tests {
v
}
/// An MCNK header and one MCVT after it, the `0x14` offset pointing at MCVT's magic, 128 in.
fn mcnk_with_mcvt() -> Vec<u8> {
let mut data = vec![0u8; 128];
data[0x14..0x18].copy_from_slice(&128u32.to_le_bytes());
/// An MCNK payload: the 128-byte header with `flags`, MCVT (heights 0, 1, 2, …) and MCRF, the
/// sub-record offsets counted from the MCNK's magic as the files do, so 8 more than in the payload.
fn mcnk(flags: u32, doodad_refs: &[u32], object_refs: &[u32]) -> Vec<u8> {
let heights: Vec<u8> = (0..145u32).flat_map(|i| (i as f32).to_le_bytes()).collect();
data.extend(chunk(b"TVCM", &heights));
data
let mcvt = chunk(b"TVCM", &heights);
let refs: Vec<u8> = doodad_refs
.iter()
.chain(object_refs)
.flat_map(|r| r.to_le_bytes())
.collect();
let mcrf = chunk(b"FRCM", &refs);
let mut h = vec![0u8; 128];
let mut put = |at: usize, v: u32| h[at..at + 4].copy_from_slice(&v.to_le_bytes());
put(0x00, flags);
put(0x10, doodad_refs.len() as u32);
put(0x14, 8 + 128);
put(0x20, 8 + 128 + mcvt.len() as u32);
put(0x38, object_refs.len() as u32);
[h, mcvt, mcrf].concat()
}
/// A tile as the reference lays it out: MVER, MHDR (64 bytes), MCIN, the tables, then the MCNK
/// records, MHDR's offsets and MCIN's entries filled in. An MCIN entry's size and third dword
/// are junk, as the reference reads neither.
#[derive(Default)]
struct Tile {
mtex: Vec<u8>,
mmdx: Vec<u8>,
mmid: Vec<u8>,
mwmo: Vec<u8>,
mwid: Vec<u8>,
mddf: Vec<u8>,
modf: Vec<u8>,
/// `(MCIN index, payload)` in file order.
mcnks: Vec<(usize, Vec<u8>)>,
/// Noggit's convention: an MCNK's size counts its own 8-byte header.
mcnk_size_counts_header: bool,
}
impl Tile {
fn bytes(&self) -> Vec<u8> {
let mut out = chunk(b"REVM", &18u32.to_le_bytes());
out.extend(chunk(b"RDHM", &[0u8; 64]));
let mhdr_data = out.len() - 64;
let mut offsets = [0u32; 8]; // MCIN, MTEX, MMDX, MMID, MWMO, MWID, MDDF, MODF
let mcin_at = out.len() + 8;
let tables = [
(b"NICM", vec![0u8; 4096]),
(b"XETM", self.mtex.clone()),
(b"XDMM", self.mmdx.clone()),
(b"DIMM", self.mmid.clone()),
(b"OMWM", self.mwmo.clone()),
(b"DIWM", self.mwid.clone()),
(b"FDDM", self.mddf.clone()),
(b"FDOM", self.modf.clone()),
];
for (n, (magic, payload)) in tables.iter().enumerate() {
offsets[n] = (out.len() - mhdr_data) as u32;
out.extend(chunk(magic, payload));
}
for (n, o) in offsets.iter().enumerate() {
let at = mhdr_data + 4 + n * 4;
out[at..at + 4].copy_from_slice(&o.to_le_bytes());
}
for (index, payload) in &self.mcnks {
let at = mcin_at + index * 16;
let record_at = out.len() as u32;
out[at..at + 4].copy_from_slice(&record_at.to_le_bytes());
out[at + 4..at + 16].fill(0xff);
let mut record = chunk(b"KNCM", payload);
if self.mcnk_size_counts_header {
record[4..8].copy_from_slice(&(payload.len() as u32 + 8).to_le_bytes());
}
out.extend(record);
}
out
}
}
/// A tile of one MCNK, `payload`, at MCIN index 0.
fn one_mcnk(payload: Vec<u8>) -> Vec<u8> {
Tile {
mcnks: vec![(0, payload)],
..Tile::default()
}
.bytes()
}
fn parse(bytes: &[u8]) -> Result<ParsedAdt> {
@@ -576,27 +769,24 @@ mod tests {
}
#[test]
fn root_parses_a_minimal_well_formed_chunk_walk() {
let mtex = b"tex1.blp\0tex2.blp\0";
let mmdx = b"model.m2\0";
let mmid = 0u32.to_le_bytes();
let mwmo = b"world.wmo\0";
let mwid = 0u32.to_le_bytes();
fn root_parses_a_minimal_well_formed_tile() {
let mut mddf = vec![0u8; 36];
mddf[32..34].copy_from_slice(&7u16.to_le_bytes()); // scale
let mut modf = vec![0u8; 64];
modf[58..60].copy_from_slice(&3u16.to_le_bytes()); // doodad_set
let mut b = chunk(b"XETM", mtex);
b.extend(chunk(b"XDMM", mmdx));
b.extend(chunk(b"DIMM", &mmid));
b.extend(chunk(b"OMWM", mwmo));
b.extend(chunk(b"DIWM", &mwid));
b.extend(chunk(b"FDDM", &mddf));
b.extend(chunk(b"FDOM", &modf));
b.extend(chunk(b"KNCM", &mcnk_with_mcvt()));
let b = Tile {
mtex: b"tex1.blp\0tex2.blp\0".to_vec(),
mmdx: b"model.m2\0".to_vec(),
mmid: 0u32.to_le_bytes().to_vec(),
mwmo: b"world.wmo\0".to_vec(),
mwid: 0u32.to_le_bytes().to_vec(),
mddf,
modf,
mcnks: vec![(0, mcnk(0, &[0], &[0]))],
..Tile::default()
}
.bytes();
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert_eq!(root.textures, vec!["tex1.blp", "tex2.blp"]);
@@ -616,18 +806,134 @@ mod tests {
assert_eq!(heights[1], 1.0);
}
/// The reference never reads an MCNK's size (`0x6c2880` forms the record from its MCIN entry's
/// offset alone, `0x6c299d`), so Noggit's sizes, which count the record's header, lose nothing.
#[test]
fn an_mcnk_size_that_counts_its_header_loses_no_chunk() {
let b = Tile {
mcnks: vec![(0, mcnk(0, &[], &[])), (1, mcnk(0, &[], &[]))],
mcnk_size_counts_header: true,
..Tile::default()
}
.bytes();
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert_eq!(root.mcnk_chunks.len(), 2);
assert_eq!(
root.mcnk_chunks[1].heights.as_ref().unwrap().heights.len(),
145
);
}
/// Chunks come from MCIN's entries, row · 16 + column, wherever their records lie: out of order,
/// or with bytes between that no size accounts for.
#[test]
fn mcnks_are_found_by_their_mcin_entries_not_by_walking_sizes() {
let at = |x: u32| {
let mut m = mcnk(0, &[], &[]);
m[4..8].copy_from_slice(&x.to_le_bytes()); // the header's own index_x
m
};
let tile = Tile {
mcnks: vec![(255, at(255)), (0, at(0)), (17, at(17))],
..Tile::default()
};
let ParsedAdt::Root(root) = parse(&tile.bytes()).expect("parses");
let order: Vec<u32> = root.mcnk_chunks.iter().map(|c| c.header.index_x).collect();
assert_eq!(order, [0, 17, 255], "MCIN order");
// Five bytes before the last record; its MCIN entry moves with it.
let mut b = tile.bytes();
let last = b.windows(4).rposition(|w| w == b"KNCM").unwrap();
b.splice(last..last, [0xaa; 5]);
let entry = 20 + 64 + 8 + 17 * 16;
b[entry..entry + 4].copy_from_slice(&(last as u32 + 5).to_le_bytes());
let ParsedAdt::Root(root) = parse(&b).expect("parses with stray bytes");
assert_eq!(root.mcnk_chunks.len(), 3);
assert_eq!(root.mcnk_chunks[1].header.index_x, 17);
}
/// The reference creates a doodad or an object only from a chunk's MCRF (`0x6b00f0`), reading
/// the counts from the MCNK header (`0x6af68b`) and making one def per unique id (`0x694bc0`,
/// `0x695650`); nothing else reads MDDF or MODF.
#[test]
fn placements_are_the_ones_the_chunks_refer_to() {
let entry = |size: usize, uid: u32| {
let mut e = vec![0u8; size];
e[4..8].copy_from_slice(&uid.to_le_bytes());
e
};
// Unique ids 7, 8, 9 and 7 again.
let mddf: Vec<u8> = [7, 8, 9, 7].iter().flat_map(|&u| entry(36, u)).collect();
let modf: Vec<u8> = [7, 8, 9, 7].iter().flat_map(|&u| entry(64, u)).collect();
// Chunk 0 names doodad 0 and object 1; chunk 1 names 3 (a second 7), 2, and one out of
// range. Doodad 1 (id 8) is named by no chunk: chunk 0's second ref is past its header count.
let mut c0 = mcnk(0, &[0, 1], &[0, 1]);
c0[0x10..0x14].copy_from_slice(&1u32.to_le_bytes());
c0[0x38..0x3c].copy_from_slice(&1u32.to_le_bytes());
// With a count of 1 each, the object ref is the second dword, which held 1.
let b = Tile {
mddf,
modf,
mcnks: vec![(0, c0), (1, mcnk(0, &[3, 2, 99], &[3, 2, 99]))],
..Tile::default()
}
.bytes();
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert_eq!(root.mcnk_chunks[0].doodad_refs, [0]);
assert_eq!(
root.mcnk_chunks[0].object_refs,
[1],
"after the doodad refs"
);
let doodads: Vec<u32> = root.doodad_placements.iter().map(|d| d.unique_id).collect();
assert_eq!(
doodads,
[7, 9],
"8 is unreferenced and the second 7 is the same def"
);
// Chunk 0 names MODF entry 1 (id 8); chunk 1 names 3 (id 7, the first ref to it) and 2.
let wmos: Vec<u32> = root.wmo_placements.iter().map(|w| w.unique_id).collect();
assert_eq!(wmos, [8, 9, 7], "in MODF order");
}
/// Flag bit 15 is read off the header; `0x6b0a60` picks the 64 × 64 decoder on it.
#[test]
fn mcnk_flag_bit_15_marks_whole_alpha_maps() {
let h = |flags| {
let ParsedAdt::Root(root) = parse(&one_mcnk(mcnk(flags, &[], &[]))).unwrap();
root.mcnk_chunks[0].header.clone()
};
assert!(h(0x8000).alpha_maps_whole());
assert!(!h(0x7fff).alpha_maps_whole());
}
#[test]
fn partial_trailing_mddf_record_is_dropped_not_a_panic() {
// Shorter than the 36-byte stride: `as_chunks` drops the remainder.
let b = chunk(b"FDDM", &[0u8; 10]);
let b = Tile {
mddf: vec![0u8; 10],
..Tile::default()
}
.bytes();
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert!(root.doodad_placements.is_empty());
}
#[test]
fn truncated_mcnk_header_is_an_error_not_a_panic() {
let b = chunk(b"KNCM", &[0u8; 40]);
assert!(matches!(parse(&b), Err(Error::Truncated("MCNK header"))));
fn an_mcin_entry_no_mcnk_header_fits_at_is_skipped_not_fatal() {
// The only entry points at 40 bytes; the tile parses and holds no chunk.
let b = one_mcnk(vec![0u8; 40]);
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert!(root.mcnk_chunks.is_empty());
// An entry past the file's end, beside a good chunk, loses only itself.
let mut b = one_mcnk(mcnk(0, &[], &[]));
let entry = 20 + 64 + 8 + 16;
b[entry..entry + 4].copy_from_slice(&u32::MAX.to_le_bytes());
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert_eq!(root.mcnk_chunks.len(), 1);
}
#[test]
@@ -635,14 +941,14 @@ mod tests {
// ofs = 3: the minus-8 candidate underflows and 3 holds no magic.
let mut data = vec![0u8; 128];
data[0x14..0x18].copy_from_slice(&3u32.to_le_bytes());
let b = chunk(b"KNCM", &data);
let b = one_mcnk(data);
let ParsedAdt::Root(root) = parse(&b).expect("parses despite corrupt MCVT offset");
assert!(root.mcnk_chunks[0].heights.is_none());
// ofs points past the end of the buffer entirely.
let mut data = vec![0u8; 128];
data[0x14..0x18].copy_from_slice(&u32::MAX.to_le_bytes());
let b = chunk(b"KNCM", &data);
let b = one_mcnk(data);
let ParsedAdt::Root(root) = parse(&b).expect("parses despite out-of-range MCVT offset");
assert!(root.mcnk_chunks[0].heights.is_none());
}
@@ -653,7 +959,7 @@ mod tests {
data[0x60..0x64].copy_from_slice(&128u32.to_le_bytes()); // MCLQ offset
data[0x64..0x68].copy_from_slice(&4u32.to_le_bytes()); // size_liquid, far short of NEED
data.extend(chunk(b"QLCM", &[0u8; 4]));
let b = chunk(b"KNCM", &data);
let b = one_mcnk(data);
let ParsedAdt::Root(root) = parse(&b).expect("parses");
assert!(root.mcnk_chunks[0].liquids.is_empty());
}
@@ -668,7 +974,7 @@ mod tests {
// The MCNR offset counts from the MCNK's magic, so the 8-byte chunk header is in it.
data[0x18..0x1c].copy_from_slice(&(8u32 + 128).to_le_bytes());
data.extend(chunk(b"RNCM", &mcnr));
let b = chunk(b"KNCM", &data);
let b = one_mcnk(data);
let ParsedAdt::Root(root) = parse(&b).expect("parses");
let normals = &root.mcnk_chunks[0].normals.as_ref().expect("MCNR").normals;
@@ -708,7 +1014,7 @@ mod tests {
data[0x60..0x64].copy_from_slice(&128u32.to_le_bytes());
data[0x64..0x68].copy_from_slice(&((payload.len() + 8) as u32).to_le_bytes());
data.extend(chunk(b"QLCM", &payload));
let b = chunk(b"KNCM", &data);
let b = one_mcnk(data);
let ParsedAdt::Root(root) = parse(&b).expect("parses");
let liquids = &root.mcnk_chunks[0].liquids;
@@ -211,12 +211,11 @@ fn fragment(in: TerrainVsOut) -> @location(0) vec4<f32> {
// `terrainp_s.bls`, which reads the MCSH bit from the blend texture's alpha (1 lit, 0 shadowed)
// and uses it twice, below; with them off the reference draws a separate ambient-tint overlay.
// Our `shadow_array` is 255 shadowed, 0 lit; a chunk with no MCSH map (`uv_b.y < 0`) is lit.
var shadow_lit = 1.0;
// The sample runs unconditionally, at layer 0 for a chunk with none, and `si` selects after:
// implicit derivatives need uniform control flow, and `si` is a varying.
let si = in.uv_b.y;
if (si >= 0.0) {
let mcsh = textureSample(shadow_array, splat_samp, auv, i32(round(si))).r;
shadow_lit = 1.0 - mcsh;
}
let mcsh = textureSample(shadow_array, splat_samp, auv, max(i32(round(si)), 0)).r;
let shadow_lit = select(1.0, 1.0 - mcsh, si >= 0.0);
// The `terrainp_s.bls` combine, then an LDR clamp:
// diffuse = tex · primary · (0.3·shadow + 0.7) (a flat −30% in shadow, no tint)
+90 -8
View File
@@ -394,10 +394,13 @@ pub fn adt_to_tile_mesh(adt_bytes: &[u8]) -> Result<TileMesh> {
let layer_effect_ids: Vec<u32> = layers.iter().map(|(_, e)| *e).collect();
let base_texture = layer_textures.first().cloned();
// Layers 1..3 into one RGBA map; 1.12 alpha is 4-bit, last row and column duplicated.
// Layers 1..3 into one RGBA map; 1.12 alpha is 4-bit. A map is stored 63 × 63 with its last
// column and row copied, unless the chunk's flag bit 15 says 64 × 64 (`0x6b0a60`).
let alpha_map = (layer_textures.len() > 1).then(|| {
CombinedAlphaMap::new(
mcnk, /* has_big_alpha */ false, /* fix_alpha */ true,
mcnk,
/* has_big_alpha */ false,
/* fix_alpha */ !mcnk.header.alpha_maps_whole(),
)
.as_slice()
.to_vec()
@@ -673,19 +676,98 @@ mod tests {
}
}
fn chunk_record(magic: &[u8; 4], payload: &[u8]) -> Vec<u8> {
[&magic[..], &(payload.len() as u32).to_le_bytes(), payload].concat()
}
/// A tile as the files lay one out: MVER, MHDR, MCIN with entry 0 pointing at the one MCNK
/// `mcnk` (its sub-record offsets counted from its magic), MTEX naming `a.blp` and `b.blp`.
fn one_chunk_tile(mcnk: &[u8]) -> Vec<u8> {
let mut out = chunk_record(b"REVM", &18u32.to_le_bytes());
out.extend(chunk_record(b"RDHM", &[0u8; 64]));
let mhdr_data = 20;
let mcin = out.len() - mhdr_data;
out.extend(chunk_record(b"NICM", &[0u8; 4096]));
let mtex = out.len() - mhdr_data;
out.extend(chunk_record(b"XETM", b"a.blp\0b.blp\0"));
out[mhdr_data + 0x04..mhdr_data + 0x08].copy_from_slice(&(mcin as u32).to_le_bytes());
out[mhdr_data + 0x08..mhdr_data + 0x0c].copy_from_slice(&(mtex as u32).to_le_bytes());
let mcin_entry = mhdr_data + mcin + 8;
let record = out.len() as u32;
out[mcin_entry..mcin_entry + 4].copy_from_slice(&record.to_le_bytes());
out.extend(chunk_record(b"KNCM", mcnk));
out
}
/// One tile of a single flat MCNK (heights 0 over a base of 50) whose every MCNR normal is the
/// bytes `n`.
fn flat_tile(n: [u8; 3]) -> Vec<u8> {
let chunk = |magic: &[u8; 4], payload: &[u8]| {
[&magic[..], &(payload.len() as u32).to_le_bytes(), payload].concat()
};
let mcvt = chunk(b"TVCM", &[0u8; 145 * 4]);
let mcnr = chunk(b"RNCM", &[n.repeat(145), vec![0; 13]].concat());
let mcvt = chunk_record(b"TVCM", &[0u8; 145 * 4]);
let mcnr = chunk_record(b"RNCM", &[n.repeat(145), vec![0; 13]].concat());
let mut h = vec![0u8; 128];
h[0x14..0x18].copy_from_slice(&(8u32 + 128).to_le_bytes()); // MCVT, from the MCNK's magic
h[0x18..0x1c].copy_from_slice(&(8 + 128 + mcvt.len() as u32).to_le_bytes()); // MCNR
h[0x70..0x74].copy_from_slice(&50f32.to_le_bytes()); // base height
chunk(b"KNCM", &[h, mcvt, mcnr].concat())
one_chunk_tile(&[h, mcvt, mcnr].concat())
}
/// A tile of one flat MCNK with MCNK `flags`, a base layer and one alpha layer: nibble 15 in
/// column 63 and row 63, 0 elsewhere.
fn alpha_edge_tile(flags: u32) -> Vec<u8> {
let alpha: Vec<u8> = (0..64)
.flat_map(|y| {
(0..32).map(move |k| {
if y == 63 {
0xff
} else if k == 31 {
0xf0
} else {
0
}
})
})
.collect();
let mut layers = vec![0u8; 32];
layers[16..20].copy_from_slice(&1u32.to_le_bytes()); // layer 1: texture 1
layers[20..24].copy_from_slice(&0x100u32.to_le_bytes()); // …with an alpha map at MCAL offset 0
let mcvt = chunk_record(b"TVCM", &[0u8; 145 * 4]);
let mcly = chunk_record(b"YLCM", &layers);
let mcal = chunk_record(b"LACM", &alpha);
let mut h = vec![0u8; 128];
let at = |len: usize| (8 + 128 + len) as u32;
let mut put = |o: usize, v: u32| h[o..o + 4].copy_from_slice(&v.to_le_bytes());
put(0x00, flags);
put(0x14, at(0)); // MCVT
put(0x1c, at(mcvt.len())); // MCLY
put(0x24, at(mcvt.len() + mcly.len())); // MCAL
put(0x28, mcal.len() as u32); // its size
one_chunk_tile(&[h, mcvt, mcly, mcal].concat())
}
/// Layer 1's weight at `(x, y)` in the map handed to the terrain shader.
fn alpha_weight(flags: u32, x: usize, y: usize) -> u8 {
adt_to_tile_mesh(&alpha_edge_tile(flags)).unwrap().chunks[0]
.alpha_map
.as_ref()
.unwrap()[(y * 64 + x) * 4]
}
/// MCNK flag bit 15 set: the map is stored whole at 64 × 64 (`0x6b0ac0`), so the authored
/// column 63 and row 63 stand. Clear: 63 × 63, column 62 and row 62 copied over them
/// (`0x6b0bf0`); the dispatcher `0x6b0a60` picks on the bit.
#[test]
fn alpha_edge_follows_mcnk_flag_bit_15() {
// nibble 15 is full coverage, read as n / 15.
for (x, y) in [(63, 10), (10, 63), (63, 63)] {
assert_eq!(alpha_weight(0x8000, x, y), 255, "whole map, ({x}, {y})");
assert_eq!(
alpha_weight(0, x, y),
0,
"63 x 63 map, ({x}, {y}) copied from 62"
);
}
assert_eq!(alpha_weight(0x8000, 62, 10), 0);
assert_eq!(alpha_weight(0, 62, 10), 0);
}
/// `CMapChunk::LoadNormals` (`0x6aff10`) keeps the MCNR bytes in file order, X, Y, Z, so the
+65
View File
@@ -1375,3 +1375,68 @@ fn resolves_the_ghost_skybox_everywhere() {
.expect("the ghost sky model the DBC names must be readable from the chain");
assert_eq!(&bytes[..4], b"MD20", "DeathClouds is an M2");
}
/// An install tile's size walk and the reference's MHDR and MCIN reading (`0x6c2010`, `0x6c2880`)
/// agree: the same 256 chunks in the same order, their headers and heights as the walk's payloads
/// hold them, and every doodad listed, as every one is referenced. AhnQiraj's sub-records are not
/// contiguous.
#[test]
fn install_tiles_read_through_mhdr_and_mcin_as_the_size_walk_read_them() {
use benilla_bytes::{chunks, ByteExt};
let data = benilla_formats::wow_data_or_skip!();
let mut chain = open_chain(&data).expect("open vanilla patch chain");
for path in [
"World\\Maps\\Azeroth\\Azeroth_32_48.adt",
"World\\Maps\\Kalimdor\\Kalimdor_31_32.adt",
"World\\Maps\\AhnQiraj\\AhnQiraj_29_49.adt",
] {
let bytes = chain.read_file(path).expect(path);
let benilla_adt::ParsedAdt::Root(root) =
benilla_adt::parse_adt(&mut std::io::Cursor::new(&bytes[..])).expect("parse the tile");
let walked = |magic: &[u8; 4]| -> Vec<&[u8]> {
chunks(&bytes)
.filter(|(m, _)| m == magic)
.map(|(_, d)| d)
.collect()
};
let mcnks = walked(b"KNCM");
assert_eq!(mcnks.len(), 256, "{path}");
assert_eq!(root.mcnk_chunks.len(), 256, "{path}");
for (i, (raw, chunk)) in mcnks.iter().zip(&root.mcnk_chunks).enumerate() {
let h = &chunk.header;
assert_eq!(h.flags, raw.u32_at(0).unwrap(), "{path} chunk {i} flags");
assert_eq!(
h.index_x,
raw.u32_at(4).unwrap(),
"{path} chunk {i} index_x"
);
assert_eq!(
h.index_y,
raw.u32_at(8).unwrap(),
"{path} chunk {i} index_y"
);
assert_eq!(
h.area_id,
raw.u32_at(0x34).unwrap(),
"{path} chunk {i} area"
);
// MCVT's offset counts from the MCNK's magic; its data is 8 bytes on.
let mcvt = raw.u32_at(0x14).unwrap() as usize;
let heights = &chunk.heights.as_ref().expect("MCVT").heights;
for (n, &height) in heights.iter().enumerate().take(145) {
assert_eq!(
height,
raw.f32_at(mcvt + n * 4).unwrap(),
"{path} chunk {i}"
);
}
}
assert_eq!(
root.doodad_placements.len(),
walked(b"FDDM")[0].len() / 36,
"{path}"
);
}
}
+71
View File
@@ -55,4 +55,75 @@ mod tests {
// One per `embedded_asset!` line in `plugin`.
assert_eq!(found, 8, "the engine's shader set changed size");
}
/// The lines of `src` that take an implicit derivative (`textureSample`, `textureSampleBias`,
/// `textureSampleCompare`, and the explicit builtins `dpdx`, `dpdy`, `fwidth` with their
/// `Coarse` and `Fine` variants) below a function body's top level. WGSL allows them only in
/// uniform control flow, and a branch on a fragment input is not. This reads text: it does not
/// follow helper calls, a sample after a `return` inside a branch, or `/* */` comments.
fn derivatives_in_nested_blocks(src: &str) -> Vec<usize> {
const IMPLICIT: [&str; 12] = [
"textureSample",
"textureSampleBias",
"textureSampleCompare",
"dpdx",
"dpdxCoarse",
"dpdxFine",
"dpdy",
"dpdyCoarse",
"dpdyFine",
"fwidth",
"fwidthCoarse",
"fwidthFine",
];
let mut depth = 0usize;
let mut hits = Vec::new();
for (n, line) in src.lines().enumerate() {
let code = line.split("//").next().unwrap_or("");
let mut word = String::new();
for c in code.chars().chain(std::iter::once(' ')) {
if c.is_alphanumeric() || c == '_' {
word.push(c);
continue;
}
if depth > 1 && IMPLICIT.contains(&word.as_str()) {
hits.push(n + 1);
}
word.clear();
match c {
'{' => depth += 1,
'}' => depth = depth.saturating_sub(1),
_ => {}
}
}
}
hits
}
/// naga's check for this is compiled out of the fragment stage
/// (`DISABLE_UNIFORMITY_REQ_FOR_FRAGMENT_STAGE`, naga 27 and 30), so these shaders validate
/// natively and fail in a browser's compiler. This reads the text instead: no sample that
/// takes implicit derivatives sits in a nested block, where a branch on a fragment input
/// would make it non-uniform. Hoist the sample and `select` after.
#[test]
fn derivative_samples_stay_in_uniform_control_flow() {
let bad = "fn fragment(in: V) -> vec4<f32> {\n if (in.flag != 0u) {\n let c = textureSampleBias(t, s, in.uv, 0.0);\n }\n let d = textureSample(t, s, in.uv);\n}\n";
assert_eq!(
derivatives_in_nested_blocks(bad),
[3],
"the scanner finds the nested sample"
);
let root = env!("CARGO_MANIFEST_DIR");
for path in [
"/src/shaders/static_gx.wgsl",
"/../benilla-assets/src/shaders/terrain.wgsl",
] {
let src = std::fs::read_to_string(format!("{root}{path}")).expect(path);
assert_eq!(
derivatives_in_nested_blocks(&src),
Vec::<usize>::new(),
"{path}"
);
}
}
}
+20 -17
View File
@@ -282,25 +282,28 @@ fn fragment(in: GxVsOut) -> @location(0) vec4<f32> {
discard;
}
// The sampler follows the wrap flags; a mixed batch clamps its clamped axis a half texel in.
// Every sample runs unconditionally: implicit derivatives need uniform control flow.
var base = vec4<f32>(1.0);
// Every sample runs unconditionally, then `textured` selects: implicit derivatives need
// uniform control flow, and `word` is a per-primitive fragment input. An untextured item
// samples layer 0, whose result the select drops.
let textured = (in.word & WORD_TEXTURED) != 0u;
let wrap_x = (in.word & WORD_WRAP_X) != 0u;
let wrap_y = (in.word & WORD_WRAP_Y) != 0u;
if ((in.word & WORD_TEXTURED) != 0u) {
let layer = i32(recs[in.word & 0xffffu].x);
let dims = vec2<f32>(textureDimensions(tex_array).xy);
let inset = 0.5 / dims;
var uv_mixed = in.uv;
if (!wrap_x) {
uv_mixed.x = clamp(uv_mixed.x, inset.x, 1.0 - inset.x);
}
if (!wrap_y) {
uv_mixed.y = clamp(uv_mixed.y, inset.y, 1.0 - inset.y);
}
// The render-scale LOD bias: bevy's material path applies it for free, this lane by hand.
let c_repeat = textureSampleBias(tex_array, samp_repeat, in.uv, layer, view.mip_bias);
let c_clamp = textureSampleBias(tex_array, samp_clamp, in.uv, layer, view.mip_bias);
let c_mixed = textureSampleBias(tex_array, samp_repeat, uv_mixed, layer, view.mip_bias);
let layer = select(0, i32(recs[in.word & 0xffffu].x), textured);
let dims = vec2<f32>(textureDimensions(tex_array).xy);
let inset = 0.5 / dims;
var uv_mixed = in.uv;
if (!wrap_x) {
uv_mixed.x = clamp(uv_mixed.x, inset.x, 1.0 - inset.x);
}
if (!wrap_y) {
uv_mixed.y = clamp(uv_mixed.y, inset.y, 1.0 - inset.y);
}
// The render-scale LOD bias: bevy's material path applies it for free, this lane by hand.
let c_repeat = textureSampleBias(tex_array, samp_repeat, in.uv, layer, view.mip_bias);
let c_clamp = textureSampleBias(tex_array, samp_clamp, in.uv, layer, view.mip_bias);
let c_mixed = textureSampleBias(tex_array, samp_repeat, uv_mixed, layer, view.mip_bias);
var base = vec4<f32>(1.0);
if (textured) {
if (wrap_x && wrap_y) {
base = c_repeat;
} else if (!wrap_x && !wrap_y) {