wow_dbc/vanilla_tables/
spell_effect_names.rs

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