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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
//! Parsing, Processing and serializing of `.sm` files.
//!
//! This module handles parsing of `.sm` files. Parsing SSC is not yet supported.
//! For parsing the raw `.msd` format that underpins these formats, see [`sm_msd`].

use crate::{
	sm_msd::{self, MsdElement, MsdFile},
	utils::ByteString,
};
use std::{
	ffi::{OsStr, OsString},
	fmt::Display,
	fs, io,
	path::{Path, PathBuf},
	str::Utf8Error,
};
use thiserror::Error;

/// Parsing an SM file can fail in a couple of ways. Mainly nonsensical/invalid data.
#[derive(Error, Debug, PartialEq, Eq)]
pub enum LoadError {
	/// This simply wasn't valid SM. This could be anything like having a bpm of `-1`,
	/// having a bpm that wasn't a parsable number, like the empty string,
	/// or not having enough fields in #NOTES.
	#[error("this was not valid sm ({0})")]
	InvalidSM(String),

	/// We expected valid UTF-8 at some point in the process, but got something else.
	/// This can happen if non-utf8 is placed into the BPM or OFFSET fields.
	#[error("expected {0} to parse as utf-8, got Utf8Error {1}")]
	UnexpectedNonUtf8(String, Utf8Error),
}

/// A bpm change in the SM format.
#[derive(Debug, Clone, PartialEq)]
pub struct Bpm {
	/// The BPM this chart is as a float.
	pub bpm: f64,
	/// When this BPM occurs. The first BPM **must** occur at 0.0.
	pub offset_beats: f64,
}

impl Bpm {
	fn from_bytes(bytes: &[u8]) -> Result<Self, LoadError> {
		let str = match std::str::from_utf8(bytes) {
			Ok(str) => str,
			Err(utf8err) => return Err(LoadError::UnexpectedNonUtf8("BPM".into(), utf8err)),
		};

		let elements = str.split('=').collect::<Vec<&str>>();

		if elements.len() < 2 {
			return Err(LoadError::InvalidSM(format!(
				"Invalid SM BPM string '{}'.",
				str
			)));
		}

		let (offset, bpm) = (elements[0], elements[1]);

		let (offset_beats, _) = match lexical::parse_partial(offset) {
			Ok(v) => v,
			Err(_) => {
				return Err(LoadError::InvalidSM(format!(
					"Failed to parse {offset} as a float."
				)))
			}
		};

		let (bpm, _) = match lexical::parse_partial(bpm) {
			Ok(v) => v,
			Err(_) => {
				return Err(LoadError::InvalidSM(format!(
					"Failed to parse {bpm} as a float."
				)))
			}
		};

		if bpm <= 0.0 {
			return Err(LoadError::InvalidSM(format!("BPM was negative ({bpm})")));
		}

		Ok(Bpm { bpm, offset_beats })
	}
}

/// Various parsed metadata and tags from the SM file.
#[derive(Debug, Clone)]
pub struct SongMetadata {
	/// The offset (in seconds) that this chart has. This is a parsed variant of the
	/// `#OFFSET` tag, and is multiplied by -1.0 versus the actual stored format.
	pub offset_secs: Option<f64>,

	/// All the bpm changes in this chart.
	pub bpms: Vec<Bpm>,

	/// The subtitle for this chart, if it had one. This is converted into utf8.
	pub subtitle: Option<String>,
	/// The artist for this chart. This is parsed as utf8 lossily.
	pub artist: String,
	/// The title for this chart. This is parsed as utf8 lossily.
	pub title: String,
	/// The path to the audio file for this chart. This is a raw strip of the bytes that
	/// were in the `#MUSIC` tag, instead of being converted into utf8.
	///
	/// **NOTE:** The actual value of this field - in the file - is pretty much irrelevant
	/// because Stepmania runs inference in the case where it doesn't point to an audio
	/// file. We basically ignore this field unless it *definitely* 100% points to a
	/// real audio file.
	pub music: Option<OsString>,
}

/// A complete SM chart, with song metadata and the chart data.
#[derive(Debug, Clone)]
pub struct Chart {
	/// all metadata on this chart. **All keys are UPPERCASED!**.
	pub tags: MsdFile,

	/// Info about the song this chart belonged to.
	pub song_info: SongMetadata,
	/// Info about the actual chart itself.
	pub chart_data: ChartData,
	/// Where this chart was loaded from on disk.
	pub path: PathBuf,
}

macro_rules! msd_tag_fallback {
	($metadata: expr, $tag: expr, $fallback: expr) => {{
		match $metadata.first_tag_first_val($tag) {
			Some(slice) => String::from_utf8_lossy(&slice).into_owned(),
			None => $fallback.to_owned(),
		}
	}};
}

macro_rules! msd_tag {
	($metadata: expr, $tag: expr) => {{
		match $metadata.first_tag_first_val($tag) {
			Some(slice) => Some(String::from_utf8_lossy(&slice).into_owned()),
			None => None,
		}
	}};
}

impl Chart {
	/// Try and infer the author of this chart from the file path.
	///
	/// More specifically, this infers from the *directory* name that contains this
	/// SM file, and not the name of the SM file itself.
	///
	/// As such, the provided file path should look like:
	///
	/// ```txt
	/// "Hello (Kommisar)/chart.sm"
	/// ```
	///
	/// Authors are usually placed in the folder name in one of four forms:
	/// Chart Name (Charter)
	/// Chart Name [Charter]
	/// (Charter) Chart Name
	/// [Charter] Chart Name
	///
	/// The latter three are *very* old and non-standard, but we support them
	/// for compatibility.
	///
	/// Sometimes, this function fails to correctly infer this information
	/// as some songs have brackets in them and the charter isn't mentioned in there
	/// This can happen if the folder name is something like:
	///
	/// Song Title (Speed Up Ver.)
	///
	/// However, it's rare for those charts to not have an author attached.
	fn infer_author(path: impl AsRef<Path>) -> Option<String> {
		let path = path.as_ref().clone();

		let name = path.parent()?.file_name()?;
		let name = OsStr::to_string_lossy(name);

		use regex::Regex;

		let standard = Regex::new(r"\((.+)\) *$").expect("Invalid standard regex.");
		let square = Regex::new(r"\[(.+)\] *$").expect("Invalid square regex.");
		let std_start = Regex::new(r"^ *\((.+)\)").expect("Invalid std_start regex.");
		let sqr_start = Regex::new(r"^ *\[(.+)\]").expect("Invalid sqr_start regex.");

		if let Some(m) = standard.captures(&name) {
			return m.get(1).map(|f| f.as_str().trim().to_owned());
		}

		if let Some(m) = square.captures(&name) {
			return m.get(1).map(|f| f.as_str().trim().to_owned());
		}

		if let Some(m) = std_start.captures(&name) {
			return m.get(1).map(|f| f.as_str().trim().to_owned());
		}

		if let Some(m) = sqr_start.captures(&name) {
			return m.get(1).map(|f| f.as_str().trim().to_owned());
		}

		None
	}

	/// Look for an audio file in this directory. This does not do any recursion, and
	/// looks for anything with an `.mp3`, `.ogg`, `.wav` or `.oga` extension.
	///
	/// Returns None if the file could not be found. Also returns None if we failed to
	/// read the directory in question.
	fn look_for_audio(path: impl AsRef<Path>) -> Option<OsString> {
		let dir = path.as_ref().parent()?;

		let entries = fs::read_dir(dir).ok()?;

		for entry in entries.flatten() {
			match entry.path().extension().and_then(OsStr::to_str) {
				// see "FT_SOUND" in the sm codebase.
				Some("ogg" | "mp3" | "wav" | "oga") => return Some(entry.file_name()),
				_ => continue,
			}
		}

		None
	}
}

/// Load an SM file from a path.
///
/// This returns an error if the file was unable to be read.
///
/// This returns an inner error if the file was able to be read, but the contents
/// were not valid SM.
///
/// In the event that one chart fails to parse, all charts fail to parse.
pub fn from_path(sm_path: impl AsRef<Path>) -> io::Result<Result<Vec<Chart>, LoadError>> {
	let bytes = fs::read(&sm_path)?;

	Ok(from_bytes(&bytes, sm_path))
}

/// Load an SM file from bytes.
///
/// This method is kind of useless, as to parse the sm file we **need** to know its
/// location on disk (for resolving #MUSIC and other information).
///
/// You probably want [`from_path`]. This method is only publically exposed for testing
/// reasons, where you might want the byte content and the path to be disjoint.
///
/// In the event that one chart fails to parse, all charts fail to parse.
pub fn from_bytes(bytes: &[u8], sm_path: impl AsRef<Path>) -> Result<Vec<Chart>, LoadError> {
	let (metadata, charts) = parse(bytes);

	let bpms: Vec<Bpm> = match metadata.first_tag_first_val("BPMS") {
		Some(bpm_str) => parse_bpms(&bpm_str).unwrap_or(vec![Bpm {
			bpm: 60.0,
			offset_beats: 0.0,
		}]),
		None => vec![Bpm {
			bpm: 60.0,
			offset_beats: 0.0,
		}],
	};

	let offset = match metadata.first_tag_first_val("OFFSET") {
		Some(v) => {
			let str = match std::str::from_utf8(&v) {
				Ok(s) => s,
				Err(err) => return Err(LoadError::UnexpectedNonUtf8("OFFSET".into(), err)),
			};

			match lexical::parse_partial::<f64, &str>(str) {
				Ok((float, _)) => {
					// an offset of 0 is irrelevant
					// n.b. you can't have floats in match arms (lol)
					if float == 0.0 {
						None
					} else {
						// i have literally no idea why SM stores offset * -1
						// the sm codebase *also* immediately multiplies by -1 so
						// who knows, lol
						Some(-1.0 * float)
					}
				}

				// an invalid offset is treated as 0 offset.
				Err(_) => None,
			}
		}
		None => None,
	};

	let music_path = metadata.first_tag_first_val("MUSIC").map(|bytes| {
		// we have arbitrary bytes. lets try and shunt these into a path.
		// if we can't do that, we can't find the audio file

		#[cfg(unix)]
		{
			use std::os::unix::ffi::OsStrExt;

			OsStr::from_bytes(&bytes).to_owned()
		}

		#[cfg(windows)]
		{
			use std::os::windows::ffi::OsStrExt;

			// probably wide bytes. might not be. I actually don't know if this is
			// correct or not.
			OsString::from_wide(&bytes)
		}
	});

	let music_path = music_path.map(|os_str| {
		let path = sm_path.as_ref().to_path_buf();
		let path = path.join(&os_str);

		if path.exists() {
			os_str
		} else {
			// great. now we have to look for the audio file using stepmania error
			// correction.
			match Chart::look_for_audio(&sm_path) {
				Some(data) => data,
				// whatever. can't find anything to error correct on, just blindly
				// believe whatever the MUSIC tag says.
				None => os_str,
			}
		}
	});

	let song_info = SongMetadata {
		artist: msd_tag_fallback!(metadata, "ARTIST", "Unknown Artist"),
		title: match metadata.first_tag_first_val("TITLE") {
			Some(v) => String::from_utf8_lossy(&v).into_owned(),
			None => {
				// gotta infer the song title from the file path, since it wasn't
				// specified in the file.

				let mut path = sm_path.as_ref().to_path_buf();

				path.pop();

				match path.file_name() {
					Some(name) => name.to_string_lossy().into_owned(),
					None => "Untitled Song".to_owned(),
				}
			}
		},
		subtitle: msd_tag!(metadata, "SUBTITLE"),
		bpms,
		music: music_path,
		offset_secs: offset,
	};

	let mut ok_charts = vec![];

	for chart in charts {
		// if any chart failed to parse, bail out.
		let mut chart = chart?;

		// even if we know who made this chart, we should ignore
		// anything that says "Blank" or "Copied From", as
		// that basically also means unknown.
		if chart.author.is_empty() || &*chart.author == b"Copied From" || &*chart.author == b"Blank"
		{
			if let Some(inferred_name) = Chart::infer_author(&sm_path) {
				chart.author = inferred_name.as_bytes().into();
			}
		}

		let full_file = Chart {
			tags: metadata.clone(),
			song_info: song_info.clone(),
			chart_data: chart,
			path: sm_path.as_ref().to_path_buf(),
		};

		ok_charts.push(full_file);
	}

	Ok(ok_charts)
}

/// This is all the possible modes SM supports as of 2023/07/27.
///
/// There are still potentially more than this, for those cases they fall into "Unknown".
///
/// Note that most of these modes are effectively useless, and have never been played or
/// even tested by anyone. I'm not even honestly sure why I bothered writing them all out,
/// but it's done now.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum StepsType {
	DanceSingle,
	DanceDouble,
	DanceCouple,
	DanceSolo,
	DanceThreepanel,
	DanceRoutine,
	PumpSingle,
	PumpHalfDouble,
	PumpDouble,
	PumpCouple,
	PumpRoutine,
	Kb7Single,
	Ez2Single,
	Ez2Double,
	Ez2Real,
	ParaSingle,
	Ds3ddxSingle,
	BmSingle5,
	BmVersus5,
	BmDouble5,
	BmSingle7,
	BmVersus7,
	BmDouble7,
	ManiaxSingle,
	ManiaxDouble,
	TechnoSingle4,
	TechnoSingle5,
	TechnoSingle8,
	TechnoDouble4,
	TechnoDouble5,
	TechnoDouble8,
	PnmFive,
	PnmNine,
	LightsCabinet,
	KickboxHuman,
	KickboxQuadarm,
	KickboxInsect,
	KickboxArachnid,

	/// Some unknown gamemode.
	Other(ByteString),
}

impl Display for StepsType {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let str = match self {
			StepsType::DanceSingle => "dance-single".into(),
			StepsType::DanceDouble => "dance-double".into(),
			StepsType::DanceCouple => "dance-couple".into(),
			StepsType::DanceSolo => "dance-solo".into(),
			StepsType::DanceThreepanel => "dance-threepanel".into(),
			StepsType::DanceRoutine => "dance-routine".into(),
			StepsType::PumpSingle => "pump-single".into(),
			StepsType::PumpHalfDouble => "pump-halfdouble".into(),
			StepsType::PumpDouble => "pump-double".into(),
			StepsType::PumpCouple => "pump-couple".into(),
			StepsType::PumpRoutine => "pump-routine".into(),
			StepsType::Kb7Single => "kb7-single".into(),
			StepsType::Ez2Single => "ez2-single".into(),
			StepsType::Ez2Double => "ez2-double".into(),
			StepsType::Ez2Real => "ez2-real".into(),
			StepsType::ParaSingle => "para-single".into(),
			StepsType::Ds3ddxSingle => "ds3ddx-single".into(),
			StepsType::BmSingle5 => "bm-single5".into(),
			StepsType::BmVersus5 => "bm-versus5".into(),
			StepsType::BmDouble5 => "bm-double5".into(),
			StepsType::BmSingle7 => "bm-single7".into(),
			StepsType::BmVersus7 => "bm-versus7".into(),
			StepsType::BmDouble7 => "bm-double7".into(),
			StepsType::ManiaxSingle => "maniax-single".into(),
			StepsType::ManiaxDouble => "maniax-double".into(),
			StepsType::TechnoSingle4 => "techno-single4".into(),
			StepsType::TechnoSingle5 => "techno-single5".into(),
			StepsType::TechnoSingle8 => "techno-single8".into(),
			StepsType::TechnoDouble4 => "techno-double4".into(),
			StepsType::TechnoDouble5 => "techno-double5".into(),
			StepsType::TechnoDouble8 => "techno-double8".into(),
			StepsType::PnmFive => "pnm-five".into(),
			StepsType::PnmNine => "pnm-nine".into(),
			StepsType::LightsCabinet => "lights-cabinet".into(),
			StepsType::KickboxHuman => "kickbox-human".into(),
			StepsType::KickboxQuadarm => "kickbox-quadarm".into(),
			StepsType::KickboxInsect => "kickbox-insect".into(),
			StepsType::KickboxArachnid => "kickbox-arachnid".into(),
			StepsType::Other(a) => String::from_utf8_lossy(a).into_owned(),
		};

		f.write_str(&str)
	}
}

impl StepsType {
	fn from_bytes(bytes: &ByteString) -> Self {
		match &**bytes {
			b"dance-single" => StepsType::DanceSingle,
			b"dance-double" => StepsType::DanceDouble,
			b"dance-couple" => StepsType::DanceCouple,
			b"dance-solo" => StepsType::DanceSolo,
			b"dance-threepanel" => StepsType::DanceThreepanel,
			b"dance-routine" => StepsType::DanceRoutine,
			b"pump-single" => StepsType::PumpSingle,
			b"pump-halfdouble" => StepsType::PumpHalfDouble,
			b"pump-double" => StepsType::PumpDouble,
			b"pump-couple" => StepsType::PumpCouple,
			b"pump-routine" => StepsType::PumpRoutine,
			b"kb7-single" => StepsType::Kb7Single,
			b"ez2-single" => StepsType::Ez2Single,
			b"ez2-double" => StepsType::Ez2Double,
			b"ez2-real" => StepsType::Ez2Real,
			b"para-single" => StepsType::ParaSingle,
			b"ds3ddx-single" => StepsType::Ds3ddxSingle,
			b"bm-single5" => StepsType::BmSingle5,
			b"bm-versus5" => StepsType::BmVersus5,
			b"bm-double5" => StepsType::BmDouble5,
			b"bm-single7" => StepsType::BmSingle7,
			b"bm-versus7" => StepsType::BmVersus7,
			b"bm-double7" => StepsType::BmDouble7,
			b"maniax-single" => StepsType::ManiaxSingle,
			b"maniax-double" => StepsType::ManiaxDouble,
			b"techno-single4" => StepsType::TechnoSingle4,
			b"techno-single5" => StepsType::TechnoSingle5,
			b"techno-single8" => StepsType::TechnoSingle8,
			b"techno-double4" => StepsType::TechnoDouble4,
			b"techno-double5" => StepsType::TechnoDouble5,
			b"techno-double8" => StepsType::TechnoDouble8,
			b"pnm-five" => StepsType::PnmFive,
			b"pnm-nine" => StepsType::PnmNine,
			b"lights-cabinet" => StepsType::LightsCabinet,
			b"kickbox-human" => StepsType::KickboxHuman,
			b"kickbox-quadarm" => StepsType::KickboxQuadarm,
			b"kickbox-insect" => StepsType::KickboxInsect,
			b"kickbox-arachnid" => StepsType::KickboxArachnid,
			_ => StepsType::Other(bytes.clone()),
		}
	}
}

/// There are 5 possible difficulties for an SM chart, which correspond to multiple
/// possible names in the format.
///
/// There is also a 6th overflow difficulty, called "Edit". This takes one argument
/// which disambiguates further, as multiple edits are legal for the same song.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Difficulty {
	/// This is a beginner chart.
	Beginner,
	/// This is an easy, basic or light chart.
	Easy,
	/// This is a medium, another, trick, standard or difficult chart.
	Medium,
	/// This is a hard, ssr, maniac or heavy chart.
	Hard,
	/// This is a challenge, expert or oni chart.
	Challenge,
	/// This is an edit chart. The `Author` information becomes part of the difficulty
	/// name for disambiguation between multiple Edit charts.
	Edit(ByteString),
}

impl Display for Difficulty {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		use Difficulty::*;

		let str = match self {
			Beginner => "Beginner".into(),
			Easy => "Easy".into(),
			Medium => "Medium".into(),
			Hard => "Hard".into(),
			Challenge => "Challenge".into(),
			Edit(txt) => format!("Edit {}", String::from_utf8_lossy(txt)),
		};

		write!(f, "{str}")
	}
}

/// Actual chart/notes data for an SM file. This has no song metadata attached onto it.
/// For a convenient combination of [`ChartData`] and [`SongMetadata`], see [`Chart`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChartData {
	/// What [`StepsType`] this notedata says it is.
	pub steps_type: StepsType,
	/// Who made this chart. If this isn't present in the file, or is set to "Copied From"
	/// or "Blank", this is inferred from the name of the folder.
	pub author: ByteString,
	/// What difficulty this chart is.
	pub difficulty: Difficulty,

	/// What level this chart is. Negative numbers and 0 are converted into `1`.
	pub level: usize,

	/// The actual note data in parsed form. It's rare, but if you *really* need to access
	/// the raw note data, you can use [`fn@SongData::raw_notedata()`].
	pub notedata: Vec<Measure>,
}

/// An event in SM is one of the following variants.
#[derive(Debug, PartialEq, Clone, Eq)]
pub enum NoteVariant {
	/// A note was here. This corresponds to "1" in the SM file.
	Note,
	/// A hold started here. This corresponds to "2" in the SM file.
	HoldStart,
	/// A roll started here. This corresponds to "4" in the SM file.
	RollStart,
	/// A hold or roll was terminated here. This corresponds to "3" in the SM file.
	HoldOrRollEnd,

	/// A keysound should trigger here. This corresponds to "K" in the SM file.
	AutoKeysound,
	/// A lift was here. This corresponds to "L" in the SM file.
	Lift,
	/// A fake was here. This corresponds to "F" in the SM file.
	Fake,
	/// A mine was here. This corresponds to "M" in the SM file.
	Mine,

	/// An unknown note type was here -- any char that doesn't match one of the other
	/// kinds.
	Unknown(u8),
}

/// An event (note, hold start, mine, etc.) that happened in an SM chart's notedata.
/// This tells you what row it occured on, what column it occured on, and what kind
/// of note it was.
#[derive(Debug, PartialEq, Clone, Eq)]
pub struct Event {
	/// What row (into the measure) this event occured on. How far this is into the chart
	/// is relative to the containing [`Measure`]s `size` property.
	pub row: usize,

	/// What column this note occured on. This is indexed from 0.
	///
	/// NOTE: there is absolutely no guarantee how many columns can appear in a chart.
	/// A chart is totally within its right to change how many columns it has at any time,
	/// for any reason. SM simply discards columns it doesn't care for. You should likely
	/// do the same.
	pub column: usize,

	/// What kind of event this was.
	pub variant: NoteVariant,
}

/// A measure in an SM file. This is a collection of `size` rows, with `events` happening
/// on a given column with a given type.
#[derive(Debug, PartialEq, Clone, Eq)]
pub struct Measure {
	/// How many rows were in this measure.
	pub size: usize,
	/// What events are in this measure. See [`Event`] for more information.
	pub events: Vec<Event>,
}

fn parse_notedata(raw_notedata: &[u8]) -> Vec<Measure> {
	let mut measures = vec![];

	for measure in raw_notedata.split(|char| *char == b',') {
		let mut size = 0;
		let mut events = vec![];

		for row in measure.split(|char| *char == b'\n') {
			let row = row.trim_ascii();

			if row.is_empty() {
				continue;
			}

			// we can't use enumerate because of stupid SM functionality
			// where you can strap keysounds onto column events.
			let mut skip_until_closebrck = false;
			let mut index = 0;

			for ch in row.iter() {
				// Stepmania supports annotating events with keysounds
				// but basically nobody has ever used this feature and it is deep
				// in the guts of the SM codebase.
				//
				// the syntax for this is `1[0]001`, which corresponds to `1001`, but
				//                                                         ^
				//          this guy has the 0th keysound associated with it.
				//
				// as such, if we see [ we skip until ]. simple.
				if *ch == b'[' {
					skip_until_closebrck = true;
					continue;
				}

				if *ch == b']' {
					skip_until_closebrck = false;
					continue;
				}

				if skip_until_closebrck {
					continue;
				}

				let variant = match ch {
					// skip all non-data.
					b'0' => {
						index += 1;
						continue;
					}
					b'1' => NoteVariant::Note,
					b'2' => NoteVariant::HoldStart,
					b'3' => NoteVariant::HoldOrRollEnd,
					b'4' => NoteVariant::RollStart,
					b'M' => NoteVariant::Mine,
					b'K' => NoteVariant::AutoKeysound,
					b'L' => NoteVariant::Lift,
					b'F' => NoteVariant::Fake,
					ch => NoteVariant::Unknown(*ch),
				};

				events.push(Event {
					row: size,
					column: index,
					variant,
				});

				index += 1;
			}

			size += 1;
		}

		if size == 0 {
			// entire measure is empty?
			continue;
		}

		measures.push(Measure { size, events })
	}

	measures
}

/// Actually parse all the `#NOTES` tags in this SM file. Since technically all of these
/// can fail independently, this returns a vector of results.
fn parse(sm: &[u8]) -> (MsdFile, Vec<Result<ChartData, LoadError>>) {
	let msd_file = sm_msd::from_bytes(sm);

	let charts = msd_file
		.all_with_tag("NOTES")
		.iter()
		.map(|el| parse_notes(el))
		.collect();

	(msd_file, charts)
}

fn parse_bpms(bpms: &[u8]) -> Result<Vec<Bpm>, LoadError> {
	let mut bpm_vec = vec![];

	for bpm in bpms.split(|by| *by == b',') {
		match Bpm::from_bytes(bpm.trim_ascii()) {
			Ok(v) => bpm_vec.push(v),
			Err(err) => return Err(err),
		}
	}

	Ok(bpm_vec)
}

fn parse_notes(el: &MsdElement) -> Result<ChartData, LoadError> {
	// this is absolutely *unbelievably* ridiculous. We expect exactly 6 values.
	//
	// A lot of charts (for whatever reason) in modern packs have comments like this
	// //--------------- dance-single - sorae 80/31
	// ---------------
	// The newline means the comment doesn't tear out the whole thing
	// and there's a trailing "-------" in the list of values
	// as such, even though we only expect 6 params
	// we may find more than that. that's completely fine, stepmania will accept it.
	if el.values.len() < 6 {
		return Err(LoadError::InvalidSM(format!(
			"Invalid amount of fields inside #NOTES. Got {}, expected at least 6.",
			el.values.len()
		)));
	}

	let fields = &el.values;

	let steps_type = StepsType::from_bytes(&fields[0]);

	let author = &fields[1];
	let diff = &fields[2];

	let difficulty = match diff.to_ascii_lowercase().as_slice() {
		b"beginner" => Difficulty::Beginner,
		b"easy" | b"basic" | b"light" => Difficulty::Easy,
		b"medium" | b"another" | b"trick" | b"standard" | b"difficult" => Difficulty::Medium,
		b"hard" | b"ssr" | b"maniac" | b"heavy" => Difficulty::Hard,
		b"challenge" | b"expert" | b"oni" => Difficulty::Challenge,
		b"edit" => Difficulty::Edit(author.clone()),
		d => {
			return Err(LoadError::InvalidSM(format!(
				"Unknown difficulty {}",
				String::from_utf8_lossy(d),
			)))
		}
	};

	let level = String::from_utf8_lossy(&fields[3]).parse().unwrap_or(1);

	let raw_notedata = &fields[5];

	let notedata = parse_notedata(raw_notedata);

	Ok(ChartData {
		steps_type,
		author: author.clone(),
		difficulty,
		level,
		notedata,
	})
}

#[cfg(test)]
mod tests {
	use pretty_assertions::assert_eq;

	use super::*;

	#[test]
	fn bpms() {
		assert_eq!(
			parse_bpms(b"0.000=104.03"),
			Ok(vec![Bpm {
				bpm: 104.03,
				offset_beats: 0.0
			}])
		);

		assert_eq!(
			parse_bpms(b"0.000=104.03,1.000=400"),
			Ok(vec![
				Bpm {
					bpm: 104.03,
					offset_beats: 0.0
				},
				Bpm {
					bpm: 400.00,
					offset_beats: 1.0
				}
			])
		);

		assert_eq!(
			parse_bpms(b"0.000=-104.03"),
			Err(LoadError::InvalidSM("BPM was negative (-104.03)".into()))
		);
	}

	#[test]
	fn bpm_partial() {
		assert_eq!(
			parse_bpms(b"0.000=123.456.789"),
			Ok(vec![Bpm {
				bpm: 123.456,
				offset_beats: 0.0
			}])
		);
	}

	#[test]
	fn load_notes() {
		assert_eq!(
			parse_notes(&MsdElement {
				tag: Box::new(*b"NOTES"),
				values: vec![
					Box::new(*b"dance-single"),
					Box::new(*b"Author"),
					Box::new(*b"Hard"),
					Box::new(*b"1"),
					Box::new(*b"nonsense groove"),
					Box::new(
						*b"1000
0100
0010
0001,
M000
00000
1234
LKMF"
					)
				]
			}),
			Ok(ChartData {
				steps_type: StepsType::DanceSingle,
				author: Box::new(*b"Author"),
				difficulty: Difficulty::Hard,
				level: 1,
				notedata: vec![
					Measure {
						size: 4,
						events: vec![
							Event {
								column: 0,
								row: 0,
								variant: NoteVariant::Note
							},
							Event {
								column: 1,
								row: 1,
								variant: NoteVariant::Note
							},
							Event {
								column: 2,
								row: 2,
								variant: NoteVariant::Note
							},
							Event {
								column: 3,
								row: 3,
								variant: NoteVariant::Note
							},
						]
					},
					Measure {
						size: 4,
						events: vec![
							Event {
								column: 0,
								row: 0,
								variant: NoteVariant::Mine
							},
							Event {
								column: 0,
								row: 2,
								variant: NoteVariant::Note
							},
							Event {
								column: 1,
								row: 2,
								variant: NoteVariant::HoldStart
							},
							Event {
								column: 2,
								row: 2,
								variant: NoteVariant::HoldOrRollEnd
							},
							Event {
								column: 3,
								row: 2,
								variant: NoteVariant::RollStart
							},
							Event {
								column: 0,
								row: 3,
								variant: NoteVariant::Lift
							},
							Event {
								column: 1,
								row: 3,
								variant: NoteVariant::AutoKeysound
							},
							Event {
								column: 2,
								row: 3,
								variant: NoteVariant::Mine
							},
							Event {
								column: 3,
								row: 3,
								variant: NoteVariant::Fake
							},
						]
					}
				]
			})
		)
	}

	#[test]
	fn load_notes_obscurekeysounds() {
		assert_eq!(
			parse_notes(&MsdElement {
				tag: Box::new(*b"NOTES"),
				values: vec![
					Box::new(*b"dance-single"),
					Box::new(*b"Author"),
					Box::new(*b"Hard"),
					Box::new(*b"1"),
					Box::new(*b"nonsense groove"),
					Box::new(
						*b"1000
0100[1]
001[100000]0
0001[1,
[1]M000
00[1]000
123[999}>)]4
LKMF"
					)
				]
			}),
			Ok(ChartData {
				steps_type: StepsType::DanceSingle,
				author: Box::new(*b"Author"),
				difficulty: Difficulty::Hard,
				level: 1,
				notedata: vec![
					Measure {
						size: 4,
						events: vec![
							Event {
								column: 0,
								row: 0,
								variant: NoteVariant::Note
							},
							Event {
								column: 1,
								row: 1,
								variant: NoteVariant::Note
							},
							Event {
								column: 2,
								row: 2,
								variant: NoteVariant::Note
							},
							Event {
								column: 3,
								row: 3,
								variant: NoteVariant::Note
							},
						]
					},
					Measure {
						size: 4,
						events: vec![
							Event {
								column: 0,
								row: 0,
								variant: NoteVariant::Mine
							},
							Event {
								column: 0,
								row: 2,
								variant: NoteVariant::Note
							},
							Event {
								column: 1,
								row: 2,
								variant: NoteVariant::HoldStart
							},
							Event {
								column: 2,
								row: 2,
								variant: NoteVariant::HoldOrRollEnd
							},
							Event {
								column: 3,
								row: 2,
								variant: NoteVariant::RollStart
							},
							Event {
								column: 0,
								row: 3,
								variant: NoteVariant::Lift
							},
							Event {
								column: 1,
								row: 3,
								variant: NoteVariant::AutoKeysound
							},
							Event {
								column: 2,
								row: 3,
								variant: NoteVariant::Mine
							},
							Event {
								column: 3,
								row: 3,
								variant: NoteVariant::Fake
							},
						]
					}
				]
			})
		)
	}

	#[test]
	fn infer_author_normal() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/Hello (Kommisar)/chart.sm"),
			Some("Kommisar".into())
		);
	}

	#[test]
	fn infer_author_square() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/Hello [Kommisar]/chart.sm"),
			Some("Kommisar".into())
		);
	}

	#[test]
	fn infer_author_start() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/(Kommisar) Hello/chart.sm"),
			Some("Kommisar".into())
		);
	}

	#[test]
	fn infer_author_sq_start() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/[Kommisar] Hello/chart.sm"),
			Some("Kommisar".into())
		);
	}

	#[test]
	fn infer_author_space() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/[ Kommisar ] Hello/chart.sm"),
			Some("Kommisar".into())
		);
	}

	#[test]
	fn infer_author_space2() {
		assert_eq!(
			Chart::infer_author("Songs/Tachyon Epsilon/( Kommisar ) Hello/chart.sm"),
			Some("Kommisar".into())
		);
	}
}