BililiveRecorder/BililiveRecorder.WPF/App.xaml.cs

96 lines
4.0 KiB
C#
Raw Normal View History

2018-11-10 11:55:30 +08:00
using System;
using System.IO;
2018-11-10 11:55:30 +08:00
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
2020-11-28 13:02:57 +08:00
using NLog;
using Squirrel;
2018-03-12 18:57:20 +08:00
namespace BililiveRecorder.WPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
2018-11-10 11:55:30 +08:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2018-03-21 20:56:56 +08:00
2018-11-10 11:55:30 +08:00
private void CheckUpdate(object sender, StartupEventArgs e)
{
logger.Debug($"Starting. FileV:{typeof(App).Assembly.GetName().Version.ToString(4)}, BuildV:{BuildInfo.Version}, Hash:{BuildInfo.HeadSha1}");
logger.Debug("Environment.CommandLine: " + Environment.CommandLine);
logger.Debug("Environment.CurrentDirectory: " + Environment.CurrentDirectory);
2018-11-10 14:57:58 +08:00
#if !DEBUG
Task.Run(RunCheckUpdate);
#endif
}
2018-11-10 11:55:30 +08:00
2018-11-10 14:57:58 +08:00
private async Task RunCheckUpdate()
{
try
2018-11-10 11:55:30 +08:00
{
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("BILILIVE_RECORDER_DISABLE_UPDATE"))
|| File.Exists("BILILIVE_RECORDER_DISABLE_UPDATE"))
{
return;
}
2018-11-10 14:57:58 +08:00
var envPath = Environment.GetEnvironmentVariable("BILILIVE_RECORDER_OVERWRITE_UPDATE");
var serverUrl = @"https://soft.danmuji.org/BililiveRecorder/";
2018-11-10 14:57:58 +08:00
if (!string.IsNullOrWhiteSpace(envPath)) { serverUrl = envPath; }
logger.Debug("Checking updates.");
using (var manager = new UpdateManager(urlOrPath: serverUrl))
2018-11-10 11:55:30 +08:00
{
2018-11-10 14:57:58 +08:00
var update = await manager.CheckForUpdate();
if (update.CurrentlyInstalledVersion == null)
2018-11-10 11:55:30 +08:00
{
2018-11-10 14:57:58 +08:00
logger.Debug("Squirrel 无当前版本");
}
2018-11-10 11:55:30 +08:00
2018-11-10 14:57:58 +08:00
if (!update.ReleasesToApply.Any())
{
logger.Info($@"当前运行的是最新版本 ({
update.CurrentlyInstalledVersion?.Version?.ToString() ?? "×"
}\{
typeof(App).Assembly.GetName().Version.ToString(4)
})");
}
else
{
if (update.CurrentlyInstalledVersion != null
&& update.FutureReleaseEntry.Version < update.CurrentlyInstalledVersion.Version)
2018-11-10 11:55:30 +08:00
{
2018-11-10 14:57:58 +08:00
logger.Warn("服务器回滚了一个更新,本地版本比服务器版本高。");
2018-11-10 11:55:30 +08:00
}
2018-11-10 14:57:58 +08:00
logger.Info($@"服务器最新版本: {
update.FutureReleaseEntry?.Version?.ToString() ?? "×"
} : {
update.CurrentlyInstalledVersion?.Version?.ToString() ?? "×"
}");
2018-11-10 11:55:30 +08:00
2018-11-10 14:57:58 +08:00
logger.Info("开始后台下载新版本(不会影响软件运行)");
await manager.DownloadReleases(update.ReleasesToApply);
logger.Info("新版本下载完成,开始安装(不会影响软件运行)");
await manager.ApplyReleases(update);
logger.Info("新版本安装完毕,你可以暂时继续使用当前版本。下次启动时会自动启动最新版本。");
2018-11-10 11:55:30 +08:00
}
}
2018-11-10 14:57:58 +08:00
}
catch (Exception ex)
{
logger.Warn(ex, "检查更新时出错,如持续出错请联系开发者 rec@danmuji.org");
2018-11-10 14:57:58 +08:00
}
2020-11-24 20:55:47 +08:00
2021-01-01 14:46:27 +08:00
_ = Task.Run(async () => { await Task.Delay(TimeSpan.FromDays(1)); await this.RunCheckUpdate(); });
2018-11-10 11:55:30 +08:00
}
2020-12-05 18:27:54 +08:00
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
2021-01-29 20:31:25 +08:00
if (e != null)
e.Cancel = true;
(Current.MainWindow as NewMainWindow)?.CloseWithoutConfirmAction();
2020-12-05 18:27:54 +08:00
}
2018-03-12 18:57:20 +08:00
}
}