l6t_file/
types.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/// The Type ID of a chunk.
#[derive(Clone,Copy,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct TypeID(pub [u8; 4]);

macro_rules! type_id {
    ($name:ident, $val:expr) => (
        pub const $name: TypeID = TypeID([$val[0], $val[1], $val[2], $val[3]]);
    );
    ($name:ident) => (
        type_id!($name, stringify!($name).as_bytes());
    )
}

type_id!(FORM);
type_id!(LIST);
type_id!(FORM_LE, b"MROF");
type_id!(LIST_LE, b"TSIL");

impl TypeID {
    pub fn from_data(data: &[u8; 4], little_endian: bool) -> Self {
        if little_endian {
            TypeID([data[3], data[2], data[1], data[0]])
        } else {
            TypeID([data[0], data[1], data[2], data[3]])
        }
    }

    pub fn to_data(&self, data: &mut [u8; 4], little_endian: bool) {
        if little_endian {
            data[0] = self.0[3];
            data[1] = self.0[2];
            data[2] = self.0[1];
            data[3] = self.0[0];
        } else {
            data[0] = self.0[0];
            data[1] = self.0[1];
            data[2] = self.0[2];
            data[3] = self.0[3];
        }
    }


    pub fn is_le_envelope(self) -> bool {
        match self {
            FORM_LE | LIST_LE => true,
            _ => false,
        }
    }

    pub fn is_envelope(self) -> bool {
        match self {
            FORM | LIST => true,
            _ => false,
        }
    }
    pub fn reverse(&self) -> TypeID {
        TypeID([self.0[3], self.0[2], self.0[1], self.0[0]])
    }
}

impl std::fmt::Display for TypeID {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(),std::fmt::Error> {
        write!(f, "{}{}{}{}", self.0[0] as char, self.0[1] as char, self.0[2] as char, self.0[3] as char)
    }
}

impl std::fmt::Debug for TypeID {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "TypeID(b\"{}\")", self)
    }
}

impl<'a> From<&'a [u8; 4]> for TypeID {
    fn from(id: &'a [u8; 4]) -> TypeID {
        TypeID([id[0], id[1], id[2], id[3]])
    }
}

type_id!(L6BA);
type_id!(L6CO);
type_id!(L6PA);
type_id!(L6TE);
type_id!(L6AS);
type_id!(L6FS);
type_id!(HEAD);
type_id!(BANK);
type_id!(FLDR);
type_id!(BINF);
type_id!(PINF);
type_id!(MINF);
type_id!(MODL);
type_id!(PARM);
type_id!(PATC);
type_id!(UNFO);
type_id!(INFO);
type_id!(META);
type_id!(TUNE);
type_id!(TN12);
type_id!(TNAC);
type_id!(TNGS);
type_id!(TNG2);
type_id!(PKPS);
type_id!(PKP_);
type_id!(L6GP);
type_id!(TONE);
type_id!(AMP_);
type_id!(CAB_);
type_id!(PRFX);
type_id!(EFX_);
type_id!(SSLB);
type_id!(LHDR);
type_id!(WSEQ);
type_id!(LENT);

pub const UNALIGNED_CHUNKS: &[&TypeID] = &[ &SSLB ];
pub const PATCH_IDS: &[&TypeID] = &[ &L6PA, &L6AS, &L6FS, &L6TE, &L6GP ];
pub const BUNDLE_IDS: &[&TypeID] = &[ &L6BA, &L6CO, &SSLB ];

type_id!(IAUT);
type_id!(IBND);
type_id!(ICMT);
type_id!(IGTR);
type_id!(ISNG);
type_id!(ISTL);
type_id!(IPUS);
type_id!(IPUP);
type_id!(IDAT);
type_id!(IAMP);
type_id!(IAPP);
type_id!(IAPV);