wow_vanilla_dbc/tables/
durability_quality.rs

1use crate::header::{HEADER_SIZE, DbcHeader};
2use crate::header;
3use crate::DbcTable;
4use std::io::Write;
5use crate::Indexable;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct DurabilityQuality {
9    pub rows: Vec<DurabilityQualityRow>,
10}
11
12impl DbcTable for DurabilityQuality {
13    type Row = DurabilityQualityRow;
14
15    fn filename() -> &'static str { "DurabilityQuality.dbc" }
16
17    fn rows(&self) -> &[Self::Row] { &self.rows }
18    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
19
20    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
21        let mut header = [0_u8; HEADER_SIZE];
22        b.read_exact(&mut header)?;
23        let header = header::parse_header(&header)?;
24
25        if header.record_size != 8 {
26            return Err(crate::DbcError::InvalidHeader(
27                crate::InvalidHeaderError::RecordSize {
28                    expected: 8,
29                    actual: header.record_size,
30                },
31            ));
32        }
33
34        if header.field_count != 2 {
35            return Err(crate::DbcError::InvalidHeader(
36                crate::InvalidHeaderError::FieldCount {
37                    expected: 8,
38                    actual: header.field_count,
39                },
40            ));
41        }
42
43        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
44        b.read_exact(&mut r)?;
45
46        let mut rows = Vec::with_capacity(header.record_count as usize);
47
48        for mut chunk in r.chunks(header.record_size as usize) {
49            let chunk = &mut chunk;
50
51            // id: primary_key (DurabilityQuality) uint32
52            let id = DurabilityQualityKey::new(crate::util::read_u32_le(chunk)?);
53
54            // data: float
55            let data = crate::util::read_f32_le(chunk)?;
56
57
58            rows.push(DurabilityQualityRow {
59                id,
60                data,
61            });
62        }
63
64        Ok(DurabilityQuality { rows, })
65    }
66
67    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
68        let header = DbcHeader {
69            record_count: self.rows.len() as u32,
70            field_count: 2,
71            record_size: 8,
72            string_block_size: 1,
73        };
74
75        b.write_all(&header.write_header())?;
76
77        for row in &self.rows {
78            // id: primary_key (DurabilityQuality) uint32
79            b.write_all(&row.id.id.to_le_bytes())?;
80
81            // data: float
82            b.write_all(&row.data.to_le_bytes())?;
83
84        }
85
86        b.write_all(&[0_u8])?;
87
88        Ok(())
89    }
90
91}
92
93impl Indexable for DurabilityQuality {
94    type PrimaryKey = DurabilityQualityKey;
95    fn get(&self, key: &Self::PrimaryKey) -> Option<&Self::Row> {
96        self.rows.iter().find(|a| a.id.id == key.id)
97    }
98
99    fn get_mut(&mut self, key: &Self::PrimaryKey) -> Option<&mut Self::Row> {
100        self.rows.iter_mut().find(|a| a.id.id == key.id)
101    }
102
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
106pub struct DurabilityQualityKey {
107    pub id: u32
108}
109
110impl DurabilityQualityKey {
111    pub const fn new(id: u32) -> Self {
112        Self { id }
113    }
114
115}
116
117#[derive(Debug, Clone, PartialEq)]
118pub struct DurabilityQualityRow {
119    pub id: DurabilityQualityKey,
120    pub data: f32,
121}
122