wow_dbc/tbc_tables/
faction.rs1use 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 Faction {
11 pub rows: Vec<FactionRow>,
12}
13
14impl DbcTable for Faction {
15 type Row = FactionRow;
16
17 const FILENAME: &'static str = "Faction.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 != 212 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 212,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 53 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 53,
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 let id = FactionKey::new(crate::util::read_i32_le(chunk)?);
57
58 let reputation_index = crate::util::read_i32_le(chunk)?;
60
61 let reputation_race_mask = crate::util::read_array_i32::<4>(chunk)?;
63
64 let reputation_class_mask = crate::util::read_array_i32::<4>(chunk)?;
66
67 let reputation_base = crate::util::read_array_i32::<4>(chunk)?;
69
70 let reputation_flags = crate::util::read_array_i32::<4>(chunk)?;
72
73 let parent_faction_id = FactionKey::new(crate::util::read_i32_le(chunk)?.into());
75
76 let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
78
79 let description_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
81
82
83 rows.push(FactionRow {
84 id,
85 reputation_index,
86 reputation_race_mask,
87 reputation_class_mask,
88 reputation_base,
89 reputation_flags,
90 parent_faction_id,
91 name_lang,
92 description_lang,
93 });
94 }
95
96 Ok(Faction { rows, })
97 }
98
99 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
100 let header = DbcHeader {
101 record_count: self.rows.len() as u32,
102 field_count: 53,
103 record_size: 212,
104 string_block_size: self.string_block_size(),
105 };
106
107 b.write_all(&header.write_header())?;
108
109 let mut string_index = 1;
110 for row in &self.rows {
111 b.write_all(&row.id.id.to_le_bytes())?;
113
114 b.write_all(&row.reputation_index.to_le_bytes())?;
116
117 for i in row.reputation_race_mask {
119 b.write_all(&i.to_le_bytes())?;
120 }
121
122
123 for i in row.reputation_class_mask {
125 b.write_all(&i.to_le_bytes())?;
126 }
127
128
129 for i in row.reputation_base {
131 b.write_all(&i.to_le_bytes())?;
132 }
133
134
135 for i in row.reputation_flags {
137 b.write_all(&i.to_le_bytes())?;
138 }
139
140
141 b.write_all(&(row.parent_faction_id.id as i32).to_le_bytes())?;
143
144 b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;
146
147 b.write_all(&row.description_lang.string_indices_as_array(&mut string_index))?;
149
150 }
151
152 self.write_string_block(b)?;
153
154 Ok(())
155 }
156
157}
158
159impl Indexable for Faction {
160 type PrimaryKey = FactionKey;
161 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
162 let key = key.try_into().ok()?;
163 self.rows.iter().find(|a| a.id.id == key.id)
164 }
165
166 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
167 let key = key.try_into().ok()?;
168 self.rows.iter_mut().find(|a| a.id.id == key.id)
169 }
170}
171
172impl Faction {
173 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
174 b.write_all(&[0])?;
175
176 for row in &self.rows {
177 row.name_lang.string_block_as_array(b)?;
178 row.description_lang.string_block_as_array(b)?;
179 }
180
181 Ok(())
182 }
183
184 fn string_block_size(&self) -> u32 {
185 let mut sum = 1;
186 for row in &self.rows {
187 sum += row.name_lang.string_block_size();
188 sum += row.description_lang.string_block_size();
189 }
190
191 sum as u32
192 }
193
194}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
197pub struct FactionKey {
198 pub id: i32
199}
200
201impl FactionKey {
202 pub const fn new(id: i32) -> Self {
203 Self { id }
204 }
205
206}
207
208impl From<u8> for FactionKey {
209 fn from(v: u8) -> Self {
210 Self::new(v.into())
211 }
212}
213
214impl From<u16> for FactionKey {
215 fn from(v: u16) -> Self {
216 Self::new(v.into())
217 }
218}
219
220impl From<i8> for FactionKey {
221 fn from(v: i8) -> Self {
222 Self::new(v.into())
223 }
224}
225
226impl From<i16> for FactionKey {
227 fn from(v: i16) -> Self {
228 Self::new(v.into())
229 }
230}
231
232impl From<i32> for FactionKey {
233 fn from(v: i32) -> Self {
234 Self::new(v)
235 }
236}
237
238impl TryFrom<u32> for FactionKey {
239 type Error = u32;
240 fn try_from(v: u32) -> Result<Self, Self::Error> {
241 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
242 }
243}
244
245impl TryFrom<usize> for FactionKey {
246 type Error = usize;
247 fn try_from(v: usize) -> Result<Self, Self::Error> {
248 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
249 }
250}
251
252impl TryFrom<u64> for FactionKey {
253 type Error = u64;
254 fn try_from(v: u64) -> Result<Self, Self::Error> {
255 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
256 }
257}
258
259impl TryFrom<i64> for FactionKey {
260 type Error = i64;
261 fn try_from(v: i64) -> Result<Self, Self::Error> {
262 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
263 }
264}
265
266impl TryFrom<isize> for FactionKey {
267 type Error = isize;
268 fn try_from(v: isize) -> Result<Self, Self::Error> {
269 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
270 }
271}
272
273#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
274pub struct FactionRow {
275 pub id: FactionKey,
276 pub reputation_index: i32,
277 pub reputation_race_mask: [i32; 4],
278 pub reputation_class_mask: [i32; 4],
279 pub reputation_base: [i32; 4],
280 pub reputation_flags: [i32; 4],
281 pub parent_faction_id: FactionKey,
282 pub name_lang: ExtendedLocalizedString,
283 pub description_lang: ExtendedLocalizedString,
284}
285