Skip to content

Commit

Permalink
Use KarEncoder/KarDecoder instead.
Browse files Browse the repository at this point in the history
Also, rename some naming from .lrc to .kar
  • Loading branch information
andy840119 committed Aug 10, 2024
1 parent f9823b1 commit 2e116b1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 3 additions & 14 deletions osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Lyric>().First();
var decoder = new KarDecoder();
return decoder.Decode(str).First();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => { }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +31,7 @@ public KaraokeLegacyBeatmapDecoder(int version = LATEST_VERSION)
{
}

private readonly IList<string> lrcLines = new List<string>();
private readonly IList<string> karFormatLines = new List<string>();
private readonly IList<string> noteLines = new List<string>();
private readonly IList<string> translations = new List<string>();

Expand All @@ -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))
{
Expand All @@ -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<HitObject>().ToList();

processNotes(beatmap, noteLines);
processTranslations(beatmap, translations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,10 +14,10 @@ public class KaraokeLegacyBeatmapEncoder
{
public string Encode(Beatmap output)
{
var lrcEncoder = new LrcEncoder();
var encoder = new KarEncoder();
var results = new List<string>
{
lrcEncoder.Encode(output),
encoder.Encode(output),
string.Join("\n", encodeNotes(output)),
string.Join("\n", encodeTranslations(output)),
};
Expand Down
10 changes: 5 additions & 5 deletions osu.Game.Rulesets.Karaoke/Edit/Export/ExportLyricManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void ImportLyricFile(FileInfo fileInfo)

try
{
importManager.ImportLrcFile(fileInfo);
importManager.ImportFile(fileInfo);
DialogOverlay.Push(createCompleteDialog());
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) andy840119 <[email protected]>. 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;
using osu.Framework.Bindables;
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;
Expand All @@ -19,15 +20,15 @@ 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!;

[Resolved]
private IBindable<WorkingBeatmap> 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!");
Expand All @@ -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<Lyric>();
editorBeatmap.AddRange(lyrics);
}

private static Lyric[] decodeLyrics(string content, string extension)
{
IDecoder<Lyric[]> 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();
Expand Down

0 comments on commit 2e116b1

Please sign in to comment.