wow_dbc/vanilla_tables/
unit_blood_levels.rs1use 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 UnitBloodLevels {
11 pub rows: Vec<UnitBloodLevelsRow>,
12}
13
14impl DbcTable for UnitBloodLevels {
15 type Row = UnitBloodLevelsRow;
16
17 const FILENAME: &'static str = "UnitBloodLevels.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 != 16 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 16,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 4 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 4,
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 let id = UnitBloodLevelsKey::new(crate::util::read_u32_le(chunk)?);
55
56 let violence_level = crate::util::read_array_i32::<3>(chunk)?;
58
59
60 rows.push(UnitBloodLevelsRow {
61 id,
62 violence_level,
63 });
64 }
65
66 Ok(UnitBloodLevels { 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: 4,
73 record_size: 16,
74 string_block_size: 1,
75 };
76
77 b.write_all(&header.write_header())?;
78
79 for row in &self.rows {
80 b.write_all(&row.id.id.to_le_bytes())?;
82
83 for i in row.violence_level {
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 UnitBloodLevels {
99 type PrimaryKey = UnitBloodLevelsKey;
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 UnitBloodLevelsKey {
113 pub id: u32
114}
115
116impl UnitBloodLevelsKey {
117 pub const fn new(id: u32) -> Self {
118 Self { id }
119 }
120
121}
122
123impl From<u8> for UnitBloodLevelsKey {
124 fn from(v: u8) -> Self {
125 Self::new(v.into())
126 }
127}
128
129impl From<u16> for UnitBloodLevelsKey {
130 fn from(v: u16) -> Self {
131 Self::new(v.into())
132 }
133}
134
135impl From<u32> for UnitBloodLevelsKey {
136 fn from(v: u32) -> Self {
137 Self::new(v)
138 }
139}
140
141impl TryFrom<u64> for UnitBloodLevelsKey {
142 type Error = u64;
143 fn try_from(v: u64) -> Result<Self, Self::Error> {
144 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
145 }
146}
147
148impl TryFrom<usize> for UnitBloodLevelsKey {
149 type Error = usize;
150 fn try_from(v: usize) -> Result<Self, Self::Error> {
151 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
152 }
153}
154
155impl TryFrom<i8> for UnitBloodLevelsKey {
156 type Error = i8;
157 fn try_from(v: i8) -> Result<Self, Self::Error> {
158 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
159 }
160}
161
162impl TryFrom<i16> for UnitBloodLevelsKey {
163 type Error = i16;
164 fn try_from(v: i16) -> Result<Self, Self::Error> {
165 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
166 }
167}
168
169impl TryFrom<i32> for UnitBloodLevelsKey {
170 type Error = i32;
171 fn try_from(v: i32) -> Result<Self, Self::Error> {
172 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
173 }
174}
175
176impl TryFrom<i64> for UnitBloodLevelsKey {
177 type Error = i64;
178 fn try_from(v: i64) -> Result<Self, Self::Error> {
179 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
180 }
181}
182
183impl TryFrom<isize> for UnitBloodLevelsKey {
184 type Error = isize;
185 fn try_from(v: isize) -> Result<Self, Self::Error> {
186 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
191pub struct UnitBloodLevelsRow {
192 pub id: UnitBloodLevelsKey,
193 pub violence_level: [i32; 3],
194}
195