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
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
//! Unparse Bms model into `Vec<Token>` without duplicate parsing logic.
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use num::Integer;
use crate::bms::prelude::*;
impl Bms {
/// Convert Bms to `Vec<Token>` (in conventional order: header -> definitions -> resources -> messages).
/// - Avoid duplicate parsing: directly construct Tokens using model data;
/// - For messages requiring `ObjId`, prioritize reusing existing definitions; if missing, allocate new `ObjId` and add definition Token (only reflected in returned Token list).
#[must_use]
pub fn unparse<'a, T: KeyLayoutMapper>(&'a self) -> Vec<Token<'a>> {
let mut tokens: Vec<Token<'a>> = Vec::new();
// Others section lines FIRST to preserve order equality on roundtrip
self.unparse_headers(&mut tokens);
// Check if any of the used IDs require base62 (not base36 but valid base62)
let mut base62_checker = Base62Checker::new();
self.unparse_messages::<T>(&mut tokens, &mut base62_checker);
// Add ObjIds from definition tokens that are not covered by managers
// ExWav definitions
base62_checker.check(self.wav.exwav_defs.keys().copied());
// WavCmd events (use wav_index ObjId)
base62_checker.check(self.wav.wavcmd_events.values().map(|ev| ev.wav_index));
// AtBga definitions (use both id and source_bmp ObjIds)
{
base62_checker.check(self.bmp.atbga_defs.keys().copied());
base62_checker.check(self.bmp.atbga_defs.values().map(|def| def.source_bmp));
}
// Bga definitions (use both id and source_bmp ObjIds)
{
base62_checker.check(self.bmp.bga_defs.keys().copied());
base62_checker.check(self.bmp.bga_defs.values().map(|def| def.source_bmp));
}
// Argb definitions
base62_checker.check(self.bmp.argb_defs.keys().copied());
// SwBga events
base62_checker.check(self.bmp.swbga_events.keys().copied());
// Wav resource files
base62_checker.check(self.wav.wav_files.keys().copied());
// Bmp/ExBmp resource files
base62_checker.check(self.bmp.bmp_files.keys().copied());
// Add Base62 token if needed
if base62_checker.into_using_base62() {
tokens.push(Token::Header {
name: "BASE".into(),
args: "62".into(),
});
}
tokens
}
fn unparse_headers<'a>(&'a self, tokens: &mut Vec<Token<'a>>) {
// Options
if let Some(options) = self.option.options.as_ref() {
for option in options {
tokens.push(Token::Header {
name: "OPTION".into(),
args: option.into(),
});
}
}
// Octave mode
if self.metadata.is_octave {
tokens.push(Token::Header {
name: "OCT/FP".into(),
args: "".into(),
});
}
// CDDA events
for cdda in &self.resources.cdda {
tokens.push(Token::Header {
name: "CDDA".into(),
args: cdda.to_string().into(),
});
}
// Extended character events
for ExtChrEvent {
sprite_num,
bmp_num,
start_x,
start_y,
end_x,
end_y,
offset_x,
offset_y,
abs_x,
abs_y,
} in &self.sprite.extchr_events
{
use itertools::Itertools;
let buf = [sprite_num, bmp_num, start_x, start_y, end_x, end_y]
.into_iter()
.copied()
.chain(
offset_x
.zip(*offset_y)
.map(Into::<[i32; 2]>::into)
.into_iter()
.flatten(),
)
.chain(
abs_x
.zip(*abs_y)
.map(Into::<[i32; 2]>::into)
.into_iter()
.flatten(),
)
.join(" ");
tokens.push(Token::Header {
name: "EXTCHR".into(),
args: buf.into(),
});
}
// Change options
for (id, option) in &self.option.change_options {
tokens.push(Token::Header {
name: format!("CHANGEOPTION{id}").into(),
args: option.as_str().into(),
});
}
// Divide property
if let Some(divide_prop) = self.metadata.divide_prop.as_ref() {
tokens.push(Token::Header {
name: "DIVIDEPROP".into(),
args: divide_prop.as_str().into(),
});
}
// Materials path
if let Some(materials_path) = self.resources.materials_path.as_ref()
&& !materials_path.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "MATERIALS".into(),
args: materials_path.display().to_string().into(),
});
}
for line in &self.repr.non_command_lines {
tokens.push(Token::NotACommand(line.as_str()));
}
// Header
if let Some(player) = self.metadata.player {
tokens.push(Token::Header {
name: "PLAYER".into(),
args: player.to_string().into(),
});
}
if let Some(maker) = self.music_info.maker.as_deref() {
tokens.push(Token::Header {
name: "MAKER".into(),
args: maker.into(),
});
}
if let Some(genre) = self.music_info.genre.as_deref() {
tokens.push(Token::Header {
name: "GENRE".into(),
args: genre.into(),
});
}
if let Some(title) = self.music_info.title.as_deref() {
tokens.push(Token::Header {
name: "TITLE".into(),
args: title.into(),
});
}
if let Some(artist) = self.music_info.artist.as_deref() {
tokens.push(Token::Header {
name: "ARTIST".into(),
args: artist.into(),
});
}
if let Some(sub_artist) = self.music_info.sub_artist.as_deref() {
tokens.push(Token::Header {
name: "SUBARTIST".into(),
args: sub_artist.into(),
});
}
if let Some(bpm) = self.bpm.bpm.as_ref() {
tokens.push(Token::Header {
name: "BPM".into(),
args: bpm.to_string().into(),
});
}
if let Some(play_level) = self.metadata.play_level {
tokens.push(Token::Header {
name: "PLAYLEVEL".into(),
args: play_level.to_string().into(),
});
}
if let Some(rank) = self.judge.rank {
tokens.push(Token::Header {
name: "RANK".into(),
args: rank.to_string().into(),
});
}
if let Some(subtitle) = self.music_info.subtitle.as_deref() {
tokens.push(Token::Header {
name: "SUBTITLE".into(),
args: subtitle.into(),
});
}
if let Some(stage_file) = self.sprite.stage_file.as_ref()
&& !stage_file.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "STAGEFILE".into(),
args: stage_file.display().to_string().into(),
});
}
if let Some(back_bmp) = self.sprite.back_bmp.as_ref()
&& !back_bmp.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "BACKBMP".into(),
args: back_bmp.display().to_string().into(),
});
}
if let Some(banner) = self.sprite.banner.as_ref()
&& !banner.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "BANNER".into(),
args: banner.display().to_string().into(),
});
}
if let Some(difficulty) = self.metadata.difficulty {
tokens.push(Token::Header {
name: "DIFFICULTY".into(),
args: difficulty.to_string().into(),
});
}
if let Some(preview) = self.music_info.preview_music.as_ref()
&& !preview.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "PREVIEW".into(),
args: preview.display().to_string().into(),
});
}
if let Some(movie) = self.video.video_file.as_ref()
&& !movie.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "MOVIE".into(),
args: movie.display().to_string().into(),
});
}
if let Some(comment_lines) = self.music_info.comment.as_ref() {
for line in comment_lines {
tokens.push(Token::NotACommand(line.as_str()));
}
}
if let Some(total) = self.judge.total.as_ref() {
tokens.push(Token::Header {
name: "TOTAL".into(),
args: total.to_string().into(),
});
}
if let Some(email) = self.metadata.email.as_deref() {
tokens.push(Token::Header {
name: "EMAIL".into(),
args: email.into(),
});
}
if let Some(url) = self.metadata.url.as_deref() {
tokens.push(Token::Header {
name: "URL".into(),
args: url.into(),
});
}
// LnType
if self.repr.ln_type == LnType::Mgq {
tokens.push(Token::Header {
name: "LNTYPE".into(),
args: "2".into(),
});
}
// LnMode
if self.repr.ln_mode != LnMode::default() {
tokens.push(Token::Header {
name: "LNMODE".into(),
args: (self.repr.ln_mode as u8).to_string().into(),
});
}
tokens.extend(
self.wav
.wav_files
.iter()
.filter(|(_, path)| !path.as_path().as_os_str().is_empty())
.map(|(id, path)| {
(
*id,
Token::Header {
name: format!("WAV{id}").into(),
args: path.display().to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
// PoorBga mode
if self.bmp.poor_bga_mode != PoorMode::default() {
tokens.push(Token::Header {
name: "POORBGA".into(),
args: self.bmp.poor_bga_mode.as_str().into(),
});
}
// Add basic definitions
if let Some(base_bpm) = self.bpm.base_bpm.as_ref() {
tokens.push(Token::Header {
name: "BASEBPM".into(),
args: base_bpm.to_string().into(),
});
}
tokens.extend(
self.bpm
.bpm_defs
.iter()
.map(|(id, v)| {
(
*id,
Token::Header {
name: format!("BPM{id}").into(),
args: v.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
self.unparse_def_tokens(tokens);
self.unparse_resource_tokens(tokens);
}
fn unparse_def_tokens<'a>(&'a self, tokens: &mut Vec<Token<'a>>) {
// Definitions in scope (existing ones first)
// Use iterator chains to efficiently collect all definition tokens
tokens.extend(
self.stop
.stop_defs
.iter()
.map(|(id, v)| {
(
*id,
Token::Header {
name: format!("STOP{id}").into(),
args: v.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.video
.seek_defs
.iter()
.map(|(id, v)| {
(
*id,
Token::Header {
name: format!("SEEK{id}").into(),
args: v.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.scroll
.scroll_defs
.iter()
.map(|(id, v)| {
(
*id,
Token::Header {
name: format!("SCROLL{id}").into(),
args: v.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.speed
.speed_defs
.iter()
.map(|(id, v)| {
(
*id,
Token::Header {
name: format!("SPEED{id}").into(),
args: v.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.text
.texts
.iter()
.map(|(id, text)| {
(
*id,
Token::Header {
name: format!("TEXT{id}").into(),
args: text.as_str().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.judge
.exrank_defs
.iter()
.map(|(id, exrank)| {
(
*id,
Token::Header {
name: format!("EXRANK{id}").into(),
args: exrank.judge_level.to_string().into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
{
tokens.extend(
self.wav
.exwav_defs
.iter()
.map(|(id, def)| {
(
*id,
def.frequency.map_or_else(
|| Token::Header {
name: format!("EXWAV{id}").into(),
args: format!(
"pv {} {} {}",
def.pan.as_ref(),
def.volume.as_ref(),
def.path.display()
)
.into(),
},
|freq| Token::Header {
name: format!("EXWAV{id}").into(),
args: format!(
"pvf {} {} {} {}",
def.pan.as_ref(),
def.volume.as_ref(),
freq.value(),
def.path.display()
)
.into(),
},
),
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
// wavcmd_events should be sorted by wav_index for consistent output
let mut wavcmd_events: Vec<_> = self.wav.wavcmd_events.values().collect();
wavcmd_events.sort_by_key(|ev| ev.wav_index);
tokens.extend(wavcmd_events.into_iter().map(|ev| Token::Header {
name: "WAVCMD".into(),
args: format!("{} {} {}", ev.param.to_str(), ev.wav_index, ev.value).into(),
}));
tokens.extend(
self.bmp
.atbga_defs
.iter()
.map(|(id, def)| {
(
*id,
Token::Header {
name: format!("@BGA{id}").into(),
args: format!(
"{} {} {} {} {} {} {}",
def.source_bmp,
def.trim_top_left.x,
def.trim_top_left.y,
def.trim_size.width,
def.trim_size.height,
def.draw_point.x,
def.draw_point.y,
)
.into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.bmp
.bga_defs
.iter()
.map(|(id, def)| {
(
*id,
Token::Header {
name: format!("BGA{id}").into(),
args: format!(
"{} {} {} {} {} {} {}",
def.source_bmp,
def.trim_top_left.x,
def.trim_top_left.y,
def.trim_bottom_right.x,
def.trim_bottom_right.y,
def.draw_point.x,
def.draw_point.y,
)
.into(),
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
tokens.extend(
self.bmp
.argb_defs
.iter()
.map(
|(
id,
Argb {
alpha,
red,
green,
blue,
},
)| {
(
*id,
Token::Header {
name: format!("ARGB{id}").into(),
args: format!("{alpha},{red},{green},{blue}").into(),
},
)
},
)
.collect::<BTreeMap<_, _>>()
.into_values(),
);
// SWBGA events, sorted by ObjId for consistent output
let mut swbga_events: Vec<_> = self.bmp.swbga_events.iter().collect();
swbga_events.sort_by_key(|(id, _)| *id);
tokens.extend(
swbga_events
.into_iter()
.map(|(id, SwBgaEvent { frame_rate, total_time, line, loop_mode, argb: Argb { alpha, red, green, blue }, pattern })| Token::Header {
name: format!("SWBGA{id}").into(),
args: format!(
"{frame_rate}:{total_time}:{line}:{}:{alpha},{red},{green},{blue} {pattern}",
i32::from(*loop_mode)
).into(),
}),
);
}
}
fn unparse_resource_tokens<'a>(&'a self, tokens: &mut Vec<Token<'a>>) {
// Resources - Use iterator chains to efficiently collect resource tokens
// Add basic resource tokens
if let Some(path_root) = self.metadata.wav_path_root.as_ref() {
tokens.push(Token::Header {
name: "PATH_WAV".into(),
args: path_root.display().to_string().into(),
});
}
{
if let Some(midi_file) = self.resources.midi_file.as_ref()
&& !midi_file.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "MIDIFILE".into(),
args: midi_file.display().to_string().into(),
});
}
if let Some(materials_wav) = self.resources.materials_wav.first()
&& !materials_wav.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "MATERIALSWAV".into(),
args: materials_wav.display().to_string().into(),
});
}
}
if let Some(video_file) = self.video.video_file.as_ref()
&& !video_file.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "VIDEOFILE".into(),
args: video_file.display().to_string().into(),
});
}
{
if let Some(colors) = self.video.video_colors {
tokens.push(Token::Header {
name: "VIDEOCOLORS".into(),
args: colors.to_string().into(),
});
}
if let Some(delay) = self.video.video_dly.as_ref() {
tokens.push(Token::Header {
name: "VIDEODLY".into(),
args: delay.to_string().into(),
});
}
if let Some(fps) = self.video.video_fs.as_ref() {
tokens.push(Token::Header {
name: "VIDEOF/S".into(),
args: fps.to_string().into(),
});
}
if let Some(char_file) = self.sprite.char_file.as_ref()
&& !char_file.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "CHARFILE".into(),
args: char_file.display().to_string().into(),
});
}
if let Some(materials_bmp) = self.resources.materials_bmp.first()
&& !materials_bmp.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "MATERIALSBMP".into(),
args: materials_bmp.display().to_string().into(),
});
}
}
// VolWav as an expansion command
if self.volume.volume != Volume::default() {
tokens.push(Token::Header {
name: "VOLWAV".into(),
args: self.volume.volume.relative_percent.to_string().into(),
});
}
}
fn unparse_messages<'a, T: KeyLayoutMapper>(
&'a self,
tokens: &mut Vec<Token<'a>>,
checker: &mut Base62Checker,
) {
// Collect late definition tokens and message tokens
let mut late_def_tokens: Vec<Token<'a>> = Vec::new();
let mut message_tokens: Vec<Token<'a>> = Vec::new();
// Messages: Section length - Use iterator chain to collect tokens (sorted by track for consistent output)
let mut section_len_tokens: Vec<_> = self
.section_len
.section_len_changes
.values()
.map(|obj| Token::Message {
track: obj.track,
channel: Channel::SectionLen,
message: Cow::Owned(obj.length.to_string()),
})
.collect();
section_len_tokens.sort_by_key(|token| match token {
Token::Message { track, .. } => *track,
_ => Track(0),
});
message_tokens.extend(section_len_tokens);
// Helper closures for mapping definitions
// Note: We use f64::to_bits() as key since FinF64 doesn't implement Hash
let bpm_value_to_id: HashMap<u64, ObjId> = self
.bpm
.bpm_defs
.iter()
.filter_map(|(k, v)| {
v.value()
.as_ref()
.ok()
.map(|val| (val.as_f64().to_bits(), *k))
})
.collect();
let stop_value_to_id: HashMap<u64, ObjId> = self
.stop
.stop_defs
.iter()
.filter_map(|(k, v)| {
v.value()
.as_ref()
.ok()
.map(|val| (val.as_f64().to_bits(), *k))
})
.collect();
let scroll_value_to_id: HashMap<u64, ObjId> = self
.scroll
.scroll_defs
.iter()
.filter_map(|(k, v)| {
v.value()
.as_ref()
.ok()
.map(|val| (val.as_f64().to_bits(), *k))
})
.collect();
let speed_value_to_id: HashMap<u64, ObjId> = self
.speed
.speed_defs
.iter()
.filter_map(|(k, v)| {
v.value()
.as_ref()
.ok()
.map(|val| (val.as_f64().to_bits(), *k))
})
.collect();
let text_value_to_id: HashMap<&'a str, ObjId> = self
.text
.texts
.iter()
.map(|(k, v)| (v.as_str(), *k))
.collect();
let exrank_value_to_id: HashMap<&'a JudgeLevel, ObjId> = self
.judge
.exrank_defs
.iter()
.map(|(k, v)| (&v.judge_level, *k))
.collect();
let seek_value_to_id: HashMap<u64, ObjId> = self
.video
.seek_defs
.iter()
.filter_map(|(k, v)| {
v.value()
.as_ref()
.ok()
.map(|val| (val.as_f64().to_bits(), *k))
})
.collect();
// Messages: BPM change (#xxx08 or #xxx03)
let mut bpm_message_tokens = Vec::new();
// Process U8 type BPM changes
let EventProcessingResult {
message_tokens: bpm_u8_message_tokens,
..
} = build_event_messages(
self.bpm.bpm_changes_u8.iter(),
None::<(
fn(ObjId, &()) -> Token,
fn(&_) -> &(),
&mut ObjIdManager<()>,
)>,
|_ev| Channel::BpmChangeU8,
|bpm, _id| {
let s = format!("{bpm:02X}");
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
bpm_message_tokens.extend(bpm_u8_message_tokens);
// Process other type BPM changes using build_event_messages_owned
let mut bpm_manager: HashMap<u64, ObjId> = bpm_value_to_id;
let EventProcessingResult {
late_def_tokens: other_late_def_tokens,
message_tokens: other_message_tokens,
} = build_event_messages_owned(
self.bpm.bpm_changes.iter(),
Some((
|id, bpm_bits: &u64| Token::Header {
name: format!("BPM{id}").into(),
args: f64::from_bits(*bpm_bits).to_string().into(),
},
|ev: &'a BpmChangeObj| ev.bpm.as_f64().to_bits(),
&mut bpm_manager,
)),
|_ev| Channel::BpmChange,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
// Update id_manager with the results
late_def_tokens.extend(other_late_def_tokens);
bpm_message_tokens.extend(other_message_tokens);
message_tokens.extend(bpm_message_tokens);
// Messages: STOP (#xxx09)
let mut stop_manager: HashMap<u64, ObjId> = stop_value_to_id;
let EventProcessingResult {
late_def_tokens: stop_late_def_tokens,
message_tokens: stop_message_tokens,
} = build_event_messages_owned(
self.stop.stops.iter(),
Some((
|id, duration_bits: &u64| Token::Header {
name: format!("STOP{id}").into(),
args: f64::from_bits(*duration_bits).to_string().into(),
},
|ev: &'a StopObj| ev.duration.as_f64().to_bits(),
&mut stop_manager,
)),
|_ev| Channel::Stop,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
late_def_tokens.extend(stop_late_def_tokens);
message_tokens.extend(stop_message_tokens);
// Messages: SCROLL (#xxxSC)
let mut scroll_manager: HashMap<u64, ObjId> = scroll_value_to_id;
let EventProcessingResult {
late_def_tokens: scroll_late_def_tokens,
message_tokens: scroll_message_tokens,
} = build_event_messages_owned(
self.scroll.scrolling_factor_changes.iter(),
Some((
|id, factor_bits: &u64| Token::Header {
name: format!("SCROLL{id}").into(),
args: f64::from_bits(*factor_bits).to_string().into(),
},
|ev: &'a ScrollingFactorObj| ev.factor.as_f64().to_bits(),
&mut scroll_manager,
)),
|_ev| Channel::Scroll,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
late_def_tokens.extend(scroll_late_def_tokens);
message_tokens.extend(scroll_message_tokens);
// Messages: SPEED (#xxxSP)
let mut speed_manager: HashMap<u64, ObjId> = speed_value_to_id;
let EventProcessingResult {
late_def_tokens: speed_late_def_tokens,
message_tokens: speed_message_tokens,
} = build_event_messages_owned(
self.speed.speed_factor_changes.iter(),
Some((
|id, factor_bits: &u64| Token::Header {
name: format!("SPEED{id}").into(),
args: f64::from_bits(*factor_bits).to_string().into(),
},
|ev: &'a SpeedObj| ev.factor.as_f64().to_bits(),
&mut speed_manager,
)),
|_ev| Channel::Speed,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
late_def_tokens.extend(speed_late_def_tokens);
message_tokens.extend(speed_message_tokens);
{
// STP events, sorted by time for consistent output
let mut stp_events: Vec<_> = self.stop.stp_events.values().collect();
stp_events.sort_by_key(|ev| ev.time);
tokens.extend(stp_events.into_iter().map(|ev| {
Token::Header {
name: "STP".into(),
args: format!(
"{:03}.{:03} {}",
ev.time.track(),
ev.time.numerator() * ev.time.denominator_u64() / 1000,
ev.duration.as_millis()
)
.into(),
}
}));
}
// Messages: BGA changes (#xxx04/#xxx07/#xxx06/#xxx0A)
let EventProcessingResult {
message_tokens: bga_message_tokens,
..
} = build_event_messages(
self.bmp.bga_changes.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|bga| bga.layer.to_channel(),
|bga, _id| {
let s = bga.id.to_string();
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(bga_message_tokens);
{
// Messages: BGA opacity changes (#xxx0B/#xxx0C/#xxx0D/#xxx0E)
for (layer, opacity_changes) in &self.bmp.bga_opacity_changes {
let EventProcessingResult {
message_tokens: opacity_message_tokens,
..
} = build_event_messages(
opacity_changes.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| match layer {
BgaLayer::Base => Channel::BgaBaseOpacity,
BgaLayer::Poor => Channel::BgaPoorOpacity,
BgaLayer::Overlay => Channel::BgaLayerOpacity,
BgaLayer::Overlay2 => Channel::BgaLayer2Opacity,
},
|ev, _id| {
let s = format!("{:02X}", ev.opacity);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(opacity_message_tokens);
}
// Messages: BGA ARGB changes (#xxxA1/#xxxA2/#xxxA3/#xxxA4)
for (layer, argb_changes) in &self.bmp.bga_argb_changes {
let EventProcessingResult {
message_tokens: argb_message_tokens,
..
} = build_event_messages(
argb_changes.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| match layer {
BgaLayer::Base => Channel::BgaBaseArgb,
BgaLayer::Poor => Channel::BgaPoorArgb,
BgaLayer::Overlay => Channel::BgaLayerArgb,
BgaLayer::Overlay2 => Channel::BgaLayer2Argb,
},
|ev, _id| {
let s = format!("{:02X}", ev.argb.alpha);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(argb_message_tokens);
}
}
// Messages: BGM (#xxx01) and Notes (various #xx)
// Use build_event_messages to process note and BGM objects
// We need to preserve the original insertion order, so we process each object individually
let EventProcessingResult {
message_tokens: notes_message_tokens,
..
} = build_event_messages(
self.wav
.notes
.all_notes_insertion_order()
.map(|obj| (&obj.offset, obj)),
None::<(
fn(ObjId, &()) -> Token,
fn(&_) -> &(),
&mut ObjIdManager<()>,
)>,
|obj| {
// Channel mapping: determine channel based on channel_id
obj.channel_id
.try_into_map::<T>()
.map_or(Channel::Bgm, |_map| Channel::Note {
channel_id: obj.channel_id,
})
},
|obj, _id| {
let s = obj.wav_id.to_string();
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
}, // Message formatting: use wav_id
);
message_tokens.extend(notes_message_tokens);
// Messages: BGM volume (#97)
let EventProcessingResult {
message_tokens: bgm_volume_message_tokens,
..
} = build_event_messages(
self.volume.bgm_volume_changes.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| Channel::BgmVolume,
|ev, _id| {
let s = format!("{:02X}", ev.volume);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(bgm_volume_message_tokens);
// Messages: KEY volume (#98)
let EventProcessingResult {
message_tokens: key_volume_message_tokens,
..
} = build_event_messages(
self.volume.key_volume_changes.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| Channel::KeyVolume,
|ev, _id| {
let s = format!("{:02X}", ev.volume);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(key_volume_message_tokens);
// Messages: TEXT (#99)
let mut text_manager =
ObjIdManager::from_entries(text_value_to_id.iter().map(|(k, v)| (*k, *v)));
let EventProcessingResult {
late_def_tokens: text_late_def_tokens,
message_tokens: text_message_tokens,
} = build_event_messages(
self.text.text_events.iter(),
Some((
|id, text: &'a str| Token::Header {
name: format!("TEXT{id}").into(),
args: text.into(),
},
|ev: &'a TextObj| ev.text.as_str(),
&mut text_manager,
)),
|_ev| Channel::Text,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
checker.check(text_manager.into_assigned_ids());
late_def_tokens.extend(text_late_def_tokens);
message_tokens.extend(text_message_tokens);
let mut exrank_manager =
ObjIdManager::from_entries(exrank_value_to_id.iter().map(|(k, v)| (*k, *v)));
let EventProcessingResult {
late_def_tokens: judge_late_def_tokens,
message_tokens: judge_message_tokens,
} = build_event_messages(
self.judge.judge_events.iter(),
Some((
|id, judge_level: &JudgeLevel| Token::Header {
name: format!("EXRANK{id}").into(),
args: judge_level.to_string().into(),
},
|ev: &'a JudgeObj| &ev.judge_level,
&mut exrank_manager,
)),
|_ev| Channel::Judge,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
id.into_chars()
},
);
checker.check(exrank_manager.into_assigned_ids());
late_def_tokens.extend(judge_late_def_tokens);
message_tokens.extend(judge_message_tokens);
if let Some(poor_bmp) = self.bmp.poor_bmp.as_ref()
&& !poor_bmp.as_path().as_os_str().is_empty()
{
tokens.push(Token::Header {
name: "BMP00".into(),
args: poor_bmp.display().to_string().into(),
});
}
tokens.extend(
self.bmp
.bmp_files
.iter()
.filter(|(_, bmp)| !bmp.file.as_path().as_os_str().is_empty())
.map(|(id, bmp)| {
(
*id,
if bmp.transparent_color == Argb::default() {
Token::Header {
name: format!("BMP{id}").into(),
args: bmp.file.display().to_string().into(),
}
} else {
Token::Header {
name: format!("EXBMP{id}").into(),
args: format!(
"{},{},{},{} {}",
bmp.transparent_color.alpha,
bmp.transparent_color.red,
bmp.transparent_color.green,
bmp.transparent_color.blue,
bmp.file.display()
)
.into(),
}
},
)
})
.collect::<BTreeMap<_, _>>()
.into_values(),
);
{
// Messages: SEEK (#xxx05)
let mut seek_manager: HashMap<u64, ObjId> = seek_value_to_id;
let EventProcessingResult {
late_def_tokens: seek_late_def_tokens,
message_tokens: seek_message_tokens,
} = build_event_messages_owned(
self.video.seek_events.iter(),
Some((
|id, position_bits: &u64| Token::Header {
name: format!("SEEK{id}").into(),
args: f64::from_bits(*position_bits).to_string().into(),
},
|ev: &'a SeekObj| ev.position.as_f64().to_bits(),
&mut seek_manager,
)),
|_ev| Channel::Seek,
|_ev, id| {
let id = id.unwrap_or(ObjId::null());
let s = id.to_string();
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
late_def_tokens.extend(seek_late_def_tokens);
message_tokens.extend(seek_message_tokens);
// Messages: BGA keybound (#xxxA5)
let EventProcessingResult {
message_tokens: bga_keybound_message_tokens,
..
} = build_event_messages(
self.bmp.bga_keybound_events.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| Channel::BgaKeybound,
|ev, _id| {
let s = format!("{:02X}", ev.event.line);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
},
);
message_tokens.extend(bga_keybound_message_tokens);
// Messages: OPTION (#xxxA6)
let EventProcessingResult {
message_tokens: option_message_tokens,
..
} = build_event_messages(
self.option.option_events.iter(),
None::<(
fn(ObjId, &'a ()) -> Token<'a>,
fn(&_) -> &'a (),
&mut ObjIdManager<()>,
)>,
|_ev| Channel::OptionChange,
|_ev, _id| {
let s = format!("{:02X}", 0);
let mut chars = s.chars();
[chars.next().unwrap_or('0'), chars.next().unwrap_or('0')]
}, // Option events don't use values
);
checker.check(seek_manager.values().copied());
message_tokens.extend(option_message_tokens);
};
// Assembly: header/definitions/resources/others -> late definitions -> messages
if !late_def_tokens.is_empty() {
tokens.extend(late_def_tokens);
}
if !message_tokens.is_empty() {
tokens.extend(message_tokens);
}
}
}
#[derive(Debug, Default)]
struct Base62Checker {
using_base62: bool,
}
impl Base62Checker {
const fn new() -> Self {
Self {
using_base62: false,
}
}
fn check(&mut self, iter: impl IntoIterator<Item = ObjId>) {
if !self.using_base62 && iter.into_iter().any(|id| !id.is_base36() && id.is_base62()) {
self.using_base62 = true;
}
}
const fn into_using_base62(self) -> bool {
self.using_base62
}
}
/// A unit of event processing containing all necessary information for token generation
#[derive(Debug, Clone)]
struct EventUnit<'a, Event> {
time: ObjTime,
event: &'a Event,
channel: Channel,
id: Option<ObjId>,
}
/// Complete result from `build_messages_event` containing all processing outputs
struct EventProcessingResult<'a> {
message_tokens: Vec<Token<'a>>,
late_def_tokens: Vec<Token<'a>>,
}
/// Generic function to process message types with optional ID allocation
///
/// This function processes time-indexed events from an iterator and converts them into message tokens.
/// It supports both ID allocation mode (using `token_creator` and `key_extractor`) and direct mode (without ID allocation).
///
/// # PROCESSING FLOW OVERVIEW:
/// 1. **GROUP EVENTS**: Events are grouped by track, channel, and non-strictly increasing time
/// 2. **SPLIT INTO MESSAGE SEGMENTS**: Each group is further split into message segments with stricter rules:
/// - Strictly increasing time (prevents overlaps)
/// - Consistent denominators (ensures accurate representation)
/// 3. **GENERATE TOKENS**: Each message segment becomes one `Token::Message` with all events encoded
///
/// Arguments:
/// events: An iterator yielding (&time, &event) pairs to process
/// `id_allocation`: Optional tuple containing (`token_creator`, `key_extractor`, `id_manager`) for ID allocation mode
/// `channel_mapper`: Function to map events to channels
/// `message_formatter`: Function to format events into [char; 2]
///
/// Returns:
/// `EventProcessingResult` containing `message_tokens`, `late_def_tokens`, and updated maps
///
/// The function leverages Rust's iterator chains for efficient processing and supports
/// both ID-based and direct value-based event processing.
fn build_event_messages<
'a,
Event: 'a,
Key: 'a + ?Sized + std::hash::Hash + Eq,
EventIterator,
TokenCreator,
KeyExtractor,
ChannelMapper,
MessageFormatter,
>(
event_iter: EventIterator,
mut id_allocation: Option<(TokenCreator, KeyExtractor, &mut ObjIdManager<'a, Key>)>,
channel_mapper: ChannelMapper,
message_formatter: MessageFormatter,
) -> EventProcessingResult<'a>
where
EventIterator: Iterator<Item = (&'a ObjTime, &'a Event)>,
TokenCreator: Fn(ObjId, &'a Key) -> Token<'a>,
KeyExtractor: Fn(&'a Event) -> &'a Key,
ChannelMapper: Fn(&'a Event) -> Channel,
MessageFormatter: Fn(&'a Event, Option<ObjId>) -> [char; 2],
{
let mut late_def_tokens: Vec<Token<'a>> = Vec::new();
// Process events based on whether id_allocation tuple is provided
// Keep original order from event_iter instead of grouping by track/channel
let processed_events: Vec<EventUnit<'a, Event>> = event_iter
.map(|(&time, event)| {
let id = id_allocation
.as_mut()
.and_then(|(token_creator, key_extractor, manager)| {
let key = key_extractor(event);
let is_assigned = manager.is_assigned(key);
let id = manager.get_or_new_id(key);
if !is_assigned && let Some(new) = id {
late_def_tokens.push(token_creator(new, key));
}
id
});
EventUnit {
time,
event,
channel: channel_mapper(event),
id,
}
})
.collect();
// === STEP 1: GROUP EVENTS BY TRACK, CHANNEL, AND TIME ===
// Group events by adjacent same track, channel and non-strictly increasing time
//
// This creates the first level of grouping where events that share:
// - Preserve the original event iterator order
// - Same track number
// - Same channel type
// - Non-strictly increasing time (last_time <= current_time)
// ...are grouped together. This is the foundation for efficient message generation.
let grouped_events = group_events_by_track_channel_time(processed_events);
// === STEP 2: SPLIT GROUPS INTO MESSAGE SEGMENTS ===
// Split each group into message segments based on time ordering and denominator consistency
//
// This creates the second level of grouping with stricter rules:
// - Not preserve the original event iterator order
// - Time must be strictly increasing (last_time < current_time)
// - Denominators must be the same starting from the second element
// - First element can have 0 numerator, or the same denominator as elements after it
//
// The purpose is to ensure that events within a message segment can be represented
// in a single message string without conflicts or information loss.
let message_segmented_events: Vec<Vec<_>> = grouped_events
.into_iter()
.flat_map(split_group_into_message_segments)
.collect();
// === STEP 3: GENERATE MESSAGE TOKENS FROM MESSAGE SEGMENTS ===
// Generate message tokens: each message segment generates one Token::Message
//
// This is the final step where each message segment is converted into a single Token::Message.
// The process ensures that all events in a message segment are represented in one message string
// with correct timing and without information loss.
let message_tokens: Vec<Token<'a>> = message_segmented_events
.into_iter()
.map(|message_segment| {
convert_message_segment_to_token(message_segment, &message_formatter)
})
.collect();
EventProcessingResult {
message_tokens,
late_def_tokens,
}
}
/// A version of `build_event_messages` that supports owned keys (like u64 instead of &u64)
fn build_event_messages_owned<
'a,
Event: 'a,
Key: std::hash::Hash + Eq + Clone,
EventIterator,
TokenCreator,
KeyExtractor,
ChannelMapper,
MessageFormatter,
>(
event_iter: EventIterator,
mut id_allocation: Option<(TokenCreator, KeyExtractor, &mut HashMap<Key, ObjId>)>,
channel_mapper: ChannelMapper,
message_formatter: MessageFormatter,
) -> EventProcessingResult<'a>
where
EventIterator: Iterator<Item = (&'a ObjTime, &'a Event)>,
TokenCreator: Fn(ObjId, &Key) -> Token<'a>,
KeyExtractor: Fn(&'a Event) -> Key,
ChannelMapper: Fn(&'a Event) -> Channel,
MessageFormatter: Fn(&'a Event, Option<ObjId>) -> [char; 2],
{
let mut late_def_tokens: Vec<Token<'a>> = Vec::new();
// Process events based on whether id_allocation tuple is provided
let processed_events: Vec<EventUnit<'a, Event>> = event_iter
.map(|(&time, event)| {
let id = id_allocation
.as_mut()
.and_then(|(token_creator, key_extractor, manager)| {
let key = key_extractor(event);
let _is_assigned = manager.contains_key(&key);
manager.get(&key).copied().or_else(|| {
let new_id =
ObjId::all_values().find(|id| !manager.values().any(|&v| v == *id));
if let Some(new_id) = new_id {
manager.insert(key.clone(), new_id);
late_def_tokens.push(token_creator(new_id, &key));
}
new_id
})
});
EventUnit {
time,
event,
channel: channel_mapper(event),
id,
}
})
.collect();
let grouped_events = group_events_by_track_channel_time(processed_events);
let message_segmented_events: Vec<Vec<_>> = grouped_events
.into_iter()
.flat_map(split_group_into_message_segments)
.collect();
let message_tokens: Vec<Token<'a>> = message_segmented_events
.into_iter()
.map(|message_segment| {
convert_message_segment_to_token(message_segment, &message_formatter)
})
.collect();
EventProcessingResult {
message_tokens,
late_def_tokens,
}
}
/// Group events by track, channel, and non-strictly increasing time
fn group_events_by_track_channel_time<'a, Event>(
processed_events: Vec<EventUnit<'a, Event>>,
) -> Vec<Vec<EventUnit<'a, Event>>> {
let mut groups = Vec::new();
let mut current_group = Vec::new();
for event_unit in processed_events {
let should_join = current_group
.last()
.is_some_and(|last_unit: &EventUnit<'a, Event>| {
event_unit.time.track() == last_unit.time.track()
&& last_unit.channel == event_unit.channel
&& last_unit.time <= event_unit.time
});
if should_join {
current_group.push(event_unit);
} else {
if !current_group.is_empty() {
groups.push(current_group);
}
current_group = vec![event_unit];
}
}
if !current_group.is_empty() {
groups.push(current_group);
}
groups
}
/// Split a group into message segments based on time ordering and denominator consistency
fn split_group_into_message_segments<'a, Event>(
group: Vec<EventUnit<'a, Event>>,
) -> Vec<Vec<EventUnit<'a, Event>>> {
let mut message_segments = Vec::new();
let mut current_message_segment = Vec::new();
for event_unit in group {
let should_join =
current_message_segment
.last()
.is_none_or(|last_unit: &EventUnit<'a, Event>| {
// MESSAGE SEGMENT JOINING RULES:
// 1. Time must be strictly increasing (prevents overlapping events)
// 2. Denominators must be compatible:
// - If current message segment is empty, accept any denominator
// - Otherwise, denominators must share a factor relationship (either is a factor of the other)
// - Reference denominator is the maximum denominator currently in the message segment
(last_unit.time < event_unit.time)
&& (current_message_segment.is_empty()
|| is_denominator_compatible(&event_unit, ¤t_message_segment))
}); // Empty message segment always accepts the first event
if should_join {
current_message_segment.push(event_unit);
} else {
if !current_message_segment.is_empty() {
message_segments.push(current_message_segment);
}
current_message_segment = vec![event_unit];
}
}
if !current_message_segment.is_empty() {
message_segments.push(current_message_segment);
}
message_segments
}
/// Check if an event unit's denominator is compatible with the current message segment
/// Two denominators are compatible if either is a factor of the other
fn is_denominator_compatible<'a, Event>(
event_unit: &EventUnit<'a, Event>,
message_segment: &[EventUnit<'a, Event>],
) -> bool {
// Find the maximum denominator from the current message segment as reference
let reference_denominator = message_segment
.iter()
.map(|unit| unit.time.denominator_u64())
.max()
.unwrap_or(1);
// Check if the event unit's denominator shares a common factor relationship
let event_denominator = event_unit.time.denominator_u64();
reference_denominator.is_multiple_of(event_denominator)
|| event_denominator.is_multiple_of(reference_denominator)
}
/// Convert a message segment of events into a single `Token::Message`
fn convert_message_segment_to_token<'a, Event, MessageFormatter>(
message_segment: Vec<EventUnit<'a, Event>>,
message_formatter: &MessageFormatter,
) -> Token<'a>
where
MessageFormatter: Fn(&'a Event, Option<ObjId>) -> [char; 2],
{
if message_segment.is_empty() {
return Token::Message {
track: Track(0),
channel: Channel::Bgm,
message: Cow::Borrowed(""),
};
}
// EXTRACT METADATA FROM MESSAGE SEGMENT
// All events in message segment should have same track and channel (guaranteed by grouping logic)
let Some(first_event) = message_segment.first() else {
return Token::Message {
track: Track(0),
channel: Channel::Bgm,
message: Cow::Borrowed(""),
};
};
let (track, channel) = (first_event.time.track(), first_event.channel);
// CALCULATE MESSAGE LENGTH
// Find the least common multiple (LCM) of all denominators to determine message length - this ensures
// all events in the message segment can be accurately positioned in the message string.
// Example: if we have events at 1/3 and 1/5, LCM(3,5)=15, so we need length 15 to represent them both accurately.
let denominators: Vec<u64> = message_segment
.iter()
.map(|event_unit| event_unit.time.denominator_u64())
.collect();
let lcm_denom = lcm_slice(&denominators);
let message_len = lcm_denom as usize;
let mut message_parts: Vec<String> = vec!["00".to_string(); message_len];
// PLACE EVENTS IN MESSAGE STRING
// For each event in the message segment, calculate its exact position in the message
// and place its value there. The time_idx calculation converts fractional time
// to array index using the formula: (numerator * lcm_denom / denominator)
for event_unit in message_segment {
let EventUnit {
event, id, time, ..
} = event_unit;
let chars = message_formatter(event, id);
let denom_u64 = time.denominator_u64();
// Calculate exact position: convert fraction to index in the message array
// Example: time=3/4, lcm_denom=4: (3 * 4 / 4) = 3, so place at index 3
// Example: time=1/3, lcm_denom=15: (1 * 15 / 3) = 5, so place at index 5
let time_idx = (time.numerator() * (lcm_denom / denom_u64)) as usize;
// Ensure we don't go out of bounds (safety check)
let Some(slot) = message_parts.get_mut(time_idx) else {
continue;
};
*slot = chars.iter().collect::<String>();
}
Token::Message {
track,
channel,
message: Cow::Owned(message_parts.join("")),
}
}
/// Calculate the least common multiple (LCM) of a slice of u64 values
/// Returns 1 if the slice is empty
fn lcm_slice(denominators: &[u64]) -> u64 {
denominators.iter().fold(1, |acc, denom| acc.lcm(denom))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lcm_slice() {
// Test empty slice
assert_eq!(lcm_slice(&[]), 1);
// Test single value
assert_eq!(lcm_slice(&[3]), 3);
assert_eq!(lcm_slice(&[5]), 5);
// Test two values
assert_eq!(lcm_slice(&[3, 5]), 15);
assert_eq!(lcm_slice(&[4, 6]), 12);
assert_eq!(lcm_slice(&[2, 4, 8]), 8);
// Test multiple values
assert_eq!(lcm_slice(&[2, 3, 4]), 12);
assert_eq!(lcm_slice(&[3, 5, 7]), 105);
assert_eq!(lcm_slice(&[6, 8, 10]), 120);
// Test with 1
assert_eq!(lcm_slice(&[1, 3]), 3);
assert_eq!(lcm_slice(&[3, 1]), 3);
}
}