wow_dbc/tbc_tables/
light.rs1use crate::{
2 DbcTable, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::map::MapKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, PartialOrd)]
11pub struct Light {
12 pub rows: Vec<LightRow>,
13}
14
15impl DbcTable for Light {
16 type Row = LightRow;
17
18 const FILENAME: &'static str = "Light.dbc";
19
20 fn rows(&self) -> &[Self::Row] { &self.rows }
21 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
22
23 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
24 let mut header = [0_u8; HEADER_SIZE];
25 b.read_exact(&mut header)?;
26 let header = parse_header(&header)?;
27
28 if header.record_size != 48 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 48,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 12 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 12,
41 actual: header.field_count,
42 },
43 ));
44 }
45
46 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
47 b.read_exact(&mut r)?;
48
49 let mut rows = Vec::with_capacity(header.record_count as usize);
50
51 for mut chunk in r.chunks(header.record_size as usize) {
52 let chunk = &mut chunk;
53
54 let id = LightKey::new(crate::util::read_i32_le(chunk)?);
56
57 let continent_id = MapKey::new(crate::util::read_i32_le(chunk)?.into());
59
60 let game_coords = crate::util::read_array_f32::<3>(chunk)?;
62
63 let game_falloff_start = crate::util::read_f32_le(chunk)?;
65
66 let game_falloff_end = crate::util::read_f32_le(chunk)?;
68
69 let light_params_id = crate::util::read_array_i32::<5>(chunk)?;
71
72
73 rows.push(LightRow {
74 id,
75 continent_id,
76 game_coords,
77 game_falloff_start,
78 game_falloff_end,
79 light_params_id,
80 });
81 }
82
83 Ok(Light { rows, })
84 }
85
86 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
87 let header = DbcHeader {
88 record_count: self.rows.len() as u32,
89 field_count: 12,
90 record_size: 48,
91 string_block_size: 1,
92 };
93
94 b.write_all(&header.write_header())?;
95
96 for row in &self.rows {
97 b.write_all(&row.id.id.to_le_bytes())?;
99
100 b.write_all(&(row.continent_id.id as i32).to_le_bytes())?;
102
103 for i in row.game_coords {
105 b.write_all(&i.to_le_bytes())?;
106 }
107
108
109 b.write_all(&row.game_falloff_start.to_le_bytes())?;
111
112 b.write_all(&row.game_falloff_end.to_le_bytes())?;
114
115 for i in row.light_params_id {
117 b.write_all(&i.to_le_bytes())?;
118 }
119
120
121 }
122
123 b.write_all(&[0_u8])?;
124
125 Ok(())
126 }
127
128}
129
130impl Indexable for Light {
131 type PrimaryKey = LightKey;
132 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
133 let key = key.try_into().ok()?;
134 self.rows.iter().find(|a| a.id.id == key.id)
135 }
136
137 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
138 let key = key.try_into().ok()?;
139 self.rows.iter_mut().find(|a| a.id.id == key.id)
140 }
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
144pub struct LightKey {
145 pub id: i32
146}
147
148impl LightKey {
149 pub const fn new(id: i32) -> Self {
150 Self { id }
151 }
152
153}
154
155impl From<u8> for LightKey {
156 fn from(v: u8) -> Self {
157 Self::new(v.into())
158 }
159}
160
161impl From<u16> for LightKey {
162 fn from(v: u16) -> Self {
163 Self::new(v.into())
164 }
165}
166
167impl From<i8> for LightKey {
168 fn from(v: i8) -> Self {
169 Self::new(v.into())
170 }
171}
172
173impl From<i16> for LightKey {
174 fn from(v: i16) -> Self {
175 Self::new(v.into())
176 }
177}
178
179impl From<i32> for LightKey {
180 fn from(v: i32) -> Self {
181 Self::new(v)
182 }
183}
184
185impl TryFrom<u32> for LightKey {
186 type Error = u32;
187 fn try_from(v: u32) -> Result<Self, Self::Error> {
188 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
189 }
190}
191
192impl TryFrom<usize> for LightKey {
193 type Error = usize;
194 fn try_from(v: usize) -> Result<Self, Self::Error> {
195 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
196 }
197}
198
199impl TryFrom<u64> for LightKey {
200 type Error = u64;
201 fn try_from(v: u64) -> Result<Self, Self::Error> {
202 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
203 }
204}
205
206impl TryFrom<i64> for LightKey {
207 type Error = i64;
208 fn try_from(v: i64) -> Result<Self, Self::Error> {
209 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
210 }
211}
212
213impl TryFrom<isize> for LightKey {
214 type Error = isize;
215 fn try_from(v: isize) -> Result<Self, Self::Error> {
216 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
217 }
218}
219
220#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
221pub struct LightRow {
222 pub id: LightKey,
223 pub continent_id: MapKey,
224 pub game_coords: [f32; 3],
225 pub game_falloff_start: f32,
226 pub game_falloff_end: f32,
227 pub light_params_id: [i32; 5],
228}
229