-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConverter.cs
91 lines (72 loc) · 3.04 KB
/
Converter.cs
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
using System.IO;
using System.Linq;
using MaiLib;
using Microsoft.Extensions.Logging;
namespace Sitreamai;
public partial class Converter
{
private ILogger Logger { get; }
public int MusicId { get; }
public string FromDir { get; }
public string AssetsDir { get; }
public string MusicPadId => MusicId.ToString().PadLeft(6, '0');
public int MusicIdDx => IsDx ? MusicId + 10000 : MusicId;
public string MusicPadIdDx => MusicIdDx.ToString().PadLeft(6, '0');
public SimaiTokenizer SimaiTokenizer = new();
public bool IsDx { get; }
public double Bpm { get; }
public string MusicDir => Path.Join(AssetsDir, "music", $"music{MusicPadIdDx}");
public MusicXmlConverter XmlConverter{ private get; init; } = new();
public Converter(int musicId, string fromDir, string assetsDir)
{
MusicId = musicId;
FromDir = fromDir;
AssetsDir = assetsDir;
SimaiTokenizer.UpdateFromPath(Path.Join(FromDir, "maidata.txt"));
var parser = new SimaiParser();
var candidate = parser.ChartOfToken(SimaiTokenizer.ChartCandidates.Values.First());
IsDx = SimaiTokenizer.SimaiTrackInformation.IsDXChart || candidate.IsDxChart;
Bpm = double.TryParse(SimaiTokenizer.SimaiTrackInformation.TrackBPM, out var bpm) ? bpm : candidate.BPMChanges.ChangeNotes[0].BPM;
using var factory = LoggerFactory.Create(builder => builder.AddConsole());
Logger = factory.CreateLogger($"Converter - {SimaiTokenizer.SimaiTrackInformation.TrackName}({MusicId})");
}
public void Convert()
{
Logger.LogInformation("开始转换");
// 写出歌曲 XML
var xml = XmlConverter.Convert(this);
if (!Directory.Exists(MusicDir))
{
Directory.CreateDirectory(MusicDir);
}
File.WriteAllText(Path.Join(MusicDir, "Music.xml"), xml);
// 转换谱面
foreach (var (level, tokensCandidates) in SimaiTokenizer.ChartCandidates)
{
var parser = new SimaiParser();
var candidate = parser.ChartOfToken(tokensCandidates);
var converted = new Ma2(candidate) { ChartVersion = ChartEnum.ChartVersion.Ma2_104 };
var result = converted.Compose();
File.WriteAllText(Path.Join(MusicDir, $"{MusicPadIdDx}_0{int.Parse(level) - 2}.ma2"), result);
}
// 找到图片
foreach (var ext in new[] { ".jpg", ".png", ".webp", ".bmp", ".gif" })
{
var imgPath = Path.Join(FromDir, "bg" + ext);
if (File.Exists(imgPath))
{
File.Copy(imgPath, Path.Join(AssetsDir, "LocalAssets", MusicPadId + ext), true);
}
}
// 找到音频
foreach (var ext in new[] { ".mp3", ".wav", ".aac" })
{
var mp3Path = Path.Join(FromDir, "track" + ext);
if (File.Exists(mp3Path))
{
Audio.ConvertToMai(mp3Path, Path.Join(AssetsDir, "SoundData", $"music{MusicPadId}"));
}
}
Logger.LogInformation("转换完成");
}
}