1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::io::{Read, Write};
use crate::Guid;
use crate::shared::gold_vanilla_tbc_wrath::Gold;
use crate::tbc::{
MailListItem, MailListItemEnchant, MailType,
};
/// Auto generated from the original `wowm` in file [`wow_message_parser/wowm/world/mail/smsg_mail_list_result.wowm:53`](https://github.com/gtker/wow_messages/tree/main/wow_message_parser/wowm/world/mail/smsg_mail_list_result.wowm#L53):
/// ```text
/// struct Mail {
/// u16 size = self.size;
/// u32 message_id;
/// MailType message_type;
/// if (message_type == NORMAL) {
/// Guid sender;
/// }
/// else if (message_type == CREATURE
/// || message_type == GAMEOBJECT) {
/// u32 sender_id;
/// }
/// else if (message_type == AUCTION) {
/// u32 auction_id;
/// }
/// else if (message_type == ITEM) {
/// Item item;
/// }
/// Gold cash_on_delivery;
/// u32 item_text_id;
/// u32 unknown;
/// u32 stationery;
/// Gold money;
/// u32 flags;
/// f32 expiration_time;
/// u32 mail_template_id;
/// CString subject;
/// u8 amount_of_items;
/// MailListItem[amount_of_items] items;
/// }
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct Mail {
pub message_id: u32,
pub message_type: Mail_MailType,
pub cash_on_delivery: Gold,
pub item_text_id: u32,
pub unknown: u32,
pub stationery: u32,
pub money: Gold,
pub flags: u32,
pub expiration_time: f32,
/// cmangos/vmangos/mangoszero: mail template (MailTemplate.dbc)
pub mail_template_id: u32,
pub subject: String,
pub items: Vec<MailListItem>,
}
impl Mail {
pub(crate) fn write_into_vec(&self, mut w: impl Write) -> Result<(), std::io::Error> {
// size: u16
w.write_all(&((self.size() - 2) as u16).to_le_bytes())?;
// message_id: u32
w.write_all(&self.message_id.to_le_bytes())?;
// message_type: MailType
w.write_all(&(self.message_type.as_int().to_le_bytes()))?;
match &self.message_type {
Mail_MailType::Normal {
sender,
} => {
// sender: Guid
w.write_all(&sender.guid().to_le_bytes())?;
}
Mail_MailType::Auction {
auction_id,
} => {
// auction_id: u32
w.write_all(&auction_id.to_le_bytes())?;
}
Mail_MailType::Creature {
sender_id,
} => {
// sender_id: u32
w.write_all(&sender_id.to_le_bytes())?;
}
Mail_MailType::Gameobject {
sender_id,
} => {
// sender_id: u32
w.write_all(&sender_id.to_le_bytes())?;
}
Mail_MailType::Item {
item,
} => {
// item: Item
w.write_all(&item.to_le_bytes())?;
}
}
// cash_on_delivery: Gold
w.write_all((self.cash_on_delivery.as_int()).to_le_bytes().as_slice())?;
// item_text_id: u32
w.write_all(&self.item_text_id.to_le_bytes())?;
// unknown: u32
w.write_all(&self.unknown.to_le_bytes())?;
// stationery: u32
w.write_all(&self.stationery.to_le_bytes())?;
// money: Gold
w.write_all((self.money.as_int()).to_le_bytes().as_slice())?;
// flags: u32
w.write_all(&self.flags.to_le_bytes())?;
// expiration_time: f32
w.write_all(&self.expiration_time.to_le_bytes())?;
// mail_template_id: u32
w.write_all(&self.mail_template_id.to_le_bytes())?;
// subject: CString
// TODO: Guard against strings that are already null-terminated
assert_ne!(self.subject.as_bytes().iter().next_back(), Some(&0_u8), "String `subject` must not be null-terminated.");
w.write_all(self.subject.as_bytes())?;
// Null terminator
w.write_all(&[0])?;
// amount_of_items: u8
w.write_all(&(self.items.len() as u8).to_le_bytes())?;
// items: MailListItem[amount_of_items]
for i in self.items.iter() {
i.write_into_vec(&mut w)?;
}
Ok(())
}
}
impl Mail {
pub(crate) fn read<R: std::io::Read>(mut r: R) -> Result<Self, crate::errors::ParseErrorKind> {
// size: u16
let _size = crate::util::read_u16_le(&mut r)?;
// size is dynamic size of the object
// message_id: u32
let message_id = crate::util::read_u32_le(&mut r)?;
// message_type: MailType
let message_type = crate::util::read_u8_le(&mut r)?.try_into()?;
let message_type_if = match message_type {
MailType::Normal => {
// sender: Guid
let sender = crate::util::read_guid(&mut r)?;
Mail_MailType::Normal {
sender,
}
}
MailType::Auction => {
// auction_id: u32
let auction_id = crate::util::read_u32_le(&mut r)?;
Mail_MailType::Auction {
auction_id,
}
}
MailType::Creature => {
// sender_id: u32
let sender_id = crate::util::read_u32_le(&mut r)?;
Mail_MailType::Creature {
sender_id,
}
}
MailType::Gameobject => {
// sender_id: u32
let sender_id = crate::util::read_u32_le(&mut r)?;
Mail_MailType::Gameobject {
sender_id,
}
}
MailType::Item => {
// item: Item
let item = crate::util::read_u32_le(&mut r)?;
Mail_MailType::Item {
item,
}
}
};
// cash_on_delivery: Gold
let cash_on_delivery = Gold::new(crate::util::read_u32_le(&mut r)?);
// item_text_id: u32
let item_text_id = crate::util::read_u32_le(&mut r)?;
// unknown: u32
let unknown = crate::util::read_u32_le(&mut r)?;
// stationery: u32
let stationery = crate::util::read_u32_le(&mut r)?;
// money: Gold
let money = Gold::new(crate::util::read_u32_le(&mut r)?);
// flags: u32
let flags = crate::util::read_u32_le(&mut r)?;
// expiration_time: f32
let expiration_time = crate::util::read_f32_le(&mut r)?;
// mail_template_id: u32
let mail_template_id = crate::util::read_u32_le(&mut r)?;
// subject: CString
let subject = {
let subject = crate::util::read_c_string_to_vec(&mut r)?;
String::from_utf8(subject)?
};
// amount_of_items: u8
let amount_of_items = crate::util::read_u8_le(&mut r)?;
// items: MailListItem[amount_of_items]
let items = {
let mut items = Vec::with_capacity(amount_of_items as usize);
for _ in 0..amount_of_items {
items.push(MailListItem::read(&mut r)?);
}
items
};
Ok(Self {
message_id,
message_type: message_type_if,
cash_on_delivery,
item_text_id,
unknown,
stationery,
money,
flags,
expiration_time,
mail_template_id,
subject,
items,
})
}
}
impl Mail {
pub(crate) fn size(&self) -> usize {
2 // size: u16
+ 4 // message_id: u32
+ self.message_type.size() // message_type: Mail_MailType
+ 4 // cash_on_delivery: Gold
+ 4 // item_text_id: u32
+ 4 // unknown: u32
+ 4 // stationery: u32
+ 4 // money: Gold
+ 4 // flags: u32
+ 4 // expiration_time: f32
+ 4 // mail_template_id: u32
+ self.subject.len() + 1 // subject: CString
+ 1 // amount_of_items: u8
+ self.items.len() * 102 // items: MailListItem[amount_of_items]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Mail_MailType {
Normal {
sender: Guid,
},
Auction {
auction_id: u32,
},
Creature {
sender_id: u32,
},
Gameobject {
sender_id: u32,
},
Item {
item: u32,
},
}
impl Default for Mail_MailType {
fn default() -> Self {
// First enumerator without any fields
Self::Normal {
sender: Default::default(),
}
}
}
impl Mail_MailType {
pub(crate) const fn as_int(&self) -> u8 {
match self {
Self::Normal { .. } => 0,
Self::Auction { .. } => 2,
Self::Creature { .. } => 3,
Self::Gameobject { .. } => 4,
Self::Item { .. } => 5,
}
}
}
impl std::fmt::Display for Mail_MailType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Normal{ .. } => f.write_str("Normal"),
Self::Auction{ .. } => f.write_str("Auction"),
Self::Creature{ .. } => f.write_str("Creature"),
Self::Gameobject{ .. } => f.write_str("Gameobject"),
Self::Item{ .. } => f.write_str("Item"),
}
}
}
impl Mail_MailType {
pub(crate) const fn size(&self) -> usize {
match self {
Self::Normal {
..
} => {
1
+ 8 // sender: Guid
}
Self::Auction {
..
} => {
1
+ 4 // auction_id: u32
}
Self::Creature {
..
} => {
1
+ 4 // sender_id: u32
}
Self::Gameobject {
..
} => {
1
+ 4 // sender_id: u32
}
Self::Item {
..
} => {
1
+ 4 // item: Item
}
}
}
}