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