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