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