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 let id = VideoHardwareKey::new(crate::util::read_i32_le(chunk)?);
57
58 let vendor_id = crate::util::read_i32_le(chunk)?;
60
61 let device_id = crate::util::read_i32_le(chunk)?;
63
64 let farclip_idx = crate::util::read_i32_le(chunk)?;
66
67 let terrain_l_o_d_dist_idx = crate::util::read_i32_le(chunk)?;
69
70 let terrain_shadow_l_o_d = crate::util::read_i32_le(chunk)?;
72
73 let detail_doodad_density_idx = crate::util::read_i32_le(chunk)?;
75
76 let detail_doodad_alpha = crate::util::read_i32_le(chunk)?;
78
79 let animating_doodad_idx = crate::util::read_i32_le(chunk)?;
81
82 let trilinear = crate::util::read_i32_le(chunk)?;
84
85 let num_lights = crate::util::read_i32_le(chunk)?;
87
88 let specularity = crate::util::read_i32_le(chunk)?;
90
91 let water_l_o_d_idx = crate::util::read_i32_le(chunk)?;
93
94 let particle_density_idx = crate::util::read_i32_le(chunk)?;
96
97 let unit_draw_dist_idx = crate::util::read_i32_le(chunk)?;
99
100 let small_cull_dist_idx = crate::util::read_i32_le(chunk)?;
102
103 let resolution_idx = crate::util::read_i32_le(chunk)?;
105
106 let base_mip_level = crate::util::read_i32_le(chunk)?;
108
109 let ogl_overrides = {
111 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
112 String::from_utf8(s)?
113 };
114
115 let d3d_overrides = {
117 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
118 String::from_utf8(s)?
119 };
120
121 let fix_lag = crate::util::read_i32_le(chunk)?;
123
124 let multisample = crate::util::read_i32_le(chunk)?;
126
127 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 b.write_all(&row.id.id.to_le_bytes())?;
175
176 b.write_all(&row.vendor_id.to_le_bytes())?;
178
179 b.write_all(&row.device_id.to_le_bytes())?;
181
182 b.write_all(&row.farclip_idx.to_le_bytes())?;
184
185 b.write_all(&row.terrain_l_o_d_dist_idx.to_le_bytes())?;
187
188 b.write_all(&row.terrain_shadow_l_o_d.to_le_bytes())?;
190
191 b.write_all(&row.detail_doodad_density_idx.to_le_bytes())?;
193
194 b.write_all(&row.detail_doodad_alpha.to_le_bytes())?;
196
197 b.write_all(&row.animating_doodad_idx.to_le_bytes())?;
199
200 b.write_all(&row.trilinear.to_le_bytes())?;
202
203 b.write_all(&row.num_lights.to_le_bytes())?;
205
206 b.write_all(&row.specularity.to_le_bytes())?;
208
209 b.write_all(&row.water_l_o_d_idx.to_le_bytes())?;
211
212 b.write_all(&row.particle_density_idx.to_le_bytes())?;
214
215 b.write_all(&row.unit_draw_dist_idx.to_le_bytes())?;
217
218 b.write_all(&row.small_cull_dist_idx.to_le_bytes())?;
220
221 b.write_all(&row.resolution_idx.to_le_bytes())?;
223
224 b.write_all(&row.base_mip_level.to_le_bytes())?;
226
227 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 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 b.write_all(&row.fix_lag.to_le_bytes())?;
247
248 b.write_all(&row.multisample.to_le_bytes())?;
250
251 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