Skip to main content

rpfm_lib/files/anim/
mod.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Animation file handler with partial support.
12//!
13//! This module provides the [`Anim`] type for handling animation files (`.anim`) in
14//! Total War PackFiles. Animation files contain skeletal animation data used by the
15//! game engine for character and unit animations.
16//!
17//! # File Format
18//!
19//! Animation files consist of a header followed by binary animation data. The header
20//! contains metadata about the animation:
21//! - Version number
22//! - Frame rate
23//! - Skeleton name
24//! - End time (duration)
25//! - Bone count
26//!
27//! # Limited Support
28//!
29//! Support is currently limited because:
30//! - **Header only**: Only the header is parsed into structured fields
31//! - **Binary data**: Animation data remains as raw bytes
32//! - **No keyframe access**: Individual animation keyframes cannot be accessed or modified
33//!
34//! This allows basic metadata inspection and preservation of the animation data when
35//! re-encoding, but does not enable deep editing of animation curves or keyframes.
36//!
37//! # Use Cases
38//!
39//! - Extracting animation metadata (skeleton name, frame rate, duration)
40//! - Identifying which skeleton an animation is for
41//! - Re-packing animations without modification
42//! - Bulk operations on animation files
43//!
44//! # Example
45//!
46//! ```no_run
47//! use rpfm_lib::files::{Decodeable, anim::Anim};
48//! use std::io::Cursor;
49//!
50//! # let anim_bytes = vec![];
51//! let mut reader = Cursor::new(anim_bytes);
52//! let anim = Anim::decode(&mut reader, &None).unwrap();
53//!
54//! // Access metadata
55//! println!("Skeleton: {}", anim.skeleton_name());
56//! println!("Frame rate: {} fps", anim.frame_rate());
57//! println!("Duration: {} seconds", anim.end_time());
58//! println!("Bones: {}", anim.bone_count());
59//! ```
60
61use getset::*;
62use serde_derive::{Serialize, Deserialize};
63
64use crate::binary::{ReadBytes, WriteBytes};
65use crate::error::Result;
66use crate::files::{DecodeableExtraData, Decodeable, EncodeableExtraData, Encodeable};
67use crate::utils::check_size_mismatch;
68
69pub use crate::files::detection::anim::EXTENSION;
70
71//mod versions;
72
73#[cfg(test)] mod anim_test;
74
75//---------------------------------------------------------------------------//
76//                              Enum & Structs
77//---------------------------------------------------------------------------//
78
79/// Partially decoded animation file.
80///
81/// Contains parsed header metadata and raw binary animation data. The header provides
82/// information about the animation's properties, while the actual keyframe data remains
83/// in binary form.
84///
85/// # Fields
86///
87/// * `version` - Animation file format version
88/// * `uk_1` - Unknown field (purpose not yet identified)
89/// * `frame_rate` - Animation playback speed in frames per second
90/// * `skeleton_name` - Name of the skeleton this animation is for
91/// * `end_time` - Animation duration in seconds
92/// * `bone_count` - Number of bones animated in this file
93/// * `data` - Raw binary animation data (keyframes, curves, etc.)
94///
95/// # Getters/Setters
96///
97/// All fields have public getters, mutable getters, and setters via the `getset` crate:
98/// - `version()`, `version_mut()`, `set_version()`
99/// - `uk_1()`, `uk_1_mut()`, `set_uk_1()`
100/// - `frame_rate()`, `frame_rate_mut()`, `set_frame_rate()`
101/// - `skeleton_name()`, `skeleton_name_mut()`, `set_skeleton_name()`
102/// - `end_time()`, `end_time_mut()`, `set_end_time()`
103/// - `bone_count()`, `bone_count_mut()`, `set_bone_count()`
104/// - `data()`, `data_mut()`, `set_data()`
105///
106/// # Example
107///
108/// ```no_run
109/// use rpfm_lib::files::{Decodeable, anim::Anim};
110/// use std::io::Cursor;
111///
112/// # let anim_data = vec![];
113/// let mut reader = Cursor::new(anim_data);
114/// let anim = Anim::decode(&mut reader, &None).unwrap();
115///
116/// // Check if animation matches expected skeleton
117/// if anim.skeleton_name().contains("humanoid") {
118///     println!("Found humanoid animation: {} seconds at {} fps",
119///         anim.end_time(), anim.frame_rate());
120/// }
121/// ```
122#[derive(PartialEq, Clone, Debug, Default, Getters, MutGetters, Setters, Serialize, Deserialize)]
123#[getset(get = "pub", get_mut = "pub", set = "pub")]
124pub struct Anim {
125    /// Animation file format version.
126    version: u32,
127
128    /// Unknown field (purpose not yet identified).
129    uk_1: u32,
130
131    /// Animation playback speed in frames per second.
132    frame_rate: f32,
133
134    /// Name of the skeleton this animation targets.
135    skeleton_name: String,
136
137    /// Animation duration in seconds.
138    end_time: f32,
139
140    /// Number of bones animated in this file.
141    bone_count: u32,
142
143    /// Raw binary animation data (keyframes, curves, etc.).
144    data: Vec<u8>,
145}
146
147//---------------------------------------------------------------------------//
148//                          Implementation of Anim
149//---------------------------------------------------------------------------//
150
151impl Decodeable for Anim {
152
153    fn decode<R: ReadBytes>(data: &mut R, _extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
154        let mut anim = Self::default();
155        anim.version = data.read_u32()?;
156        anim.uk_1 = data.read_u32()?;
157        anim.frame_rate = data.read_f32()?;
158        anim.skeleton_name = data.read_sized_string_u8()?;
159        anim.end_time = data.read_f32()?;
160        anim.bone_count = data.read_u32()?;
161
162        let data_left = data.len()?.checked_sub(data.stream_position()?);
163        if let Some(data_left) = data_left {
164            anim.data = data.read_slice(data_left as usize, false)?;
165        }
166
167        // If we are not in the last byte, it means we didn't parse the entire file, which means this file is corrupt.
168        check_size_mismatch(data.stream_position()? as usize, data.len()? as usize)?;
169
170        Ok(anim)
171    }
172}
173
174impl Encodeable for Anim {
175
176    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData>) -> Result<()> {
177        buffer.write_u32(self.version)?;
178        buffer.write_u32(self.uk_1)?;
179        buffer.write_f32(self.frame_rate)?;
180        buffer.write_sized_string_u8(self.skeleton_name())?;
181        buffer.write_f32(self.end_time)?;
182        buffer.write_u32(self.bone_count)?;
183        buffer.write_all(self.data())?;
184
185        Ok(())
186    }
187}