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
use crate::{NbsError, NbsFormat};
use byteorder::LittleEndian;
use std::time::Duration;
#[derive(Debug)]
pub struct Header {
pub(crate) old_song_length: i16,
pub(crate) version_number: Option<i8>,
pub vannila_instrument_count: Option<i8>,
pub(crate) song_length: Option<i16>,
pub layer_count: i16,
pub song_name: String,
pub song_author: String,
pub original_song_author: String,
pub song_description: String,
pub song_tempo: i16,
pub auto_saving: bool,
pub auto_saving_duration: i8,
pub time_signature: i8,
pub minutes_spent: i32,
pub left_clicks: i32,
pub right_clicks: i32,
pub noteblocks_added: i32,
pub noteblocks_removed: i32,
pub imported_file_name: String,
pub is_loop: Option<bool>,
pub max_loop_count: Option<i8>,
pub loop_start_tick: Option<i16>,
pub format: NbsFormat,
}
impl Header {
pub fn new(format: NbsFormat) -> Self {
Header {
old_song_length: 0,
version_number: Some(format.version()),
vannila_instrument_count: Some(16),
song_length: Some(0),
layer_count: 0,
song_name: String::new(),
song_author: String::new(),
original_song_author: String::new(),
song_description: String::new(),
song_tempo: 1000,
auto_saving: false,
auto_saving_duration: 0,
time_signature: 4,
minutes_spent: 0,
left_clicks: 0,
right_clicks: 0,
noteblocks_added: 0,
noteblocks_removed: 0,
imported_file_name: String::new(),
is_loop: Some(false),
max_loop_count: Some(0),
loop_start_tick: Some(0),
format: format,
}
}
pub fn decode<R>(reader: &mut R) -> Result<Self, NbsError>
where
R: crate::ReadStringExt,
{
let old_song_length = reader.read_i16::<LittleEndian>()?;
let version = if old_song_length != 0 {
NbsFormat::NoteBlockStudio
} else {
NbsFormat::OpenNoteBlockStudio(reader.read_i8()?)
};
let version_number = match version {
NbsFormat::NoteBlockStudio => None,
NbsFormat::OpenNoteBlockStudio(v) => Some(v),
};
let vannila_instrument_count = if version.is_new() {
Some(reader.read_i8()?)
} else {
None
};
let song_length = if version.is_new() {
Some(reader.read_i16::<LittleEndian>()?)
} else {
None
};
let layer_count = reader.read_i16::<LittleEndian>()?;
let song_name = reader.read_string()?;
let song_author = reader.read_string()?;
let original_song_author = reader.read_string()?;
let song_description = reader.read_string()?;
let song_tempo = reader.read_i16::<LittleEndian>()?;
let auto_saving = if reader.read_i8()? == 1 { true } else { false };
let auto_saving_duration = reader.read_i8()?;
let time_signature = reader.read_i8()?;
let minutes_spent = reader.read_i32::<LittleEndian>()?;
let left_clicks = reader.read_i32::<LittleEndian>()?;
let right_clicks = reader.read_i32::<LittleEndian>()?;
let noteblocks_added = reader.read_i32::<LittleEndian>()?;
let noteblocks_removed = reader.read_i32::<LittleEndian>()?;
let imported_file_name = reader.read_string()?;
let is_loop = if version.is_new() {
Some(if reader.read_i8()? == 1 { true } else { false })
} else {
None
};
let max_loop_count = if version.is_new() {
Some(reader.read_i8()?)
} else {
None
};
let loop_start_tick = if version.is_new() {
Some(reader.read_i16::<LittleEndian>()?)
} else {
None
};
Ok(Header {
old_song_length,
version_number,
vannila_instrument_count,
song_length,
layer_count,
song_name,
song_author,
original_song_author,
song_description,
song_tempo,
auto_saving,
auto_saving_duration,
time_signature,
minutes_spent,
left_clicks,
right_clicks,
noteblocks_added,
noteblocks_removed,
imported_file_name,
is_loop,
max_loop_count,
loop_start_tick,
format: version,
})
}
pub fn encode<W>(&self, format: NbsFormat, writer: &mut W) -> Result<(), NbsError>
where
W: crate::WriteStringExt,
{
writer.write_i16::<LittleEndian>(self.old_song_length)?;
if format.version() > 0 {
writer.write_i8(self.version_number.ok_or(NbsError::InvalidTargetFormat)?)?;
writer.write_i8(
self.vannila_instrument_count
.ok_or(NbsError::InvalidTargetFormat)?,
)?;
}
if format.version() >= 3 {
writer.write_i16::<LittleEndian>(
self.song_length.ok_or(NbsError::InvalidTargetFormat)?,
)?;
}
writer.write_i16::<LittleEndian>(self.layer_count)?;
writer.write_string(&self.song_name)?;
writer.write_string(&self.song_author)?;
writer.write_string(&self.original_song_author)?;
writer.write_string(&self.song_description)?;
writer.write_i16::<LittleEndian>(self.song_tempo)?;
writer.write_i8(if self.auto_saving { 1 } else { 0 })?;
writer.write_i8(self.auto_saving_duration)?;
writer.write_i8(self.time_signature)?;
writer.write_i32::<LittleEndian>(self.minutes_spent)?;
writer.write_i32::<LittleEndian>(self.left_clicks)?;
writer.write_i32::<LittleEndian>(self.right_clicks)?;
writer.write_i32::<LittleEndian>(self.noteblocks_added)?;
writer.write_i32::<LittleEndian>(self.noteblocks_removed)?;
writer.write_string(&self.imported_file_name)?;
if format.version() > 0 {
writer.write_i8(if self.is_loop.ok_or(NbsError::InvalidTargetFormat)? {
1
} else {
0
})?;
writer.write_i8(self.max_loop_count.ok_or(NbsError::InvalidTargetFormat)?)?;
writer.write_i16::<LittleEndian>(
self.loop_start_tick.ok_or(NbsError::InvalidTargetFormat)?,
)?;
}
Ok(())
}
pub fn song_ticks(&self) -> Option<i16> {
match self.format {
NbsFormat::NoteBlockStudio => Some(self.old_song_length),
NbsFormat::OpenNoteBlockStudio(v) => {
if v >= 3 {
Some(self.song_length.unwrap())
} else {
None
}
}
}
}
pub fn song_length(&self) -> Option<Duration> {
let song_ticks = self.song_ticks();
if song_ticks.is_none() {
return None;
}
Some(Duration::from_secs_f32(
song_ticks.unwrap() as f32 / (self.song_tempo as f32 / 100.0),
))
}
}