wow_dbc 0.3.0

Library for parsing World of Warcraft DBC files for 1.12, 2.4.3 and 3.3.5.
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use crate::{
    DbcTable, Indexable,
};
use crate::header::{
    DbcHeader, HEADER_SIZE, parse_header,
};
use std::io::Write;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SpellChainEffects {
    pub rows: Vec<SpellChainEffectsRow>,
}

impl DbcTable for SpellChainEffects {
    type Row = SpellChainEffectsRow;

    const FILENAME: &'static str = "SpellChainEffects.dbc";

    fn rows(&self) -> &[Self::Row] { &self.rows }
    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }

    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
        let mut header = [0_u8; HEADER_SIZE];
        b.read_exact(&mut header)?;
        let header = parse_header(&header)?;

        if header.record_size != 173 {
            return Err(crate::DbcError::InvalidHeader(
                crate::InvalidHeaderError::RecordSize {
                    expected: 173,
                    actual: header.record_size,
                },
            ));
        }

        if header.field_count != 47 {
            return Err(crate::DbcError::InvalidHeader(
                crate::InvalidHeaderError::FieldCount {
                    expected: 47,
                    actual: header.field_count,
                },
            ));
        }

        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
        b.read_exact(&mut r)?;
        let mut string_block = vec![0_u8; header.string_block_size as usize];
        b.read_exact(&mut string_block)?;

        let mut rows = Vec::with_capacity(header.record_count as usize);

        for mut chunk in r.chunks(header.record_size as usize) {
            let chunk = &mut chunk;

            // id: primary_key (SpellChainEffects) int32
            let id = SpellChainEffectsKey::new(crate::util::read_i32_le(chunk)?);

            // avg_seg_len: float
            let avg_seg_len = crate::util::read_f32_le(chunk)?;

            // width: float
            let width = crate::util::read_f32_le(chunk)?;

            // noise_scale: float
            let noise_scale = crate::util::read_f32_le(chunk)?;

            // tex_coord_scale: float
            let tex_coord_scale = crate::util::read_f32_le(chunk)?;

            // seg_duration: int32
            let seg_duration = crate::util::read_i32_le(chunk)?;

            // seg_delay: int32
            let seg_delay = crate::util::read_i32_le(chunk)?;

            // texture: string_ref
            let texture = {
                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
                String::from_utf8(s)?
            };

            // flags: int32
            let flags = crate::util::read_i32_le(chunk)?;

            // joint_count: int32
            let joint_count = crate::util::read_i32_le(chunk)?;

            // joint_offset_radius: float
            let joint_offset_radius = crate::util::read_f32_le(chunk)?;

            // joints_per_minor_joint: int32
            let joints_per_minor_joint = crate::util::read_i32_le(chunk)?;

            // minor_joints_per_major_joint: int32
            let minor_joints_per_major_joint = crate::util::read_i32_le(chunk)?;

            // minor_joint_scale: float
            let minor_joint_scale = crate::util::read_f32_le(chunk)?;

            // major_joint_scale: float
            let major_joint_scale = crate::util::read_f32_le(chunk)?;

            // joint_move_speed: float
            let joint_move_speed = crate::util::read_f32_le(chunk)?;

            // joint_smoothness: float
            let joint_smoothness = crate::util::read_f32_le(chunk)?;

            // min_duration_between_joint_jumps: float
            let min_duration_between_joint_jumps = crate::util::read_f32_le(chunk)?;

            // max_duration_between_joint_jumps: float
            let max_duration_between_joint_jumps = crate::util::read_f32_le(chunk)?;

            // wave_height: float
            let wave_height = crate::util::read_f32_le(chunk)?;

            // wave_freq: float
            let wave_freq = crate::util::read_f32_le(chunk)?;

            // wave_speed: float
            let wave_speed = crate::util::read_f32_le(chunk)?;

            // min_wave_angle: float
            let min_wave_angle = crate::util::read_f32_le(chunk)?;

            // max_wave_angle: float
            let max_wave_angle = crate::util::read_f32_le(chunk)?;

            // min_wave_spin: float
            let min_wave_spin = crate::util::read_f32_le(chunk)?;

            // max_wave_spin: float
            let max_wave_spin = crate::util::read_f32_le(chunk)?;

            // arc_height: float
            let arc_height = crate::util::read_f32_le(chunk)?;

            // min_arc_angle: float
            let min_arc_angle = crate::util::read_f32_le(chunk)?;

            // max_arc_angle: float
            let max_arc_angle = crate::util::read_f32_le(chunk)?;

            // min_arc_spin: float
            let min_arc_spin = crate::util::read_f32_le(chunk)?;

            // max_arc_spin: float
            let max_arc_spin = crate::util::read_f32_le(chunk)?;

            // delay_between_effects: float
            let delay_between_effects = crate::util::read_f32_le(chunk)?;

            // min_flicker_on_duration: float
            let min_flicker_on_duration = crate::util::read_f32_le(chunk)?;

            // max_flicker_on_duration: float
            let max_flicker_on_duration = crate::util::read_f32_le(chunk)?;

            // min_flicker_off_duration: float
            let min_flicker_off_duration = crate::util::read_f32_le(chunk)?;

            // max_flicker_off_duration: float
            let max_flicker_off_duration = crate::util::read_f32_le(chunk)?;

            // pulse_speed: float
            let pulse_speed = crate::util::read_f32_le(chunk)?;

            // pulse_on_length: float
            let pulse_on_length = crate::util::read_f32_le(chunk)?;

            // pulse_fade_length: float
            let pulse_fade_length = crate::util::read_f32_le(chunk)?;

            // alpha: int8
            let alpha = crate::util::read_i8_le(chunk)?;

            // red: int8
            let red = crate::util::read_i8_le(chunk)?;

            // green: int8
            let green = crate::util::read_i8_le(chunk)?;

            // blue: int8
            let blue = crate::util::read_i8_le(chunk)?;

            // blend_mode: int8
            let blend_mode = crate::util::read_i8_le(chunk)?;

            // combo: string_ref
            let combo = {
                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
                String::from_utf8(s)?
            };

            // render_layer: int32
            let render_layer = crate::util::read_i32_le(chunk)?;

            // texture_length: float
            let texture_length = crate::util::read_f32_le(chunk)?;


            rows.push(SpellChainEffectsRow {
                id,
                avg_seg_len,
                width,
                noise_scale,
                tex_coord_scale,
                seg_duration,
                seg_delay,
                texture,
                flags,
                joint_count,
                joint_offset_radius,
                joints_per_minor_joint,
                minor_joints_per_major_joint,
                minor_joint_scale,
                major_joint_scale,
                joint_move_speed,
                joint_smoothness,
                min_duration_between_joint_jumps,
                max_duration_between_joint_jumps,
                wave_height,
                wave_freq,
                wave_speed,
                min_wave_angle,
                max_wave_angle,
                min_wave_spin,
                max_wave_spin,
                arc_height,
                min_arc_angle,
                max_arc_angle,
                min_arc_spin,
                max_arc_spin,
                delay_between_effects,
                min_flicker_on_duration,
                max_flicker_on_duration,
                min_flicker_off_duration,
                max_flicker_off_duration,
                pulse_speed,
                pulse_on_length,
                pulse_fade_length,
                alpha,
                red,
                green,
                blue,
                blend_mode,
                combo,
                render_layer,
                texture_length,
            });
        }

        Ok(SpellChainEffects { rows, })
    }

    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
        let header = DbcHeader {
            record_count: self.rows.len() as u32,
            field_count: 47,
            record_size: 173,
            string_block_size: self.string_block_size(),
        };

        b.write_all(&header.write_header())?;

        let mut string_index = 1;
        for row in &self.rows {
            // id: primary_key (SpellChainEffects) int32
            b.write_all(&row.id.id.to_le_bytes())?;

            // avg_seg_len: float
            b.write_all(&row.avg_seg_len.to_le_bytes())?;

            // width: float
            b.write_all(&row.width.to_le_bytes())?;

            // noise_scale: float
            b.write_all(&row.noise_scale.to_le_bytes())?;

            // tex_coord_scale: float
            b.write_all(&row.tex_coord_scale.to_le_bytes())?;

            // seg_duration: int32
            b.write_all(&row.seg_duration.to_le_bytes())?;

            // seg_delay: int32
            b.write_all(&row.seg_delay.to_le_bytes())?;

            // texture: string_ref
            if !row.texture.is_empty() {
                b.write_all(&(string_index as u32).to_le_bytes())?;
                string_index += row.texture.len() + 1;
            }
            else {
                b.write_all(&(0_u32).to_le_bytes())?;
            }

            // flags: int32
            b.write_all(&row.flags.to_le_bytes())?;

            // joint_count: int32
            b.write_all(&row.joint_count.to_le_bytes())?;

            // joint_offset_radius: float
            b.write_all(&row.joint_offset_radius.to_le_bytes())?;

            // joints_per_minor_joint: int32
            b.write_all(&row.joints_per_minor_joint.to_le_bytes())?;

            // minor_joints_per_major_joint: int32
            b.write_all(&row.minor_joints_per_major_joint.to_le_bytes())?;

            // minor_joint_scale: float
            b.write_all(&row.minor_joint_scale.to_le_bytes())?;

            // major_joint_scale: float
            b.write_all(&row.major_joint_scale.to_le_bytes())?;

            // joint_move_speed: float
            b.write_all(&row.joint_move_speed.to_le_bytes())?;

            // joint_smoothness: float
            b.write_all(&row.joint_smoothness.to_le_bytes())?;

            // min_duration_between_joint_jumps: float
            b.write_all(&row.min_duration_between_joint_jumps.to_le_bytes())?;

            // max_duration_between_joint_jumps: float
            b.write_all(&row.max_duration_between_joint_jumps.to_le_bytes())?;

            // wave_height: float
            b.write_all(&row.wave_height.to_le_bytes())?;

            // wave_freq: float
            b.write_all(&row.wave_freq.to_le_bytes())?;

            // wave_speed: float
            b.write_all(&row.wave_speed.to_le_bytes())?;

            // min_wave_angle: float
            b.write_all(&row.min_wave_angle.to_le_bytes())?;

            // max_wave_angle: float
            b.write_all(&row.max_wave_angle.to_le_bytes())?;

            // min_wave_spin: float
            b.write_all(&row.min_wave_spin.to_le_bytes())?;

            // max_wave_spin: float
            b.write_all(&row.max_wave_spin.to_le_bytes())?;

            // arc_height: float
            b.write_all(&row.arc_height.to_le_bytes())?;

            // min_arc_angle: float
            b.write_all(&row.min_arc_angle.to_le_bytes())?;

            // max_arc_angle: float
            b.write_all(&row.max_arc_angle.to_le_bytes())?;

            // min_arc_spin: float
            b.write_all(&row.min_arc_spin.to_le_bytes())?;

            // max_arc_spin: float
            b.write_all(&row.max_arc_spin.to_le_bytes())?;

            // delay_between_effects: float
            b.write_all(&row.delay_between_effects.to_le_bytes())?;

            // min_flicker_on_duration: float
            b.write_all(&row.min_flicker_on_duration.to_le_bytes())?;

            // max_flicker_on_duration: float
            b.write_all(&row.max_flicker_on_duration.to_le_bytes())?;

            // min_flicker_off_duration: float
            b.write_all(&row.min_flicker_off_duration.to_le_bytes())?;

            // max_flicker_off_duration: float
            b.write_all(&row.max_flicker_off_duration.to_le_bytes())?;

            // pulse_speed: float
            b.write_all(&row.pulse_speed.to_le_bytes())?;

            // pulse_on_length: float
            b.write_all(&row.pulse_on_length.to_le_bytes())?;

            // pulse_fade_length: float
            b.write_all(&row.pulse_fade_length.to_le_bytes())?;

            // alpha: int8
            b.write_all(&row.alpha.to_le_bytes())?;

            // red: int8
            b.write_all(&row.red.to_le_bytes())?;

            // green: int8
            b.write_all(&row.green.to_le_bytes())?;

            // blue: int8
            b.write_all(&row.blue.to_le_bytes())?;

            // blend_mode: int8
            b.write_all(&row.blend_mode.to_le_bytes())?;

            // combo: string_ref
            if !row.combo.is_empty() {
                b.write_all(&(string_index as u32).to_le_bytes())?;
                string_index += row.combo.len() + 1;
            }
            else {
                b.write_all(&(0_u32).to_le_bytes())?;
            }

            // render_layer: int32
            b.write_all(&row.render_layer.to_le_bytes())?;

            // texture_length: float
            b.write_all(&row.texture_length.to_le_bytes())?;

        }

        self.write_string_block(b)?;

        Ok(())
    }

}

impl Indexable for SpellChainEffects {
    type PrimaryKey = SpellChainEffectsKey;
    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
        let key = key.try_into().ok()?;
        self.rows.iter().find(|a| a.id.id == key.id)
    }

    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
        let key = key.try_into().ok()?;
        self.rows.iter_mut().find(|a| a.id.id == key.id)
    }
}

impl SpellChainEffects {
    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
        b.write_all(&[0])?;

        for row in &self.rows {
            if !row.texture.is_empty() { b.write_all(row.texture.as_bytes())?; b.write_all(&[0])?; };
            if !row.combo.is_empty() { b.write_all(row.combo.as_bytes())?; b.write_all(&[0])?; };
        }

        Ok(())
    }

    fn string_block_size(&self) -> u32 {
        let mut sum = 1;
        for row in &self.rows {
            if !row.texture.is_empty() { sum += row.texture.len() + 1; };
            if !row.combo.is_empty() { sum += row.combo.len() + 1; };
        }

        sum as u32
    }

}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct SpellChainEffectsKey {
    pub id: i32
}

impl SpellChainEffectsKey {
    pub const fn new(id: i32) -> Self {
        Self { id }
    }

}

impl From<u8> for SpellChainEffectsKey {
    fn from(v: u8) -> Self {
        Self::new(v.into())
    }
}

impl From<u16> for SpellChainEffectsKey {
    fn from(v: u16) -> Self {
        Self::new(v.into())
    }
}

impl From<i8> for SpellChainEffectsKey {
    fn from(v: i8) -> Self {
        Self::new(v.into())
    }
}

impl From<i16> for SpellChainEffectsKey {
    fn from(v: i16) -> Self {
        Self::new(v.into())
    }
}

impl From<i32> for SpellChainEffectsKey {
    fn from(v: i32) -> Self {
        Self::new(v)
    }
}

impl TryFrom<u32> for SpellChainEffectsKey {
    type Error = u32;
    fn try_from(v: u32) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<usize> for SpellChainEffectsKey {
    type Error = usize;
    fn try_from(v: usize) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<u64> for SpellChainEffectsKey {
    type Error = u64;
    fn try_from(v: u64) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<i64> for SpellChainEffectsKey {
    type Error = i64;
    fn try_from(v: i64) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<isize> for SpellChainEffectsKey {
    type Error = isize;
    fn try_from(v: isize) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SpellChainEffectsRow {
    pub id: SpellChainEffectsKey,
    pub avg_seg_len: f32,
    pub width: f32,
    pub noise_scale: f32,
    pub tex_coord_scale: f32,
    pub seg_duration: i32,
    pub seg_delay: i32,
    pub texture: String,
    pub flags: i32,
    pub joint_count: i32,
    pub joint_offset_radius: f32,
    pub joints_per_minor_joint: i32,
    pub minor_joints_per_major_joint: i32,
    pub minor_joint_scale: f32,
    pub major_joint_scale: f32,
    pub joint_move_speed: f32,
    pub joint_smoothness: f32,
    pub min_duration_between_joint_jumps: f32,
    pub max_duration_between_joint_jumps: f32,
    pub wave_height: f32,
    pub wave_freq: f32,
    pub wave_speed: f32,
    pub min_wave_angle: f32,
    pub max_wave_angle: f32,
    pub min_wave_spin: f32,
    pub max_wave_spin: f32,
    pub arc_height: f32,
    pub min_arc_angle: f32,
    pub max_arc_angle: f32,
    pub min_arc_spin: f32,
    pub max_arc_spin: f32,
    pub delay_between_effects: f32,
    pub min_flicker_on_duration: f32,
    pub max_flicker_on_duration: f32,
    pub min_flicker_off_duration: f32,
    pub max_flicker_off_duration: f32,
    pub pulse_speed: f32,
    pub pulse_on_length: f32,
    pub pulse_fade_length: f32,
    pub alpha: i8,
    pub red: i8,
    pub green: i8,
    pub blue: i8,
    pub blend_mode: i8,
    pub combo: String,
    pub render_layer: i32,
    pub texture_length: f32,
}