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