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