rakata_formats/lip/
writer.rs

1//! LIP binary writer.
2
3use std::io::{Cursor, Write};
4
5use crate::binary::{write_f32, write_u32, write_u8};
6
7use super::{Lip, LipBinaryError, LIP_MAGIC, LIP_VERSION_V10};
8
9/// Writes a LIP file to a writer.
10#[cfg_attr(
11    feature = "tracing",
12    tracing::instrument(level = "debug", skip(writer, lip))
13)]
14pub fn write_lip<W: Write>(writer: &mut W, lip: &Lip) -> Result<(), LipBinaryError> {
15    let entry_count = u32::try_from(lip.keyframes.len())
16        .map_err(|_| LipBinaryError::ValueOverflow("entry_count"))?;
17
18    writer.write_all(&LIP_MAGIC)?;
19    writer.write_all(&LIP_VERSION_V10)?;
20    write_f32(writer, lip.length)?;
21    write_u32(writer, entry_count)?;
22
23    for keyframe in &lip.keyframes {
24        write_f32(writer, keyframe.time)?;
25        write_u8(writer, keyframe.shape.raw_id())?;
26    }
27
28    Ok(())
29}
30
31/// Serializes a LIP file into bytes.
32#[cfg_attr(feature = "tracing", tracing::instrument(level = "debug", skip(lip)))]
33pub fn write_lip_to_vec(lip: &Lip) -> Result<Vec<u8>, LipBinaryError> {
34    let mut cursor = Cursor::new(Vec::new());
35    write_lip(&mut cursor, lip)?;
36    Ok(cursor.into_inner())
37}