wow_dbc/wrath_tables/
game_tables.rs1use crate::DbcTable;
2use crate::header::{
3 DbcHeader, HEADER_SIZE, parse_header,
4};
5use std::io::Write;
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct GameTables {
9 pub rows: Vec<GameTablesRow>,
10}
11
12impl DbcTable for GameTables {
13 type Row = GameTablesRow;
14
15 const FILENAME: &'static str = "GameTables.dbc";
16
17 fn rows(&self) -> &[Self::Row] { &self.rows }
18 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
19
20 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
21 let mut header = [0_u8; HEADER_SIZE];
22 b.read_exact(&mut header)?;
23 let header = parse_header(&header)?;
24
25 if header.record_size != 12 {
26 return Err(crate::DbcError::InvalidHeader(
27 crate::InvalidHeaderError::RecordSize {
28 expected: 12,
29 actual: header.record_size,
30 },
31 ));
32 }
33
34 if header.field_count != 3 {
35 return Err(crate::DbcError::InvalidHeader(
36 crate::InvalidHeaderError::FieldCount {
37 expected: 3,
38 actual: header.field_count,
39 },
40 ));
41 }
42
43 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
44 b.read_exact(&mut r)?;
45 let mut string_block = vec![0_u8; header.string_block_size as usize];
46 b.read_exact(&mut string_block)?;
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 let name = {
55 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
56 String::from_utf8(s)?
57 };
58
59 let num_rows = crate::util::read_i32_le(chunk)?;
61
62 let num_columns = crate::util::read_i32_le(chunk)?;
64
65
66 rows.push(GameTablesRow {
67 name,
68 num_rows,
69 num_columns,
70 });
71 }
72
73 Ok(GameTables { rows, })
74 }
75
76 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
77 let header = DbcHeader {
78 record_count: self.rows.len() as u32,
79 field_count: 3,
80 record_size: 12,
81 string_block_size: self.string_block_size(),
82 };
83
84 b.write_all(&header.write_header())?;
85
86 let mut string_index = 1;
87 for row in &self.rows {
88 if !row.name.is_empty() {
90 b.write_all(&(string_index as u32).to_le_bytes())?;
91 string_index += row.name.len() + 1;
92 }
93 else {
94 b.write_all(&(0_u32).to_le_bytes())?;
95 }
96
97 b.write_all(&row.num_rows.to_le_bytes())?;
99
100 b.write_all(&row.num_columns.to_le_bytes())?;
102
103 }
104
105 self.write_string_block(b)?;
106
107 Ok(())
108 }
109
110}
111
112impl GameTables {
113 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
114 b.write_all(&[0])?;
115
116 for row in &self.rows {
117 if !row.name.is_empty() { b.write_all(row.name.as_bytes())?; b.write_all(&[0])?; };
118 }
119
120 Ok(())
121 }
122
123 fn string_block_size(&self) -> u32 {
124 let mut sum = 1;
125 for row in &self.rows {
126 if !row.name.is_empty() { sum += row.name.len() + 1; };
127 }
128
129 sum as u32
130 }
131
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
135pub struct GameTablesRow {
136 pub name: String,
137 pub num_rows: i32,
138 pub num_columns: i32,
139}
140