wow_dbc/vanilla_tables/
faction.rs1use crate::{
2 DbcTable, Indexable, LocalizedString,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use std::io::Write;
8use wow_world_base::vanilla::{
9 AllowedRace, ReputationFlags,
10};
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct Faction {
14 pub rows: Vec<FactionRow>,
15}
16
17impl DbcTable for Faction {
18 type Row = FactionRow;
19
20 const FILENAME: &'static str = "Faction.dbc";
21
22 fn rows(&self) -> &[Self::Row] { &self.rows }
23 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
24
25 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
26 let mut header = [0_u8; HEADER_SIZE];
27 b.read_exact(&mut header)?;
28 let header = parse_header(&header)?;
29
30 if header.record_size != 148 {
31 return Err(crate::DbcError::InvalidHeader(
32 crate::InvalidHeaderError::RecordSize {
33 expected: 148,
34 actual: header.record_size,
35 },
36 ));
37 }
38
39 if header.field_count != 37 {
40 return Err(crate::DbcError::InvalidHeader(
41 crate::InvalidHeaderError::FieldCount {
42 expected: 37,
43 actual: header.field_count,
44 },
45 ));
46 }
47
48 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
49 b.read_exact(&mut r)?;
50 let mut string_block = vec![0_u8; header.string_block_size as usize];
51 b.read_exact(&mut string_block)?;
52
53 let mut rows = Vec::with_capacity(header.record_count as usize);
54
55 for mut chunk in r.chunks(header.record_size as usize) {
56 let chunk = &mut chunk;
57
58 let id = FactionKey::new(crate::util::read_u32_le(chunk)?);
60
61 let reputation_index = crate::util::read_u32_le(chunk)?;
63
64 let reputation_race_mask = {
66 let mut arr = [AllowedRace::default(); 4];
67 for i in arr.iter_mut() {
68 *i = AllowedRace::new(crate::util::read_i32_le(chunk)? as _);
69 }
70
71 arr
72 };
73
74 let reputation_class_mask = crate::util::read_array_u32::<4>(chunk)?;
76
77 let reputation_base = crate::util::read_array_u32::<4>(chunk)?;
79
80 let reputation_flags = {
82 let mut arr = [ReputationFlags::default(); 4];
83 for i in arr.iter_mut() {
84 *i = ReputationFlags::new(crate::util::read_i32_le(chunk)? as _);
85 }
86
87 arr
88 };
89
90 let parent_faction = FactionKey::new(crate::util::read_u32_le(chunk)?.into());
92
93 let name = crate::util::read_localized_string(chunk, &string_block)?;
95
96 let description = crate::util::read_localized_string(chunk, &string_block)?;
98
99
100 rows.push(FactionRow {
101 id,
102 reputation_index,
103 reputation_race_mask,
104 reputation_class_mask,
105 reputation_base,
106 reputation_flags,
107 parent_faction,
108 name,
109 description,
110 });
111 }
112
113 Ok(Faction { rows, })
114 }
115
116 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
117 let header = DbcHeader {
118 record_count: self.rows.len() as u32,
119 field_count: 37,
120 record_size: 148,
121 string_block_size: self.string_block_size(),
122 };
123
124 b.write_all(&header.write_header())?;
125
126 let mut string_index = 1;
127 for row in &self.rows {
128 b.write_all(&row.id.id.to_le_bytes())?;
130
131 b.write_all(&row.reputation_index.to_le_bytes())?;
133
134 for i in row.reputation_race_mask {
136 b.write_all(&(i.as_int() as i32).to_le_bytes())?;
137 }
138
139
140 for i in row.reputation_class_mask {
142 b.write_all(&i.to_le_bytes())?;
143 }
144
145
146 for i in row.reputation_base {
148 b.write_all(&i.to_le_bytes())?;
149 }
150
151
152 for i in row.reputation_flags {
154 b.write_all(&(i.as_int() as i32).to_le_bytes())?;
155 }
156
157
158 b.write_all(&(row.parent_faction.id as u32).to_le_bytes())?;
160
161 b.write_all(&row.name.string_indices_as_array(&mut string_index))?;
163
164 b.write_all(&row.description.string_indices_as_array(&mut string_index))?;
166
167 }
168
169 self.write_string_block(b)?;
170
171 Ok(())
172 }
173
174}
175
176impl Indexable for Faction {
177 type PrimaryKey = FactionKey;
178 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
179 let key = key.try_into().ok()?;
180 self.rows.iter().find(|a| a.id.id == key.id)
181 }
182
183 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
184 let key = key.try_into().ok()?;
185 self.rows.iter_mut().find(|a| a.id.id == key.id)
186 }
187}
188
189impl Faction {
190 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
191 b.write_all(&[0])?;
192
193 for row in &self.rows {
194 row.name.string_block_as_array(b)?;
195 row.description.string_block_as_array(b)?;
196 }
197
198 Ok(())
199 }
200
201 fn string_block_size(&self) -> u32 {
202 let mut sum = 1;
203 for row in &self.rows {
204 sum += row.name.string_block_size();
205 sum += row.description.string_block_size();
206 }
207
208 sum as u32
209 }
210
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
214pub struct FactionKey {
215 pub id: u32
216}
217
218impl FactionKey {
219 pub const fn new(id: u32) -> Self {
220 Self { id }
221 }
222
223}
224
225impl From<u8> for FactionKey {
226 fn from(v: u8) -> Self {
227 Self::new(v.into())
228 }
229}
230
231impl From<u16> for FactionKey {
232 fn from(v: u16) -> Self {
233 Self::new(v.into())
234 }
235}
236
237impl From<u32> for FactionKey {
238 fn from(v: u32) -> Self {
239 Self::new(v)
240 }
241}
242
243impl TryFrom<u64> for FactionKey {
244 type Error = u64;
245 fn try_from(v: u64) -> Result<Self, Self::Error> {
246 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
247 }
248}
249
250impl TryFrom<usize> for FactionKey {
251 type Error = usize;
252 fn try_from(v: usize) -> Result<Self, Self::Error> {
253 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
254 }
255}
256
257impl TryFrom<i8> for FactionKey {
258 type Error = i8;
259 fn try_from(v: i8) -> Result<Self, Self::Error> {
260 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
261 }
262}
263
264impl TryFrom<i16> for FactionKey {
265 type Error = i16;
266 fn try_from(v: i16) -> Result<Self, Self::Error> {
267 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
268 }
269}
270
271impl TryFrom<i32> for FactionKey {
272 type Error = i32;
273 fn try_from(v: i32) -> Result<Self, Self::Error> {
274 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
275 }
276}
277
278impl TryFrom<i64> for FactionKey {
279 type Error = i64;
280 fn try_from(v: i64) -> Result<Self, Self::Error> {
281 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
282 }
283}
284
285impl TryFrom<isize> for FactionKey {
286 type Error = isize;
287 fn try_from(v: isize) -> Result<Self, Self::Error> {
288 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
289 }
290}
291
292#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
293pub struct FactionRow {
294 pub id: FactionKey,
295 pub reputation_index: u32,
296 pub reputation_race_mask: [AllowedRace; 4],
297 pub reputation_class_mask: [u32; 4],
298 pub reputation_base: [u32; 4],
299 pub reputation_flags: [ReputationFlags; 4],
300 pub parent_faction: FactionKey,
301 pub name: LocalizedString,
302 pub description: LocalizedString,
303}
304