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