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