wow_dbc/vanilla_tables/
attack_anim_kits.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::animation_data::AnimationDataKey;
8use crate::vanilla_tables::attack_anim_types::AttackAnimTypesKey;
9use std::io::Write;
10use wow_world_base::vanilla::AttackHand;
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct AttackAnimKits {
14    pub rows: Vec<AttackAnimKitsRow>,
15}
16
17impl DbcTable for AttackAnimKits {
18    type Row = AttackAnimKitsRow;
19
20    const FILENAME: &'static str = "AttackAnimKits.dbc";
21
22    fn rows(&self) -> &[Self::Row] { &self.rows }
23    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
24
25    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
26        let mut header = [0_u8; HEADER_SIZE];
27        b.read_exact(&mut header)?;
28        let header = parse_header(&header)?;
29
30        if header.record_size != 20 {
31            return Err(crate::DbcError::InvalidHeader(
32                crate::InvalidHeaderError::RecordSize {
33                    expected: 20,
34                    actual: header.record_size,
35                },
36            ));
37        }
38
39        if header.field_count != 5 {
40            return Err(crate::DbcError::InvalidHeader(
41                crate::InvalidHeaderError::FieldCount {
42                    expected: 5,
43                    actual: header.field_count,
44                },
45            ));
46        }
47
48        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
49        b.read_exact(&mut r)?;
50
51        let mut rows = Vec::with_capacity(header.record_count as usize);
52
53        for mut chunk in r.chunks(header.record_size as usize) {
54            let chunk = &mut chunk;
55
56            // id: primary_key (AttackAnimKits) uint32
57            let id = AttackAnimKitsKey::new(crate::util::read_u32_le(chunk)?);
58
59            // animation_data: foreign_key (AnimationData) uint32
60            let animation_data = AnimationDataKey::new(crate::util::read_u32_le(chunk)?.into());
61
62            // attack_anim_type: foreign_key (AttackAnimTypes) uint32
63            let attack_anim_type = AttackAnimTypesKey::new(crate::util::read_u32_le(chunk)?.into());
64
65            // animation_frequency: uint32
66            let animation_frequency = crate::util::read_u32_le(chunk)?;
67
68            // flags: AttackHand
69            let flags = crate::util::read_i32_le(chunk)?.try_into()?;
70
71
72            rows.push(AttackAnimKitsRow {
73                id,
74                animation_data,
75                attack_anim_type,
76                animation_frequency,
77                flags,
78            });
79        }
80
81        Ok(AttackAnimKits { rows, })
82    }
83
84    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
85        let header = DbcHeader {
86            record_count: self.rows.len() as u32,
87            field_count: 5,
88            record_size: 20,
89            string_block_size: 1,
90        };
91
92        b.write_all(&header.write_header())?;
93
94        for row in &self.rows {
95            // id: primary_key (AttackAnimKits) uint32
96            b.write_all(&row.id.id.to_le_bytes())?;
97
98            // animation_data: foreign_key (AnimationData) uint32
99            b.write_all(&(row.animation_data.id as u32).to_le_bytes())?;
100
101            // attack_anim_type: foreign_key (AttackAnimTypes) uint32
102            b.write_all(&(row.attack_anim_type.id as u32).to_le_bytes())?;
103
104            // animation_frequency: uint32
105            b.write_all(&row.animation_frequency.to_le_bytes())?;
106
107            // flags: AttackHand
108            b.write_all(&(row.flags.as_int() as i32).to_le_bytes())?;
109
110        }
111
112        b.write_all(&[0_u8])?;
113
114        Ok(())
115    }
116
117}
118
119impl Indexable for AttackAnimKits {
120    type PrimaryKey = AttackAnimKitsKey;
121    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
122        let key = key.try_into().ok()?;
123        self.rows.iter().find(|a| a.id.id == key.id)
124    }
125
126    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
127        let key = key.try_into().ok()?;
128        self.rows.iter_mut().find(|a| a.id.id == key.id)
129    }
130}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
133pub struct AttackAnimKitsKey {
134    pub id: u32
135}
136
137impl AttackAnimKitsKey {
138    pub const fn new(id: u32) -> Self {
139        Self { id }
140    }
141
142}
143
144impl From<u8> for AttackAnimKitsKey {
145    fn from(v: u8) -> Self {
146        Self::new(v.into())
147    }
148}
149
150impl From<u16> for AttackAnimKitsKey {
151    fn from(v: u16) -> Self {
152        Self::new(v.into())
153    }
154}
155
156impl From<u32> for AttackAnimKitsKey {
157    fn from(v: u32) -> Self {
158        Self::new(v)
159    }
160}
161
162impl TryFrom<u64> for AttackAnimKitsKey {
163    type Error = u64;
164    fn try_from(v: u64) -> Result<Self, Self::Error> {
165        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
166    }
167}
168
169impl TryFrom<usize> for AttackAnimKitsKey {
170    type Error = usize;
171    fn try_from(v: usize) -> Result<Self, Self::Error> {
172        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
173    }
174}
175
176impl TryFrom<i8> for AttackAnimKitsKey {
177    type Error = i8;
178    fn try_from(v: i8) -> Result<Self, Self::Error> {
179        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
180    }
181}
182
183impl TryFrom<i16> for AttackAnimKitsKey {
184    type Error = i16;
185    fn try_from(v: i16) -> Result<Self, Self::Error> {
186        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
187    }
188}
189
190impl TryFrom<i32> for AttackAnimKitsKey {
191    type Error = i32;
192    fn try_from(v: i32) -> Result<Self, Self::Error> {
193        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
194    }
195}
196
197impl TryFrom<i64> for AttackAnimKitsKey {
198    type Error = i64;
199    fn try_from(v: i64) -> Result<Self, Self::Error> {
200        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
201    }
202}
203
204impl TryFrom<isize> for AttackAnimKitsKey {
205    type Error = isize;
206    fn try_from(v: isize) -> Result<Self, Self::Error> {
207        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
208    }
209}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
212pub struct AttackAnimKitsRow {
213    pub id: AttackAnimKitsKey,
214    pub animation_data: AnimationDataKey,
215    pub attack_anim_type: AttackAnimTypesKey,
216    pub animation_frequency: u32,
217    pub flags: AttackHand,
218}
219