|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using GeneralUpdate.ClientCore.Strategys; |
| 11 | +using GeneralUpdate.Common.Download; |
| 12 | +using GeneralUpdate.Common.FileBasic; |
| 13 | +using GeneralUpdate.Common.Internal; |
| 14 | +using GeneralUpdate.Common.Internal.Bootstrap; |
| 15 | +using GeneralUpdate.Common.Internal.JsonContext; |
| 16 | +using GeneralUpdate.Common.Internal.Strategy; |
| 17 | +using GeneralUpdate.Common.Shared; |
| 18 | +using GeneralUpdate.Common.Shared.Object; |
| 19 | +using GeneralUpdate.Common.Shared.Object.Enum; |
| 20 | +using GeneralUpdate.Common.Shared.Service; |
| 21 | + |
| 22 | +namespace GeneralUpdate.ClientCore; |
| 23 | + |
| 24 | +internal sealed class SilentUpdateMode |
| 25 | +{ |
| 26 | + private const string ProcessInfoEnvironmentKey = "ProcessInfo"; |
| 27 | + private static readonly TimeSpan PollingInterval = TimeSpan.FromMinutes(20); |
| 28 | + private readonly GlobalConfigInfo _configInfo; |
| 29 | + private readonly Encoding _encoding; |
| 30 | + private readonly string _format; |
| 31 | + private readonly int _downloadTimeOut; |
| 32 | + private readonly bool _patchEnabled; |
| 33 | + private readonly bool _backupEnabled; |
| 34 | + private Task? _pollingTask; |
| 35 | + private int _prepared; |
| 36 | + private int _updaterStarted; |
| 37 | + |
| 38 | + public SilentUpdateMode(GlobalConfigInfo configInfo, Encoding encoding, string format, int downloadTimeOut, bool patchEnabled, bool backupEnabled) |
| 39 | + { |
| 40 | + _configInfo = configInfo; |
| 41 | + _encoding = encoding; |
| 42 | + _format = format; |
| 43 | + _downloadTimeOut = downloadTimeOut; |
| 44 | + _patchEnabled = patchEnabled; |
| 45 | + _backupEnabled = backupEnabled; |
| 46 | + } |
| 47 | + |
| 48 | + public Task StartAsync() |
| 49 | + { |
| 50 | + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; |
| 51 | + _pollingTask = Task.Run(PollLoopAsync); |
| 52 | + _pollingTask.ContinueWith(task => |
| 53 | + { |
| 54 | + if (task.Exception != null) |
| 55 | + { |
| 56 | + GeneralTracer.Error("The StartAsync method in SilentUpdateMode captured a polling exception.", task.Exception); |
| 57 | + } |
| 58 | + }, TaskContinuationOptions.OnlyOnFaulted); |
| 59 | + return Task.CompletedTask; |
| 60 | + } |
| 61 | + |
| 62 | + private async Task PollLoopAsync() |
| 63 | + { |
| 64 | + while (Volatile.Read(ref _prepared) == 0) |
| 65 | + { |
| 66 | + try |
| 67 | + { |
| 68 | + await PrepareUpdateIfNeededAsync(); |
| 69 | + } |
| 70 | + catch (Exception exception) |
| 71 | + { |
| 72 | + GeneralTracer.Error("The PollLoopAsync method in SilentUpdateMode throws an exception.", exception); |
| 73 | + } |
| 74 | + |
| 75 | + if (Volatile.Read(ref _prepared) == 1) |
| 76 | + break; |
| 77 | + |
| 78 | + await Task.Delay(PollingInterval); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private async Task PrepareUpdateIfNeededAsync() |
| 83 | + { |
| 84 | + var mainResp = await VersionService.Validate(_configInfo.UpdateUrl |
| 85 | + , _configInfo.ClientVersion |
| 86 | + , AppType.ClientApp |
| 87 | + , _configInfo.AppSecretKey |
| 88 | + , GetPlatform() |
| 89 | + , _configInfo.ProductId |
| 90 | + , _configInfo.Scheme |
| 91 | + , _configInfo.Token); |
| 92 | + |
| 93 | + if (mainResp?.Code != 200 || mainResp.Body == null || mainResp.Body.Count == 0) |
| 94 | + return; |
| 95 | + |
| 96 | + var versions = mainResp.Body.OrderBy(x => x.ReleaseDate).ToList(); |
| 97 | + var latestVersion = versions.Last().Version; |
| 98 | + if (CheckFail(latestVersion)) |
| 99 | + return; |
| 100 | + |
| 101 | + BlackListManager.Instance?.AddBlackFiles(_configInfo.BlackFiles); |
| 102 | + BlackListManager.Instance?.AddBlackFileFormats(_configInfo.BlackFormats); |
| 103 | + BlackListManager.Instance?.AddSkipDirectorys(_configInfo.SkipDirectorys); |
| 104 | + |
| 105 | + _configInfo.Encoding = _encoding; |
| 106 | + _configInfo.Format = _format; |
| 107 | + _configInfo.DownloadTimeOut = _downloadTimeOut; |
| 108 | + _configInfo.PatchEnabled = _patchEnabled; |
| 109 | + _configInfo.IsMainUpdate = true; |
| 110 | + _configInfo.LastVersion = latestVersion; |
| 111 | + _configInfo.UpdateVersions = versions; |
| 112 | + _configInfo.TempPath = StorageManager.GetTempDirectory("main_temp"); |
| 113 | + _configInfo.BackupDirectory = Path.Combine(_configInfo.InstallPath, $"{StorageManager.DirectoryName}{_configInfo.ClientVersion}"); |
| 114 | + |
| 115 | + if (_backupEnabled) |
| 116 | + { |
| 117 | + StorageManager.Backup(_configInfo.InstallPath, _configInfo.BackupDirectory, BlackListManager.Instance.SkipDirectorys); |
| 118 | + } |
| 119 | + |
| 120 | + var processInfo = ConfigurationMapper.MapToProcessInfo( |
| 121 | + _configInfo, |
| 122 | + versions, |
| 123 | + BlackListManager.Instance.BlackFileFormats.ToList(), |
| 124 | + BlackListManager.Instance.BlackFiles.ToList(), |
| 125 | + BlackListManager.Instance.SkipDirectorys.ToList()); |
| 126 | + _configInfo.ProcessInfo = JsonSerializer.Serialize(processInfo, ProcessInfoJsonContext.Default.ProcessInfo); |
| 127 | + |
| 128 | + var manager = new DownloadManager(_configInfo.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut); |
| 129 | + foreach (var versionInfo in _configInfo.UpdateVersions) |
| 130 | + { |
| 131 | + manager.Add(new DownloadTask(manager, versionInfo)); |
| 132 | + } |
| 133 | + await manager.LaunchTasksAsync(); |
| 134 | + |
| 135 | + var strategy = CreateStrategy(); |
| 136 | + strategy.Create(_configInfo); |
| 137 | + await strategy.ExecuteAsync(); |
| 138 | + |
| 139 | + Interlocked.Exchange(ref _prepared, 1); |
| 140 | + } |
| 141 | + |
| 142 | + private void OnProcessExit(object? sender, EventArgs e) |
| 143 | + { |
| 144 | + if (Volatile.Read(ref _prepared) != 1 || Interlocked.Exchange(ref _updaterStarted, 1) == 1) |
| 145 | + return; |
| 146 | + |
| 147 | + try |
| 148 | + { |
| 149 | + Environments.SetEnvironmentVariable(ProcessInfoEnvironmentKey, _configInfo.ProcessInfo); |
| 150 | + var updaterPath = Path.Combine(_configInfo.InstallPath, _configInfo.AppName); |
| 151 | + if (File.Exists(updaterPath)) |
| 152 | + { |
| 153 | + Process.Start(new ProcessStartInfo |
| 154 | + { |
| 155 | + UseShellExecute = true, |
| 156 | + FileName = updaterPath |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | + catch (Exception exception) |
| 161 | + { |
| 162 | + GeneralTracer.Error("The OnProcessExit method in SilentUpdateMode throws an exception.", exception); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private IStrategy CreateStrategy() |
| 167 | + { |
| 168 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 169 | + return new WindowsStrategy(); |
| 170 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 171 | + return new LinuxStrategy(); |
| 172 | + throw new PlatformNotSupportedException("The current operating system is not supported!"); |
| 173 | + } |
| 174 | + |
| 175 | + private static int GetPlatform() |
| 176 | + { |
| 177 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 178 | + return PlatformType.Windows; |
| 179 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 180 | + return PlatformType.Linux; |
| 181 | + return -1; |
| 182 | + } |
| 183 | + |
| 184 | + private static bool CheckFail(string version) |
| 185 | + { |
| 186 | + var fail = Environments.GetEnvironmentVariable("UpgradeFail"); |
| 187 | + if (string.IsNullOrEmpty(fail) || string.IsNullOrEmpty(version)) |
| 188 | + return false; |
| 189 | + |
| 190 | + var failVersion = new Version(fail); |
| 191 | + var latestVersion = new Version(version); |
| 192 | + return failVersion >= latestVersion; |
| 193 | + } |
| 194 | +} |
0 commit comments