|
| 1 | +using Microsoft.Extensions.Options; |
| 2 | +using Moesocks.Client.Services.Configuration; |
| 3 | +using Octokit; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Net.NetworkInformation; |
| 10 | +using System.Reflection; |
| 11 | +using System.Text; |
| 12 | +using System.Threading; |
| 13 | +using System.Threading.Tasks; |
| 14 | + |
| 15 | +namespace Moesocks.Client.Services.Update |
| 16 | +{ |
| 17 | + class UpdateService : IUpdateService |
| 18 | + { |
| 19 | + private readonly UpdateConfiguration _update; |
| 20 | + private CancellationTokenSource _cts; |
| 21 | + private readonly Timer _checkUpdate; |
| 22 | + private readonly TimeSpan _checkPeriod = TimeSpan.FromHours(1); |
| 23 | + private readonly GitHubClient _github; |
| 24 | + private readonly Version _currentVersion; |
| 25 | + private FileInfo _updatePack = null; |
| 26 | + private string _newVersion; |
| 27 | + |
| 28 | + public event EventHandler<NewReleaseFoundEventArgs> NewReleaseFound; |
| 29 | + |
| 30 | + protected bool CanUpdate => _updatePack == null && _update.Enabled && NetworkInterface.GetIsNetworkAvailable(); |
| 31 | + |
| 32 | + public UpdateService(IOptions<UpdateConfiguration> updateConfig, IProductInformation productInfo) |
| 33 | + { |
| 34 | + _currentVersion = productInfo.ProductVersion; |
| 35 | + _github = new GitHubClient(new ProductHeaderValue(productInfo.ProductName, _currentVersion.ToString())); |
| 36 | + _checkUpdate = new Timer(CheckUpdate, this, Timeout.InfiniteTimeSpan, _checkPeriod); |
| 37 | + _update = updateConfig.Value; |
| 38 | + _update.PropertyChanged += update_PropertyChanged; |
| 39 | + } |
| 40 | + |
| 41 | + private void update_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 42 | + { |
| 43 | + UpdateJob(); |
| 44 | + } |
| 45 | + |
| 46 | + private void UpdateJob() |
| 47 | + { |
| 48 | + if (!CanUpdate) |
| 49 | + { |
| 50 | + var oldCts = Interlocked.Exchange(ref _cts, null); |
| 51 | + if (oldCts != null) |
| 52 | + { |
| 53 | + oldCts.Cancel(); |
| 54 | + _checkUpdate.Change(Timeout.InfiniteTimeSpan, _checkPeriod); |
| 55 | + _cts = null; |
| 56 | + } |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + var cts = new CancellationTokenSource(); |
| 61 | + if (Interlocked.CompareExchange(ref _cts, cts, null) == null) |
| 62 | + _checkUpdate.Change(TimeSpan.Zero, _checkPeriod); |
| 63 | + } |
| 64 | + UpdateProduct(); |
| 65 | + } |
| 66 | + |
| 67 | + private static async void CheckUpdate(object me) |
| 68 | + { |
| 69 | + var service = (UpdateService)me; |
| 70 | + try |
| 71 | + { |
| 72 | + var token = service._cts.Token; |
| 73 | + token.ThrowIfCancellationRequested(); |
| 74 | + if (service.CanUpdate) |
| 75 | + { |
| 76 | + var releaseClient = service._github.Repository.Release; |
| 77 | + var latest = await releaseClient.GetLatest("sunnycase", "Moesocks"); |
| 78 | + token.ThrowIfCancellationRequested(); |
| 79 | + var latestVersion = Version.Parse(latest.TagName); |
| 80 | + if (latestVersion > service._currentVersion) |
| 81 | + { |
| 82 | + var asset = latest.Assets.FirstOrDefault(o => o.Name == "client.bin.zip"); |
| 83 | + if (asset != null) |
| 84 | + { |
| 85 | + var updatePackFile = CreateUpdatePackFile(latestVersion); |
| 86 | + if (!updatePackFile.Exists || updatePackFile.Length != asset.Size) |
| 87 | + { |
| 88 | + updatePackFile.Directory.Create(); |
| 89 | + using (var updatePack = updatePackFile.OpenWrite()) |
| 90 | + using (var client = new HttpClient()) |
| 91 | + { |
| 92 | + token.ThrowIfCancellationRequested(); |
| 93 | + var source = await client.GetStreamAsync(asset.BrowserDownloadUrl); |
| 94 | + token.ThrowIfCancellationRequested(); |
| 95 | + await source.CopyToAsync(updatePack); |
| 96 | + } |
| 97 | + } |
| 98 | + token.ThrowIfCancellationRequested(); |
| 99 | + service._newVersion = latest.Name; |
| 100 | + service._updatePack = updatePackFile; |
| 101 | + service.UpdateProduct(); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + catch |
| 107 | + { |
| 108 | + |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + Interlocked.Exchange(ref service._cts, null); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void UpdateProduct() |
| 117 | + { |
| 118 | + var pack = _updatePack; |
| 119 | + if (pack != null) |
| 120 | + NewReleaseFound?.Invoke(this, new NewReleaseFoundEventArgs(_newVersion, pack)); |
| 121 | + } |
| 122 | + |
| 123 | + private static FileInfo CreateUpdatePackFile(Version latestVersion) |
| 124 | + { |
| 125 | + return new FileInfo(Path.Combine("update", "client-v" + latestVersion + ".zip")); |
| 126 | + } |
| 127 | + |
| 128 | + public void Startup() |
| 129 | + { |
| 130 | + UpdateJob(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments