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
use crate::hii::{FontStyle, StringId};
use crate::util::{c_str_at_end, w_str_at_end};

#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum SibtKind {
    End = 0x00,
    StringScsu = 0x10,
    StringScsuFont = 0x11,
    StringsScsu = 0x12,
    StringsScsuFont = 0x13,
    StringUcs2 = 0x14,
    StringUcs2Font = 0x15,
    StringsUcs2 = 0x16,
    StringsUcs2Font = 0x17,
    Duplicate = 0x20,
    Skip2 = 0x21,
    Skip1 = 0x22,
    Ext1 = 0x30,
    Ext2 = 0x31,
    Ext4 = 0x32,
    Font = 0x40,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtHeader {
    pub BlockType: SibtKind
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtDuplicate {
    pub Header: SibtHeader,
    pub StringId: StringId,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtEnd {
    pub Header: SibtHeader,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtExt1 {
    pub Header: SibtHeader,
    pub BlockType2: u8,
    pub Length: u8,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtExt2 {
    pub Header: SibtHeader,
    pub BlockType2: u8,
    pub Length: u16,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtExt4 {
    pub Header: SibtHeader,
    pub BlockType2: u8,
    pub Length: u32,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtFont {
    pub Header: SibtHeader,
    pub FontId: u8,
    pub FontSize: u16,
    pub FontStyle: FontStyle
}

impl SibtFont {
    pub fn FontName(&self) -> &[u8] {
        unsafe { c_str_at_end(self, 0) }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtSkip1 {
    pub Header: SibtHeader,
    pub SkipCount: u8,
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtSkip2 {
    pub Header: SibtHeader,
    // For some reason using u16 caused the size of the struct to be 4
    // It seems that it should actually be 3
    pub SkipCountBytes: [u8; 2],
}

impl SibtSkip2 {
    // TODO: big endian?
    pub fn SkipCount(&self) -> u16 {
        self.SkipCountBytes[0] as u16 |
        (self.SkipCountBytes[1] as u16) << 8
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringScsu {
    pub Header: SibtHeader,
}

impl SibtStringScsu {
    pub fn StringText(&self) -> &[u8] {
        unsafe { c_str_at_end(self, 0) }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringScsuFont {
    pub Header: SibtHeader,
    pub FontIdentifier: u8,
}

impl SibtStringScsuFont {
    pub fn StringText(&self) -> &[u8] {
        unsafe { c_str_at_end(self, 0) }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringsScsu {
    pub Header: SibtHeader,
    pub StringCount: u16,
}

impl SibtStringsScsu {
    pub fn StringText(&self, string: u16) -> &[u8] {
        if string < self.StringCount {
            unsafe { c_str_at_end(self, string as usize) }
        } else {
            &[]
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringsScsuFont {
    pub Header: SibtHeader,
    pub FontIdentifier: u8,
    pub StringCount: u16,
}

impl SibtStringsScsuFont {
    pub fn StringText(&self, string: u16) -> &[u8] {
        if string < self.StringCount {
            unsafe { c_str_at_end(self, string as usize) }
        } else {
            &[]
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringUcs2 {
    pub Header: SibtHeader,
}

impl SibtStringUcs2 {
    pub fn StringText(&self) -> &[u16] {
        unsafe { w_str_at_end(self, 0) }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringUcs2Font {
    pub Header: SibtHeader,
    pub FontIdentifier: u8,
}

impl SibtStringUcs2Font {
    pub fn StringText(&self) -> &[u16] {
        unsafe { w_str_at_end(self, 0) }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringsUcs2 {
    pub Header: SibtHeader,
    pub StringCount: u16,
}

impl SibtStringsUcs2 {
    pub fn StringText(&self, string: u16) -> &[u16] {
        if string < self.StringCount {
            unsafe { w_str_at_end(self, string as usize) }
        } else {
            &[]
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct SibtStringsUcs2Font {
    pub Header: SibtHeader,
    pub FontIdentifier: u8,
    pub StringCount: u16,
}

impl SibtStringsUcs2Font {
    pub fn StringText(&self, string: u16) -> &[u16] {
        if string < self.StringCount {
            unsafe { w_str_at_end(self, string as usize) }
        } else {
            &[]
        }
    }
}