From 2e116b1ad396a94f02bee4be5498a36d6db93512 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 10 Aug 2024 00:05:10 +0800 Subject: [PATCH] Use KarEncoder/KarDecoder instead. Also, rename some naming from .lrc to .kar --- .../KaraokeLegacyBeatmapDecoderTest.cs | 4 +-- .../Helper/TestCaseTagHelper.cs | 17 ++------- .../Edit/Beatmap/TestSceneEditorMenuBar.cs | 4 +-- .../Formats/KaraokeLegacyBeatmapDecoder.cs | 35 +++++++------------ .../Formats/KaraokeLegacyBeatmapEncoder.cs | 5 +-- .../Edit/Export/ExportLyricManager.cs | 10 +++--- .../Edit/Beatmaps/KaraokeBeatmapEditor.cs | 4 +-- .../Lyrics/DragFile/DragFileStepScreen.cs | 2 +- .../Edit/Import/Lyrics/ImportLyricManager.cs | 32 +++++++++++------ 9 files changed, 53 insertions(+), 60 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke.Tests/Beatmaps/Formats/KaraokeLegacyBeatmapDecoderTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Beatmaps/Formats/KaraokeLegacyBeatmapDecoderTest.cs index 52fb001b1..22a1f91a4 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Beatmaps/Formats/KaraokeLegacyBeatmapDecoderTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Beatmaps/Formats/KaraokeLegacyBeatmapDecoderTest.cs @@ -113,10 +113,10 @@ private static KaraokeBeatmap decodeBeatmap(string fileName) using var stream = new LineBufferedReader(resStream); // Create karaoke beatmap decoder - var lrcDecoder = new KaraokeLegacyBeatmapDecoder(); + var decoder = new KaraokeLegacyBeatmapDecoder(); // Create initial beatmap - var beatmap = lrcDecoder.Decode(stream); + var beatmap = decoder.Decode(stream); // Convert to karaoke beatmap return (KaraokeBeatmap)new KaraokeBeatmapConverter(beatmap, new KaraokeRuleset()).Convert(); diff --git a/osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs b/osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs index c1b62837f..1ae610cac 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs @@ -3,15 +3,13 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Text.RegularExpressions; using osu.Framework.Graphics.Sprites; -using osu.Game.IO; using osu.Game.Rulesets.Karaoke.Beatmaps; -using osu.Game.Rulesets.Karaoke.Beatmaps.Formats; using osu.Game.Rulesets.Karaoke.Beatmaps.Metadatas; using osu.Game.Rulesets.Karaoke.Extensions; +using osu.Game.Rulesets.Karaoke.Integration.Formats; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Tests.Extensions; @@ -203,18 +201,9 @@ public static Lyric ParseLyricWithTimeTag(string? str) if (string.IsNullOrEmpty(str)) return new Lyric(); - using var stream = new MemoryStream(); - using var writer = new StreamWriter(stream); - using var reader = new LineBufferedReader(stream); - - // Create stream - writer.Write(str); - writer.Flush(); - stream.Position = 0; - // Create karaoke note decoder - var decoder = new LrcDecoder(); - return decoder.Decode(reader).HitObjects.OfType().First(); + var decoder = new KarDecoder(); + return decoder.Decode(str).First(); } /// diff --git a/osu.Game.Rulesets.Karaoke.Tests/Screens/Edit/Beatmap/TestSceneEditorMenuBar.cs b/osu.Game.Rulesets.Karaoke.Tests/Screens/Edit/Beatmap/TestSceneEditorMenuBar.cs index bcf3b8544..48e4dd1ab 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Screens/Edit/Beatmap/TestSceneEditorMenuBar.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Screens/Edit/Beatmap/TestSceneEditorMenuBar.cs @@ -45,9 +45,9 @@ private void load() Items = new MenuItem[] { new ImportLyricMenu(null, "Import from text", null!), - new ImportLyricMenu(null, "Import from .lrc file", null!), + new ImportLyricMenu(null, "Import from .kar file", null!), new OsuMenuItemSpacer(), - new EditorMenuItem("Export to .lrc", MenuItemType.Standard, () => { }), + new EditorMenuItem("Export to .kar", MenuItemType.Standard, () => { }), new EditorMenuItem("Export to text", MenuItemType.Standard, () => { }), new EditorMenuItem("Export to json", MenuItemType.Destructive, () => { }), }, diff --git a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs index 216f29944..98c17ef0c 100644 --- a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs +++ b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs @@ -4,13 +4,13 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Linq; using osu.Game.Beatmaps; using osu.Game.Beatmaps.Formats; -using osu.Game.IO; using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Notes; +using osu.Game.Rulesets.Karaoke.Integration.Formats; using osu.Game.Rulesets.Karaoke.Objects; +using osu.Game.Rulesets.Objects; namespace osu.Game.Rulesets.Karaoke.Beatmaps.Formats; @@ -31,7 +31,7 @@ public KaraokeLegacyBeatmapDecoder(int version = LATEST_VERSION) { } - private readonly IList lrcLines = new List(); + private readonly IList karFormatLines = new List(); private readonly IList noteLines = new List(); private readonly IList translations = new List(); @@ -52,8 +52,8 @@ protected override void ParseLine(Beatmap beatmap, Section section, string line) if (line.ToLower().StartsWith("@ruby", StringComparison.Ordinal)) { - // lrc queue - lrcLines.Add(line); + // kar format queue + karFormatLines.Add(line); } else if (line.ToLower().StartsWith("@note", StringComparison.Ordinal)) { @@ -67,28 +67,19 @@ protected override void ParseLine(Beatmap beatmap, Section section, string line) } else if (line.StartsWith('@')) { - // Remove @ in time tag and add into lrc queue - lrcLines.Add(line[1..]); + // Remove @ in time tag and add into kar queue + karFormatLines.Add(line[1..]); } else if (line.ToLower() == "end") { - // Start processing file - using (var stream = new MemoryStream()) - using (var writer = new StreamWriter(stream)) - using (var reader = new LineBufferedReader(stream)) - { - // Create stream - writer.Write(string.Join("\n", lrcLines)); - writer.Flush(); - stream.Position = 0; + string content = string.Join("\n", karFormatLines); - // Create lrc decoder - var decoder = new LrcDecoder(); - var lrcBeatmap = decoder.Decode(reader); + // Create decoder + var decoder = new KarDecoder(); + var lyrics = decoder.Decode(content); - // Apply hitobjects - beatmap.HitObjects = lrcBeatmap.HitObjects; - } + // Apply hitobjects + beatmap.HitObjects = lyrics.OfType().ToList(); processNotes(beatmap, noteLines); processTranslations(beatmap, translations); diff --git a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapEncoder.cs b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapEncoder.cs index cd2efa9dc..526399017 100644 --- a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapEncoder.cs +++ b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapEncoder.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using osu.Game.Beatmaps; +using osu.Game.Rulesets.Karaoke.Integration.Formats; using osu.Game.Rulesets.Karaoke.Objects; namespace osu.Game.Rulesets.Karaoke.Beatmaps.Formats; @@ -13,10 +14,10 @@ public class KaraokeLegacyBeatmapEncoder { public string Encode(Beatmap output) { - var lrcEncoder = new LrcEncoder(); + var encoder = new KarEncoder(); var results = new List { - lrcEncoder.Encode(output), + encoder.Encode(output), string.Join("\n", encodeNotes(output)), string.Join("\n", encodeTranslations(output)), }; diff --git a/osu.Game.Rulesets.Karaoke/Edit/Export/ExportLyricManager.cs b/osu.Game.Rulesets.Karaoke/Edit/Export/ExportLyricManager.cs index 89cd8eced..eefdce9ae 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Export/ExportLyricManager.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Export/ExportLyricManager.cs @@ -7,7 +7,7 @@ using osu.Framework.Graphics; using osu.Framework.Platform; using osu.Game.Beatmaps; -using osu.Game.Rulesets.Karaoke.Beatmaps.Formats; +using osu.Game.Rulesets.Karaoke.Integration.Formats; using osu.Game.Screens.Edit; namespace osu.Game.Rulesets.Karaoke.Edit.Export; @@ -20,15 +20,15 @@ public partial class ExportLyricManager : Component [Resolved] private EditorBeatmap beatmap { get; set; } = null!; - public void ExportToLrc() + public void ExportToKar() { - var exportStorage = storage.GetStorageForDirectory("lrc"); - string filename = $"{beatmap.Name}.lrc"; + var exportStorage = storage.GetStorageForDirectory("kar"); + string filename = $"{beatmap.Name}.kar"; using (var outputStream = exportStorage.GetStream(filename, FileAccess.Write, FileMode.Create)) using (var sw = new StreamWriter(outputStream)) { - var encoder = new LrcEncoder(); + var encoder = new KarEncoder(); sw.WriteLine(encoder.Encode(new Beatmap { HitObjects = beatmap.HitObjects.ToList(), diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/KaraokeBeatmapEditor.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/KaraokeBeatmapEditor.cs index ab6bddfb4..cf3ed4451 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/KaraokeBeatmapEditor.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/KaraokeBeatmapEditor.cs @@ -102,9 +102,9 @@ protected override MenuItem[] GenerateMenuItems(KaraokeBeatmapEditorScreenMode s Items = new MenuItem[] { new ImportLyricMenu(this, "Import from text", importBeatmapChangeHandler), - new ImportLyricMenu(this, "Import from .lrc file", importBeatmapChangeHandler), + new ImportLyricMenu(this, "Import from .kar file", importBeatmapChangeHandler), new OsuMenuItemSpacer(), - new EditorMenuItem("Export to .lrc", MenuItemType.Standard, () => exportLyricManager.ExportToLrc()), + new EditorMenuItem("Export to .kar", MenuItemType.Standard, () => exportLyricManager.ExportToKar()), new EditorMenuItem("Export to text", MenuItemType.Standard, () => exportLyricManager.ExportToText()), }, }, diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/DragFile/DragFileStepScreen.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/DragFile/DragFileStepScreen.cs index 422cbbfba..f4c4f6769 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/DragFile/DragFileStepScreen.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/DragFile/DragFileStepScreen.cs @@ -86,7 +86,7 @@ public void ImportLyricFile(FileInfo fileInfo) try { - importManager.ImportLrcFile(fileInfo); + importManager.ImportFile(fileInfo); DialogOverlay.Push(createCompleteDialog()); } catch (Exception ex) diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/ImportLyricManager.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/ImportLyricManager.cs index 4d5c144d7..1c2fc1a46 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/ImportLyricManager.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Import/Lyrics/ImportLyricManager.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.IO; using System.Linq; using osu.Framework.Allocation; @@ -8,7 +9,7 @@ using osu.Framework.Graphics; using osu.Game.Beatmaps; using osu.Game.IO; -using osu.Game.Rulesets.Karaoke.Beatmaps.Formats; +using osu.Game.Rulesets.Karaoke.Integration.Formats; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Screens.Edit; using FileInfo = System.IO.FileInfo; @@ -19,7 +20,7 @@ public partial class ImportLyricManager : Component { public static string[] LyricFormatExtensions { get; } = { ".lrc", ".kar", ".txt" }; - private const string backup_lrc_name = "backup.lrc"; + private const string backup_file_name = "backup"; [Resolved] private EditorBeatmap editorBeatmap { get; set; } = null!; @@ -27,7 +28,7 @@ public partial class ImportLyricManager : Component [Resolved] private IBindable beatmap { get; set; } = null!; - public void ImportLrcFile(FileInfo info) + public void ImportFile(FileInfo info) { if (!info.Exists) throw new FileNotFoundException("Lyric file does not found!"); @@ -37,32 +38,43 @@ public void ImportLrcFile(FileInfo info) throw new FileLoadException("Only .lrc or .kar karaoke file is supported now"); var set = beatmap.Value.BeatmapSetInfo; - var oldFile = set.Files.FirstOrDefault(f => f.Filename == backup_lrc_name); + var oldFile = set.Files.FirstOrDefault(f => f.Filename == backup_file_name); using var stream = info.OpenRead(); // todo : make a backup if has new lyric file. /* if (oldFile != null) - beatmaps.ReplaceFile(set, oldFile, stream, backup_lrc_name); + beatmaps.ReplaceFile(set, oldFile, stream, backup_file_name); else - beatmaps.AddFile(set, stream, backup_lrc_name); + beatmaps.AddFile(set, stream, backup_file_name); */ // Import and replace all the file. using var reader = new LineBufferedReader(stream); - - var decoder = new LrcDecoder(); - var lrcBeatmap = decoder.Decode(reader); + string content = reader.ReadToEnd(); + var lyrics = decodeLyrics(content, info.Extension); // remove all hit objects (note and lyric) from beatmap editorBeatmap.Clear(); // then re-add the lyric. - var lyrics = lrcBeatmap.HitObjects.OfType(); editorBeatmap.AddRange(lyrics); } + private static Lyric[] decodeLyrics(string content, string extension) + { + IDecoder decoder = extension switch + { + ".lrc" => new LrcDecoder(), + ".kar" => new KarDecoder(), + ".txt" => new LyricTextDecoder(), + _ => throw new NotSupportedException("Unsupported lyric file format"), + }; + + return decoder.Decode(content); + } + public void AbortImport() { editorBeatmap.Clear();