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