rg_formats 0.1.2

Parsers and Serializers for various rhythm game formats.
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Parsing, Processing and serializing of `.msd` files. This is the file format that
//! underpins `.sm` and `.ssc`.
//!
//! **You might be looking for an `.sm` parser. Use [`sm`][crate::sm] for that.**
//!
//! MSD or "Music Score Data" is an extremely old format for very early dance pad
//! emulators. While nowadays the `.msd` extension is effectively dead, it lives on in
//! `.sm` and `.ssc`, as its grammar underpins both.
//!
//! This module implements a stepmania-accurate MSD parsers, with error correction and all.
//!
//! This includes handling *extremely* obscure parse cases, like `#FOO;BAR;BAR`, which
//! every other SM parser I've tested chokes on.
//!
//! # What is MSD?
//!
//! An average MSD file looks like this:
//!
//! ```msd
//! #TITLE:Hello World;
//! #ARTIST:McLusky;
//! //--------------- dance-single - Todestrieb ----------------
//! #NOTES:
//!      dance-single:
//!      Todestrieb:
//!      Hard:
//!      15:
//!      0,0,0,0,0:
//! 0000
//! 1000
//! 0000
//! 1000;
//! ```
//!
//! Despite what you might intuit by looking at it, MSD parses into a 2 dimensional array.
//!
//! The `#` value indicates a tag. A file may have multiple of the same tag, and there are
//! formats implemented ontop of MSD (like SSC) that depend on this behaviour.
//!
//! Subsequent colons -- `:` -- indicate the values associated with that column. For
//! example, the `#NOTES` field in the above example should parse into:
//!
//! ```json
//! ["dance-single", "Todestrieb", "Hard", "15", "0,0,0,0,0", "0000\n1000\n0000\n1000"]
//! ```
//!
//! MSD is a grammar which is theoretically as follows:
//!
//! ```ebnf
//! "#" tag_name (":" value)* ";""
//! ```
//!
//! However, many people back in the day were editing these files in notepad, so Stepmania
//! added very helpful error correction to `.sm` files. This allowed things such as:
//!
//! ```msd
//! #ARTIST:McLusky
//!   #Title:Without MSD I Am Nothing;
//! ```
//!
//! to parse perfectly fine (note the lack of semi-colon, indentation, and missing
//! capitalization on the `#TITLE` field.)
//!
//! Even more devilishly, SM's error correction allows this to parse:
//!
//! ```msd
//! #NOTES:
//!      dance-single
//!      Todestrieb; // <-- note the SEMICOLON HERE!!!
//!      Hard:
//!      15:
//!      0,0,0,0,0:
//! 0000
//! 1000
//! 0000
//! 1000;
//! ```
//!
//! As a result of this error correction, Stepmania's implementation of MSD ends up
//! looking like this:
//!
//! ```ebnf
//! ("\n" | start_of_file) "#" tag_name ( ( ":" | ";" ) tag_value )+
//! ```
//!
//! # Other Things
//!
//! MSD also supports `//` comments, and `\` as an escape character. This allows:
//!
//! ```msd
//! #TITLE\//:SongTitle\;
//! \ #ARTIST:McLusky
//! ```
//!
//! to parse as
//! ```json
//! ["TITLE//", "SongTitle"]
//! ["ARTIST", "McLusky"]
//! ```
//!
//! [sm]: crate::sm

use std::fmt::Debug;

use super::utils::ByteIterator;
use crate::{read, utils::ByteString};

/// An MSD file is a vector of [`MsdElement`]s. Tags may be repeated, and will *always*
/// be uppercased for SM compatibility.
#[derive(Debug, Clone, Default)]
pub struct MsdFile {
	/// The elements this MsdFile defined.
	pub elements: Vec<MsdElement>,
}

impl MsdFile {
	/// Make an empty MsdFile.
	pub fn new() -> Self {
		Self::default()
	}

	/// Get the first element with this tag.
	pub fn with_tag(&self, tag: &str) -> Option<MsdElement> {
		let tag_upper = tag.to_ascii_uppercase();
		let tag_upper = tag_upper.as_bytes();

		for el in &self.elements {
			if *el.tag == *tag_upper {
				return Some(el.clone());
			}
		}

		None
	}

	/// Get the first instance of this tag, and get the first value.
	/// Common for things like
	/// #TITLE:Foo;
	///        ^^^
	///
	/// Returns Some("foo") (boxed and sliced whatever)
	pub fn first_tag_first_val(&self, tag: &str) -> Option<Box<[u8]>> {
		let v = self.with_tag(tag)?;

		match v.values.first() {
			Some(v) => {
				// empty strings count as none
				if v.is_empty() {
					return None;
				}

				Some(v.clone())
			}
			None => None,
		}
	}

	/// Get all elements with this tag. This is for things like #NOTES, which may
	/// appear multiple times.
	pub fn all_with_tag(&self, tag: &str) -> Vec<&MsdElement> {
		let tag_upper = tag.to_ascii_uppercase();
		let tag_upper = tag_upper.as_bytes();

		self.elements
			.iter()
			.filter(|f| *f.tag == *tag_upper)
			.collect()
	}
}

/// An element in the MSD format. This is parsed from data like:
///
/// ```txt
/// #TITLE:Hello World;
/// #ARTIST:Foo;
/// ```
#[derive(Clone)]
pub struct MsdElement {
	/// The first value in this element -- this is prefixed with a `#` and ALWAYS
	/// uppercased.
	pub tag: ByteString,
	/// The values this tag pointed to. MSD elements can have an arbitrary amount of
	/// elements tied to their tag:
	///
	/// ```txt
	/// #TAG:Value1:value2:Value3;
	/// ```
	pub values: Vec<ByteString>,
}

impl Debug for MsdElement {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("MsdElement")
			.field("tag", &String::from_utf8_lossy(&self.tag))
			.field(
				"values",
				&self
					.values
					.iter()
					.map(|f| String::from_utf8_lossy(f))
					.collect::<Vec<_>>(),
			)
			.finish()
	}
}

fn write_param(data: &mut Vec<u8>, param: &ByteString) {
	for byte in param.iter() {
		let byte = *byte;

		if matches!(byte, b':' | b';' | b'#' | b'\\') {
			data.push(b'\\');
		}

		data.push(byte);
	}
}

/// Given a list of [`MsdElement`]s, serialize them. This can be used to serialize SM
/// files, or SSC files, or whatnot.
pub fn serialize_msd_elements(elements: Vec<MsdElement>) -> Box<[u8]> {
	let mut data = vec![];

	for el in elements {
		data.push(b'#');

		write_param(&mut data, &el.tag);

		for value in el.values {
			data.push(b':');

			write_param(&mut data, &value);
		}

		data.push(b';');
		data.push(b'\n');
	}

	data.into()
}

/// SM's "MSD" format is the internal underpinning for the
/// SM, SSC, DWI and SMA formats.
///
/// It is a list of tuples with the intended grammar of:
///
/// ```bnf
/// "#" tag_name (":" value)+ ";"
/// ```
///
/// However, some error correction code built into stepmania results in the grammar
/// being *vastly* different than intended. Instead, we have
///
/// ```bnf
/// separator ::= ":" | ";"
///
/// "#" tag_name (separator value)+ \n
/// ```
///
/// Most importantly, even though it looks like a hashmap
/// repeated keys are perfectly allowed and infact important
/// to the syntax.
pub fn from_bytes(buf: &[u8]) -> MsdFile {
	let mut elements: Vec<MsdElement> = vec![];
	let mut b_iter = ByteIterator::new(buf);

	loop {
		let (param_list, term_reason) = parse_param_list(&mut b_iter);

		// only add the tag to the elements if it's non-empty
		if let Some(tag) = param_list.first() {
			elements.push(MsdElement {
				tag: tag.to_ascii_uppercase().into(),
				values: param_list.into_iter().skip(1).collect(),
			})
		}

		// found the last thing in this file
		if matches!(term_reason, ParamListTermReason::Eof) {
			break;
		}
	}

	MsdFile { elements }
}

enum ParamListTermReason {
	Eof,
	Hash,
}

/// Parse "#TAG:VALUE:VALUE2;" into a vector of ["TAG", "VALUE", "VALUE2"]
/// Also returns the reason why we terminated.
fn parse_param_list(b_iter: &mut ByteIterator) -> (Vec<ByteString>, ParamListTermReason) {
	let mut params = vec![];

	// firstly skip until values actually start
	loop {
		match b_iter.read() {
			// if we see a hash, values have started
			Some(b'#') => break,
			// if we hit EOF, return params
			None => return (params, ParamListTermReason::Eof),
			// otherwise, keep going
			_ => continue,
		}
	}

	loop {
		let (param, terminate) = parse_param(b_iter);

		match terminate {
			ParamTermReason::Hash => {
				// walk back one since we've *passed* the "#", but it's important
				// for the next iteration.
				b_iter.rewind();

				// only add the last param if it's not empty
				// this prevents
				// #ARTIST:FOO;
				// from being parsed as ["ARTIST", "FOO", ""]
				if !param.is_empty() {
					params.push(param);
				}

				return (params, ParamListTermReason::Hash);
			}
			ParamTermReason::Eof => {
				// see above
				if !param.is_empty() {
					params.push(param);
				}

				return (params, ParamListTermReason::Eof);
			}
			// the other separators are completely equivalent
			// #TAG:VALUE;#TAG2:VALUE2;
			// should parse as ["TAG", "VALUE", "#TAG2", "VALUE2"]
			// which is weird
			_ => {}
		}

		params.push(param);

		// otherwise keep slurping as many params as you can.
	}
}

enum ParamTermReason {
	Hash,
	Colon,
	Semicolon,
	Eof,
}

fn parse_param(b_iter: &mut ByteIterator) -> (ByteString, ParamTermReason) {
	// inner function for convenience since we have multiple annoying returns.
	fn parse_val_inner(b_iter: &mut ByteIterator) -> ByteString {
		let mut param = vec![];

		loop {
			let byte = read!(b_iter);

			match byte {
				b'\\' => {
					// escape char means read the next char literally.
					let next_byte = read!(b_iter);

					// even if this is a special char, add it to the param.
					param.push(next_byte);
				}
				// since read!() increments the cursor
				// peek will be off-by-one.
				// comments are two /'s literally in a row.
				b'/' if b_iter.peek() == Some(&b'/') => {
					// it's a comment, read until newline or eof

					let mut chomp = read!(b_iter);
					while chomp != b'\n' {
						chomp = read!(b_iter);
					}

					// keep the newline
					param.push(b'\n');
				}
				b'#' => {
					// a hash could be part of the value:
					// #TITLE:Magical #girl;
					// or could be the sign of a missing semicolon:
					// #TITLE:Magical
					// #ARTIST:Girl

					// this depends on whether we see a newline preceding this.
					// go backwards through what we've already read.
					for backtrack_byte in param.iter().rev() {
						if *backtrack_byte == b'\n' {
							// this was meant to be a separator, exit the function.
							return param.into();
						}

						if *backtrack_byte == b' ' || *backtrack_byte == b'\t' {
							// things like
							// #Title:foo
							//    #artist:bar
							// should still be understood as a missing semicolon
							continue;
						}

						// no, this # is part of the value literally
						break;
					}

					param.push(byte);
				}
				b':' | b';' => {
					// end of value
					return param.into();
				}
				_ => {
					// everything else is just part of the value
					param.push(byte)
				}
			}
		}

		param.into()
	}

	// trim all leading and trailing whitespace from the value.
	let trimmed = parse_val_inner(b_iter).trim_ascii().into();

	let t_reason = match b_iter.prev() {
		Some(b'#') => ParamTermReason::Hash,
		Some(b':') => ParamTermReason::Colon,
		Some(b';') => ParamTermReason::Semicolon,
		None => ParamTermReason::Eof,

		// should be impossible
		u => panic!("Unexpected terminator {u:?}"),
	};

	(trimmed, t_reason)
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::test_utils::test_file_read;
	use pretty_assertions::assert_eq;

	macro_rules! test_parse_prm {
		($input: expr, $index: expr, $out: expr) => {{
			let mut biter = ByteIterator::new($input);
			biter.set_index($index);

			assert_eq!(
				std::str::from_utf8(&parse_param(&mut biter).0).unwrap(),
				$out
			)
		}};
	}

	#[test]
	fn p_val() {
		test_parse_prm!(b"#TITLE:AMONG US;", 1, "TITLE");
		test_parse_prm!(b"#TITLE:AMONG US;", 7, "AMONG US");
	}

	#[test]
	fn newlines() {
		test_parse_prm!(
			b"#NOTEDATA:
0000
0100
0000
0001, // measure 1
0000
0000
0000
0000
, // measure 2
;",
			10,
			"0000
0100
0000
0001, 
0000
0000
0000
0000
,"
		);

		test_parse_prm!(
			b"#TITLE: New Line
Here;",
			7,
			"New Line
Here"
		);

		test_parse_prm!(
			b"#TITLE: Missing Semicolon
#ARTIST: foo;",
			7,
			"Missing Semicolon"
		);

		test_parse_prm!(
			b"#TITLE: Missing Semicolon wspace
	  #ARTIST: foo;",
			7,
			"Missing Semicolon wspace"
		);

		test_parse_prm!(
			b"#TITLE: Missing Semicolon
	  heyo #ARTIST: foo;",
			7,
			"Missing Semicolon
	  heyo #ARTIST"
		);
	}

	#[test]
	fn escape() {
		test_parse_prm!(br"#TITLE: foo\:bar;", 7, r"foo:bar");
		test_parse_prm!(br"#TITLE: foo\\:bar;", 7, r"foo\");

		test_parse_prm!(br"#TITLE: foo\\\:bar;", 7, r"foo\:bar");
		test_parse_prm!(
			br"#TITLE: foo\
#artist",
			7,
			r"foo"
		);
	}

	#[test]
	fn multival() {
		test_parse_prm!(b"#MULTI:VALUE:VALUE2;", 7, "VALUE");
		test_parse_prm!(b"#MULTI:VALUE:VALUE2;", 13, "VALUE2");
	}

	#[test]
	fn comment() {
		test_parse_prm!(b"#TITLE: A/BCD;", 7, "A/BCD");
		test_parse_prm!(b"#TITLE: A//BCD;", 7, "A");
	}

	macro_rules! test_parse_plist {
		($input: expr, $out: expr) => {{
			let mut biter = ByteIterator::new($input);

			assert_eq!(
				parse_param_list(&mut biter)
					.0
					.iter()
					.map(|b| std::str::from_utf8(&b).unwrap())
					.collect::<Vec<_>>(),
				$out
			);
		}};
	}

	#[test]
	fn list() {
		test_parse_plist!(b"#TITLE:FOO", vec!["TITLE", "FOO"]);
		test_parse_plist!(b"#TITLE:FOO;BAR:BAZ", vec!["TITLE", "FOO", "BAR", "BAZ"]);

		test_parse_plist!(
			b"#TITLE:FOO;
#ARTIST:AMONG_US;",
			vec!["TITLE", "FOO"]
		);

		// lol, wtf
		// very surprising parse, but this is how SM handles it aswell.
		// /shrug
		test_parse_plist!(b"#TITLE:FOO;#BAR:BAZ", vec!["TITLE", "FOO", "#BAR", "BAZ"]);

		test_parse_plist!(
			b"#TITLE:FOO
;#BAR:BAZ",
			vec!["TITLE", "FOO", "#BAR", "BAZ"]
		);

		test_parse_plist!(
			b"#TITLE:FOO
#BAR:BAZ",
			vec!["TITLE", "FOO"]
		);
		test_parse_plist!(
			b"#TITLE:FOO:
#BAR:BAZ",
			vec!["TITLE", "FOO"]
		);

		test_parse_plist!(
			br"#TITLE:FOO:
\ #BAR:BAZ",
			vec!["TITLE", "FOO"]
		);
	}

	fn full_comp(buf: &[u8], expected: Vec<Vec<&str>>) {
		let parse_res = from_bytes(buf);

		for (i, values) in expected.iter().enumerate() {
			let e = parse_res.elements[i].clone();

			assert_eq!(
				std::str::from_utf8(&e.tag).unwrap(),
				values.first().unwrap().to_owned()
			);

			assert_eq!(
				e.values
					.into_iter()
					.map(|f| std::str::from_utf8(&f).unwrap().to_owned())
					.collect::<Vec<String>>(),
				values
					.iter()
					.skip(1)
					// lolwhat
					.map(|f| f.to_owned().to_owned())
					.collect::<Vec<String>>()
			);
		}
	}

	#[test]
	fn full_parse() {
		let expected = vec![
			vec!["TITLE", "[11] [120] Le Perv (zk test ed.)"],
			vec!["ARTIST", "Carpenter Brut"],
			vec!["SUBTITLE", ""],
		];

		full_comp(&test_file_read("zk-test.sm"), expected);
	}

	#[test]
	fn full_parse_simple() {
		let expected = vec![
			vec!["TITLE", "Song Title"],
			vec!["ARTIST", "Song Artist"],
			vec!["SUBTITLE", ""],
			vec!["WHITESPACE", "Foo"],
		];

		full_comp(
			br"#TITLE:Song Title;
#Artist:Song Artist;
#SUBTITLE:;
#WHITESPACE:   Foo;",
			expected,
		);
	}
}