wolfrpg_map_parser/command/sound_command/
filename.rs

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
use crate::byte_utils::{as_u32_le, parse_string};
use crate::command::sound_command::operation::Operation;
use crate::command::sound_command::options::Options;
use crate::command::sound_command::variable::Variable;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq)]
pub struct Filename {
    variable_state: Variable,
    volume: u32,
    tempo: u32,
    loop_point: Option<u32>,
    sound_filename: String
}

impl Filename {
    pub(crate) fn parse(bytes: &[u8], options: &Options) -> (usize, Self) {
        let mut offset: usize = 0;

        let (bytes_read, variable_state): (usize, Variable) = Variable::parse(bytes, options);
        offset += bytes_read;

        let volume: u32 = as_u32_le(&bytes[offset..offset + 4]);
        offset += 4;

        let tempo: u32 = as_u32_le(&bytes[offset..offset + 4]);
        offset += 4;

        let loop_point: Option<u32> = match *options.operation() {
            Operation::SetSE => None,
            _ => {
                let loop_point: u32 = as_u32_le(&bytes[offset..offset + 4]);
                offset += 4;

                Some(loop_point)
            }
        };

        let (bytes_read, sound_filename): (usize, String) = parse_string(&bytes[offset..]);
        offset += bytes_read;

        (offset, Self {
            variable_state,
            volume,
            tempo,
            loop_point,
            sound_filename
        })
    }

    pub fn delay_playback(&self) -> Option<u32> {
        self.variable_state.delay_playback()
    }

    pub fn delay_playback_mut(&mut self) -> &mut Option<u32> {
        self.variable_state.delay_playback_mut()
    }

    pub fn fade_time(&self) -> Option<u32> {
        self.variable_state.fade_time()
    }

    pub fn fade_time_mut(&mut self) -> &mut Option<u32> {
        self.variable_state.fade_time_mut()
    }

    pub fn variable(&self) -> u32 {
        self.variable_state.variable()
    }

    pub fn variable_mut(&mut self) -> &mut u32 {
        self.variable_state.variable_mut()
    }

    pub fn start_time(&self) -> u32 {
        self.variable_state.start_time()
    }

    pub fn start_time_mut(&mut self) -> &mut u32 {
        self.variable_state.start_time_mut()
    }

    pub fn volume(&self) -> u32 {
        self.volume
    }
    
    pub fn volume_mut(&mut self) -> &mut u32 {
        &mut self.volume
    }

    pub fn tempo(&self) -> u32 {
        self.tempo
    }
    
    pub fn tempo_mut(&mut self) -> &mut u32 {
        &mut self.tempo
    }

    pub fn loop_point(&self) -> Option<u32> {
        self.loop_point
    }
    
    pub fn loop_point_mut(&mut self) -> &mut Option<u32> {
        &mut self.loop_point
    }

    pub fn sound_filename(&self) -> &str {
        &self.sound_filename
    }
    
    pub fn sound_filename_mut(&mut self) -> &mut String {
        &mut self.sound_filename
    }
}