wow_dbc/vanilla_tables/
cfg_configs.rs

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