wow_dbc/tbc_tables/
liquid_type.rs

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