Skip to content

Commit 09c5ead

Browse files
committed
[R] Chart相关重构(第三步)——移除各个谱面中的Resolution,而是parser解析存成分数、generator使用写死的固定值
1 parent ba1bbcc commit 09c5ead

10 files changed

Lines changed: 56 additions & 68 deletions

File tree

chart/chu/C2sChart.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ public class C2sChart : BaseChart<ChuNote>, IChuChart
1515
public string DisplayLevel { get; set; } = ""; // 显示等级,字符串
1616
public decimal Level { get; set; } // 定数,小数
1717
public string MusicId { get; set; } = "0";
18-
public int Resolution { get; set; } = 384;
1918
public List<(Rational Time, Rational Duration, decimal Multiplier)> SflList = []; // 所有变速声明构成的列表。
2019
}

chart/chu/SusChart.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ public class SusChart : BaseChart<ChuNote>, IChuChart
1010
public string Title { get; set; } = "";
1111
public string Artist { get; set; } = "";
1212
public string Designer { get; set; } = "";
13-
public int TicksPerBeat { get; set; } = 480;
1413
}

chart/chu/UgcChart.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ public class UgcChart : BaseChart<ChuNote>, IChuChart
1515
public string DisplayLevel = "";
1616
public decimal Level { get; set; }
1717
public string MusicId { get; set; } = "";
18-
public int TicksPerBeat { get; set; } = 480;
1918
public List<(Rational Time, Rational Duration, decimal Multiplier)> SflList = []; // 所有变速声明构成的列表。
2019
}

generator/chu/C2sGenerator.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace MuConvert.chu;
1313
*/
1414
public class C2sGenerator : IGenerator<IChuChart>
1515
{
16+
private const int RSL = 384;
17+
1618
public (string, List<Alert>) Generate(IChuChart chart)
1719
{
1820
var alerts = new List<Alert>();
@@ -68,36 +70,35 @@ private static string Serialize(C2sChart chart)
6870
var bpm_def = chart.BpmList.BPM_DEF();
6971
sb.AppendLine($"BPM_DEF\t{bpm_def.Item1}\t{bpm_def.Item2}\t{bpm_def.Item3}\t{bpm_def.Item4}");
7072
sb.AppendLine("MET_DEF\t4\t4");
71-
sb.AppendLine($"RESOLUTION\t{chart.Resolution}");
72-
sb.AppendLine($"CLK_DEF\t{chart.Resolution}");
73+
sb.AppendLine($"RESOLUTION\t{RSL}");
74+
sb.AppendLine($"CLK_DEF\t{RSL}");
7375
sb.AppendLine("PROGJUDGE_BPM\t240.000");
7476
sb.AppendLine("PROGJUDGE_AER\t0.999");
7577
sb.AppendLine("TUTORIAL\t0");
7678
sb.AppendLine();
7779

78-
var res = chart.Resolution;
7980
foreach (var b in chart.BpmList)
8081
{
81-
var (m, o) = Utils.BarAndTick(b.Time, res);
82+
var (m, o) = Utils.BarAndTick(b.Time, RSL);
8283
sb.AppendLine($"BPM\t{m}\t{o}\t{b.Bpm:0.000}");
8384
}
8485

8586
foreach (var met in chart.MetList)
8687
{
87-
var (m, o) = Utils.BarAndTick(met.Time, res);
88+
var (m, o) = Utils.BarAndTick(met.Time, RSL);
8889
sb.AppendLine($"MET\t{m}\t{o}\t{met.Denominator}\t{met.Numerator}");
8990
}
9091

9192
foreach (var s in chart.SflList.OrderBy(s => s.Time))
9293
{
93-
var (m, o) = Utils.BarAndTick(s.Time, res);
94-
var durTicks = Utils.Tick(s.Duration, res);
94+
var (m, o) = Utils.BarAndTick(s.Time, RSL);
95+
var durTicks = Utils.Tick(s.Duration, RSL);
9596
sb.AppendLine($"SFL\t{m}\t{o}\t{durTicks}\t{s.Multiplier:0.000000}");
9697
}
9798
sb.AppendLine();
9899

99100
foreach (var n in chart.Notes)
100-
sb.AppendLine(FormatNote(n, chart.Resolution));
101+
sb.AppendLine(FormatNote(n, RSL));
101102

102103
sb.AppendLine();
103104
return sb.ToString();

generator/chu/SusGenerator.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace MuConvert.chu;
1212
*/
1313
public class SusGenerator : IGenerator<IChuChart>
1414
{
15-
private const int SusTpb = 480;
15+
private static int RSL = 480 * 4;
1616

1717
public (string, List<Alert>) Generate(IChuChart chart)
1818
{
@@ -32,7 +32,7 @@ private static SusChart ConvertToSus(IChuChart chart, List<Alert> alerts)
3232
if (chart is C2sChart c2s)
3333
{
3434
bpm = c2s.BpmList.Count > 0 ? (double)c2s.BpmList[0].Bpm : 120.0;
35-
var result = new SusChart { TicksPerBeat = SusTpb, Title = title, Artist = artist };
35+
var result = new SusChart { Title = title, Artist = artist };
3636
result.BpmList.Add(new BPM(0, (decimal)bpm));
3737
result.Notes = c2s.Notes;
3838
return result;
@@ -41,7 +41,7 @@ private static SusChart ConvertToSus(IChuChart chart, List<Alert> alerts)
4141
if (chart is UgcChart ugc)
4242
{
4343
bpm = ugc.BpmList.Count > 0 ? (double)ugc.BpmList[0].Bpm : 120.0;
44-
var result = new SusChart { TicksPerBeat = SusTpb, Title = ugc.Title, Artist = ugc.Artist };
44+
var result = new SusChart { Title = ugc.Title, Artist = ugc.Artist };
4545
result.BpmList.Add(new BPM(0, (decimal)bpm));
4646
result.Notes = ugc.Notes;
4747
return result;
@@ -60,14 +60,13 @@ private static string Serialize(SusChart sus)
6060
if (!string.IsNullOrEmpty(sus.Artist)) sb.AppendLine($"#ARTIST \"{sus.Artist}\"");
6161
if (!string.IsNullOrEmpty(sus.Designer)) sb.AppendLine($"#DESIGNER \"{sus.Designer}\"");
6262
sb.AppendLine($"#BPM_DEF {sus.StartBpm:F2}");
63-
sb.AppendLine($"#REQUEST \"{sus.TicksPerBeat}\"");
63+
sb.AppendLine($"#REQUEST \"{RSL / 4}\"");
6464
sb.AppendLine();
6565

66-
var tpm = sus.TicksPerBeat * 4;
6766
foreach (var n in sus.Notes)
6867
{
69-
var (m, o) = Utils.BarAndTick(n.Time, tpm, 0);
70-
sb.AppendLine($"#{m:X2}{o:X3}:{FormatData(n, tpm)}");
68+
var (m, o) = Utils.BarAndTick(n.Time, RSL);
69+
sb.AppendLine($"#{m:X2}{o:X3}:{FormatData(n, RSL)}");
7170
}
7271

7372
return sb.ToString();

generator/chu/UgcGenerator.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace MuConvert.chu;
1111
*/
1212
public class UgcGenerator : IGenerator<IChuChart>
1313
{
14-
private const int UgcTicksPerBeat = 480;
14+
private static int RSL = 480 * 4;
1515

1616
public (string, List<Alert>) Generate(IChuChart chart)
1717
{
@@ -29,7 +29,6 @@ private static UgcChart ConvertToUgc(IChuChart chart, List<Alert> alerts)
2929
{
3030
var result = new UgcChart
3131
{
32-
TicksPerBeat = UgcTicksPerBeat,
3332
Designer = c2s.Designer,
3433
Difficulty = c2s.Difficulty,
3534
MusicId = c2s.MusicId,
@@ -58,23 +57,22 @@ private static string Serialize(UgcChart ugc)
5857
sb.AppendLine($"@LEVEL\t{ugc.DisplayLevel}");
5958
sb.AppendLine($"@CONST\t{ugc.Level:F5}");
6059
sb.AppendLine($"@SONGID\t{ugc.MusicId}");
61-
sb.AppendLine($"@TICKS\t{ugc.TicksPerBeat}");
62-
var tpm = ugc.TicksPerBeat * 4;
60+
sb.AppendLine($"@TICKS\t{RSL / 4}");
6361
foreach (var met in ugc.MetList)
6462
{
65-
var (m, _) = Utils.BarAndTick(met.Time, tpm);
63+
var (m, _) = Utils.BarAndTick(met.Time, RSL);
6664
sb.AppendLine($"@BEAT\t{m}\t{met.Numerator}\t{met.Denominator}");
6765
}
6866
foreach (var b in ugc.BpmList)
6967
{
70-
var (m, o) = Utils.BarAndTick(b.Time, tpm);
68+
var (m, o) = Utils.BarAndTick(b.Time, RSL);
7169
sb.AppendLine($"@BPM\t{m}'{o}\t{b.Bpm:F5}");
7270
}
7371
sb.AppendLine("@TIL\t0\t0'0\t1.00000");
7472

7573
foreach (var s in ugc.SflList.OrderBy(x => x.Time))
7674
{
77-
var (m, o) = Utils.BarAndTick(s.Time, tpm);
75+
var (m, o) = Utils.BarAndTick(s.Time, RSL);
7876
sb.AppendLine($"@SPDMOD\t{m}'{o}\t{s.Multiplier:0.00000}");
7977
}
8078

@@ -84,10 +82,10 @@ private static string Serialize(UgcChart ugc)
8482

8583
foreach (var n in ugc.Notes)
8684
{
87-
var (m, o) = Utils.BarAndTick(n.Time, tpm);
88-
sb.Append($"#{m}'{o}:{UCode(n, tpm)}");
85+
var (m, o) = Utils.BarAndTick(n.Time, RSL);
86+
sb.Append($"#{m}'{o}:{UCode(n, RSL)}");
8987
sb.AppendLine();
90-
var durTicks = Utils.Tick(n.Duration, tpm);
88+
var durTicks = Utils.Tick(n.Duration, RSL);
9189
if (n.Type == "HLD" && durTicks > 0)
9290
sb.AppendLine($"#{durTicks}>s");
9391
else if (n.Type == "SLD" && durTicks > 0)

parser/chu/C2sParser.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace MuConvert.chu;
1313
*/
1414
public class C2sParser : IParser<C2sChart>
1515
{
16+
private static int RSL = 384;
1617
private static readonly HashSet<string> HeadTags = new(StringComparer.OrdinalIgnoreCase)
1718
{ "VERSION", "MUSIC", "SEQUENCEID", "DIFFICULT", "LEVEL", "CREATOR", "BPM_DEF", "MET_DEF", "RESOLUTION", "CLK_DEF", "PROGJUDGE_BPM", "PROGJUDGE_AER", "TUTORIAL" };
1819
private static readonly HashSet<string> TimingTags = new(StringComparer.OrdinalIgnoreCase)
@@ -61,26 +62,25 @@ private static void ParseHeader(string[] p, C2sChart chart)
6162
case "MUSIC": chart.MusicId = Int(p, 1).ToString(); break;
6263
case "DIFFICULT": chart.Difficulty = Int(p, 1); break;
6364
case "CREATOR": chart.Designer = Str(p, 1); break;
64-
case "RESOLUTION": chart.Resolution = Math.Max(1, Int(p, 1, 384)); break;
65+
case "RESOLUTION": RSL = Math.Max(1, Int(p, 1, 384)); break;
6566
}
6667
}
6768

6869
private static void ParseTiming(string[] p, C2sChart chart)
6970
{
7071
var tag = p[0].ToUpperInvariant();
71-
var tpm = chart.Resolution;
7272
switch (tag)
7373
{
7474
case "BPM":
75-
chart.BpmList.Add(new BPM(Int(p, 1) + new Rational(Int(p, 2), tpm), decimal.Parse(p[3])));
75+
chart.BpmList.Add(new BPM(Int(p, 1) + new Rational(Int(p, 2), RSL), decimal.Parse(p[3])));
7676
break;
7777
case "MET":
78-
chart.MetList.Add(new MET(Int(p, 1) + new Rational(Int(p, 2), tpm), Int(p, 4, 4), Int(p, 3, 4)));
78+
chart.MetList.Add(new MET(Int(p, 1) + new Rational(Int(p, 2), RSL), Int(p, 4, 4), Int(p, 3, 4)));
7979
break;
8080
case "SFL":
8181
chart.SflList.Add((
82-
Int(p, 1) + new Rational(Int(p, 2), tpm),
83-
new Rational(Int(p, 3), tpm),
82+
Int(p, 1) + new Rational(Int(p, 2), RSL),
83+
new Rational(Int(p, 3), RSL),
8484
decimal.Parse(p[4])));
8585
break;
8686
}
@@ -89,8 +89,7 @@ private static void ParseTiming(string[] p, C2sChart chart)
8989
private static void ParseNote(string[] p, C2sChart chart, List<Alert> alerts, int lineNum)
9090
{
9191
var tag = p[0].ToUpperInvariant();
92-
var tpm = chart.Resolution;
93-
var note = new ChuNote { Type = tag, Time = Int(p, 1) + new Rational(Int(p, 2), tpm) };
92+
var note = new ChuNote { Type = tag, Time = Int(p, 1) + new Rational(Int(p, 2), RSL) };
9493

9594
switch (tag)
9695
{
@@ -99,10 +98,10 @@ private static void ParseNote(string[] p, C2sChart chart, List<Alert> alerts, in
9998
case "CHR":
10099
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1)); note.Tag = Str(p, 5); break;
101100
case "HLD": case "HXD":
102-
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1)); note.Duration = new Rational(Int(p, 5), tpm); break;
101+
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1)); note.Duration = new Rational(Int(p, 5), RSL); break;
103102
case "SLD": case "SLC": case "SXD": case "SXC":
104103
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1));
105-
note.Duration = new Rational(Int(p, 5), tpm);
104+
note.Duration = new Rational(Int(p, 5), RSL);
106105
note.EndCell = Int(p, 6); note.EndWidth = Math.Max(1, Int(p, 7, 1));
107106
break;
108107
case "FLK":
@@ -113,7 +112,7 @@ private static void ParseNote(string[] p, C2sChart chart, List<Alert> alerts, in
113112
break;
114113
case "AHD": case "AHX":
115114
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1));
116-
note.TargetNote = Str(p, 5); note.Duration = new Rational(Int(p, 6), tpm);
115+
note.TargetNote = Str(p, 5); note.Duration = new Rational(Int(p, 6), RSL);
117116
if (p.Length >= 8) note.Tag = Str(p, 7);
118117
break;
119118
case "ASD": case "ASC":
@@ -126,7 +125,7 @@ private static void ParseNote(string[] p, C2sChart chart, List<Alert> alerts, in
126125
note.Cell = Int(p, 3); note.Width = Math.Max(1, Int(p, 4, 1));
127126
note.TargetNote = Str(p, 5);
128127
note.ExtraData = [Int(p, 6), Int(p, 10)];
129-
note.Duration = new Rational(Int(p, 7), tpm);
128+
note.Duration = new Rational(Int(p, 7), RSL);
130129
note.EndCell = Int(p, 8); note.EndWidth = Math.Max(1, Int(p, 9, 1));
131130
note.Tag = Str(p, 11);
132131
break;

parser/chu/SusParser.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace MuConvert.chu;
1313
*/
1414
public class SusParser : IParser<SusChart>
1515
{
16+
private static int RSL = 480 * 4;
17+
1618
private static readonly Dictionary<int, string> TypeMap = new()
1719
{
1820
[0x01] = "TAP",
@@ -93,7 +95,7 @@ private static void ParseHeaderLine(string content, SusChart chart, List<Alert>
9395
{
9496
var reqStr = content[8..].Trim().Trim('"');
9597
if (int.TryParse(reqStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out var ticks))
96-
chart.TicksPerBeat = ticks;
98+
RSL = ticks * 4;
9799
else
98100
alerts.Add(new Alert(Warning, $"REQUEST 格式错误: {content}") { Line = lineNum });
99101
}
@@ -119,7 +121,6 @@ private static void ParseNoteLine(string content, SusChart chart, List<Alert> al
119121

120122
var measure = HexToInt(timingStr[..2]);
121123
var tick = HexToInt(timingStr[2..5]);
122-
var tpm = chart.TicksPerBeat * 4;
123124

124125
if (dataStr.Length < 6)
125126
{
@@ -140,7 +141,7 @@ private static void ParseNoteLine(string content, SusChart chart, List<Alert> al
140141
var note = new ChuNote
141142
{
142143
Type = typeName,
143-
Time = measure + new Rational(tick, tpm),
144+
Time = measure + new Rational(tick, RSL),
144145
Cell = lane / 2,
145146
Width = Math.Max(1, width / 2),
146147
};
@@ -154,20 +155,20 @@ private static void ParseNoteLine(string content, SusChart chart, List<Alert> al
154155
break;
155156

156157
case "HLD":
157-
ParseHoldData(dataStr, note, tpm, alerts, lineNum);
158+
ParseHoldData(dataStr, note, RSL, alerts, lineNum);
158159
break;
159160

160161
case "SLD":
161-
ParseSlideData(dataStr, note, tpm, alerts, lineNum);
162+
ParseSlideData(dataStr, note, RSL, alerts, lineNum);
162163
break;
163164

164165
case "AIR":
165166
case "ADW":
166-
ParseAirTarget(dataStr, note, tpm, alerts, lineNum);
167+
ParseAirTarget(dataStr, note, RSL, alerts, lineNum);
167168
break;
168169

169170
case "AHD":
170-
ParseAhdData(dataStr, note, tpm, alerts, lineNum);
171+
ParseAhdData(dataStr, note, RSL, alerts, lineNum);
171172
break;
172173
}
173174

0 commit comments

Comments
 (0)