wow_dbc/wrath_tables/
helmet_geoset_vis_data.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 HelmetGeosetVisData {
11 pub rows: Vec<HelmetGeosetVisDataRow>,
12}
13
14impl DbcTable for HelmetGeosetVisData {
15 type Row = HelmetGeosetVisDataRow;
16
17 const FILENAME: &'static str = "HelmetGeosetVisData.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 != 32 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 32,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 8 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 8,
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 = HelmetGeosetVisDataKey::new(crate::util::read_i32_le(chunk)?);
55
56 let hide_geoset = crate::util::read_array_i32::<7>(chunk)?;
58
59
60 rows.push(HelmetGeosetVisDataRow {
61 id,
62 hide_geoset,
63 });
64 }
65
66 Ok(HelmetGeosetVisData { 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: 8,
73 record_size: 32,
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.hide_geoset {
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 HelmetGeosetVisData {
99 type PrimaryKey = HelmetGeosetVisDataKey;
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 HelmetGeosetVisDataKey {
113 pub id: i32
114}
115
116impl HelmetGeosetVisDataKey {
117 pub const fn new(id: i32) -> Self {
118 Self { id }
119 }
120
121}
122
123impl From<u8> for HelmetGeosetVisDataKey {
124 fn from(v: u8) -> Self {
125 Self::new(v.into())
126 }
127}
128
129impl From<u16> for HelmetGeosetVisDataKey {
130 fn from(v: u16) -> Self {
131 Self::new(v.into())
132 }
133}
134
135impl From<i8> for HelmetGeosetVisDataKey {
136 fn from(v: i8) -> Self {
137 Self::new(v.into())
138 }
139}
140
141impl From<i16> for HelmetGeosetVisDataKey {
142 fn from(v: i16) -> Self {
143 Self::new(v.into())
144 }
145}
146
147impl From<i32> for HelmetGeosetVisDataKey {
148 fn from(v: i32) -> Self {
149 Self::new(v)
150 }
151}
152
153impl TryFrom<u32> for HelmetGeosetVisDataKey {
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 HelmetGeosetVisDataKey {
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 HelmetGeosetVisDataKey {
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 HelmetGeosetVisDataKey {
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 HelmetGeosetVisDataKey {
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 HelmetGeosetVisDataRow {
190 pub id: HelmetGeosetVisDataKey,
191 pub hide_geoset: [i32; 7],
192}
193