wow_dbc/vanilla_tables/
npc_sounds.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 NPCSounds {
11    pub rows: Vec<NPCSoundsRow>,
12}
13
14impl DbcTable for NPCSounds {
15    type Row = NPCSoundsRow;
16
17    const FILENAME: &'static str = "NPCSounds.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 != 20 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 20,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 5 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 5,
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
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            // id: primary_key (NPCSounds) uint32
54            let id = NPCSoundsKey::new(crate::util::read_u32_le(chunk)?);
55
56            // sound_entries: uint32[4]
57            let sound_entries = crate::util::read_array_u32::<4>(chunk)?;
58
59
60            rows.push(NPCSoundsRow {
61                id,
62                sound_entries,
63            });
64        }
65
66        Ok(NPCSounds { rows, })
67    }
68
69    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
70        let header = DbcHeader {
71            record_count: self.rows.len() as u32,
72            field_count: 5,
73            record_size: 20,
74            string_block_size: 1,
75        };
76
77        b.write_all(&header.write_header())?;
78
79        for row in &self.rows {
80            // id: primary_key (NPCSounds) uint32
81            b.write_all(&row.id.id.to_le_bytes())?;
82
83            // sound_entries: uint32[4]
84            for i in row.sound_entries {
85                b.write_all(&i.to_le_bytes())?;
86            }
87
88
89        }
90
91        b.write_all(&[0_u8])?;
92
93        Ok(())
94    }
95
96}
97
98impl Indexable for NPCSounds {
99    type PrimaryKey = NPCSoundsKey;
100    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
101        let key = key.try_into().ok()?;
102        self.rows.iter().find(|a| a.id.id == key.id)
103    }
104
105    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
106        let key = key.try_into().ok()?;
107        self.rows.iter_mut().find(|a| a.id.id == key.id)
108    }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
112pub struct NPCSoundsKey {
113    pub id: u32
114}
115
116impl NPCSoundsKey {
117    pub const fn new(id: u32) -> Self {
118        Self { id }
119    }
120
121}
122
123impl From<u8> for NPCSoundsKey {
124    fn from(v: u8) -> Self {
125        Self::new(v.into())
126    }
127}
128
129impl From<u16> for NPCSoundsKey {
130    fn from(v: u16) -> Self {
131        Self::new(v.into())
132    }
133}
134
135impl From<u32> for NPCSoundsKey {
136    fn from(v: u32) -> Self {
137        Self::new(v)
138    }
139}
140
141impl TryFrom<u64> for NPCSoundsKey {
142    type Error = u64;
143    fn try_from(v: u64) -> Result<Self, Self::Error> {
144        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
145    }
146}
147
148impl TryFrom<usize> for NPCSoundsKey {
149    type Error = usize;
150    fn try_from(v: usize) -> Result<Self, Self::Error> {
151        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
152    }
153}
154
155impl TryFrom<i8> for NPCSoundsKey {
156    type Error = i8;
157    fn try_from(v: i8) -> Result<Self, Self::Error> {
158        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
159    }
160}
161
162impl TryFrom<i16> for NPCSoundsKey {
163    type Error = i16;
164    fn try_from(v: i16) -> Result<Self, Self::Error> {
165        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
166    }
167}
168
169impl TryFrom<i32> for NPCSoundsKey {
170    type Error = i32;
171    fn try_from(v: i32) -> Result<Self, Self::Error> {
172        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
173    }
174}
175
176impl TryFrom<i64> for NPCSoundsKey {
177    type Error = i64;
178    fn try_from(v: i64) -> Result<Self, Self::Error> {
179        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
180    }
181}
182
183impl TryFrom<isize> for NPCSoundsKey {
184    type Error = isize;
185    fn try_from(v: isize) -> Result<Self, Self::Error> {
186        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
187    }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
191pub struct NPCSoundsRow {
192    pub id: NPCSoundsKey,
193    pub sound_entries: [u32; 4],
194}
195