Skip to content

Commit

Permalink
1. Import import manager
Browse files Browse the repository at this point in the history
2. Move dialog and handle some exception message.
3. Create ok dialog to show message only.
4. Add ImportManager into injection in test case.
  • Loading branch information
andy840119 committed Oct 31, 2020
1 parent 438bfcd commit 72ad04b
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using osu.Game.Overlays;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Karaoke.Beatmaps.Formats;
using osu.Game.Rulesets.Karaoke.Edit.Import;
using osu.Game.Rulesets.Karaoke.Edit.LyricEditor;
using osu.Game.Rulesets.Karaoke.Tests.Beatmaps;
using osu.Game.Rulesets.Karaoke.Tests.Resources;
Expand All @@ -28,6 +29,8 @@ public class TestSceneLyricEditorScreen : EditorClockTestScene

private LyricEditorScreen screen;

private ImportManager importManager;

public TestSceneLyricEditorScreen()
{
// It's a tricky to let osu! to read karaoke testing beatmap
Expand All @@ -54,10 +57,12 @@ private void load()
base.Content.AddRange(new Drawable[]
{
Content,
dialogOverlay = new DialogOverlay()
dialogOverlay = new DialogOverlay(),
importManager = new ImportManager()
});

Dependencies.Cache(dialogOverlay);
Dependencies.Cache(importManager);
}

[SetUp]
Expand Down
83 changes: 83 additions & 0 deletions osu.Game.Rulesets.Karaoke/Edit/Import/ImportLyricDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using System;
using System.IO;
using osu.Game.Rulesets.Karaoke.Graphics.Overlays.Dialog;

namespace osu.Game.Rulesets.Karaoke.Edit.Import
{
public class ImportLyricDialog : PopupDialog
{
[Resolved]
private ImportManager importManager { get; set; }

[Resolved]
private DialogOverlay dialogOverlay { get; set; }

public ImportLyricDialog(FileInfo info, Action<bool> resetAction = null)
{
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 = () => {
var success = processImport(info);
resetAction?.Invoke(true);
}
},
new PopupDialogCancelButton
{
Text = @"No! Abort mission!",
},
};
}

private bool processImport(FileInfo info)
{
try
{
importManager.ImportLrcFile(info);
dialogOverlay.Push(new OkPopupDialog
{
Icon = FontAwesome.Regular.CheckCircle,
HeaderText = @"Import success",
BodyText = "Lyrics has been imported."
});
return true;
}
catch (Exception ex)
{
switch (ex)
{
case FileNotFoundException fileNotFoundException:
dialogOverlay.Push(new OkPopupDialog
{
Icon = FontAwesome.Regular.QuestionCircle,
HeaderText = @"File not found",
BodyText = fileNotFoundException.Message,
});
break;
case FileLoadException loadException:
dialogOverlay.Push(new OkPopupDialog
{
Icon = FontAwesome.Regular.QuestionCircle,
HeaderText = @"File not found",
BodyText = loadException.Message,
});
break;
}
return false;
}
}
}
}
73 changes: 73 additions & 0 deletions osu.Game.Rulesets.Karaoke/Edit/Import/ImportManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Karaoke.Beatmaps.Formats;
using osu.Game.Screens.Edit;
using System.IO;
using System.Linq;

namespace osu.Game.Rulesets.Karaoke.Edit.Import
{
public class ImportManager : Component
{
public static string[] LyricFotmatExtensions { get; } = { ".lrc", ".kar" };
public static string[] NicokaraSkinFotmatExtensions { get; } = { ".nkmproj" };

private const string backup_lrc_name = "backup.lrc";

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

[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }

public void ImportLrcFile(FileInfo info)
{
if (!info.Exists)
throw new FileNotFoundException("Lyric file does not found!");

var isFormatMatch = LyricFotmatExtensions.Contains(info.Extension);
if (!isFormatMatch)
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);
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);

// todo : remove all notes and lyric
// or just clear all beatmap because not really sure is singer should be removed also?

// then re-add the lyric.
}
}
}

public void ImportNicokaraSkinFile(FileInfo info)
{
if (!info.Exists)
throw new FileNotFoundException("Nicokara file does not found!");

var isFormatMatch = NicokaraSkinFotmatExtensions.Contains(info.Extension);
if (isFormatMatch)
throw new FileLoadException("Nicokara's skin extension should be .nkmproj");
}
}
}
32 changes: 0 additions & 32 deletions osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs

This file was deleted.

34 changes: 4 additions & 30 deletions osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
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.Import;
using osu.Game.Rulesets.Karaoke.Edit.Timelines;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Objects;
Expand All @@ -26,15 +26,11 @@ namespace osu.Game.Rulesets.Karaoke.Edit.LyricEditor
{
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" };
public IEnumerable<string> HandledExtensions => ImportManager.LyricFotmatExtensions;

[Resolved]
private EditorBeatmap beatmap { get; set; }
Expand All @@ -56,7 +52,7 @@ Task ICanAcceptFiles.Import(params string[] paths)
{
var firstFile = new FileInfo(paths.First());

if (LyricFotmatExtensions.Contains(firstFile.Extension))
if (HandledExtensions.Contains(firstFile.Extension))
{
// Import lyric file
ImportLyricFile(firstFile);
Expand All @@ -70,29 +66,7 @@ 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.
}));
}
}
dialogOverlay?.Push(new ImportLyricDialog(info));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Game.Overlays.Dialog;

namespace osu.Game.Rulesets.Karaoke.Graphics.Overlays.Dialog
{
public class OkPopupDialog : PopupDialog
{
public OkPopupDialog()
{
Buttons = new PopupDialogButton[]
{
new PopupDialogCancelButton
{
Text = @"OK",
},
};
}
}
}

0 comments on commit 72ad04b

Please sign in to comment.