Skip to content

Commit

Permalink
Merge pull request #230 from andy840119/meow/import-file
Browse files Browse the repository at this point in the history
Implement drag .lrc/.kar file to import.
  • Loading branch information
andy840119 authored Oct 27, 2020
2 parents 266fcff + d844fae commit 438bfcd
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public TestKaraokeBeatmap(RulesetInfo ruleset)
List = "https://assets.ppy.sh/beatmaps/163112/covers/list.jpg"
}
};
BeatmapInfo.BeatmapSet.Files = new List<BeatmapSetFileInfo>();
}

private static Beatmap createTestBeatmap()
Expand Down
37 changes: 35 additions & 2 deletions osu.Game.Rulesets.Karaoke.Tests/Edit/TestSceneLyricEditorScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@

using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
using osu.Game.Overlays;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Karaoke.Beatmaps.Formats;
using osu.Game.Rulesets.Karaoke.Edit.LyricEditor;
using osu.Game.Rulesets.Karaoke.Tests.Beatmaps;
using osu.Game.Rulesets.Karaoke.Tests.Resources;
using osu.Game.Screens.Edit;
using osu.Game.Tests.Visual;
using System.IO;

namespace osu.Game.Rulesets.Karaoke.Tests.Edit
{
[TestFixture]
[Ignore("Will fail if run multiple tests. No idea how to fix that.")]
[Ignore("This test case run failed in appveyor : (")]
public class TestSceneLyricEditorScreen : EditorClockTestScene
{
protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };

private DialogOverlay dialogOverlay;

private LyricEditorScreen screen;

public TestSceneLyricEditorScreen()
{
// It's a tricky to let osu! to read karaoke testing beatmap
Expand All @@ -40,7 +51,29 @@ private void load()
Dependencies.CacheAs(editorBeatmap);
Dependencies.CacheAs<IBeatSnapProvider>(editorBeatmap);

Child = new LyricEditorScreen();
base.Content.AddRange(new Drawable[]
{
Content,
dialogOverlay = new DialogOverlay()
});

Dependencies.Cache(dialogOverlay);
}

[SetUp]
public void SetUp() => Schedule(() =>
{
Child = screen = new LyricEditorScreen();
});

[Test]
public void TestImportLyricFile()
{
AddAssert($"Import lrc file.", () =>
{
var temp = TestResources.GetTestLrcForImport("default");
return screen.ImportLyricFile(new FileInfo(temp));
});
}
}
}
13 changes: 13 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Resources/TestResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.IO;
using NUnit.Framework;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.IO.Stores;
Expand All @@ -20,6 +21,18 @@ public static class TestResources

public static Stream OpenLrcResource(string name) => OpenResource($"Testing/Lrc/{name}.lrc");

public static string GetTestLrcForImport(string name)
{
var tempPath = Path.GetTempFileName() + ".lrc";

using (var stream = OpenLrcResource(name))
using (var newFile = File.Create(tempPath))
stream.CopyTo(newFile);

Assert.IsTrue(File.Exists(tempPath));
return tempPath;
}

public static Stream OpenNicoKaraResource(string name) => OpenResource($"Testing/NicoKara/{name}.nkmproj");

public static Stream OpenTrackResource(string name) => OpenResource($"Testing/Track/{name}.mp3");
Expand Down
32 changes: 32 additions & 0 deletions osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Graphics.Sprites;
using osu.Game.Overlays.Dialog;
using System;

namespace osu.Game.Rulesets.Karaoke.Edit.LyricEditor
{
public class ImportLyricDialog : PopupDialog
{
public ImportLyricDialog(Action resetAction)
{
BodyText = "Import lyric file will clean-up all exist lyric.";

Icon = FontAwesome.Regular.TrashAlt;
HeaderText = @"Confirm import lyric file?";
Buttons = new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = @"Yes. Go for it.",
Action = resetAction
},
new PopupDialogCancelButton
{
Text = @"No! Abort mission!",
},
};
}
}
}
70 changes: 69 additions & 1 deletion osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,98 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Rulesets.Karaoke.Beatmaps.Formats;
using osu.Game.Rulesets.Karaoke.Edit.Timelines;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit;
using osu.Game.Skinning;
using osuTK;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace osu.Game.Rulesets.Karaoke.Edit.LyricEditor
{
public class LyricEditorScreen : EditorScreenWithTimeline
public class LyricEditorScreen : EditorScreenWithTimeline, ICanAcceptFiles
{
private const string backup_lrc_name = "backup.lrc";

private KaraokeLyricEditorSkin skin;
private FillFlowContainer<Button> controls;
private LyricRearrangeableListContainer container;

public IEnumerable<string> HandledExtensions => LyricFotmatExtensions;

public static string[] LyricFotmatExtensions { get; } = { ".lrc", ".kar" };

[Resolved]
private EditorBeatmap beatmap { get; set; }

[Resolved]
private BeatmapManager beatmaps { get; set; }

[Resolved(CanBeNull = true)]
private DialogOverlay dialogOverlay { get; set; }

public LyricEditorScreen()
: base(EditorScreenMode.Compose)
{
}

Task ICanAcceptFiles.Import(params string[] paths)
{
Schedule(() =>
{
var firstFile = new FileInfo(paths.First());

if (LyricFotmatExtensions.Contains(firstFile.Extension))
{
// Import lyric file
ImportLyricFile(firstFile);
}
});
return Task.CompletedTask;
}

public bool ImportLyricFile(FileInfo info)
{
if (!info.Exists)
return false;

var set = Beatmap.Value.BeatmapSetInfo;
var oldFile = set.Files?.FirstOrDefault(f => f.Filename == backup_lrc_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);
else
beatmaps.AddFile(set, stream, backup_lrc_name);
*/

// Import and replace all the file.
using (var reader = new IO.LineBufferedReader(stream))
{
var decoder = new LrcDecoder();
var lrcBeatmap = decoder.Decode(reader);

dialogOverlay?.Push(new ImportLyricDialog(()=> {
// todo : replace all the lyric object.
}));
}
}

return true;
}

protected override Drawable CreateTimelineContent() => new KaraokeTimelineBlueprintContainer();

protected override Drawable CreateMainContent()
Expand Down

0 comments on commit 438bfcd

Please sign in to comment.