wow_dbc/vanilla_tables/
light_params.rs1use crate::{
2 DbcTable, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::light_skybox::LightSkyboxKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, PartialOrd)]
11pub struct LightParams {
12 pub rows: Vec<LightParamsRow>,
13}
14
15impl DbcTable for LightParams {
16 type Row = LightParamsRow;
17
18 const FILENAME: &'static str = "LightParams.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 != 36 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 36,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 9 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 9,
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 = LightParamsKey::new(crate::util::read_u32_le(chunk)?);
56
57 let highlight_sky = crate::util::read_u32_le(chunk)? != 0;
59
60 let light_skybox = LightSkyboxKey::new(crate::util::read_u32_le(chunk)?.into());
62
63 let glow = crate::util::read_f32_le(chunk)?;
65
66 let water_shallow_alpha = crate::util::read_f32_le(chunk)?;
68
69 let water_deep_alpha = crate::util::read_f32_le(chunk)?;
71
72 let ocean_shallow_alpha = crate::util::read_f32_le(chunk)?;
74
75 let ocean_deep_alpha = crate::util::read_f32_le(chunk)?;
77
78 let flags = crate::util::read_u32_le(chunk)?;
80
81
82 rows.push(LightParamsRow {
83 id,
84 highlight_sky,
85 light_skybox,
86 glow,
87 water_shallow_alpha,
88 water_deep_alpha,
89 ocean_shallow_alpha,
90 ocean_deep_alpha,
91 flags,
92 });
93 }
94
95 Ok(LightParams { rows, })
96 }
97
98 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
99 let header = DbcHeader {
100 record_count: self.rows.len() as u32,
101 field_count: 9,
102 record_size: 36,
103 string_block_size: 1,
104 };
105
106 b.write_all(&header.write_header())?;
107
108 for row in &self.rows {
109 b.write_all(&row.id.id.to_le_bytes())?;
111
112 b.write_all(&u32::from(row.highlight_sky).to_le_bytes())?;
114
115 b.write_all(&(row.light_skybox.id as u32).to_le_bytes())?;
117
118 b.write_all(&row.glow.to_le_bytes())?;
120
121 b.write_all(&row.water_shallow_alpha.to_le_bytes())?;
123
124 b.write_all(&row.water_deep_alpha.to_le_bytes())?;
126
127 b.write_all(&row.ocean_shallow_alpha.to_le_bytes())?;
129
130 b.write_all(&row.ocean_deep_alpha.to_le_bytes())?;
132
133 b.write_all(&row.flags.to_le_bytes())?;
135
136 }
137
138 b.write_all(&[0_u8])?;
139
140 Ok(())
141 }
142
143}
144
145impl Indexable for LightParams {
146 type PrimaryKey = LightParamsKey;
147 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
148 let key = key.try_into().ok()?;
149 self.rows.iter().find(|a| a.id.id == key.id)
150 }
151
152 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
153 let key = key.try_into().ok()?;
154 self.rows.iter_mut().find(|a| a.id.id == key.id)
155 }
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
159pub struct LightParamsKey {
160 pub id: u32
161}
162
163impl LightParamsKey {
164 pub const fn new(id: u32) -> Self {
165 Self { id }
166 }
167
168}
169
170impl From<u8> for LightParamsKey {
171 fn from(v: u8) -> Self {
172 Self::new(v.into())
173 }
174}
175
176impl From<u16> for LightParamsKey {
177 fn from(v: u16) -> Self {
178 Self::new(v.into())
179 }
180}
181
182impl From<u32> for LightParamsKey {
183 fn from(v: u32) -> Self {
184 Self::new(v)
185 }
186}
187
188impl TryFrom<u64> for LightParamsKey {
189 type Error = u64;
190 fn try_from(v: u64) -> Result<Self, Self::Error> {
191 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
192 }
193}
194
195impl TryFrom<usize> for LightParamsKey {
196 type Error = usize;
197 fn try_from(v: usize) -> Result<Self, Self::Error> {
198 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
199 }
200}
201
202impl TryFrom<i8> for LightParamsKey {
203 type Error = i8;
204 fn try_from(v: i8) -> Result<Self, Self::Error> {
205 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
206 }
207}
208
209impl TryFrom<i16> for LightParamsKey {
210 type Error = i16;
211 fn try_from(v: i16) -> Result<Self, Self::Error> {
212 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
213 }
214}
215
216impl TryFrom<i32> for LightParamsKey {
217 type Error = i32;
218 fn try_from(v: i32) -> Result<Self, Self::Error> {
219 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
220 }
221}
222
223impl TryFrom<i64> for LightParamsKey {
224 type Error = i64;
225 fn try_from(v: i64) -> Result<Self, Self::Error> {
226 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
227 }
228}
229
230impl TryFrom<isize> for LightParamsKey {
231 type Error = isize;
232 fn try_from(v: isize) -> Result<Self, Self::Error> {
233 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
234 }
235}
236
237#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
238pub struct LightParamsRow {
239 pub id: LightParamsKey,
240 pub highlight_sky: bool,
241 pub light_skybox: LightSkyboxKey,
242 pub glow: f32,
243 pub water_shallow_alpha: f32,
244 pub water_deep_alpha: f32,
245 pub ocean_shallow_alpha: f32,
246 pub ocean_deep_alpha: f32,
247 pub flags: u32,
248}
249