wow_dbc/tbc_tables/
world_map_transforms.rs1use crate::{
2 DbcTable, 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 WorldMapTransforms {
12 pub rows: Vec<WorldMapTransformsRow>,
13}
14
15impl DbcTable for WorldMapTransforms {
16 type Row = WorldMapTransformsRow;
17
18 const FILENAME: &'static str = "WorldMapTransforms.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 != 36 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 36,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 9 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 9,
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
49 let mut rows = Vec::with_capacity(header.record_count as usize);
50
51 for mut chunk in r.chunks(header.record_size as usize) {
52 let chunk = &mut chunk;
53
54 let id = WorldMapTransformsKey::new(crate::util::read_i32_le(chunk)?);
56
57 let map_id = MapKey::new(crate::util::read_i32_le(chunk)?.into());
59
60 let region_min = crate::util::read_array_f32::<2>(chunk)?;
62
63 let region_max = crate::util::read_array_f32::<2>(chunk)?;
65
66 let new_map_id = MapKey::new(crate::util::read_i32_le(chunk)?.into());
68
69 let region_offset = crate::util::read_array_f32::<2>(chunk)?;
71
72
73 rows.push(WorldMapTransformsRow {
74 id,
75 map_id,
76 region_min,
77 region_max,
78 new_map_id,
79 region_offset,
80 });
81 }
82
83 Ok(WorldMapTransforms { rows, })
84 }
85
86 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
87 let header = DbcHeader {
88 record_count: self.rows.len() as u32,
89 field_count: 9,
90 record_size: 36,
91 string_block_size: 1,
92 };
93
94 b.write_all(&header.write_header())?;
95
96 for row in &self.rows {
97 b.write_all(&row.id.id.to_le_bytes())?;
99
100 b.write_all(&(row.map_id.id as i32).to_le_bytes())?;
102
103 for i in row.region_min {
105 b.write_all(&i.to_le_bytes())?;
106 }
107
108
109 for i in row.region_max {
111 b.write_all(&i.to_le_bytes())?;
112 }
113
114
115 b.write_all(&(row.new_map_id.id as i32).to_le_bytes())?;
117
118 for i in row.region_offset {
120 b.write_all(&i.to_le_bytes())?;
121 }
122
123
124 }
125
126 b.write_all(&[0_u8])?;
127
128 Ok(())
129 }
130
131}
132
133impl Indexable for WorldMapTransforms {
134 type PrimaryKey = WorldMapTransformsKey;
135 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
136 let key = key.try_into().ok()?;
137 self.rows.iter().find(|a| a.id.id == key.id)
138 }
139
140 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
141 let key = key.try_into().ok()?;
142 self.rows.iter_mut().find(|a| a.id.id == key.id)
143 }
144}
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
147pub struct WorldMapTransformsKey {
148 pub id: i32
149}
150
151impl WorldMapTransformsKey {
152 pub const fn new(id: i32) -> Self {
153 Self { id }
154 }
155
156}
157
158impl From<u8> for WorldMapTransformsKey {
159 fn from(v: u8) -> Self {
160 Self::new(v.into())
161 }
162}
163
164impl From<u16> for WorldMapTransformsKey {
165 fn from(v: u16) -> Self {
166 Self::new(v.into())
167 }
168}
169
170impl From<i8> for WorldMapTransformsKey {
171 fn from(v: i8) -> Self {
172 Self::new(v.into())
173 }
174}
175
176impl From<i16> for WorldMapTransformsKey {
177 fn from(v: i16) -> Self {
178 Self::new(v.into())
179 }
180}
181
182impl From<i32> for WorldMapTransformsKey {
183 fn from(v: i32) -> Self {
184 Self::new(v)
185 }
186}
187
188impl TryFrom<u32> for WorldMapTransformsKey {
189 type Error = u32;
190 fn try_from(v: u32) -> Result<Self, Self::Error> {
191 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
192 }
193}
194
195impl TryFrom<usize> for WorldMapTransformsKey {
196 type Error = usize;
197 fn try_from(v: usize) -> Result<Self, Self::Error> {
198 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
199 }
200}
201
202impl TryFrom<u64> for WorldMapTransformsKey {
203 type Error = u64;
204 fn try_from(v: u64) -> Result<Self, Self::Error> {
205 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
206 }
207}
208
209impl TryFrom<i64> for WorldMapTransformsKey {
210 type Error = i64;
211 fn try_from(v: i64) -> Result<Self, Self::Error> {
212 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
213 }
214}
215
216impl TryFrom<isize> for WorldMapTransformsKey {
217 type Error = isize;
218 fn try_from(v: isize) -> Result<Self, Self::Error> {
219 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
220 }
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
224pub struct WorldMapTransformsRow {
225 pub id: WorldMapTransformsKey,
226 pub map_id: MapKey,
227 pub region_min: [f32; 2],
228 pub region_max: [f32; 2],
229 pub new_map_id: MapKey,
230 pub region_offset: [f32; 2],
231}
232