Skip to main content

roxlap_formats/
character.rs

1//! `.rkc` rigged-character container — the on-disk form of a complete
2//! animated voxel character (meshes + skeleton + clips).
3//!
4//! Where [`kfa`](crate::kfa) stores only a skeleton, one clip, and a
5//! *single* kv6 filename, this container stores a whole character: N
6//! KV6 meshes, the hinge skeleton, any number of named animation clips,
7//! and (v3) embedded animated voxel clips. Each bone carries a **list of
8//! attachments** — static meshes and/or animated clips, each with its own
9//! local offset — so e.g. a flame can hang off a hand. It is the build
10//! target of the **demiurg** editor and
11//! the load source for **monada** at runtime; both sides go through
12//! [`parse`](crate::character::parse) / [`serialize`](crate::character::serialize),
13//! and [`Character::to_kfa_sprite`](crate::character::Character::to_kfa_sprite) turns a
14//! parsed character into a renderable [`KfaSprite`](crate::kfa::KfaSprite).
15//!
16//! # Layout
17//!
18//! Little-endian throughout (matching [`kv6`](crate::kv6) /
19//! [`kfa`](crate::kfa)). A fixed header, then a flat list of **chunks**;
20//! a reader indexes by `tag` and skips unknown tags via `len`. This chunk
21//! list is the whole forward-compatibility story — any future content
22//! type is a new tag (top-level chunk), a new clip `kind`, or a new
23//! `mesh_kind`.
24//!
25//! ```text
26//! File:
27//!     magic   [u8;4]   = b"RKCH"
28//!     version u16      = 3
29//!     chunks  [Chunk]  until EOF
30//!
31//! Chunk:
32//!     tag     [u8;4]
33//!     len     u32              # payload byte length
34//!     payload [u8; len]
35//! ```
36//!
37//! The canonical writer emits `META`, `MSHS`, `BONS`, `CLPS`, `VCLP` in
38//! that order, followed by any preserved unknown chunks; a reader must
39//! accept any order and ignore unknown tags. See the module-level handoff doc
40//! (`docs/handoff-character-container.md`) for the full byte spec and the
41//! forward-compat rationale.
42
43use core::fmt;
44
45use crate::bytes::{Cursor, OutOfBounds};
46use crate::kfa::{Hinge, Kfa, KfaSprite, Point3, Seq};
47use crate::kv6::{self, Kv6};
48use crate::sprite::Sprite;
49use crate::voxel_clip::{self, VoxelClip};
50use crate::xform::{BoneXform, Quat};
51
52const MAGIC: [u8; 4] = *b"RKCH";
53// v3 (VCL.5): a bone carries a *list* of attachments (each a static KV6
54// mesh or an animated voxel clip, with a local offset + playback), and the
55// character gains a `VCLP` chunk of embedded clips. v2 (single mesh per
56// bone) and v1 (i16 frmval) files are rejected — regenerate them.
57const VERSION: u16 = 3;
58
59const TAG_META: [u8; 4] = *b"META";
60const TAG_MSHS: [u8; 4] = *b"MSHS";
61const TAG_BONS: [u8; 4] = *b"BONS";
62const TAG_CLPS: [u8; 4] = *b"CLPS";
63/// Embedded animated voxel clips (VCL.5), referenced by [`MeshRef::Clip`].
64const TAG_VCLP: [u8; 4] = *b"VCLP";
65
66const HINGE_SIZE: usize = 64;
67
68/// `mesh_kind` discriminant for a static KV6 mesh (index into `MSHS`).
69const MESH_KIND_STATIC: u16 = 0;
70/// `mesh_kind` discriminant for an animated voxel clip (index into
71/// [`Character::voxel_clips`]) — VCL.5.
72const MESH_KIND_CLIP: u16 = 1;
73
74/// `kind` discriminant for a skeletal animation clip. Future clip types
75/// (e.g. voxel-video) get their own value; an old reader keeps them as
76/// [`ClipData::Unknown`].
77const CLIP_KIND_SKELETAL: u16 = 0;
78
79/// A parsed rigged-character container.
80///
81/// Index conventions that the byte layout depends on:
82/// - `meshes` is indexed by `mesh_id` ([`MeshRef::Static`]).
83/// - `bones` index *is* the canonical bone index — the column index of
84///   every clip's `frmval` and of [`KfaSprite::kfaval`], and the target
85///   of every [`Hinge::parent`].
86#[derive(Debug, Clone)]
87pub struct Character {
88    /// Human-facing name (`META`). May be empty.
89    pub name: String,
90    /// World placement passed to [`KfaSprite::new`] (`META`).
91    pub root: [f32; 3],
92    /// Bone meshes (`MSHS`), indexed by `mesh_id`.
93    pub meshes: Vec<Kv6>,
94    /// The skeleton (`BONS`); index = canonical bone index.
95    pub bones: Vec<Bone>,
96    /// Named animation clips (`CLPS`). May be empty (a posable rig with
97    /// no baked animation).
98    pub clips: Vec<Clip>,
99    /// Embedded animated voxel clips (`VCLP`, VCL.5), indexed by
100    /// [`MeshRef::Clip`]. May be empty.
101    pub voxel_clips: Vec<VoxelClip>,
102    /// Unknown top-level chunks preserved verbatim so re-saving with an
103    /// older build doesn't strip newer data. Re-emitted after the known
104    /// chunks in encounter order. Empty for canonically-written files.
105    pub extra_chunks: Vec<([u8; 4], Vec<u8>)>,
106}
107
108/// One bone: a name, a list of attachments, and its hinge (VCL.5). A bone
109/// may carry several attachments (static meshes and/or animated clips),
110/// each positioned by its own `local_offset` relative to the bone — so a
111/// flame can hang off a hand alongside the hand mesh. v2 had exactly one
112/// mesh per bone; that is now a single [`MeshRef::Static`] attachment at
113/// the identity offset.
114#[derive(Debug, Clone)]
115pub struct Bone {
116    pub name: String,
117    /// What this bone draws (see [`Attachment`]). May be empty (a pure
118    /// transform bone).
119    pub attachments: Vec<Attachment>,
120    /// Packed hinge, reused from [`kfa`](crate::kfa). `hinge.parent`
121    /// references bone indices in [`Character::bones`]; `-1` = root.
122    pub hinge: Hinge,
123}
124
125/// One thing a bone draws: a mesh/clip reference, a local offset placing
126/// it on the bone, and (for clips) playback parameters (VCL.5).
127#[derive(Debug, Clone, Copy, PartialEq)]
128pub struct Attachment {
129    /// The drawn source — a static KV6 mesh or an animated voxel clip.
130    pub target: MeshRef,
131    /// Transform applied **on top of** the bone's solved world transform,
132    /// positioning/orienting/scaling this attachment relative to the bone.
133    /// Identity = sit at the bone origin.
134    pub local_offset: BoneXform,
135    /// Playback parameters; ignored for a [`MeshRef::Static`] target.
136    pub playback: ClipPlayback,
137}
138
139impl Attachment {
140    /// A static mesh attachment at the identity offset — the v2-equivalent
141    /// "one mesh per bone".
142    #[must_use]
143    pub fn static_mesh(mesh_id: usize) -> Self {
144        Self {
145            target: MeshRef::Static(mesh_id),
146            local_offset: BoneXform::IDENTITY,
147            playback: ClipPlayback::default(),
148        }
149    }
150
151    /// An animated-clip attachment at the identity offset, default playback.
152    #[must_use]
153    pub fn clip(clip_id: usize) -> Self {
154        Self {
155            target: MeshRef::Clip(clip_id),
156            local_offset: BoneXform::IDENTITY,
157            playback: ClipPlayback::default(),
158        }
159    }
160}
161
162/// Playback parameters for a [`MeshRef::Clip`] attachment. The clip's own
163/// [`LoopMode`](crate::voxel_clip::LoopMode) governs looping; these only
164/// control rate + phase, so the same clip can run fast on one attachment
165/// and slow/offset on another.
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
167pub struct ClipPlayback {
168    /// Playback speed as Q8 fixed point (`256` = 1.0×). Drives how fast the
169    /// clip clock advances.
170    pub speed_q8: i32,
171    /// Initial clock offset (ms) so several instances of one clip don't
172    /// play in lockstep.
173    pub start_phase_ms: u32,
174}
175
176impl Default for ClipPlayback {
177    fn default() -> Self {
178        Self {
179            speed_q8: 256,
180            start_phase_ms: 0,
181        }
182    }
183}
184
185/// Typed reference to what a bone attachment draws. The on-disk
186/// `mesh_kind` discriminant selects the variant.
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
188pub enum MeshRef {
189    /// `mesh_kind 0` — index into [`Character::meshes`].
190    Static(usize),
191    /// `mesh_kind 1` — index into [`Character::voxel_clips`] (VCL.5).
192    Clip(usize),
193}
194
195/// One named animation clip.
196#[derive(Debug, Clone)]
197pub struct Clip {
198    pub name: String,
199    pub data: ClipData,
200}
201
202/// A clip's payload, discriminated by the on-disk `kind`.
203#[derive(Debug, Clone, PartialEq)]
204pub enum ClipData {
205    /// `kind 0` — the `frmval` + `seq` pair. `frmval[frame][bone]` is the
206    /// bone's local [`BoneXform`] (translation, quaternion rotation, scale);
207    /// the inner length equals [`Character::bones`]`.len()`. `seq` matches
208    /// [`Kfa::seq`](crate::kfa::Kfa::seq).
209    Skeletal {
210        frmval: Vec<Vec<BoneXform>>,
211        seq: Vec<Seq>,
212    },
213    /// A clip `kind` this build doesn't model — preserved verbatim so it
214    /// survives a load/save cycle.
215    Unknown { kind: u16, bytes: Vec<u8> },
216}
217
218/// Errors returned by [`parse`].
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub enum ParseError {
221    /// First 4 bytes are not the `b"RKCH"` magic.
222    BadMagic { got: [u8; 4] },
223    /// `version` field is not one this build understands.
224    UnsupportedVersion(u16),
225    /// A sequential read ran past EOF.
226    Truncated { at: usize, need: usize },
227    /// A bone references a `mesh_kind` this build can't render. Hard
228    /// error — you can't render what you can't decode.
229    UnsupportedMeshKind(u16),
230    /// A `Skeletal` clip's `numhin` does not equal `bones.len()`.
231    ClipBoneCountMismatch,
232    /// A required chunk (`META` / `MSHS` / `BONS`) was absent.
233    MissingChunk([u8; 4]),
234    /// An embedded `MSHS` mesh blob failed to parse as a KV6.
235    BadMesh(kv6::ParseError),
236    /// An embedded `VCLP` clip blob failed to parse as a voxel clip (VCL.5).
237    BadClip(voxel_clip::ParseError),
238    /// QE.6b - an attachment references a mesh / voxel-clip index past
239    /// the corresponding table. Pre-QE.6 this surfaced as a documented
240    /// (but wrong: "parse keeps indices in range") panic later, in
241    /// `to_kfa_sprite` / the attachment runtime.
242    BadAttachmentIndex {
243        /// The owning bone.
244        bone: usize,
245        /// The out-of-range index.
246        index: usize,
247        /// The referenced table's length (meshes or voxel clips).
248        table_len: usize,
249    },
250    /// QE.6b - a bone hinge whose `parent` is neither `-1` nor a valid
251    /// bone index, or whose parent links form a cycle (previously an
252    /// OOB panic / infinite loop in the skeleton solve).
253    BadSkeleton {
254        /// The offending bone.
255        bone: usize,
256    },
257}
258
259impl fmt::Display for ParseError {
260    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261        match self {
262            Self::BadMagic { got } => {
263                write!(
264                    f,
265                    "character bad magic: got {got:02x?}, expected {MAGIC:02x?}"
266                )
267            }
268            Self::UnsupportedVersion(v) => {
269                write!(
270                    f,
271                    "character unsupported version {v} (this build reads {VERSION})"
272                )
273            }
274            Self::Truncated { at, need } => {
275                write!(f, "character truncated: need {need} bytes at offset {at}")
276            }
277            Self::UnsupportedMeshKind(k) => {
278                write!(f, "character bone references unsupported mesh_kind {k}")
279            }
280            Self::ClipBoneCountMismatch => {
281                write!(f, "character skeletal clip numhin != bones.len()")
282            }
283            Self::MissingChunk(tag) => {
284                write!(
285                    f,
286                    "character missing required chunk {:?}",
287                    String::from_utf8_lossy(tag)
288                )
289            }
290            Self::BadMesh(e) => write!(f, "character embedded kv6 mesh: {e}"),
291            Self::BadClip(e) => write!(f, "character embedded voxel clip: {e:?}"),
292            Self::BadAttachmentIndex {
293                bone,
294                index,
295                table_len,
296            } => write!(
297                f,
298                "character bone {bone}: attachment index {index} past table of {table_len}"
299            ),
300            Self::BadSkeleton { bone } => {
301                write!(f, "character bone {bone}: parent out of range or cyclic")
302            }
303        }
304    }
305}
306
307impl std::error::Error for ParseError {}
308
309impl From<OutOfBounds> for ParseError {
310    fn from(e: OutOfBounds) -> Self {
311        Self::Truncated {
312            at: e.at,
313            need: e.need,
314        }
315    }
316}
317
318/// Parse an `.rkc` file's bytes into a [`Character`].
319///
320/// Accepts chunks in any order and skips unknown top-level tags (kept in
321/// [`Character::extra_chunks`]). `META` / `MSHS` / `BONS` are required.
322///
323/// # Errors
324///
325/// Returns [`ParseError`] on a bad magic / unsupported version, a read
326/// past EOF, an unsupported `mesh_kind`, a skeletal clip whose bone count
327/// disagrees with `BONS`, a missing required chunk, or a malformed
328/// embedded KV6 mesh. Note: `Hinge::htype` is *not* validated here — the
329/// renderer only implements `htype == 0` (others are treated as no
330/// rotation), so non-zero values load but won't animate.
331pub fn parse(bytes: &[u8]) -> Result<Character, ParseError> {
332    let mut cur = Cursor::new(bytes);
333    let magic = cur.read_bytes(4)?;
334    if magic != MAGIC {
335        return Err(ParseError::BadMagic {
336            got: [magic[0], magic[1], magic[2], magic[3]],
337        });
338    }
339    let version = cur.read_u16()?;
340    if version != VERSION {
341        return Err(ParseError::UnsupportedVersion(version));
342    }
343
344    // Pass 1: split the chunk list. Keep the raw payload of each known
345    // chunk and stash unknown chunks for verbatim re-emit. Last
346    // occurrence of a known tag wins (the canonical writer emits each
347    // once).
348    let mut meta: Option<&[u8]> = None;
349    let mut mshs: Option<&[u8]> = None;
350    let mut bons: Option<&[u8]> = None;
351    let mut clps: Option<&[u8]> = None;
352    let mut vclp: Option<&[u8]> = None;
353    let mut extra_chunks = Vec::new();
354    while cur.remaining() > 0 {
355        let tag_buf = cur.read_bytes(4)?;
356        let tag = [tag_buf[0], tag_buf[1], tag_buf[2], tag_buf[3]];
357        let len = cur.read_u32()? as usize;
358        let payload = cur.read_bytes(len)?;
359        match tag {
360            TAG_META => meta = Some(payload),
361            TAG_MSHS => mshs = Some(payload),
362            TAG_BONS => bons = Some(payload),
363            TAG_CLPS => clps = Some(payload),
364            TAG_VCLP => vclp = Some(payload),
365            _ => extra_chunks.push((tag, payload.to_vec())),
366        }
367    }
368
369    let meta = meta.ok_or(ParseError::MissingChunk(TAG_META))?;
370    let mshs = mshs.ok_or(ParseError::MissingChunk(TAG_MSHS))?;
371    let bons = bons.ok_or(ParseError::MissingChunk(TAG_BONS))?;
372
373    let (name, root) = parse_meta(meta)?;
374    let meshes = parse_mshs(mshs)?;
375    let bones = parse_bons(bons)?;
376    let clips = match clps {
377        Some(p) => parse_clps(p, bones.len())?,
378        None => Vec::new(),
379    };
380    let voxel_clips = match vclp {
381        Some(p) => parse_vclp(p)?,
382        None => Vec::new(),
383    };
384
385    // QE.6b - cross-reference validation, so the doc contract
386    // "`Character` values from `parse` keep indices in range" is
387    // actually true (pre-QE.6 it was claimed but never checked:
388    // `to_kfa_sprite` / the attachment runtime panicked instead).
389    for (bi, bone) in bones.iter().enumerate() {
390        for att in &bone.attachments {
391            let (index, table_len) = match att.target {
392                MeshRef::Static(i) => (i, meshes.len()),
393                MeshRef::Clip(i) => (i, voxel_clips.len()),
394            };
395            if index >= table_len {
396                return Err(ParseError::BadAttachmentIndex {
397                    bone: bi,
398                    index,
399                    table_len,
400                });
401            }
402        }
403        // Parent range; cycles checked below over the whole set.
404        if bone.hinge.parent != -1
405            && usize::try_from(bone.hinge.parent).map_or(true, |p| p >= bones.len())
406        {
407            return Err(ParseError::BadSkeleton { bone: bi });
408        }
409    }
410    for start in 0..bones.len() {
411        let mut cur_parent = bones[start].hinge.parent;
412        let mut hops = 0usize;
413        while cur_parent != -1 {
414            hops += 1;
415            if hops > bones.len() {
416                return Err(ParseError::BadSkeleton { bone: start });
417            }
418            cur_parent = bones[usize::try_from(cur_parent).expect("validated non-negative")]
419                .hinge
420                .parent;
421        }
422    }
423
424    Ok(Character {
425        name,
426        root,
427        meshes,
428        bones,
429        clips,
430        voxel_clips,
431        extra_chunks,
432    })
433}
434
435/// Serialise a [`Character`] back to bytes. Round-trips byte-equally
436/// with the canonical output that produced this `Character` via
437/// [`parse`].
438///
439/// # Panics
440///
441/// Panics if any count (mesh / bone / clip / frame / seq) or byte length
442/// does not fit in a `u32`, or if a `Skeletal` clip's `frmval` is not
443/// rectangular with row length `bones.len()`. `Character` values produced
444/// by [`parse`] always satisfy these.
445#[must_use]
446pub fn serialize(c: &Character) -> Vec<u8> {
447    let mut out = Vec::new();
448    out.extend_from_slice(&MAGIC);
449    out.extend_from_slice(&VERSION.to_le_bytes());
450
451    write_chunk(&mut out, TAG_META, |b| write_meta(b, c));
452    write_chunk(&mut out, TAG_MSHS, |b| write_mshs(b, c));
453    write_chunk(&mut out, TAG_BONS, |b| write_bons(b, c));
454    write_chunk(&mut out, TAG_CLPS, |b| write_clps(b, c));
455    write_chunk(&mut out, TAG_VCLP, |b| write_vclp(b, c));
456
457    for (tag, payload) in &c.extra_chunks {
458        write_chunk(&mut out, *tag, |b| b.extend_from_slice(payload));
459    }
460
461    out
462}
463
464impl Character {
465    /// Build a renderable [`KfaSprite`]. `clip` selects a `Skeletal`
466    /// clip to bake in via [`KfaSprite::set_animation`]; `None` leaves
467    /// the sprite in its rest pose for the host to drive `kfaval`
468    /// directly. A non-`Skeletal` (`Unknown`) clip selection is ignored
469    /// (no animation attached).
470    ///
471    /// Meshes are cloned per call. Editors that re-pose every frame
472    /// should build the sprite once and reuse it.
473    ///
474    /// # Panics
475    ///
476    /// Panics if `clip` is out of range, or if a bone's `MeshRef::Static`
477    /// index is out of range for [`Character::meshes`]. `Character`
478    /// values from [`parse`] keep mesh indices in range.
479    #[must_use]
480    pub fn to_kfa_sprite(&self, clip: Option<usize>) -> KfaSprite {
481        // The hinge solver needs exactly one limb per bone, so this legacy
482        // path draws each bone's **first `Static` attachment** (the
483        // v2-equivalent single mesh), at the bone's transform. Extra
484        // attachments, per-attachment `local_offset`s, and `Clip`
485        // attachments are handled by the richer attachment runtime (VCL.6),
486        // not by `KfaSprite`. A bone with no static mesh gets an empty limb
487        // (draws nothing) to keep the 1:1 bone↔limb mapping.
488        let limbs = self
489            .bones
490            .iter()
491            .map(|b| {
492                let kv6 = b
493                    .attachments
494                    .iter()
495                    .find_map(|a| match a.target {
496                        MeshRef::Static(i) => Some(self.meshes[i].clone()),
497                        MeshRef::Clip(_) => None,
498                    })
499                    .unwrap_or_else(|| Kv6::from_fn(1, 1, 1, |_, _, _| None));
500                Sprite::axis_aligned(kv6, self.root)
501            })
502            .collect();
503        let hinges = self.bones.iter().map(|b| b.hinge).collect();
504        let mut k = KfaSprite::new(limbs, hinges, self.root);
505        if let Some(ci) = clip {
506            if let ClipData::Skeletal { frmval, seq } = &self.clips[ci].data {
507                // Clip frames are already per-bone TRS — hand them straight to
508                // the poser.
509                k.set_animation(frmval.clone(), seq.clone());
510            }
511        }
512        k
513    }
514
515    /// Export a **lossy** voxlap-toolchain [`Kfa`] (`.kfa`): the skeleton
516    /// plus one clip, referencing a single kv6 by filename.
517    ///
518    /// voxlap's `.kfa` is fundamentally narrower than this container — it
519    /// stores the hinge skeleton and one animation, but points at just
520    /// *one* kv6 file (voxlap rigs a single mesh with a per-voxel limb
521    /// index, which roxlap deliberately doesn't model). So this export
522    /// drops, by design:
523    /// - every embedded [`Character::meshes`] mesh — only `kv6_name` (the
524    ///   filename voxlap should load) is written. Export the bone meshes
525    ///   separately via [`kv6::serialize`] if a tool needs them.
526    /// - every clip except `clip`.
527    ///
528    /// `clip` selects the [`ClipData::Skeletal`] clip whose `frmval` /
529    /// `seq` to bake in. `None`, an out-of-range index, or a
530    /// non-`Skeletal` clip yields an empty animation table (a posable rig
531    /// with no baked motion). Serialise the result with
532    /// [`kfa::serialize`](crate::kfa::serialize).
533    #[must_use]
534    pub fn to_kfa(&self, clip: Option<usize>, kv6_name: impl Into<Vec<u8>>) -> Kfa {
535        let hinges = self.bones.iter().map(|b| b.hinge).collect();
536        // The `.kfa` file stores one Q15 angle per bone; collapse each TRS to
537        // its rotation about the bone's hinge axis (translation / scale /
538        // off-axis rotation are dropped — this export is documented as lossy).
539        let (frmval, seq) = match clip.and_then(|ci| self.clips.get(ci)) {
540            Some(Clip {
541                data: ClipData::Skeletal { frmval, seq },
542                ..
543            }) => {
544                let angles = frmval
545                    .iter()
546                    .map(|row| {
547                        row.iter()
548                            .enumerate()
549                            .map(|(bone, x)| {
550                                let v = self.bones[bone].hinge.v[0];
551                                x.hinge_angle([v.x, v.y, v.z])
552                            })
553                            .collect()
554                    })
555                    .collect();
556                (angles, seq.clone())
557            }
558            _ => (Vec::new(), Vec::new()),
559        };
560        Kfa {
561            kv6_name: kv6_name.into(),
562            hinges,
563            frmval,
564            seq,
565        }
566    }
567}
568
569// --- chunk payload readers ----------------------------------------------
570
571fn parse_meta(payload: &[u8]) -> Result<(String, [f32; 3]), ParseError> {
572    let mut cur = Cursor::new(payload);
573    let name_len = cur.read_u16()? as usize;
574    let name = String::from_utf8_lossy(cur.read_bytes(name_len)?).into_owned();
575    let root = [cur.read_f32()?, cur.read_f32()?, cur.read_f32()?];
576    Ok((name, root))
577}
578
579fn parse_mshs(payload: &[u8]) -> Result<Vec<Kv6>, ParseError> {
580    let mut cur = Cursor::new(payload);
581    let count = cur.read_u32()? as usize;
582    // QE.6b - capacity clamped by remaining bytes (>= 4 B length per
583    // mesh blob); a lying count fails as Truncated, not an alloc bomb.
584    let mut meshes = Vec::with_capacity(cur.clamped_capacity(count, 4));
585    for _ in 0..count {
586        let blob_len = cur.read_u32()? as usize;
587        let blob = cur.read_bytes(blob_len)?;
588        meshes.push(kv6::parse(blob).map_err(ParseError::BadMesh)?);
589    }
590    Ok(meshes)
591}
592
593fn parse_bons(payload: &[u8]) -> Result<Vec<Bone>, ParseError> {
594    let mut cur = Cursor::new(payload);
595    let count = cur.read_u32()? as usize;
596    // QE.6b - >= 70 B per bone (name len + attach count + 64 B hinge).
597    let mut bones = Vec::with_capacity(cur.clamped_capacity(count, 70));
598    for _ in 0..count {
599        let name_len = cur.read_u16()? as usize;
600        let name = String::from_utf8_lossy(cur.read_bytes(name_len)?).into_owned();
601        let attach_count = cur.read_u32()? as usize;
602        // QE.6b - an attachment record is 54 bytes.
603        let mut attachments = Vec::with_capacity(cur.clamped_capacity(attach_count, 54));
604        for _ in 0..attach_count {
605            attachments.push(read_attachment(&mut cur)?);
606        }
607        let hinge = read_hinge(&mut cur)?;
608        bones.push(Bone {
609            name,
610            attachments,
611            hinge,
612        });
613    }
614    Ok(bones)
615}
616
617/// One attachment: `mesh_kind u16`, `index u32`, `local_offset` (40-byte
618/// `BoneXform`), then playback (`speed_q8 i32`, `start_phase_ms u32`).
619fn read_attachment(cur: &mut Cursor<'_>) -> Result<Attachment, ParseError> {
620    let mesh_kind = cur.read_u16()?;
621    let index = cur.read_u32()? as usize;
622    let target = match mesh_kind {
623        MESH_KIND_STATIC => MeshRef::Static(index),
624        MESH_KIND_CLIP => MeshRef::Clip(index),
625        other => return Err(ParseError::UnsupportedMeshKind(other)),
626    };
627    let local_offset = read_bonexform(cur)?;
628    let speed_q8 = cur.read_i32()?;
629    let start_phase_ms = cur.read_u32()?;
630    Ok(Attachment {
631        target,
632        local_offset,
633        playback: ClipPlayback {
634            speed_q8,
635            start_phase_ms,
636        },
637    })
638}
639
640fn parse_vclp(payload: &[u8]) -> Result<Vec<VoxelClip>, ParseError> {
641    let mut cur = Cursor::new(payload);
642    let count = cur.read_u32()? as usize;
643    // QE.6b - >= 4 B length prefix per clip blob.
644    let mut clips = Vec::with_capacity(cur.clamped_capacity(count, 4));
645    for _ in 0..count {
646        let blob_len = cur.read_u32()? as usize;
647        let blob = cur.read_bytes(blob_len)?;
648        clips.push(VoxelClip::parse(blob).map_err(ParseError::BadClip)?);
649    }
650    Ok(clips)
651}
652
653fn parse_clps(payload: &[u8], numbone: usize) -> Result<Vec<Clip>, ParseError> {
654    let mut cur = Cursor::new(payload);
655    let count = cur.read_u32()? as usize;
656    // QE.6b - >= 8 B per named clip record.
657    let mut clips = Vec::with_capacity(cur.clamped_capacity(count, 8));
658    for _ in 0..count {
659        let name_len = cur.read_u16()? as usize;
660        let name = String::from_utf8_lossy(cur.read_bytes(name_len)?).into_owned();
661        let kind = cur.read_u16()?;
662        let payload_len = cur.read_u32()? as usize;
663        let body = cur.read_bytes(payload_len)?;
664        let data = if kind == CLIP_KIND_SKELETAL {
665            parse_skeletal(body, numbone)?
666        } else {
667            ClipData::Unknown {
668                kind,
669                bytes: body.to_vec(),
670            }
671        };
672        clips.push(Clip { name, data });
673    }
674    Ok(clips)
675}
676
677fn parse_skeletal(body: &[u8], numbone: usize) -> Result<ClipData, ParseError> {
678    let mut cur = Cursor::new(body);
679    let numfrm = cur.read_u32()? as usize;
680    let numhin = cur.read_u32()? as usize;
681    if numhin != numbone {
682        return Err(ParseError::ClipBoneCountMismatch);
683    }
684    // QE.6b - a BoneXform row is 40 B per bone.
685    let mut frmval = Vec::with_capacity(cur.clamped_capacity(numfrm, numhin.saturating_mul(40)));
686    for _ in 0..numfrm {
687        let mut row = Vec::with_capacity(cur.clamped_capacity(numhin, 40));
688        for _ in 0..numhin {
689            row.push(read_bonexform(&mut cur)?);
690        }
691        frmval.push(row);
692    }
693    let seqcount = cur.read_u32()? as usize;
694    let mut seq = Vec::with_capacity(cur.clamped_capacity(seqcount, 8));
695    for _ in 0..seqcount {
696        let tim = cur.read_i32()?;
697        let frm = cur.read_i32()?;
698        seq.push(Seq { tim, frm });
699    }
700    Ok(ClipData::Skeletal { frmval, seq })
701}
702
703fn read_hinge(cur: &mut Cursor<'_>) -> Result<Hinge, OutOfBounds> {
704    let parent = cur.read_i32()?;
705    let p0 = read_point3(cur)?;
706    let p1 = read_point3(cur)?;
707    let v0 = read_point3(cur)?;
708    let v1 = read_point3(cur)?;
709    let vmin = cur.read_i16()?;
710    let vmax = cur.read_i16()?;
711    let htype = cur.read_u8()?;
712    let filler_buf = cur.read_bytes(7)?;
713    let mut filler = [0u8; 7];
714    filler.copy_from_slice(filler_buf);
715    Ok(Hinge {
716        parent,
717        p: [p0, p1],
718        v: [v0, v1],
719        vmin,
720        vmax,
721        htype,
722        filler,
723    })
724}
725
726fn read_point3(cur: &mut Cursor<'_>) -> Result<Point3, OutOfBounds> {
727    Ok(Point3 {
728        x: cur.read_f32()?,
729        y: cur.read_f32()?,
730        z: cur.read_f32()?,
731    })
732}
733
734/// One per-bone keyframe transform: translation (3), rotation quaternion
735/// (x, y, z, w), scale (3) — ten little-endian `f32`s, 40 bytes.
736fn read_bonexform(cur: &mut Cursor<'_>) -> Result<BoneXform, OutOfBounds> {
737    let t = [cur.read_f32()?, cur.read_f32()?, cur.read_f32()?];
738    let r = Quat {
739        x: cur.read_f32()?,
740        y: cur.read_f32()?,
741        z: cur.read_f32()?,
742        w: cur.read_f32()?,
743    };
744    let s = [cur.read_f32()?, cur.read_f32()?, cur.read_f32()?];
745    Ok(BoneXform { t, r, s })
746}
747
748fn write_bonexform(out: &mut Vec<u8>, x: &BoneXform) {
749    for v in [
750        x.t[0], x.t[1], x.t[2], x.r.x, x.r.y, x.r.z, x.r.w, x.s[0], x.s[1], x.s[2],
751    ] {
752        out.extend_from_slice(&v.to_le_bytes());
753    }
754}
755
756// --- chunk payload writers ----------------------------------------------
757
758/// Emit `tag` + a `u32` length + the payload produced by `body`.
759fn write_chunk(out: &mut Vec<u8>, tag: [u8; 4], body: impl FnOnce(&mut Vec<u8>)) {
760    out.extend_from_slice(&tag);
761    let len_pos = out.len();
762    out.extend_from_slice(&0u32.to_le_bytes()); // placeholder
763    let start = out.len();
764    body(out);
765    let len = u32::try_from(out.len() - start).expect("chunk payload length must fit in u32");
766    out[len_pos..len_pos + 4].copy_from_slice(&len.to_le_bytes());
767}
768
769fn write_meta(out: &mut Vec<u8>, c: &Character) {
770    let name = c.name.as_bytes();
771    let name_len = u16::try_from(name.len()).expect("character name length must fit in u16");
772    out.extend_from_slice(&name_len.to_le_bytes());
773    out.extend_from_slice(name);
774    for v in c.root {
775        out.extend_from_slice(&v.to_le_bytes());
776    }
777}
778
779fn write_mshs(out: &mut Vec<u8>, c: &Character) {
780    let count = u32::try_from(c.meshes.len()).expect("mesh count must fit in u32");
781    out.extend_from_slice(&count.to_le_bytes());
782    for mesh in &c.meshes {
783        let blob = kv6::serialize(mesh);
784        let blob_len = u32::try_from(blob.len()).expect("kv6 blob length must fit in u32");
785        out.extend_from_slice(&blob_len.to_le_bytes());
786        out.extend_from_slice(&blob);
787    }
788}
789
790fn write_bons(out: &mut Vec<u8>, c: &Character) {
791    let count = u32::try_from(c.bones.len()).expect("bone count must fit in u32");
792    out.extend_from_slice(&count.to_le_bytes());
793    for bone in &c.bones {
794        let name = bone.name.as_bytes();
795        let name_len = u16::try_from(name.len()).expect("bone name length must fit in u16");
796        out.extend_from_slice(&name_len.to_le_bytes());
797        out.extend_from_slice(name);
798        let attach_count =
799            u32::try_from(bone.attachments.len()).expect("attachment count must fit in u32");
800        out.extend_from_slice(&attach_count.to_le_bytes());
801        for a in &bone.attachments {
802            write_attachment(out, a);
803        }
804        write_hinge(out, &bone.hinge);
805    }
806}
807
808fn write_attachment(out: &mut Vec<u8>, a: &Attachment) {
809    let (kind, index) = match a.target {
810        MeshRef::Static(i) => (MESH_KIND_STATIC, i),
811        MeshRef::Clip(i) => (MESH_KIND_CLIP, i),
812    };
813    out.extend_from_slice(&kind.to_le_bytes());
814    let idx = u32::try_from(index).expect("attachment index must fit in u32");
815    out.extend_from_slice(&idx.to_le_bytes());
816    write_bonexform(out, &a.local_offset);
817    out.extend_from_slice(&a.playback.speed_q8.to_le_bytes());
818    out.extend_from_slice(&a.playback.start_phase_ms.to_le_bytes());
819}
820
821fn write_vclp(out: &mut Vec<u8>, c: &Character) {
822    let count = u32::try_from(c.voxel_clips.len()).expect("voxel clip count must fit in u32");
823    out.extend_from_slice(&count.to_le_bytes());
824    for clip in &c.voxel_clips {
825        let blob = clip.serialize();
826        let blob_len = u32::try_from(blob.len()).expect("voxel clip blob length must fit in u32");
827        out.extend_from_slice(&blob_len.to_le_bytes());
828        out.extend_from_slice(&blob);
829    }
830}
831
832fn write_clps(out: &mut Vec<u8>, c: &Character) {
833    let count = u32::try_from(c.clips.len()).expect("clip count must fit in u32");
834    out.extend_from_slice(&count.to_le_bytes());
835    for clip in &c.clips {
836        let name = clip.name.as_bytes();
837        let name_len = u16::try_from(name.len()).expect("clip name length must fit in u16");
838        out.extend_from_slice(&name_len.to_le_bytes());
839        out.extend_from_slice(name);
840        match &clip.data {
841            ClipData::Skeletal { frmval, seq } => {
842                out.extend_from_slice(&CLIP_KIND_SKELETAL.to_le_bytes());
843                write_chunk_body(out, |b| write_skeletal(b, frmval, seq));
844            }
845            ClipData::Unknown { kind, bytes } => {
846                out.extend_from_slice(&kind.to_le_bytes());
847                write_chunk_body(out, |b| b.extend_from_slice(bytes));
848            }
849        }
850    }
851}
852
853/// Emit a `u32` length + the payload produced by `body` (the clip
854/// `payload_len` + `payload` pair). Shares the back-patch trick with
855/// [`write_chunk`] but without a leading tag.
856fn write_chunk_body(out: &mut Vec<u8>, body: impl FnOnce(&mut Vec<u8>)) {
857    let len_pos = out.len();
858    out.extend_from_slice(&0u32.to_le_bytes());
859    let start = out.len();
860    body(out);
861    let len = u32::try_from(out.len() - start).expect("clip payload length must fit in u32");
862    out[len_pos..len_pos + 4].copy_from_slice(&len.to_le_bytes());
863}
864
865fn write_skeletal(out: &mut Vec<u8>, frmval: &[Vec<BoneXform>], seq: &[Seq]) {
866    let numhin = frmval.first().map_or(0, Vec::len);
867    for (i, row) in frmval.iter().enumerate() {
868        assert!(
869            row.len() == numhin,
870            "skeletal frmval[{i}].len() = {} != numhin {numhin}",
871            row.len(),
872        );
873    }
874    let numfrm = u32::try_from(frmval.len()).expect("numfrm must fit in u32");
875    let numhin_u32 = u32::try_from(numhin).expect("numhin must fit in u32");
876    out.extend_from_slice(&numfrm.to_le_bytes());
877    out.extend_from_slice(&numhin_u32.to_le_bytes());
878    for row in frmval {
879        for v in row {
880            write_bonexform(out, v);
881        }
882    }
883    let seqcount = u32::try_from(seq.len()).expect("seqcount must fit in u32");
884    out.extend_from_slice(&seqcount.to_le_bytes());
885    for s in seq {
886        out.extend_from_slice(&s.tim.to_le_bytes());
887        out.extend_from_slice(&s.frm.to_le_bytes());
888    }
889}
890
891fn write_hinge(out: &mut Vec<u8>, h: &Hinge) {
892    out.extend_from_slice(&h.parent.to_le_bytes());
893    for p in h.p {
894        write_point3(out, p);
895    }
896    for v in h.v {
897        write_point3(out, v);
898    }
899    out.extend_from_slice(&h.vmin.to_le_bytes());
900    out.extend_from_slice(&h.vmax.to_le_bytes());
901    out.push(h.htype);
902    out.extend_from_slice(&h.filler);
903}
904
905fn write_point3(out: &mut Vec<u8>, p: Point3) {
906    out.extend_from_slice(&p.x.to_le_bytes());
907    out.extend_from_slice(&p.y.to_le_bytes());
908    out.extend_from_slice(&p.z.to_le_bytes());
909}
910
911// Keep the on-disk hinge width pinned to the shared 64-byte layout.
912const _: () = assert!(HINGE_SIZE == 64);
913
914// --- tests --------------------------------------------------------------
915
916#[cfg(test)]
917mod tests {
918    use super::*;
919
920    fn unit_kv6(fill: u32) -> Kv6 {
921        // A 1×1×1 model with a single voxel — enough to round-trip.
922        Kv6 {
923            xsiz: 1,
924            ysiz: 1,
925            zsiz: 1,
926            xpiv: 0.5,
927            ypiv: 0.5,
928            zpiv: 0.5,
929            voxels: vec![kv6::Voxel {
930                col: fill,
931                z: 0,
932                vis: 0x3f,
933                dir: 0,
934            }],
935            xlen: vec![1],
936            ylen: vec![vec![1]],
937            palette: None,
938        }
939    }
940
941    fn hinge(parent: i32) -> Hinge {
942        let zero = Point3 {
943            x: 0.0,
944            y: 0.0,
945            z: 0.0,
946        };
947        let axis = Point3 {
948            x: 0.0,
949            y: 0.0,
950            z: 1.0,
951        };
952        Hinge {
953            parent,
954            p: [zero, zero],
955            v: [axis, axis],
956            vmin: 0,
957            vmax: 0,
958            htype: 0,
959            filler: [0; 7],
960        }
961    }
962
963    fn synthetic_character() -> Character {
964        Character {
965            name: "anasaur".to_string(),
966            root: [70.0, -75.0, 50.0],
967            meshes: vec![unit_kv6(0x00ff_8040), unit_kv6(0x0010_2030)],
968            bones: vec![
969                Bone {
970                    name: "body".to_string(),
971                    attachments: vec![Attachment::static_mesh(0)],
972                    hinge: hinge(-1),
973                },
974                Bone {
975                    name: "arm".to_string(),
976                    attachments: vec![Attachment::static_mesh(1)],
977                    hinge: hinge(0),
978                },
979            ],
980            clips: vec![Clip {
981                name: "wave".to_string(),
982                // Bones rotate about +z; build rotation-only TRS frames from
983                // the wave's Q15 angles.
984                data: ClipData::Skeletal {
985                    frmval: [[0i16, 0], [0, 16000], [0, 0], [0, -16000]]
986                        .iter()
987                        .map(|[r, a]| {
988                            let z = [0.0, 0.0, 1.0];
989                            vec![
990                                BoneXform::from_hinge_angle(z, *r),
991                                BoneXform::from_hinge_angle(z, *a),
992                            ]
993                        })
994                        .collect(),
995                    seq: vec![
996                        Seq { tim: 0, frm: 0 },
997                        Seq { tim: 500, frm: 1 },
998                        Seq { tim: 1000, frm: 2 },
999                        Seq { tim: 1500, frm: 3 },
1000                        Seq { tim: 2000, frm: !0 },
1001                    ],
1002                },
1003            }],
1004            voxel_clips: Vec::new(),
1005            extra_chunks: Vec::new(),
1006        }
1007    }
1008
1009    #[test]
1010    fn roundtrips_byte_equal() {
1011        let c = synthetic_character();
1012        let bytes = serialize(&c);
1013        let parsed = parse(&bytes).expect("parse synthetic");
1014        let bytes2 = serialize(&parsed);
1015        assert_eq!(bytes, bytes2, "byte-level round-trip failed");
1016        assert_eq!(parsed.name, c.name);
1017        assert_eq!(parsed.root, c.root);
1018        assert_eq!(parsed.meshes.len(), c.meshes.len());
1019        assert_eq!(parsed.bones.len(), c.bones.len());
1020        assert_eq!(parsed.bones[1].name, "arm");
1021        assert_eq!(parsed.bones[1].attachments[0].target, MeshRef::Static(1));
1022        assert_eq!(parsed.bones[1].hinge.parent, 0);
1023        assert_eq!(parsed.clips.len(), 1);
1024        assert_eq!(parsed.clips[0].data, c.clips[0].data);
1025        assert!(parsed.voxel_clips.is_empty());
1026    }
1027
1028    #[test]
1029    fn roundtrips_with_clips_and_multi_attachment() {
1030        use crate::voxel_clip::{LoopMode, VoxelClip, VoxelFrame};
1031        // A tiny 1-frame clip (1×1×4, one voxel at z=0).
1032        let frame = VoxelFrame {
1033            occupancy: vec![1u32],
1034            colors: vec![0x8011_2233],
1035            color_offsets: vec![0, 1],
1036        };
1037        let clip = VoxelClip::from_frames(
1038            [1, 1, 4],
1039            [0.5, 0.5, 2.0],
1040            1.0,
1041            LoopMode::Loop,
1042            &[frame],
1043            &[],
1044            33,
1045            0,
1046        );
1047
1048        let mut c = synthetic_character();
1049        c.voxel_clips = vec![clip];
1050        // Give the body bone a SECOND attachment: the clip, at a non-identity
1051        // offset + non-default playback — so a flame hangs off it.
1052        c.bones[0].attachments.push(Attachment {
1053            target: MeshRef::Clip(0),
1054            local_offset: BoneXform {
1055                t: [1.0, 2.0, 3.0],
1056                r: Quat::IDENTITY,
1057                s: [1.0, 1.0, 1.0],
1058            },
1059            playback: ClipPlayback {
1060                speed_q8: 512,
1061                start_phase_ms: 100,
1062            },
1063        });
1064
1065        let bytes = serialize(&c);
1066        let parsed = parse(&bytes).expect("parse v3 with clips");
1067        assert_eq!(serialize(&parsed), bytes, "byte round-trip");
1068
1069        assert_eq!(parsed.voxel_clips.len(), 1);
1070        assert_eq!(parsed.voxel_clips[0], c.voxel_clips[0]);
1071
1072        // body bone: static mesh 0 + clip 0 (multi-attachment).
1073        let body = &parsed.bones[0].attachments;
1074        assert_eq!(body.len(), 2);
1075        assert_eq!(body[0].target, MeshRef::Static(0));
1076        assert_eq!(body[1].target, MeshRef::Clip(0));
1077        assert_eq!(body[1].local_offset.t, [1.0, 2.0, 3.0]);
1078        assert_eq!(
1079            body[1].playback,
1080            ClipPlayback {
1081                speed_q8: 512,
1082                start_phase_ms: 100,
1083            }
1084        );
1085
1086        // to_kfa_sprite keeps one limb per bone (first static attachment;
1087        // the clip is the attachment runtime's job).
1088        let k = parsed.to_kfa_sprite(None);
1089        assert_eq!(k.limbs.len(), parsed.bones.len());
1090    }
1091
1092    #[test]
1093    fn to_kfa_sprite_builds_renderable() {
1094        let c = synthetic_character();
1095        let mut k = c.to_kfa_sprite(Some(0));
1096        assert_eq!(k.limbs.len(), 2);
1097        assert_eq!(k.hinges.len(), 2);
1098        assert_eq!(k.p, c.root);
1099        // Baked clip advances the child bone away from rest.
1100        k.animsprite(500);
1101        assert_ne!(
1102            k.kfaval[1],
1103            crate::xform::BoneXform::IDENTITY,
1104            "baked clip should move the arm bone"
1105        );
1106
1107        // Rest pose: no curve attached → animsprite is a no-op.
1108        let mut rest = c.to_kfa_sprite(None);
1109        rest.animsprite(500);
1110        assert_eq!(rest.kfaval[1], crate::xform::BoneXform::IDENTITY);
1111    }
1112
1113    #[test]
1114    fn to_kfa_export_is_lossy_but_valid() {
1115        let c = synthetic_character();
1116        let kfa = c.to_kfa(Some(0), "coco.kv6");
1117        // Skeleton + the selected clip survive; the filename is set.
1118        assert_eq!(kfa.kv6_name, b"coco.kv6");
1119        assert_eq!(kfa.hinges.len(), 2);
1120        assert_eq!(kfa.hinges[1].parent, 0);
1121        if let ClipData::Skeletal { frmval, seq } = &c.clips[0].data {
1122            // The .kfa export collapses each TRS to its hinge angle about +z;
1123            // assert it recovers the angles the frames were built from.
1124            let z = [0.0, 0.0, 1.0];
1125            let expected: Vec<Vec<i16>> = frmval
1126                .iter()
1127                .map(|row| row.iter().map(|x| x.hinge_angle(z)).collect())
1128                .collect();
1129            assert_eq!(kfa.frmval, expected);
1130            assert_eq!(&kfa.seq, seq);
1131        } else {
1132            panic!("clip 0 should be skeletal");
1133        }
1134        // The embedded meshes are dropped (lossy) — only the name remains.
1135        // The result is a well-formed .kfa: serialise + re-parse round-trips.
1136        let bytes = crate::kfa::serialize(&kfa);
1137        let reparsed = crate::kfa::parse(&bytes).expect("export round-trips through kfa");
1138        assert_eq!(reparsed.kv6_name, kfa.kv6_name);
1139        assert_eq!(reparsed.frmval, kfa.frmval);
1140        assert_eq!(reparsed.seq, kfa.seq);
1141    }
1142
1143    #[test]
1144    fn to_kfa_without_clip_is_posable() {
1145        let c = synthetic_character();
1146        // No clip / out-of-range / Unknown → empty animation table.
1147        let kfa = c.to_kfa(None, b"x.kv6".to_vec());
1148        assert_eq!(kfa.hinges.len(), 2);
1149        assert!(kfa.frmval.is_empty());
1150        assert!(kfa.seq.is_empty());
1151        // Still a valid .kfa.
1152        let bytes = crate::kfa::serialize(&kfa);
1153        assert!(crate::kfa::parse(&bytes).is_ok());
1154    }
1155
1156    #[test]
1157    fn clips_may_be_empty() {
1158        let mut c = synthetic_character();
1159        c.clips.clear();
1160        let bytes = serialize(&c);
1161        let parsed = parse(&bytes).expect("parse");
1162        assert!(parsed.clips.is_empty());
1163        // Posable rig still builds a sprite.
1164        let k = parsed.to_kfa_sprite(None);
1165        assert_eq!(k.limbs.len(), 2);
1166    }
1167
1168    #[test]
1169    fn unknown_top_level_chunk_is_skipped_and_preserved() {
1170        let mut bytes = serialize(&synthetic_character());
1171        // Append a ZZZZ chunk with a 3-byte payload.
1172        bytes.extend_from_slice(b"ZZZZ");
1173        bytes.extend_from_slice(&3u32.to_le_bytes());
1174        bytes.extend_from_slice(&[1, 2, 3]);
1175        let parsed = parse(&bytes).expect("parse with unknown chunk");
1176        assert_eq!(parsed.bones.len(), 2, "known chunks still parse");
1177        assert_eq!(parsed.extra_chunks, vec![(*b"ZZZZ", vec![1u8, 2, 3])]);
1178        // Re-emit preserves it.
1179        let bytes2 = serialize(&parsed);
1180        assert_eq!(
1181            bytes2, bytes,
1182            "unknown chunk preserved byte-equal on re-save"
1183        );
1184    }
1185
1186    #[test]
1187    fn unknown_clip_kind_preserved() {
1188        let mut c = synthetic_character();
1189        c.clips.push(Clip {
1190            name: "mystery".to_string(),
1191            data: ClipData::Unknown {
1192                kind: 7,
1193                bytes: vec![9, 8, 7, 6],
1194            },
1195        });
1196        let bytes = serialize(&c);
1197        let parsed = parse(&bytes).expect("parse");
1198        // The skeletal clip still loads and plays.
1199        assert!(matches!(parsed.clips[0].data, ClipData::Skeletal { .. }));
1200        assert_eq!(
1201            parsed.clips[1].data,
1202            ClipData::Unknown {
1203                kind: 7,
1204                bytes: vec![9, 8, 7, 6]
1205            }
1206        );
1207        // And re-serialises byte-equal.
1208        assert_eq!(serialize(&parsed), bytes);
1209    }
1210
1211    #[test]
1212    fn bad_magic_errors() {
1213        let mut bytes = serialize(&synthetic_character());
1214        bytes[0] ^= 0xff;
1215        assert!(matches!(parse(&bytes), Err(ParseError::BadMagic { .. })));
1216    }
1217
1218    #[test]
1219    fn version_mismatch_errors() {
1220        let mut bytes = serialize(&synthetic_character());
1221        // version is the 2 bytes right after the 4-byte magic.
1222        bytes[4] = 0xff;
1223        bytes[5] = 0xff;
1224        assert!(matches!(
1225            parse(&bytes),
1226            Err(ParseError::UnsupportedVersion(0xffff))
1227        ));
1228    }
1229
1230    #[test]
1231    fn truncated_errors() {
1232        let bytes = serialize(&synthetic_character());
1233        assert!(matches!(
1234            parse(&bytes[..bytes.len() - 4]),
1235            Err(ParseError::Truncated { .. })
1236        ));
1237    }
1238
1239    #[test]
1240    fn missing_required_chunk_errors() {
1241        // A minimal valid header with no chunks at all.
1242        let mut bytes = Vec::new();
1243        bytes.extend_from_slice(&MAGIC);
1244        bytes.extend_from_slice(&VERSION.to_le_bytes());
1245        assert!(matches!(
1246            parse(&bytes),
1247            Err(ParseError::MissingChunk(TAG_META))
1248        ));
1249    }
1250
1251    #[test]
1252    fn unsupported_mesh_kind_errors() {
1253        let mut bytes = serialize(&synthetic_character());
1254        // Flip the first attachment's mesh_kind to an undefined value (2;
1255        // 0 = Static, 1 = Clip are both valid). The BONS payload begins:
1256        //   tag(4) len(4) count(4) name_len(2) name("body"=4) attach_count(4)
1257        //   kind(2)...
1258        let pos = find_tag(&bytes, *b"BONS");
1259        let kind_off = pos + 8 + 4 + 2 + 4 + 4;
1260        bytes[kind_off] = 2;
1261        bytes[kind_off + 1] = 0;
1262        assert!(matches!(
1263            parse(&bytes),
1264            Err(ParseError::UnsupportedMeshKind(2))
1265        ));
1266    }
1267
1268    #[test]
1269    fn clip_bone_count_mismatch_errors() {
1270        // Build a character whose skeletal clip has the wrong bone count
1271        // by hand-serialising bones=1 but clip numhin=2.
1272        let mut c = synthetic_character();
1273        c.bones.pop(); // now 1 bone
1274        c.bones[0].attachments = vec![Attachment::static_mesh(0)];
1275        // frmval rows still have width 2 → numhin 2 != bones 1.
1276        let bytes = serialize(&c);
1277        assert!(matches!(
1278            parse(&bytes),
1279            Err(ParseError::ClipBoneCountMismatch)
1280        ));
1281    }
1282
1283    #[test]
1284    fn bad_embedded_mesh_errors() {
1285        let mut bytes = serialize(&synthetic_character());
1286        // Corrupt the first embedded kv6's magic. MSHS payload begins:
1287        //   tag(4) len(4) count(4) blob_len(4) <kv6 magic 4 bytes>
1288        let pos = find_tag(&bytes, *b"MSHS");
1289        let kv6_magic_off = pos + 8 + 4 + 4;
1290        bytes[kv6_magic_off] ^= 0xff;
1291        assert!(matches!(parse(&bytes), Err(ParseError::BadMesh(_))));
1292    }
1293
1294    /// Find the byte offset of a top-level chunk `tag` in a serialised
1295    /// character (walks the chunk list from the 6-byte header).
1296    fn find_tag(bytes: &[u8], tag: [u8; 4]) -> usize {
1297        let mut pos = 6; // magic(4) + version(2)
1298        while pos + 8 <= bytes.len() {
1299            let here = &bytes[pos..pos + 4];
1300            let len = u32::from_le_bytes([
1301                bytes[pos + 4],
1302                bytes[pos + 5],
1303                bytes[pos + 6],
1304                bytes[pos + 7],
1305            ]) as usize;
1306            if here == tag {
1307                return pos;
1308            }
1309            pos += 8 + len;
1310        }
1311        panic!("tag {tag:?} not found");
1312    }
1313
1314    // ---- QE.6b adversarial characters: error, never panic/hang ----
1315
1316    #[test]
1317    fn parse_rejects_out_of_range_attachment_index() {
1318        // Pre-QE.6b: parse accepted this and to_kfa_sprite / the
1319        // attachment runtime panicked out-of-bounds later.
1320        let mut c = synthetic_character();
1321        c.bones[1].attachments = vec![Attachment::static_mesh(99)];
1322        let bytes = serialize(&c);
1323        assert!(matches!(
1324            parse(&bytes),
1325            Err(ParseError::BadAttachmentIndex {
1326                bone: 1,
1327                index: 99,
1328                table_len: 2,
1329            })
1330        ));
1331    }
1332
1333    #[test]
1334    fn parse_rejects_bad_or_cyclic_bone_skeleton() {
1335        // Out-of-range parent (pre-QE.6b: OOB panic in the solve).
1336        let mut c = synthetic_character();
1337        c.bones[1].hinge.parent = 42;
1338        assert!(matches!(
1339            parse(&serialize(&c)),
1340            Err(ParseError::BadSkeleton { bone: 1 })
1341        ));
1342        // Cyclic parents (pre-QE.6b: the loader hung forever).
1343        let mut c = synthetic_character();
1344        c.bones[0].hinge.parent = 1;
1345        assert!(matches!(
1346            parse(&serialize(&c)),
1347            Err(ParseError::BadSkeleton { .. })
1348        ));
1349    }
1350}