Skip to content

Commit d92a9e3

Browse files
committed
[R] Chart相关重构(最后一步)——三种Chart合并为统一的ChuChart!
实际上,经过前几步之后,各种Chart的内容(无论是字段还是类型)都实现了完全统一了。所以这里只需要修改类名就可以了。
1 parent 09c5ead commit d92a9e3

13 files changed

Lines changed: 52 additions & 234 deletions

File tree

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private static void RunConvertChuSingleFile(string filePath, string inputKind)
474474
var destNote = _outputSpec.Kind == OutputSinkKind.Stdout ? "(标准输出)" : outPath;
475475
Console.Error.WriteLine($"{inputKind.ToUpperInvariant()}{targetFormat.ToUpperInvariant()}: {full}{destNote}");
476476

477-
IChuChart chart;
477+
ChuChart chart;
478478
List<Alert> parseAlerts;
479479
switch (inputKind)
480480
{

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,7 @@ finally
215215
- **parser(解析器)**:把“源格式文本”解析成中间表示
216216
- `SimaiParser.Parse(string)``MaiChart`
217217
- `MA2Parser.Parse(string)``MaiChart`
218-
- CHUNITHM的三种Parser(`C2sParser``UgcParser``SusParser`):`Parse(string)``IChuChart`
219-
> <details>
220-
> <summary>关于IChuChart</summary>
221-
> 当前实现IChuChart是一个通用的接口而非具体的类型,这是因为目前不同Parser解析出的谱面的IR尚未能够完全统一,所以只能都各自继承自IChuChart。
222-
> 不过不用担心,任意的Generator都接受任意IChuChart对象,因此你可以不在意它们之间的差异,直接拿来用就行了。
223-
> 未来如果有机会的话,我们会把它们进一步统一成同一个具体类型的IR,以进一步提升代码的可维护性和可读性。
224-
> </details>
218+
- CHUNITHM的三种Parser(`C2sParser``UgcParser``SusParser`):`Parse(string)``ChuChart`
225219
- 解析成功时,**返回值会同时带有 `List<Alert>`**,这是转谱过程中可能遇到的警告等信息,建议打印出来(直接对`Alert`对象`ToString()`即可)。
226220
- 如果解析失败,会抛出 `ConversionException`;该异常对象中同样含有一个 `List<Alert>`,是导致转谱失败的错误信息,可以同上打印出来。
227221
@@ -232,7 +226,7 @@ finally
232226
- **generator(生成器)**:把中间表示转回“目标格式文本”
233227
- `SimaiGenerator.Generate(MaiChart)` → Simai 单谱文本(可写入 `maidata.txt``&inote_*`
234228
- `MA2Generator.Generate(MaiChart)` → MA2 文本
235-
- CHUNITHM的三种Generator(`C2sGenerator``UgcGenerator``SusGenerator`):`Generate(IChuChart)` → 目标格式的谱面文本
229+
- CHUNITHM的三种Generator(`C2sGenerator``UgcGenerator``SusGenerator`):`Generate(ChuChart)` → 目标格式的谱面文本
236230
- 与parser类似,成功生成时,**返回值会同时带有 `List<Alert>`**,这是转谱过程中可能遇到的警告等信息,建议打印出来(直接对`Alert`对象`ToString()`即可)。
237231
- 如果生成失败,会抛出 `ConversionException`;该异常对象中同样含有一个 `List<Alert>`,是导致转谱失败的错误信息,可以同上打印出来。
238232
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
namespace MuConvert.chu;
55

6-
/**
7-
* C2S 格式谱面 IR(官方格式,RESOLUTION=384 tick/小节)。
8-
*/
9-
public class C2sChart : BaseChart<ChuNote>, IChuChart
6+
public class ChuChart : BaseChart<ChuNote>
107
{
118
public string Title { get; set; } = "";
129
public string Artist { get; set; } = "";

chart/chu/IChuChart.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

chart/chu/SusChart.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

chart/chu/UgcChart.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

generator/chu/C2sGenerator.cs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,21 @@
1-
using System.Globalization;
21
using System.Text;
3-
using MuConvert.chart;
42
using MuConvert.generator;
53
using MuConvert.utils;
6-
using static MuConvert.utils.Alert.LEVEL;
74

85
namespace MuConvert.chu;
96

10-
/**
11-
* C2S 格式生成器。
12-
* 输入 IChuChart,内部自动转换后输出 C2S 文本。
13-
*/
14-
public class C2sGenerator : IGenerator<IChuChart>
7+
public class C2sGenerator : IGenerator<ChuChart>
158
{
169
private const int RSL = 384;
1710

18-
public (string, List<Alert>) Generate(IChuChart chart)
11+
public (string, List<Alert>) Generate(ChuChart chart)
1912
{
2013
var alerts = new List<Alert>();
21-
var c2s = ConvertToC2s(chart, alerts);
22-
var text = Serialize(c2s);
14+
var text = Serialize(chart);
2315
return (text, alerts);
2416
}
2517

26-
private static C2sChart ConvertToC2s(IChuChart chart, List<Alert> alerts)
27-
{
28-
if (chart is C2sChart c2s) return c2s;
29-
30-
if (chart is UgcChart ugc)
31-
{
32-
var result = new C2sChart
33-
{
34-
Designer = ugc.Designer,
35-
};
36-
result.BpmList.AddRange(ugc.BpmList);
37-
result.MetList.AddRange(ugc.MetList);
38-
result.SflList.AddRange(ugc.SflList);
39-
result.Notes = ugc.Notes;
40-
return result;
41-
}
42-
43-
if (chart is SusChart sus)
44-
{
45-
var result = new C2sChart();
46-
if (sus.BpmList.Count > 0)
47-
result.BpmList.AddRange(sus.BpmList);
48-
else
49-
result.BpmList.Add(new BPM(0, 120m));
50-
result.Notes = sus.Notes;
51-
return result;
52-
}
53-
54-
alerts.Add(new Alert(Error, string.Format(Locale.ChuGeneratorUnsupported, "→ C2S")));
55-
throw new ConversionException(alerts);
56-
}
57-
58-
private static string Serialize(C2sChart chart)
18+
private static string Serialize(ChuChart chart)
5919
{
6020
chart.Sort();
6121

generator/chu/SusGenerator.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,21 @@
11
using System.Text;
2-
using MuConvert.chart;
32
using MuConvert.generator;
43
using MuConvert.utils;
5-
using static MuConvert.utils.Alert.LEVEL;
64

75
namespace MuConvert.chu;
86

9-
/**
10-
* SUS 格式生成器。
11-
* 输入 IChuChart,内部自动转换后输出 SUS 文本。
12-
*/
13-
public class SusGenerator : IGenerator<IChuChart>
7+
public class SusGenerator : IGenerator<ChuChart>
148
{
159
private static int RSL = 480 * 4;
1610

17-
public (string, List<Alert>) Generate(IChuChart chart)
11+
public (string, List<Alert>) Generate(ChuChart chart)
1812
{
1913
var alerts = new List<Alert>();
20-
var sus = ConvertToSus(chart, alerts);
21-
var text = Serialize(sus);
14+
var text = Serialize(chart);
2215
return (text, alerts);
2316
}
2417

25-
private static SusChart ConvertToSus(IChuChart chart, List<Alert> alerts)
26-
{
27-
if (chart is SusChart sus) return sus;
28-
29-
double bpm = 120.0;
30-
string title = "", artist = "";
31-
32-
if (chart is C2sChart c2s)
33-
{
34-
bpm = c2s.BpmList.Count > 0 ? (double)c2s.BpmList[0].Bpm : 120.0;
35-
var result = new SusChart { Title = title, Artist = artist };
36-
result.BpmList.Add(new BPM(0, (decimal)bpm));
37-
result.Notes = c2s.Notes;
38-
return result;
39-
}
40-
41-
if (chart is UgcChart ugc)
42-
{
43-
bpm = ugc.BpmList.Count > 0 ? (double)ugc.BpmList[0].Bpm : 120.0;
44-
var result = new SusChart { Title = ugc.Title, Artist = ugc.Artist };
45-
result.BpmList.Add(new BPM(0, (decimal)bpm));
46-
result.Notes = ugc.Notes;
47-
return result;
48-
}
49-
50-
alerts.Add(new Alert(Error, string.Format(Locale.ChuGeneratorUnsupported, "→ SUS")));
51-
throw new ConversionException(alerts);
52-
}
53-
54-
private static string Serialize(SusChart sus)
18+
private static string Serialize(ChuChart sus)
5519
{
5620
sus.Sort();
5721

generator/chu/UgcGenerator.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,21 @@
11
using System.Text;
22
using MuConvert.generator;
33
using MuConvert.utils;
4-
using static MuConvert.utils.Alert.LEVEL;
54

65
namespace MuConvert.chu;
76

8-
/**
9-
* UGC 格式生成器。
10-
* 输入 IChuChart,内部自动转换后输出 UGC 文本。
11-
*/
12-
public class UgcGenerator : IGenerator<IChuChart>
7+
public class UgcGenerator : IGenerator<ChuChart>
138
{
149
private static int RSL = 480 * 4;
1510

16-
public (string, List<Alert>) Generate(IChuChart chart)
11+
public (string, List<Alert>) Generate(ChuChart chart)
1712
{
1813
var alerts = new List<Alert>();
19-
var ugc = ConvertToUgc(chart, alerts);
20-
var text = Serialize(ugc);
14+
var text = Serialize(chart);
2115
return (text, alerts);
2216
}
2317

24-
private static UgcChart ConvertToUgc(IChuChart chart, List<Alert> alerts)
25-
{
26-
if (chart is UgcChart ugc) return ugc;
27-
28-
if (chart is C2sChart c2s)
29-
{
30-
var result = new UgcChart
31-
{
32-
Designer = c2s.Designer,
33-
Difficulty = c2s.Difficulty,
34-
MusicId = c2s.MusicId,
35-
};
36-
result.BpmList.AddRange(c2s.BpmList);
37-
result.MetList.AddRange(c2s.MetList);
38-
result.SflList.AddRange(c2s.SflList);
39-
result.Notes = c2s.Notes;
40-
return result;
41-
}
42-
43-
alerts.Add(new Alert(Error, string.Format(Locale.ChuGeneratorUnsupported, "→ UGC")));
44-
throw new ConversionException(alerts);
45-
}
46-
47-
private static string Serialize(UgcChart ugc)
18+
private static string Serialize(ChuChart ugc)
4819
{
4920
ugc.Sort();
5021

parser/chu/C2sParser.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ namespace MuConvert.chu;
1111
* C2S 格式解析器(官方格式,RESOLUTION=384 tick/小节)。
1212
* Tab 分隔文本,识别 HEADER / TIMING / NOTES 区段。
1313
*/
14-
public class C2sParser : IParser<C2sChart>
14+
public class C2sParser : IParser<ChuChart>
1515
{
1616
private static int RSL = 384;
1717
private static readonly HashSet<string> HeadTags = new(StringComparer.OrdinalIgnoreCase)
1818
{ "VERSION", "MUSIC", "SEQUENCEID", "DIFFICULT", "LEVEL", "CREATOR", "BPM_DEF", "MET_DEF", "RESOLUTION", "CLK_DEF", "PROGJUDGE_BPM", "PROGJUDGE_AER", "TUTORIAL" };
1919
private static readonly HashSet<string> TimingTags = new(StringComparer.OrdinalIgnoreCase)
2020
{ "BPM", "MET", "SFL" };
2121

22-
public (C2sChart, List<Alert>) Parse(string text)
22+
public (ChuChart, List<Alert>) Parse(string text)
2323
{
24-
var chart = new C2sChart();
24+
var chart = new ChuChart();
2525
var alerts = new List<Alert>();
2626
var lines = text.Replace("\r\n", "\n").Split('\n');
2727
bool inNotes = false;
@@ -54,7 +54,7 @@ public class C2sParser : IParser<C2sChart>
5454
return (chart, alerts);
5555
}
5656

57-
private static void ParseHeader(string[] p, C2sChart chart)
57+
private static void ParseHeader(string[] p, ChuChart chart)
5858
{
5959
var tag = p[0].ToUpperInvariant();
6060
switch (tag)
@@ -66,7 +66,7 @@ private static void ParseHeader(string[] p, C2sChart chart)
6666
}
6767
}
6868

69-
private static void ParseTiming(string[] p, C2sChart chart)
69+
private static void ParseTiming(string[] p, ChuChart chart)
7070
{
7171
var tag = p[0].ToUpperInvariant();
7272
switch (tag)
@@ -86,7 +86,7 @@ private static void ParseTiming(string[] p, C2sChart chart)
8686
}
8787
}
8888

89-
private static void ParseNote(string[] p, C2sChart chart, List<Alert> alerts, int lineNum)
89+
private static void ParseNote(string[] p, ChuChart chart, List<Alert> alerts, int lineNum)
9090
{
9191
var tag = p[0].ToUpperInvariant();
9292
var note = new ChuNote { Type = tag, Time = Int(p, 1) + new Rational(Int(p, 2), RSL) };

0 commit comments

Comments
 (0)