BililiveRecorder/BililiveRecorder.WPF/Pages/ToolboxDanmakuMergerPage.xaml.cs

228 lines
8.8 KiB
C#
Raw Normal View History

2021-08-10 18:53:04 +08:00
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
2021-11-21 00:08:31 +08:00
using System.Windows.Threading;
2021-08-10 18:53:04 +08:00
using BililiveRecorder.ToolBox;
using BililiveRecorder.ToolBox.Tool.DanmakuMerger;
2021-11-21 00:08:31 +08:00
using BililiveRecorder.ToolBox.Tool.DanmakuStartTime;
2021-08-10 18:53:04 +08:00
using BililiveRecorder.WPF.Controls;
2021-11-21 00:08:31 +08:00
using BililiveRecorder.WPF.Models;
2021-08-10 18:53:04 +08:00
using Microsoft.WindowsAPICodePack.Dialogs;
using Serilog;
using WPFLocalizeExtension.Extensions;
#nullable enable
namespace BililiveRecorder.WPF.Pages
{
/// <summary>
/// Interaction logic for ToolboxDanmakuMergerPage.xaml
/// </summary>
public partial class ToolboxDanmakuMergerPage
{
private static readonly ILogger logger = Log.ForContext<ToolboxDanmakuMergerPage>();
private static readonly string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
2021-11-21 00:08:31 +08:00
private readonly ObservableCollection<DanmakuFileWithOffset> Files = new();
2021-08-10 18:53:04 +08:00
public ToolboxDanmakuMergerPage()
{
this.InitializeComponent();
var cvs = (CollectionViewSource)this.FindResource("cvs");
cvs.Source = this.Files;
2021-08-10 18:53:04 +08:00
}
private void RemoveFile_Click(object sender, RoutedEventArgs e)
{
var b = (Button)sender;
2021-11-21 00:08:31 +08:00
var f = (DanmakuFileWithOffset)b.DataContext;
2021-08-10 18:53:04 +08:00
this.Files.Remove(f);
2021-11-21 00:08:31 +08:00
this.CalculateOffsets();
2021-08-10 18:53:04 +08:00
}
2022-05-17 00:53:37 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
2021-11-21 00:08:31 +08:00
private async void DragDrop(object sender, DragEventArgs e)
2022-05-17 00:53:37 +08:00
#pragma warning restore VSTHRD100 // Avoid async void methods
2021-08-10 18:53:04 +08:00
{
try
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
2021-11-21 00:08:31 +08:00
await this.AddFilesAsync(files.Where(x => x.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)).ToArray());
2021-08-10 18:53:04 +08:00
}
}
catch (Exception)
{ }
}
2022-05-17 00:53:37 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
2021-11-21 00:08:31 +08:00
private async void AddFile_Click(object sender, RoutedEventArgs e)
2022-05-17 00:53:37 +08:00
#pragma warning restore VSTHRD100 // Avoid async void methods
2021-08-10 18:53:04 +08:00
{
var d = new CommonOpenFileDialog
{
Title = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_Merge_OpenFileDialogTitle"),
AllowNonFileSystemItems = false,
EnsureFileExists = true,
EnsurePathExists = true,
EnsureValidNames = true,
DefaultDirectory = DesktopPath,
DefaultExtension = "xml",
Multiselect = true,
};
d.Filters.Add(new CommonFileDialogFilter(LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_Merge_XmlDanmakuFiles"), "*.xml"));
if (d.ShowDialog() != CommonFileDialogResult.Ok)
return;
2021-11-21 00:08:31 +08:00
await this.AddFilesAsync(d.FileNames.ToArray());
}
private async Task AddFilesAsync(string[] paths)
{
// filter duplicate file paths
var req = new DanmakuStartTimeRequest { Inputs = paths.Where(x => !this.Files.Any(f => f.Path == x)).ToArray() };
2021-11-21 00:08:31 +08:00
var handler = new DanmakuStartTimeHandler();
var resp = await handler.Handle(req, default, default).ConfigureAwait(true);
if (resp.Status != ResponseStatus.OK || resp.Data is null)
return;
var toBeAdded = resp.Data.StartTimes.Select(x => new DanmakuFileWithOffset(x.Path) { StartTime = x.StartTime });
foreach (var file in toBeAdded)
2021-08-10 18:53:04 +08:00
this.Files.Add(file);
2021-11-21 00:08:31 +08:00
_ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)this.CalculateOffsets);
}
private void CalculateOffsets()
{
if (this.Files.Count == 0)
return;
var minTime = this.Files.Min(x => x.StartTime);
foreach (var item in this.Files)
{
item.Offset = (int)(item.StartTime - minTime).TotalSeconds;
2021-08-10 18:53:04 +08:00
}
}
2022-05-17 00:53:37 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
2021-08-10 18:53:04 +08:00
private async void Merge_Click(object sender, RoutedEventArgs e)
2022-05-17 00:53:37 +08:00
#pragma warning restore VSTHRD100 // Avoid async void methods
2021-08-10 18:53:04 +08:00
{
AutoFixProgressDialog? progressDialog = null;
try
{
var inputPaths = this.Files.Distinct().ToArray();
if (inputPaths.Length < 2)
{
MessageBox.Show(LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_Merge_Error_AtLeastTwo"),
LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_Merge_Title"),
MessageBoxButton.OK,
MessageBoxImage.Warning);
return;
}
logger.Debug("合并弹幕文件 {Paths}", inputPaths);
progressDialog = new AutoFixProgressDialog()
{
CancelButtonVisibility = Visibility.Collapsed,
CancellationTokenSource = new CancellationTokenSource(),
Owner = Application.Current.MainWindow
2021-08-10 18:53:04 +08:00
};
var token = progressDialog.CancellationTokenSource.Token;
var showTask = progressDialog.ShowAndDisableMinimizeToTrayAsync();
2021-08-10 18:53:04 +08:00
string? outputPath;
{
var fileDialog = new CommonSaveFileDialog()
{
Title = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_AutoFix_SelectOutputDialog_Title"),
AlwaysAppendDefaultExtension = true,
AddToMostRecentlyUsedList = false,
DefaultExtension = "xml",
EnsurePathExists = true,
EnsureValidNames = true,
NavigateToShortcut = true,
OverwritePrompt = false,
2021-11-21 00:08:31 +08:00
InitialDirectory = Path.GetDirectoryName(inputPaths[0].Path),
2021-08-10 18:53:04 +08:00
};
fileDialog.Filters.Add(new CommonFileDialogFilter(LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_Merge_XmlDanmakuFiles"), "*.xml"));
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
outputPath = fileDialog.FileName;
else
return;
}
var req = new DanmakuMergerRequest
{
2021-11-21 00:08:31 +08:00
Inputs = inputPaths.Select(x => x.Path).ToArray(),
Offsets = inputPaths.Select(x => x.Offset).ToArray(),
2021-08-10 18:53:04 +08:00
Output = outputPath,
};
var handler = new DanmakuMergerHandler();
var resp = await handler.Handle(req, token, async p =>
{
await this.Dispatcher.InvokeAsync(() =>
{
progressDialog.Progress = (int)(p * 98d);
});
}).ConfigureAwait(true);
logger.Debug("弹幕合并结果 {@Response}", resp);
if (resp.Status is not ResponseStatus.Cancelled and not ResponseStatus.OK)
2021-08-10 18:53:04 +08:00
{
logger.Warning(resp.Exception, "弹幕合并时发生错误 (@Status)", resp.Status);
await Task.Run(() => ShowErrorMessageBox(resp)).ConfigureAwait(true);
}
else
{
this.Files.Clear();
}
progressDialog.Hide();
await showTask.ConfigureAwait(true);
}
catch (Exception ex)
{
logger.Error(ex, "弹幕合并时发生未处理的错误");
}
finally
{
try
{
_ = this.Dispatcher.BeginInvoke((Action)(() => progressDialog?.Hide()));
progressDialog?.CancellationTokenSource?.Cancel();
}
catch (Exception) { }
}
}
private static void ShowErrorMessageBox<T>(CommandResponse<T> resp) where T : IResponseData
{
var title = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_AutoFix_Error_Title");
var type = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:Toolbox_AutoFix_Error_Type_" + resp.Status.ToString());
MessageBox.Show($"{type}\n{resp.ErrorMessage}", title, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}