wow_dbc/wrath_tables/
animation_data.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use std::io::Write;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct AnimationData {
11    pub rows: Vec<AnimationDataRow>,
12}
13
14impl DbcTable for AnimationData {
15    type Row = AnimationDataRow;
16
17    const FILENAME: &'static str = "AnimationData.dbc";
18
19    fn rows(&self) -> &[Self::Row] { &self.rows }
20    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
21
22    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
23        let mut header = [0_u8; HEADER_SIZE];
24        b.read_exact(&mut header)?;
25        let header = parse_header(&header)?;
26
27        if header.record_size != 32 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 32,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 8 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 8,
40                    actual: header.field_count,
41                },
42            ));
43        }
44
45        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
46        b.read_exact(&mut r)?;
47        let mut string_block = vec![0_u8; header.string_block_size as usize];
48        b.read_exact(&mut string_block)?;
49
50        let mut rows = Vec::with_capacity(header.record_count as usize);
51
52        for mut chunk in r.chunks(header.record_size as usize) {
53            let chunk = &mut chunk;
54
55            // id: primary_key (AnimationData) int32
56            let id = AnimationDataKey::new(crate::util::read_i32_le(chunk)?);
57
58            // name: string_ref
59            let name = {
60                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
61                String::from_utf8(s)?
62            };
63
64            // weaponflags: int32
65            let weaponflags = crate::util::read_i32_le(chunk)?;
66
67            // bodyflags: int32
68            let bodyflags = crate::util::read_i32_le(chunk)?;
69
70            // flags: int32
71            let flags = crate::util::read_i32_le(chunk)?;
72
73            // fallback: foreign_key (AnimationData) int32
74            let fallback = AnimationDataKey::new(crate::util::read_i32_le(chunk)?.into());
75
76            // behavior_id: foreign_key (AnimationData) int32
77            let behavior_id = AnimationDataKey::new(crate::util::read_i32_le(chunk)?.into());
78
79            // behavior_tier: int32
80            let behavior_tier = crate::util::read_i32_le(chunk)?;
81
82
83            rows.push(AnimationDataRow {
84                id,
85                name,
86                weaponflags,
87                bodyflags,
88                flags,
89                fallback,
90                behavior_id,
91                behavior_tier,
92            });
93        }
94
95        Ok(AnimationData { rows, })
96    }
97
98    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
99        let header = DbcHeader {
100            record_count: self.rows.len() as u32,
101            field_count: 8,
102            record_size: 32,
103            string_block_size: self.string_block_size(),
104        };
105
106        b.write_all(&header.write_header())?;
107
108        let mut string_index = 1;
109        for row in &self.rows {
110            // id: primary_key (AnimationData) int32
111            b.write_all(&row.id.id.to_le_bytes())?;
112
113            // name: string_ref
114            if !row.name.is_empty() {
115                b.write_all(&(string_index as u32).to_le_bytes())?;
116                string_index += row.name.len() + 1;
117            }
118            else {
119                b.write_all(&(0_u32).to_le_bytes())?;
120            }
121
122            // weaponflags: int32
123            b.write_all(&row.weaponflags.to_le_bytes())?;
124
125            // bodyflags: int32
126            b.write_all(&row.bodyflags.to_le_bytes())?;
127
128            // flags: int32
129            b.write_all(&row.flags.to_le_bytes())?;
130
131            // fallback: foreign_key (AnimationData) int32
132            b.write_all(&(row.fallback.id as i32).to_le_bytes())?;
133
134            // behavior_id: foreign_key (AnimationData) int32
135            b.write_all(&(row.behavior_id.id as i32).to_le_bytes())?;
136
137            // behavior_tier: int32
138            b.write_all(&row.behavior_tier.to_le_bytes())?;
139
140        }
141
142        self.write_string_block(b)?;
143
144        Ok(())
145    }
146
147}
148
149impl Indexable for AnimationData {
150    type PrimaryKey = AnimationDataKey;
151    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
152        let key = key.try_into().ok()?;
153        self.rows.iter().find(|a| a.id.id == key.id)
154    }
155
156    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
157        let key = key.try_into().ok()?;
158        self.rows.iter_mut().find(|a| a.id.id == key.id)
159    }
160}
161
162impl AnimationData {
163    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
164        b.write_all(&[0])?;
165
166        for row in &self.rows {
167            if !row.name.is_empty() { b.write_all(row.name.as_bytes())?; b.write_all(&[0])?; };
168        }
169
170        Ok(())
171    }
172
173    fn string_block_size(&self) -> u32 {
174        let mut sum = 1;
175        for row in &self.rows {
176            if !row.name.is_empty() { sum += row.name.len() + 1; };
177        }
178
179        sum as u32
180    }
181
182}
183
184#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
185pub struct AnimationDataKey {
186    pub id: i32
187}
188
189impl AnimationDataKey {
190    pub const fn new(id: i32) -> Self {
191        Self { id }
192    }
193
194}
195
196impl From<u8> for AnimationDataKey {
197    fn from(v: u8) -> Self {
198        Self::new(v.into())
199    }
200}
201
202impl From<u16> for AnimationDataKey {
203    fn from(v: u16) -> Self {
204        Self::new(v.into())
205    }
206}
207
208impl From<i8> for AnimationDataKey {
209    fn from(v: i8) -> Self {
210        Self::new(v.into())
211    }
212}
213
214impl From<i16> for AnimationDataKey {
215    fn from(v: i16) -> Self {
216        Self::new(v.into())
217    }
218}
219
220impl From<i32> for AnimationDataKey {
221    fn from(v: i32) -> Self {
222        Self::new(v)
223    }
224}
225
226impl TryFrom<u32> for AnimationDataKey {
227    type Error = u32;
228    fn try_from(v: u32) -> Result<Self, Self::Error> {
229        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
230    }
231}
232
233impl TryFrom<usize> for AnimationDataKey {
234    type Error = usize;
235    fn try_from(v: usize) -> Result<Self, Self::Error> {
236        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
237    }
238}
239
240impl TryFrom<u64> for AnimationDataKey {
241    type Error = u64;
242    fn try_from(v: u64) -> Result<Self, Self::Error> {
243        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
244    }
245}
246
247impl TryFrom<i64> for AnimationDataKey {
248    type Error = i64;
249    fn try_from(v: i64) -> Result<Self, Self::Error> {
250        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
251    }
252}
253
254impl TryFrom<isize> for AnimationDataKey {
255    type Error = isize;
256    fn try_from(v: isize) -> Result<Self, Self::Error> {
257        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
258    }
259}
260
261#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
262pub struct AnimationDataRow {
263    pub id: AnimationDataKey,
264    pub name: String,
265    pub weaponflags: i32,
266    pub bodyflags: i32,
267    pub flags: i32,
268    pub fallback: AnimationDataKey,
269    pub behavior_id: AnimationDataKey,
270    pub behavior_tier: i32,
271}
272