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