wow_dbc/wrath_tables/
glyph_properties.rs

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