wow_dbc/vanilla_tables/
skill_costs_data.rs

1use 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 SkillCostsData {
11    pub rows: Vec<SkillCostsDataRow>,
12}
13
14impl DbcTable for SkillCostsData {
15    type Row = SkillCostsDataRow;
16
17    const FILENAME: &'static str = "SkillCostsData.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 != 20 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 20,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 5 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 5,
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            // id: primary_key (SkillCostsData) uint32
54            let id = SkillCostsDataKey::new(crate::util::read_u32_le(chunk)?);
55
56            // skill_costs: int32
57            let skill_costs = crate::util::read_i32_le(chunk)?;
58
59            // cost: int32[3]
60            let cost = crate::util::read_array_i32::<3>(chunk)?;
61
62
63            rows.push(SkillCostsDataRow {
64                id,
65                skill_costs,
66                cost,
67            });
68        }
69
70        Ok(SkillCostsData { 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: 5,
77            record_size: 20,
78            string_block_size: 1,
79        };
80
81        b.write_all(&header.write_header())?;
82
83        for row in &self.rows {
84            // id: primary_key (SkillCostsData) uint32
85            b.write_all(&row.id.id.to_le_bytes())?;
86
87            // skill_costs: int32
88            b.write_all(&row.skill_costs.to_le_bytes())?;
89
90            // cost: int32[3]
91            for i in row.cost {
92                b.write_all(&i.to_le_bytes())?;
93            }
94
95
96        }
97
98        b.write_all(&[0_u8])?;
99
100        Ok(())
101    }
102
103}
104
105impl Indexable for SkillCostsData {
106    type PrimaryKey = SkillCostsDataKey;
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
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
119pub struct SkillCostsDataKey {
120    pub id: u32
121}
122
123impl SkillCostsDataKey {
124    pub const fn new(id: u32) -> Self {
125        Self { id }
126    }
127
128}
129
130impl From<u8> for SkillCostsDataKey {
131    fn from(v: u8) -> Self {
132        Self::new(v.into())
133    }
134}
135
136impl From<u16> for SkillCostsDataKey {
137    fn from(v: u16) -> Self {
138        Self::new(v.into())
139    }
140}
141
142impl From<u32> for SkillCostsDataKey {
143    fn from(v: u32) -> Self {
144        Self::new(v)
145    }
146}
147
148impl TryFrom<u64> for SkillCostsDataKey {
149    type Error = u64;
150    fn try_from(v: u64) -> Result<Self, Self::Error> {
151        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
152    }
153}
154
155impl TryFrom<usize> for SkillCostsDataKey {
156    type Error = usize;
157    fn try_from(v: usize) -> Result<Self, Self::Error> {
158        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
159    }
160}
161
162impl TryFrom<i8> for SkillCostsDataKey {
163    type Error = i8;
164    fn try_from(v: i8) -> Result<Self, Self::Error> {
165        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
166    }
167}
168
169impl TryFrom<i16> for SkillCostsDataKey {
170    type Error = i16;
171    fn try_from(v: i16) -> Result<Self, Self::Error> {
172        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
173    }
174}
175
176impl TryFrom<i32> for SkillCostsDataKey {
177    type Error = i32;
178    fn try_from(v: i32) -> Result<Self, Self::Error> {
179        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
180    }
181}
182
183impl TryFrom<i64> for SkillCostsDataKey {
184    type Error = i64;
185    fn try_from(v: i64) -> Result<Self, Self::Error> {
186        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
187    }
188}
189
190impl TryFrom<isize> for SkillCostsDataKey {
191    type Error = isize;
192    fn try_from(v: isize) -> Result<Self, Self::Error> {
193        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
194    }
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
198pub struct SkillCostsDataRow {
199    pub id: SkillCostsDataKey,
200    pub skill_costs: i32,
201    pub cost: [i32; 3],
202}
203