wow_dbc/tbc_tables/
video_hardware.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 VideoHardware {
11    pub rows: Vec<VideoHardwareRow>,
12}
13
14impl DbcTable for VideoHardware {
15    type Row = VideoHardwareRow;
16
17    const FILENAME: &'static str = "VideoHardware.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 != 92 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 92,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 23 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 23,
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        let mut string_block = vec![0_u8; header.string_block_size as usize];
48        b.read_exact(&mut string_block)?;
49
50        let mut rows = Vec::with_capacity(header.record_count as usize);
51
52        for mut chunk in r.chunks(header.record_size as usize) {
53            let chunk = &mut chunk;
54
55            // id: primary_key (VideoHardware) int32
56            let id = VideoHardwareKey::new(crate::util::read_i32_le(chunk)?);
57
58            // vendor_id: int32
59            let vendor_id = crate::util::read_i32_le(chunk)?;
60
61            // device_id: int32
62            let device_id = crate::util::read_i32_le(chunk)?;
63
64            // farclip_idx: int32
65            let farclip_idx = crate::util::read_i32_le(chunk)?;
66
67            // terrain_l_o_d_dist_idx: int32
68            let terrain_l_o_d_dist_idx = crate::util::read_i32_le(chunk)?;
69
70            // terrain_shadow_l_o_d: int32
71            let terrain_shadow_l_o_d = crate::util::read_i32_le(chunk)?;
72
73            // detail_doodad_density_idx: int32
74            let detail_doodad_density_idx = crate::util::read_i32_le(chunk)?;
75
76            // detail_doodad_alpha: int32
77            let detail_doodad_alpha = crate::util::read_i32_le(chunk)?;
78
79            // animating_doodad_idx: int32
80            let animating_doodad_idx = crate::util::read_i32_le(chunk)?;
81
82            // trilinear: int32
83            let trilinear = crate::util::read_i32_le(chunk)?;
84
85            // num_lights: int32
86            let num_lights = crate::util::read_i32_le(chunk)?;
87
88            // specularity: int32
89            let specularity = crate::util::read_i32_le(chunk)?;
90
91            // water_l_o_d_idx: int32
92            let water_l_o_d_idx = crate::util::read_i32_le(chunk)?;
93
94            // particle_density_idx: int32
95            let particle_density_idx = crate::util::read_i32_le(chunk)?;
96
97            // unit_draw_dist_idx: int32
98            let unit_draw_dist_idx = crate::util::read_i32_le(chunk)?;
99
100            // small_cull_dist_idx: int32
101            let small_cull_dist_idx = crate::util::read_i32_le(chunk)?;
102
103            // resolution_idx: int32
104            let resolution_idx = crate::util::read_i32_le(chunk)?;
105
106            // base_mip_level: int32
107            let base_mip_level = crate::util::read_i32_le(chunk)?;
108
109            // ogl_overrides: string_ref
110            let ogl_overrides = {
111                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
112                String::from_utf8(s)?
113            };
114
115            // d3d_overrides: string_ref
116            let d3d_overrides = {
117                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
118                String::from_utf8(s)?
119            };
120
121            // fix_lag: int32
122            let fix_lag = crate::util::read_i32_le(chunk)?;
123
124            // multisample: int32
125            let multisample = crate::util::read_i32_le(chunk)?;
126
127            // atlasdisable: int32
128            let atlasdisable = crate::util::read_i32_le(chunk)?;
129
130
131            rows.push(VideoHardwareRow {
132                id,
133                vendor_id,
134                device_id,
135                farclip_idx,
136                terrain_l_o_d_dist_idx,
137                terrain_shadow_l_o_d,
138                detail_doodad_density_idx,
139                detail_doodad_alpha,
140                animating_doodad_idx,
141                trilinear,
142                num_lights,
143                specularity,
144                water_l_o_d_idx,
145                particle_density_idx,
146                unit_draw_dist_idx,
147                small_cull_dist_idx,
148                resolution_idx,
149                base_mip_level,
150                ogl_overrides,
151                d3d_overrides,
152                fix_lag,
153                multisample,
154                atlasdisable,
155            });
156        }
157
158        Ok(VideoHardware { rows, })
159    }
160
161    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
162        let header = DbcHeader {
163            record_count: self.rows.len() as u32,
164            field_count: 23,
165            record_size: 92,
166            string_block_size: self.string_block_size(),
167        };
168
169        b.write_all(&header.write_header())?;
170
171        let mut string_index = 1;
172        for row in &self.rows {
173            // id: primary_key (VideoHardware) int32
174            b.write_all(&row.id.id.to_le_bytes())?;
175
176            // vendor_id: int32
177            b.write_all(&row.vendor_id.to_le_bytes())?;
178
179            // device_id: int32
180            b.write_all(&row.device_id.to_le_bytes())?;
181
182            // farclip_idx: int32
183            b.write_all(&row.farclip_idx.to_le_bytes())?;
184
185            // terrain_l_o_d_dist_idx: int32
186            b.write_all(&row.terrain_l_o_d_dist_idx.to_le_bytes())?;
187
188            // terrain_shadow_l_o_d: int32
189            b.write_all(&row.terrain_shadow_l_o_d.to_le_bytes())?;
190
191            // detail_doodad_density_idx: int32
192            b.write_all(&row.detail_doodad_density_idx.to_le_bytes())?;
193
194            // detail_doodad_alpha: int32
195            b.write_all(&row.detail_doodad_alpha.to_le_bytes())?;
196
197            // animating_doodad_idx: int32
198            b.write_all(&row.animating_doodad_idx.to_le_bytes())?;
199
200            // trilinear: int32
201            b.write_all(&row.trilinear.to_le_bytes())?;
202
203            // num_lights: int32
204            b.write_all(&row.num_lights.to_le_bytes())?;
205
206            // specularity: int32
207            b.write_all(&row.specularity.to_le_bytes())?;
208
209            // water_l_o_d_idx: int32
210            b.write_all(&row.water_l_o_d_idx.to_le_bytes())?;
211
212            // particle_density_idx: int32
213            b.write_all(&row.particle_density_idx.to_le_bytes())?;
214
215            // unit_draw_dist_idx: int32
216            b.write_all(&row.unit_draw_dist_idx.to_le_bytes())?;
217
218            // small_cull_dist_idx: int32
219            b.write_all(&row.small_cull_dist_idx.to_le_bytes())?;
220
221            // resolution_idx: int32
222            b.write_all(&row.resolution_idx.to_le_bytes())?;
223
224            // base_mip_level: int32
225            b.write_all(&row.base_mip_level.to_le_bytes())?;
226
227            // ogl_overrides: string_ref
228            if !row.ogl_overrides.is_empty() {
229                b.write_all(&(string_index as u32).to_le_bytes())?;
230                string_index += row.ogl_overrides.len() + 1;
231            }
232            else {
233                b.write_all(&(0_u32).to_le_bytes())?;
234            }
235
236            // d3d_overrides: string_ref
237            if !row.d3d_overrides.is_empty() {
238                b.write_all(&(string_index as u32).to_le_bytes())?;
239                string_index += row.d3d_overrides.len() + 1;
240            }
241            else {
242                b.write_all(&(0_u32).to_le_bytes())?;
243            }
244
245            // fix_lag: int32
246            b.write_all(&row.fix_lag.to_le_bytes())?;
247
248            // multisample: int32
249            b.write_all(&row.multisample.to_le_bytes())?;
250
251            // atlasdisable: int32
252            b.write_all(&row.atlasdisable.to_le_bytes())?;
253
254        }
255
256        self.write_string_block(b)?;
257
258        Ok(())
259    }
260
261}
262
263impl Indexable for VideoHardware {
264    type PrimaryKey = VideoHardwareKey;
265    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
266        let key = key.try_into().ok()?;
267        self.rows.iter().find(|a| a.id.id == key.id)
268    }
269
270    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
271        let key = key.try_into().ok()?;
272        self.rows.iter_mut().find(|a| a.id.id == key.id)
273    }
274}
275
276impl VideoHardware {
277    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
278        b.write_all(&[0])?;
279
280        for row in &self.rows {
281            if !row.ogl_overrides.is_empty() { b.write_all(row.ogl_overrides.as_bytes())?; b.write_all(&[0])?; };
282            if !row.d3d_overrides.is_empty() { b.write_all(row.d3d_overrides.as_bytes())?; b.write_all(&[0])?; };
283        }
284
285        Ok(())
286    }
287
288    fn string_block_size(&self) -> u32 {
289        let mut sum = 1;
290        for row in &self.rows {
291            if !row.ogl_overrides.is_empty() { sum += row.ogl_overrides.len() + 1; };
292            if !row.d3d_overrides.is_empty() { sum += row.d3d_overrides.len() + 1; };
293        }
294
295        sum as u32
296    }
297
298}
299
300#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
301pub struct VideoHardwareKey {
302    pub id: i32
303}
304
305impl VideoHardwareKey {
306    pub const fn new(id: i32) -> Self {
307        Self { id }
308    }
309
310}
311
312impl From<u8> for VideoHardwareKey {
313    fn from(v: u8) -> Self {
314        Self::new(v.into())
315    }
316}
317
318impl From<u16> for VideoHardwareKey {
319    fn from(v: u16) -> Self {
320        Self::new(v.into())
321    }
322}
323
324impl From<i8> for VideoHardwareKey {
325    fn from(v: i8) -> Self {
326        Self::new(v.into())
327    }
328}
329
330impl From<i16> for VideoHardwareKey {
331    fn from(v: i16) -> Self {
332        Self::new(v.into())
333    }
334}
335
336impl From<i32> for VideoHardwareKey {
337    fn from(v: i32) -> Self {
338        Self::new(v)
339    }
340}
341
342impl TryFrom<u32> for VideoHardwareKey {
343    type Error = u32;
344    fn try_from(v: u32) -> Result<Self, Self::Error> {
345        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
346    }
347}
348
349impl TryFrom<usize> for VideoHardwareKey {
350    type Error = usize;
351    fn try_from(v: usize) -> Result<Self, Self::Error> {
352        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
353    }
354}
355
356impl TryFrom<u64> for VideoHardwareKey {
357    type Error = u64;
358    fn try_from(v: u64) -> Result<Self, Self::Error> {
359        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
360    }
361}
362
363impl TryFrom<i64> for VideoHardwareKey {
364    type Error = i64;
365    fn try_from(v: i64) -> Result<Self, Self::Error> {
366        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
367    }
368}
369
370impl TryFrom<isize> for VideoHardwareKey {
371    type Error = isize;
372    fn try_from(v: isize) -> Result<Self, Self::Error> {
373        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
374    }
375}
376
377#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
378pub struct VideoHardwareRow {
379    pub id: VideoHardwareKey,
380    pub vendor_id: i32,
381    pub device_id: i32,
382    pub farclip_idx: i32,
383    pub terrain_l_o_d_dist_idx: i32,
384    pub terrain_shadow_l_o_d: i32,
385    pub detail_doodad_density_idx: i32,
386    pub detail_doodad_alpha: i32,
387    pub animating_doodad_idx: i32,
388    pub trilinear: i32,
389    pub num_lights: i32,
390    pub specularity: i32,
391    pub water_l_o_d_idx: i32,
392    pub particle_density_idx: i32,
393    pub unit_draw_dist_idx: i32,
394    pub small_cull_dist_idx: i32,
395    pub resolution_idx: i32,
396    pub base_mip_level: i32,
397    pub ogl_overrides: String,
398    pub d3d_overrides: String,
399    pub fix_lag: i32,
400    pub multisample: i32,
401    pub atlasdisable: i32,
402}
403