wow_dbc/wrath_tables/
lfg_dungeon_group.rs

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