From 72ad04b79d8c122a8f7027f32c982509b3cf6411 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 31 Oct 2020 12:37:36 +0900 Subject: [PATCH 1/3] 1. Import import manager 2. Move dialog and handle some exception message. 3. Create ok dialog to show message only. 4. Add ImportManager into injection in test case. --- .../Edit/TestSceneLyricEditorScreen.cs | 7 +- .../Edit/Import/ImportLyricDialog.cs | 83 +++++++++++++++++++ .../Edit/Import/ImportManager.cs | 73 ++++++++++++++++ .../Edit/LyricEditor/ImportLyricDialog.cs | 32 ------- .../Edit/LyricEditor/LyricEditorScreen.cs | 34 +------- .../Graphics/Overlays/Dialog/OkPopupDialog.cs | 21 +++++ 6 files changed, 187 insertions(+), 63 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke/Edit/Import/ImportLyricDialog.cs create mode 100644 osu.Game.Rulesets.Karaoke/Edit/Import/ImportManager.cs delete mode 100644 osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs create mode 100644 osu.Game.Rulesets.Karaoke/Graphics/Overlays/Dialog/OkPopupDialog.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/TestSceneLyricEditorScreen.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/TestSceneLyricEditorScreen.cs index 421f45a34..8d6a97d46 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Edit/TestSceneLyricEditorScreen.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/TestSceneLyricEditorScreen.cs @@ -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; @@ -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 @@ -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] diff --git a/osu.Game.Rulesets.Karaoke/Edit/Import/ImportLyricDialog.cs b/osu.Game.Rulesets.Karaoke/Edit/Import/ImportLyricDialog.cs new file mode 100644 index 000000000..9338f1d0f --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Edit/Import/ImportLyricDialog.cs @@ -0,0 +1,83 @@ +// Copyright (c) andy840119 . 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 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; + } + } + } +} diff --git a/osu.Game.Rulesets.Karaoke/Edit/Import/ImportManager.cs b/osu.Game.Rulesets.Karaoke/Edit/Import/ImportManager.cs new file mode 100644 index 000000000..a70a458bb --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Edit/Import/ImportManager.cs @@ -0,0 +1,73 @@ +// Copyright (c) andy840119 . 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 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"); + } + } +} diff --git a/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs b/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs deleted file mode 100644 index af8d6f1b8..000000000 --- a/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/ImportLyricDialog.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) andy840119 . 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!", - }, - }; - } - } -} diff --git a/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs b/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs index a33477500..3896c7934 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/LyricEditor/LyricEditorScreen.cs @@ -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; @@ -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