wow_dbc/vanilla_tables/
creature_spell_data.rs1use 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 CreatureSpellData {
11 pub rows: Vec<CreatureSpellDataRow>,
12}
13
14impl DbcTable for CreatureSpellData {
15 type Row = CreatureSpellDataRow;
16
17 const FILENAME: &'static str = "CreatureSpellData.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 != 36 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 36,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 9 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 9,
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
48 let mut rows = Vec::with_capacity(header.record_count as usize);
49
50 for mut chunk in r.chunks(header.record_size as usize) {
51 let chunk = &mut chunk;
52
53 let id = CreatureSpellDataKey::new(crate::util::read_u32_le(chunk)?);
55
56 let spell = crate::util::read_array_u32::<4>(chunk)?;
58
59 let cooldown_time_1 = crate::util::read_array_i32::<4>(chunk)?;
61
62
63 rows.push(CreatureSpellDataRow {
64 id,
65 spell,
66 cooldown_time_1,
67 });
68 }
69
70 Ok(CreatureSpellData { rows, })
71 }
72
73 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
74 let header = DbcHeader {
75 record_count: self.rows.len() as u32,
76 field_count: 9,
77 record_size: 36,
78 string_block_size: 1,
79 };
80
81 b.write_all(&header.write_header())?;
82
83 for row in &self.rows {
84 b.write_all(&row.id.id.to_le_bytes())?;
86
87 for i in row.spell {
89 b.write_all(&i.to_le_bytes())?;
90 }
91
92
93 for i in row.cooldown_time_1 {
95 b.write_all(&i.to_le_bytes())?;
96 }
97
98
99 }
100
101 b.write_all(&[0_u8])?;
102
103 Ok(())
104 }
105
106}
107
108impl Indexable for CreatureSpellData {
109 type PrimaryKey = CreatureSpellDataKey;
110 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
111 let key = key.try_into().ok()?;
112 self.rows.iter().find(|a| a.id.id == key.id)
113 }
114
115 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
116 let key = key.try_into().ok()?;
117 self.rows.iter_mut().find(|a| a.id.id == key.id)
118 }
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
122pub struct CreatureSpellDataKey {
123 pub id: u32
124}
125
126impl CreatureSpellDataKey {
127 pub const fn new(id: u32) -> Self {
128 Self { id }
129 }
130
131}
132
133impl From<u8> for CreatureSpellDataKey {
134 fn from(v: u8) -> Self {
135 Self::new(v.into())
136 }
137}
138
139impl From<u16> for CreatureSpellDataKey {
140 fn from(v: u16) -> Self {
141 Self::new(v.into())
142 }
143}
144
145impl From<u32> for CreatureSpellDataKey {
146 fn from(v: u32) -> Self {
147 Self::new(v)
148 }
149}
150
151impl TryFrom<u64> for CreatureSpellDataKey {
152 type Error = u64;
153 fn try_from(v: u64) -> Result<Self, Self::Error> {
154 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
155 }
156}
157
158impl TryFrom<usize> for CreatureSpellDataKey {
159 type Error = usize;
160 fn try_from(v: usize) -> Result<Self, Self::Error> {
161 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
162 }
163}
164
165impl TryFrom<i8> for CreatureSpellDataKey {
166 type Error = i8;
167 fn try_from(v: i8) -> Result<Self, Self::Error> {
168 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
169 }
170}
171
172impl TryFrom<i16> for CreatureSpellDataKey {
173 type Error = i16;
174 fn try_from(v: i16) -> Result<Self, Self::Error> {
175 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
176 }
177}
178
179impl TryFrom<i32> for CreatureSpellDataKey {
180 type Error = i32;
181 fn try_from(v: i32) -> Result<Self, Self::Error> {
182 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
183 }
184}
185
186impl TryFrom<i64> for CreatureSpellDataKey {
187 type Error = i64;
188 fn try_from(v: i64) -> Result<Self, Self::Error> {
189 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
190 }
191}
192
193impl TryFrom<isize> for CreatureSpellDataKey {
194 type Error = isize;
195 fn try_from(v: isize) -> Result<Self, Self::Error> {
196 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
197 }
198}
199
200#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
201pub struct CreatureSpellDataRow {
202 pub id: CreatureSpellDataKey,
203 pub spell: [u32; 4],
204 pub cooldown_time_1: [i32; 4],
205}
206