-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AppUpdateServer.cs
121 lines (112 loc) · 4.47 KB
/
AppUpdateServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Lively.Common;
using Lively.Common.Models;
using Lively.Common.Services.Update;
using Lively.Grpc.Common.Proto.Update;
using Lively.Services;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace Lively.RPC
{
internal class AppUpdateServer : UpdateService.UpdateServiceBase
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private readonly IAppUpdaterService updater;
private readonly IDialogService dialogService;
public AppUpdateServer(IAppUpdaterService updater, IDialogService dialogService)
{
this.updater = updater;
this.dialogService = dialogService;
}
public override async Task<Empty> CheckUpdate(Empty _, ServerCallContext context)
{
#if !DEBUG
await updater.CheckUpdate(0);
#endif
Debug.WriteLine("App Update checking disabled in DEBUG mode.");
return await Task.FromResult(new Empty());
}
public override Task<Empty> StartUpdate(Empty _, ServerCallContext context)
{
if (updater.Status == AppUpdateStatus.available)
{
try
{
try
{
// Main user interface downloads the setup.
var filePath = Path.Combine(Constants.CommonPaths.TempDir, updater.LastCheckUri.Segments.Last());
if (!File.Exists(filePath))
throw new FileNotFoundException(filePath);
// Run setup in silent mode.
Process.Start(filePath, "/SILENT /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
// Inno installer will auto retry, waiting for application exit.
App.ShutDown();
}
catch (Exception ex)
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(delegate
{
dialogService.ShowErrorDialog(Properties.Resources.TextError, $"{Properties.Resources.LivelyExceptionAppUpdateFail}\n\nException:\n{ex}");
}));
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
}
return Task.FromResult(new Empty());
}
public override Task<UpdateResponse> GetUpdateStatus(Empty _, ServerCallContext context)
{
return Task.FromResult(new UpdateResponse()
{
Status = (UpdateStatus)((int)updater.Status),
Changelog = string.Empty,
Url = updater.LastCheckUri?.OriginalString ?? string.Empty,
Version = updater.LastCheckVersion?.ToString() ?? string.Empty,
Time = Timestamp.FromDateTime(updater.LastCheckTime.ToUniversalTime()),
});
}
public override async Task SubscribeUpdateChecked(Empty _, IServerStreamWriter<Empty> responseStream, ServerCallContext context)
{
try
{
while (!context.CancellationToken.IsCancellationRequested)
{
var tcs = new TaskCompletionSource<bool>();
updater.UpdateChecked += Updater_UpdateChecked;
void Updater_UpdateChecked(object sender, AppUpdaterEventArgs e)
{
updater.UpdateChecked -= Updater_UpdateChecked;
tcs.TrySetResult(true);
}
using var item = context.CancellationToken.Register(() => { tcs.TrySetResult(false); });
await tcs.Task;
if (context.CancellationToken.IsCancellationRequested)
{
updater.UpdateChecked -= Updater_UpdateChecked;
break;
}
await responseStream.WriteAsync(new Empty());
}
}
catch (Exception e)
{
Logger.Error(e);
}
}
}
}