wow_dbc/wrath_tables/
spell_description_variables.rs1use 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 SpellDescriptionVariables {
11 pub rows: Vec<SpellDescriptionVariablesRow>,
12}
13
14impl DbcTable for SpellDescriptionVariables {
15 type Row = SpellDescriptionVariablesRow;
16
17 const FILENAME: &'static str = "SpellDescriptionVariables.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 != 8 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 8,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 2 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 2,
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 = SpellDescriptionVariablesKey::new(crate::util::read_i32_le(chunk)?);
57
58 let variables = {
60 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
61 String::from_utf8(s)?
62 };
63
64
65 rows.push(SpellDescriptionVariablesRow {
66 id,
67 variables,
68 });
69 }
70
71 Ok(SpellDescriptionVariables { rows, })
72 }
73
74 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
75 let header = DbcHeader {
76 record_count: self.rows.len() as u32,
77 field_count: 2,
78 record_size: 8,
79 string_block_size: self.string_block_size(),
80 };
81
82 b.write_all(&header.write_header())?;
83
84 let mut string_index = 1;
85 for row in &self.rows {
86 b.write_all(&row.id.id.to_le_bytes())?;
88
89 if !row.variables.is_empty() {
91 b.write_all(&(string_index as u32).to_le_bytes())?;
92 string_index += row.variables.len() + 1;
93 }
94 else {
95 b.write_all(&(0_u32).to_le_bytes())?;
96 }
97
98 }
99
100 self.write_string_block(b)?;
101
102 Ok(())
103 }
104
105}
106
107impl Indexable for SpellDescriptionVariables {
108 type PrimaryKey = SpellDescriptionVariablesKey;
109 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
110 let key = key.try_into().ok()?;
111 self.rows.iter().find(|a| a.id.id == key.id)
112 }
113
114 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
115 let key = key.try_into().ok()?;
116 self.rows.iter_mut().find(|a| a.id.id == key.id)
117 }
118}
119
120impl SpellDescriptionVariables {
121 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
122 b.write_all(&[0])?;
123
124 for row in &self.rows {
125 if !row.variables.is_empty() { b.write_all(row.variables.as_bytes())?; b.write_all(&[0])?; };
126 }
127
128 Ok(())
129 }
130
131 fn string_block_size(&self) -> u32 {
132 let mut sum = 1;
133 for row in &self.rows {
134 if !row.variables.is_empty() { sum += row.variables.len() + 1; };
135 }
136
137 sum as u32
138 }
139
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
143pub struct SpellDescriptionVariablesKey {
144 pub id: i32
145}
146
147impl SpellDescriptionVariablesKey {
148 pub const fn new(id: i32) -> Self {
149 Self { id }
150 }
151
152}
153
154impl From<u8> for SpellDescriptionVariablesKey {
155 fn from(v: u8) -> Self {
156 Self::new(v.into())
157 }
158}
159
160impl From<u16> for SpellDescriptionVariablesKey {
161 fn from(v: u16) -> Self {
162 Self::new(v.into())
163 }
164}
165
166impl From<i8> for SpellDescriptionVariablesKey {
167 fn from(v: i8) -> Self {
168 Self::new(v.into())
169 }
170}
171
172impl From<i16> for SpellDescriptionVariablesKey {
173 fn from(v: i16) -> Self {
174 Self::new(v.into())
175 }
176}
177
178impl From<i32> for SpellDescriptionVariablesKey {
179 fn from(v: i32) -> Self {
180 Self::new(v)
181 }
182}
183
184impl TryFrom<u32> for SpellDescriptionVariablesKey {
185 type Error = u32;
186 fn try_from(v: u32) -> Result<Self, Self::Error> {
187 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
188 }
189}
190
191impl TryFrom<usize> for SpellDescriptionVariablesKey {
192 type Error = usize;
193 fn try_from(v: usize) -> Result<Self, Self::Error> {
194 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
195 }
196}
197
198impl TryFrom<u64> for SpellDescriptionVariablesKey {
199 type Error = u64;
200 fn try_from(v: u64) -> Result<Self, Self::Error> {
201 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
202 }
203}
204
205impl TryFrom<i64> for SpellDescriptionVariablesKey {
206 type Error = i64;
207 fn try_from(v: i64) -> Result<Self, Self::Error> {
208 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
209 }
210}
211
212impl TryFrom<isize> for SpellDescriptionVariablesKey {
213 type Error = isize;
214 fn try_from(v: isize) -> Result<Self, Self::Error> {
215 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
216 }
217}
218
219#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
220pub struct SpellDescriptionVariablesRow {
221 pub id: SpellDescriptionVariablesKey,
222 pub variables: String,
223}
224