Skip to content

Commit 6b28e0a

Browse files
committed
fix #24
5f800482448b84c106db8e3a1c4a7c1c521fa34c에서 지운 가사 받기 취소 기능 재구현
1 parent 718b34e commit 6b28e0a

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

osu!Lyrics/Lyrics.cs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using System.Windows.Forms;
1414
using osu_Lyrics.Properties;
15+
using System.Threading;
1516

1617
namespace osu_Lyrics
1718
{
@@ -226,7 +227,7 @@ private static double Now()
226227
/// </summary>
227228
/// <param name="data">[HASH]: ... | [ARTIST]: ..., [TITLE]: ...</param>
228229
/// <returns>List&lt;string&gt;</returns>
229-
private static async Task<List<Lyric>> GetLyrics(IDictionary<string, string> data)
230+
private static async Task<List<Lyric>> GetLyricsAsync(IDictionary<string, string> data)
230231
{
231232
try
232233
{
@@ -310,7 +311,7 @@ private List<Lyric> lyricsCache
310311
}
311312
}
312313

313-
private async void Osu_Signal(string line)
314+
private void Osu_Signal(string line)
314315
{
315316
var data = line.Split('|');
316317
if (data.Length != 5)
@@ -321,27 +322,50 @@ private async void Osu_Signal(string line)
321322
// 재생 중인 곡이 바꼈다!
322323
if (data[1] != curAudio.Path)
323324
{
324-
lyricsCache = new List<Lyric>
325-
{
326-
new Lyric(0, "가사 받는 중...")
327-
};
328-
329325
curAudio = new Audio(data[1], data[4]);
326+
UpdateLyrics();
327+
}
328+
curTime = DateTimeOffset.Now.Subtract(
329+
DateTimeOffset.FromFileTime(Convert.ToInt64(data[0], 16))
330+
).TotalSeconds + Convert.ToDouble(data[2]);
331+
_playbackRate = 1 + Convert.ToDouble(data[3]) / 100;
332+
}
333+
334+
private CancellationTokenSource cts;
335+
336+
private void UpdateLyrics()
337+
{
338+
if (cts != null)
339+
{
340+
cts.Cancel();
341+
return;
342+
}
330343

344+
lyricsCache = new List<Lyric>
345+
{
346+
new Lyric(0, "가사 받는 중...")
347+
};
348+
cts = new CancellationTokenSource();
349+
Task.Run(async () =>
350+
{
351+
cts.Token.ThrowIfCancellationRequested();
331352
// 파일 해시로 가사 검색
332-
var newLyrics = await GetLyrics(new Dictionary<string, string>
353+
var newLyrics = await GetLyricsAsync(new Dictionary<string, string>
333354
{
334355
{ "[HASH]", curAudio.Hash }
335356
});
357+
336358
if (newLyrics == null && curAudio.Beatmap != null)
337359
{
360+
cts.Token.ThrowIfCancellationRequested();
338361
// 음악 정보로 가사 검색
339-
newLyrics = await GetLyrics(new Dictionary<string, string>
362+
newLyrics = await GetLyricsAsync(new Dictionary<string, string>
340363
{
341364
{ "[TITLE]", curAudio.Beatmap.TitleUnicode ?? curAudio.Beatmap.Title },
342365
{ "[ARTIST]", curAudio.Beatmap.ArtistUnicode ?? curAudio.Beatmap.Artist }
343366
});
344367
}
368+
345369
if (newLyrics != null)
346370
{
347371
newLyrics.Insert(0, new Lyric());
@@ -354,12 +378,19 @@ private async void Osu_Signal(string line)
354378
};
355379
}
356380

357-
lyricsCache = newLyrics;
358-
}
359-
curTime = DateTimeOffset.Now.Subtract(
360-
DateTimeOffset.FromFileTime(Convert.ToInt64(data[0], 16))
361-
).TotalSeconds + Convert.ToDouble(data[2]);
362-
_playbackRate = 1 + Convert.ToDouble(data[3]) / 100;
381+
cts.Token.ThrowIfCancellationRequested();
382+
Invoke(new MethodInvoker(() =>
383+
{
384+
lyricsCache = newLyrics;
385+
}));
386+
}, cts.Token).ContinueWith(result => Invoke(new MethodInvoker(() =>
387+
{
388+
cts = null;
389+
if (result.IsCanceled)
390+
{
391+
UpdateLyrics();
392+
}
393+
})));
363394
}
364395

365396

0 commit comments

Comments
 (0)