2020-11-27 18:51:02 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-12-10 17:02:56 +08:00
|
|
|
using System.Threading.Tasks;
|
2020-11-27 18:51:02 +08:00
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Controls;
|
|
|
|
using System.Windows.Input;
|
2020-12-10 17:02:56 +08:00
|
|
|
using System.Windows.Threading;
|
2020-11-27 18:51:02 +08:00
|
|
|
using Autofac;
|
|
|
|
using BililiveRecorder.Core;
|
|
|
|
using BililiveRecorder.FlvProcessor;
|
|
|
|
using BililiveRecorder.WPF.Controls;
|
|
|
|
using BililiveRecorder.WPF.Models;
|
|
|
|
using CommandLine;
|
|
|
|
using ModernWpf.Controls;
|
2020-12-10 17:02:56 +08:00
|
|
|
using ModernWpf.Media.Animation;
|
2020-12-17 21:46:27 +08:00
|
|
|
using NLog;
|
2020-11-27 18:51:02 +08:00
|
|
|
using Path = System.IO.Path;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.WPF.Pages
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Interaction logic for RootPage.xaml
|
|
|
|
/// </summary>
|
|
|
|
public partial class RootPage : UserControl
|
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2020-11-27 18:51:02 +08:00
|
|
|
private readonly Dictionary<string, Type> PageMap = new Dictionary<string, Type>();
|
|
|
|
private readonly string lastdir_path = Path.Combine(Path.GetDirectoryName(typeof(RootPage).Assembly.Location), "lastdir.txt");
|
2020-12-10 17:02:56 +08:00
|
|
|
private readonly NavigationTransitionInfo transitionInfo = new DrillInNavigationTransitionInfo();
|
2020-11-27 18:51:02 +08:00
|
|
|
|
|
|
|
private IContainer Container { get; set; }
|
|
|
|
private ILifetimeScope RootScope { get; set; }
|
|
|
|
|
|
|
|
private int SettingsClickCount = 0;
|
|
|
|
|
|
|
|
internal RootModel Model { get; private set; }
|
|
|
|
|
|
|
|
public RootPage()
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
void AddType(Type t) => this.PageMap.Add(t.Name, t);
|
2020-11-27 18:51:02 +08:00
|
|
|
AddType(typeof(RoomListPage));
|
|
|
|
AddType(typeof(LogPage));
|
|
|
|
AddType(typeof(SettingsPage));
|
|
|
|
AddType(typeof(AdvancedSettingsPage));
|
2020-12-11 19:22:04 +08:00
|
|
|
AddType(typeof(AnnouncementPage));
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2021-01-01 14:46:27 +08:00
|
|
|
this.Model = new RootModel();
|
|
|
|
this.DataContext = this.Model;
|
2020-11-27 18:51:02 +08:00
|
|
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
builder.RegisterModule<FlvProcessorModule>();
|
|
|
|
builder.RegisterModule<CoreModule>();
|
2021-01-01 14:46:27 +08:00
|
|
|
this.Container = builder.Build();
|
|
|
|
this.RootScope = this.Container.BeginLifetimeScope("recorder_root");
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2021-01-01 14:46:27 +08:00
|
|
|
this.InitializeComponent();
|
|
|
|
this.AdvancedSettingsPageItem.Visibility = Visibility.Hidden;
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2021-01-03 13:21:36 +08:00
|
|
|
var mw = Application.Current.MainWindow as NewMainWindow;
|
|
|
|
if (mw is not null)
|
|
|
|
mw.NativeBeforeWindowClose += this.RootPage_NativeBeforeWindowClose;
|
|
|
|
|
2020-11-27 18:51:02 +08:00
|
|
|
Loaded += RootPage_Loaded;
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:01:12 +08:00
|
|
|
private void RootPage_NativeBeforeWindowClose(object sender, EventArgs e)
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.Model.Dispose();
|
2020-12-03 07:01:12 +08:00
|
|
|
SingleInstance.Cleanup();
|
|
|
|
}
|
|
|
|
|
2020-11-27 18:51:02 +08:00
|
|
|
private async void RootPage_Loaded(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
var recorder = this.RootScope.Resolve<IRecorder>();
|
2020-12-17 21:46:27 +08:00
|
|
|
var first_time = true;
|
2020-11-27 18:51:02 +08:00
|
|
|
var error = string.Empty;
|
|
|
|
string path;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
CommandLineOption commandLineOption = null;
|
|
|
|
if (first_time)
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
// while 循环第一次运行时检查命令行参数
|
|
|
|
try
|
|
|
|
{
|
|
|
|
first_time = false;
|
|
|
|
Parser.Default
|
|
|
|
.ParseArguments<CommandLineOption>(Environment.GetCommandLineArgs())
|
|
|
|
.WithParsed(x => commandLineOption = x);
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory))
|
|
|
|
{
|
|
|
|
// 如果有参数直接跳到检查路径
|
|
|
|
path = Path.GetFullPath(commandLineOption.WorkDirectory);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 无命令行参数
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// 出错直接重新来,不显示 error
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
2020-12-17 21:46:27 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// 尝试读取上次选择的路径
|
|
|
|
var lastdir = string.Empty;
|
|
|
|
try
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
if (File.Exists(this.lastdir_path))
|
|
|
|
lastdir = File.ReadAllText(this.lastdir_path).Replace("\r", "").Replace("\n", "").Trim();
|
2020-12-17 21:46:27 +08:00
|
|
|
}
|
|
|
|
catch (Exception) { }
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2020-12-17 21:46:27 +08:00
|
|
|
// 显示路径选择界面
|
|
|
|
var dialog = new WorkDirectorySelectorDialog
|
|
|
|
{
|
|
|
|
Error = error,
|
|
|
|
Path = lastdir
|
|
|
|
};
|
2021-01-03 13:21:36 +08:00
|
|
|
|
2020-12-17 21:46:27 +08:00
|
|
|
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
|
|
|
|
{
|
|
|
|
(Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction();
|
|
|
|
return;
|
|
|
|
}
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2020-12-17 21:46:27 +08:00
|
|
|
try
|
|
|
|
{ path = Path.GetFullPath(dialog.Path); }
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
error = "不支持该路径";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2020-12-17 21:46:27 +08:00
|
|
|
var config = Path.Combine(path, "config.json");
|
|
|
|
|
|
|
|
if (!Directory.Exists(path))
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
error = "目录不存在";
|
|
|
|
continue;
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
2020-12-17 21:46:27 +08:00
|
|
|
else if (!Directory.EnumerateFiles(path).Any())
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
// 可用的空文件夹
|
|
|
|
}
|
|
|
|
else if (!File.Exists(config))
|
|
|
|
{
|
|
|
|
error = "目录已有其他文件";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 已经选定工作目录
|
|
|
|
|
|
|
|
// 如果不是从命令行参数传入的路径,写入 lastdir_path 记录
|
|
|
|
try
|
2021-01-01 14:46:27 +08:00
|
|
|
{ if (string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory)) File.WriteAllText(this.lastdir_path, path); }
|
2020-12-17 21:46:27 +08:00
|
|
|
catch (Exception) { }
|
2020-12-10 17:02:56 +08:00
|
|
|
|
2020-12-17 21:46:27 +08:00
|
|
|
// 检查已经在同目录运行的其他进程
|
|
|
|
if (SingleInstance.CheckMutex(path))
|
|
|
|
{
|
|
|
|
// 无已经在同目录运行的进程
|
|
|
|
if (recorder.Initialize(path))
|
2020-12-10 17:02:56 +08:00
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.Model.Recorder = recorder;
|
2020-12-17 21:46:27 +08:00
|
|
|
|
|
|
|
_ = Task.Run(async () =>
|
2020-12-10 17:02:56 +08:00
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
await Task.Delay(100);
|
2021-01-01 14:46:27 +08:00
|
|
|
_ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method: new Action(() =>
|
2020-12-17 21:46:27 +08:00
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.RoomListPageNavigationViewItem.IsSelected = true;
|
2020-12-17 21:46:27 +08:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = "配置文件加载失败";
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
// 有已经在其他目录运行的进程,已经通知该进程,本进程退出
|
|
|
|
(Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction();
|
|
|
|
return;
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 21:46:27 +08:00
|
|
|
catch (Exception ex)
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2020-12-17 21:46:27 +08:00
|
|
|
error = "发生了未知错误";
|
2020-12-20 18:42:46 +08:00
|
|
|
logger.Warn(ex, "选择工作目录时发生了未知错误");
|
2020-12-17 21:46:27 +08:00
|
|
|
continue;
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.SettingsClickCount = 0;
|
2020-11-27 18:51:02 +08:00
|
|
|
if (args.IsSettingsSelected)
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.MainFrame.Navigate(typeof(SettingsPage), null, this.transitionInfo);
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var selectedItem = (NavigationViewItem)args.SelectedItem;
|
|
|
|
var selectedItemTag = (string)selectedItem.Tag;
|
2020-12-11 19:22:04 +08:00
|
|
|
if (selectedItemTag.StartsWith("http"))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.MainFrame.Navigate(new Uri(selectedItemTag), null, this.transitionInfo);
|
2020-12-11 19:22:04 +08:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:46:27 +08:00
|
|
|
else if (this.PageMap.ContainsKey(selectedItemTag))
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
var pageType = this.PageMap[selectedItemTag];
|
|
|
|
this.MainFrame.Navigate(pageType, null, this.transitionInfo);
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void NavigationViewItem_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
|
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
if (++this.SettingsClickCount > 3)
|
2020-11-27 18:51:02 +08:00
|
|
|
{
|
2021-01-01 14:46:27 +08:00
|
|
|
this.SettingsClickCount = 0;
|
|
|
|
this.AdvancedSettingsPageItem.Visibility = this.AdvancedSettingsPageItem.Visibility != Visibility.Visible ? Visibility.Visible : Visibility.Hidden;
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 13:02:57 +08:00
|
|
|
|
2020-12-10 17:02:56 +08:00
|
|
|
private void MainFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (sender is not ModernWpf.Controls.Frame frame) return;
|
|
|
|
|
|
|
|
while (frame.BackStackDepth > 0)
|
|
|
|
{
|
|
|
|
frame.RemoveBackEntry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
}
|