Skip to content

Commit dfa4a3d

Browse files
committed
优化
1 parent 690ecfd commit dfa4a3d

File tree

2 files changed

+58
-73
lines changed

2 files changed

+58
-73
lines changed

AimpLyricsPlugin/LyricInfo.cs

+32-55
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,52 @@ public class LyricInfo
99
{
1010
public LyricInfo(string lrcString)
1111
{
12-
LrcLines = InitLines(lrcString);
12+
LrcLines = LrcFormat(lrcString);
1313
}
1414

15-
public static string LrcFormat(string oriLrc)
15+
public static LrcLine[] LrcFormat(string oriLrc)
1616
{
17-
if (string.IsNullOrWhiteSpace(oriLrc))
18-
return string.Empty;
19-
var lines = oriLrc.Split('\n').Where(line => line.StartsWith("["));
20-
var offset = new TimeSpan();
21-
var os = lines.FirstOrDefault(o => Regex.IsMatch(o, @"\[offset:[-\+]?\d+\]"));
22-
if (os != null)
23-
offset = TimeSpan.FromMilliseconds(int.Parse(Regex.Match(os, @"-?\d+").Value));
24-
var list = new List<LrcLine>();
25-
foreach (var item in lines)
17+
try
2618
{
27-
var matches = Regex.Matches(item, @"\[(\d{1,2}):(\d{1,2})([\.:](\d{1,3}))?\]").Cast<Match>();
28-
if (matches.Count() == 0)
29-
continue;
30-
var content = item.Substring(matches.Sum(m => m.Value.Length)).Trim();
31-
foreach (Match m in matches)
19+
if (string.IsNullOrWhiteSpace(oriLrc))
20+
return null;
21+
var lines = oriLrc.Split('\n').Where(line => line.StartsWith("["));
22+
var offset = new TimeSpan();
23+
var os = lines.FirstOrDefault(o => Regex.IsMatch(o, @"\[offset:[-\+]?\d+\]"));
24+
if (os != null)
25+
offset = TimeSpan.FromMilliseconds(int.Parse(Regex.Match(os, @"-?\d+").Value));
26+
var list = new List<LrcLine>();
27+
foreach (var item in lines)
3228
{
33-
var s = $"0:{m.Groups[1].Value}:{m.Groups[2].Value}";
34-
if (m.Groups[4].Success)
35-
s = $"{s}.{m.Groups[4].Value}";
36-
list.Add(new LrcLine()
29+
var matches = Regex.Matches(item, @"\[(\d{1,2}):(\d{1,2})([\.:](\d{1,3}))?\]").Cast<Match>();
30+
if (matches.Count() == 0)
31+
continue;
32+
var content = item.Substring(matches.Sum(m => m.Value.Length)).Trim();
33+
foreach (Match m in matches)
3734
{
38-
Content = content,
39-
TimePoint = TimeSpan.Parse(s).Add(offset).TotalSeconds,
40-
});
35+
var s = $"0:{m.Groups[1].Value}:{m.Groups[2].Value}";
36+
if (m.Groups[4].Success)
37+
s = $"{s}.{m.Groups[4].Value}";
38+
list.Add(new LrcLine()
39+
{
40+
Content = content,
41+
TimePoint = TimeSpan.Parse(s).Add(offset).TotalSeconds,
42+
});
43+
}
4144
}
42-
}
43-
return string.Join("\n", list.OrderBy(lrc => lrc.TimePoint));
44-
}
45-
46-
public LrcLine[] LrcLines { get; set; }
47-
48-
public string Lyric => string.Join("\n", LrcLines.AsEnumerable());
49-
50-
private LrcLine[] InitLines(string lrcString)
51-
{
52-
if (string.IsNullOrWhiteSpace(lrcString))
53-
return null;
54-
try
55-
{
56-
return ParseLine(lrcString);
45+
return list.OrderBy(lrc => lrc.TimePoint).ToArray();
5746
}
5847
catch
5948
{
60-
lrcString = LrcFormat(lrcString);
61-
return ParseLine(lrcString);
49+
return null;
6250
}
6351
}
6452

65-
private LrcLine[] ParseLine(string str)
66-
{
67-
var lines = str.Split('\n');
68-
var list = new List<LrcLine>();
69-
foreach (var item in lines)
70-
{
71-
list.Add(new LrcLine()
72-
{
73-
Content = item.Substring(10).Trim(),
74-
TimePoint = TimeSpan.Parse("00:" + item.Substring(1, 8)).TotalSeconds,
75-
});
76-
}
77-
return list.ToArray();
78-
}
53+
public LrcLine[] LrcLines { get; set; }
54+
55+
public string Lyric => string.Join("\n", LrcLines.AsEnumerable());
7956

80-
public string Seek(double sec) => LrcLines.LastOrDefault(ll => ll.TimePoint < sec)?.Content;
57+
public string Seek(double sec) => LrcLines?.LastOrDefault(ll => ll.TimePoint < sec)?.Content;
8158
}
8259

8360
public class LrcLine

AimpLyricsPlugin/LyricWindow.xaml.cs

+26-18
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,35 @@ public LyricWindow(IAimpPlayer player)
4242

4343
private void Player_TrackChanged(object sender, EventArgs e)
4444
{
45-
var fi = player.CurrentFileInfo;
46-
var file = fi?.FileName;
47-
if (file == null)
45+
try
4846
{
47+
var fi = player.CurrentFileInfo;
48+
var file = fi?.FileName;
49+
if (file == null)
50+
{
51+
lyric = null;
52+
return;
53+
}
54+
var str = "";
55+
var innerLrc = string.IsNullOrWhiteSpace(fi.Lyrics) ? null : fi.Lyrics;
56+
if (setting.Inner)
57+
{
58+
str = innerLrc ?? GetLrcString(file);
59+
}
60+
else
61+
{
62+
str = GetLrcString(file) ?? innerLrc;
63+
}
64+
if (!string.IsNullOrEmpty(str))
65+
{
66+
lyric = new LyricInfo(str);
67+
if (lyric.LrcLines != null)
68+
return;
69+
}
4970
lyric = null;
50-
return;
51-
}
52-
var str = "";
53-
if (setting.Inner)
54-
{
55-
str = fi.Lyrics ?? GetLrcString(file);
56-
}
57-
else
58-
{
59-
str = GetLrcString(file) ?? fi.Lyrics;
60-
}
61-
if (!string.IsNullOrEmpty(str))
62-
{
63-
lyric = new LyricInfo(str);
71+
ChangedText("");
6472
}
65-
else
73+
catch
6674
{
6775
lyric = null;
6876
ChangedText("");

0 commit comments

Comments
 (0)