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