wow_dbc/wrath_tables/
object_effect_modifier.rs

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