1919namespace GeneralUpdate . ClientCore ;
2020
2121/// <summary>
22- /// This component is used only for client application bootstrapping classes.
22+ /// This component is used only for client application bootstrapping classes.
2323/// </summary>
2424public class GeneralClientBootstrap : AbstractBootstrap < GeneralClientBootstrap , IStrategy >
2525{
@@ -41,19 +41,7 @@ public override async Task<GeneralClientBootstrap> LaunchAsync()
4141 {
4242 ExecuteCustomOptions ( ) ;
4343 ClearEnvironmentVariable ( ) ;
44- await InitializeDataAsync ( ) ;
45-
46- var manager = new DownloadManager ( _configinfo . TempPath , _configinfo . Format , _configinfo . DownloadTimeOut ) ;
47- manager . MultiAllDownloadCompleted += OnMultiAllDownloadCompleted ;
48- manager . MultiDownloadCompleted += OnMultiDownloadCompleted ;
49- manager . MultiDownloadError += OnMultiDownloadError ;
50- manager . MultiDownloadProgressChanged += OnMultiDownloadProgressChanged ;
51- manager . MultiDownloadStatistics += OnMultiDownloadStatistics ;
52- foreach ( var versionInfo in _configinfo . UpdateVersions )
53- {
54- manager . Add ( new DownloadTask ( manager , versionInfo ) ) ;
55- }
56- await manager . LaunchTasksAsync ( ) ;
44+ await ExecuteWorkflowAsync ( ) ;
5745 return this ;
5846 }
5947
@@ -144,7 +132,7 @@ public GeneralClientBootstrap AddListenerException(Action<object, ExceptionEvent
144132
145133 #region Private Methods
146134
147- private async Task InitializeDataAsync ( )
135+ private async Task ExecuteWorkflowAsync ( )
148136 {
149137 //Request the upgrade information needed by the client and upgrade end, and determine if an upgrade is necessary.
150138 var mainResp = await VersionService . Validate ( _configinfo . UpdateUrl
@@ -160,40 +148,95 @@ private async Task InitializeDataAsync()
160148 , _configinfo . AppSecretKey
161149 , _configinfo . Platform
162150 , _configinfo . ProductId ) ;
163-
164- _configinfo . IsUpgradeUpdate = upgradeResp . Body . Count > 0 ;
165- _configinfo . IsMainUpdate = mainResp . Body . Count > 0 ;
166- //No need to update, return directly.
167- if ( ! _configinfo . IsMainUpdate && ! _configinfo . IsUpgradeUpdate ) return ;
168-
151+
152+ _configinfo . IsUpgradeUpdate = CheckUpgrade ( upgradeResp ) ;
153+ _configinfo . IsMainUpdate = CheckUpgrade ( mainResp ) ;
154+
169155 //If the main program needs to be forced to update, the skip will not take effect.
170156 var isForcibly = CheckForcibly ( mainResp . Body ) || CheckForcibly ( upgradeResp . Body ) ;
171157 if ( CanSkip ( isForcibly ) ) return ;
172158
173- _configinfo . UpdateVersions = upgradeResp . Body . OrderBy ( x => x . ReleaseDate ) . ToList ( ) ;
174- _configinfo . LastVersion = _configinfo . UpdateVersions . Last ( ) . Version ;
175159 _configinfo . Encoding = GetOption ( UpdateOption . Encoding ) ?? Encoding . Default ;
176- _configinfo . Format = GetOption ( UpdateOption . Format ) ?? "zip" ;
160+ _configinfo . Format = GetOption ( UpdateOption . Format ) ?? ". zip" ;
177161 _configinfo . DownloadTimeOut = GetOption ( UpdateOption . DownloadTimeOut ) == 0 ? 60 : GetOption ( UpdateOption . DownloadTimeOut ) ;
178162 _configinfo . DriveEnabled = GetOption ( UpdateOption . Drive ) ;
179- _configinfo . TempPath = GeneralFileManager . GetTempDirectory ( _configinfo . LastVersion ) ;
163+ _configinfo . TempPath = GeneralFileManager . GetTempDirectory ( "main_temp" ) ;
164+
165+ if ( _configinfo . IsMainUpdate )
166+ {
167+ _configinfo . UpdateVersions = mainResp . Body . OrderBy ( x => x . ReleaseDate ) . ToList ( ) ;
168+ _configinfo . LastVersion = _configinfo . UpdateVersions . Last ( ) . Version ;
169+
170+ //Initialize the process transfer parameter object.
171+ var processInfo = new ProcessInfo ( _configinfo . MainAppName
172+ , _configinfo . InstallPath
173+ , _configinfo . ClientVersion
174+ , _configinfo . LastVersion
175+ , _configinfo . UpdateLogUrl
176+ , _configinfo . Encoding
177+ , _configinfo . Format
178+ , _configinfo . DownloadTimeOut
179+ , _configinfo . AppSecretKey
180+ , mainResp . Body
181+ , _configinfo . ReportUrl ) ;
182+
183+ _configinfo . ProcessInfo = JsonSerializer . Serialize ( processInfo ) ;
184+ }
185+
186+ StrategyFactory ( ) ;
180187
181- //Initialize the process transfer parameter object.
182- var processInfo = new ProcessInfo ( _configinfo . MainAppName
183- , _configinfo . InstallPath
184- , _configinfo . ClientVersion
185- , _configinfo . LastVersion
186- , _configinfo . UpdateLogUrl
187- , _configinfo . Encoding
188- , _configinfo . Format
189- , _configinfo . DownloadTimeOut
190- , _configinfo . AppSecretKey
191- , mainResp . Body ) ;
192- _configinfo . ProcessInfo = JsonSerializer . Serialize ( processInfo ) ;
188+ switch ( _configinfo . IsUpgradeUpdate )
189+ {
190+ case true when _configinfo . IsMainUpdate :
191+ //Both upgrade and main program update.
192+ await Download ( ) ;
193+ await _strategy ? . ExecuteAsync ( ) ! ;
194+ _strategy ? . StartApp ( ) ;
195+ break ;
196+ case true when ! _configinfo . IsMainUpdate :
197+ //Upgrade program update.
198+ await Download ( ) ;
199+ await _strategy ? . ExecuteAsync ( ) ! ;
200+ break ;
201+ case false when _configinfo . IsMainUpdate :
202+ //Main program update.
203+ _strategy ? . StartApp ( ) ;
204+ break ;
205+ }
193206 }
207+
208+ private async Task Download ( )
209+ {
210+ var manager = new DownloadManager ( _configinfo . TempPath , _configinfo . Format , _configinfo . DownloadTimeOut ) ;
211+ manager . MultiAllDownloadCompleted += OnMultiAllDownloadCompleted ;
212+ manager . MultiDownloadCompleted += OnMultiDownloadCompleted ;
213+ manager . MultiDownloadError += OnMultiDownloadError ;
214+ manager . MultiDownloadProgressChanged += OnMultiDownloadProgressChanged ;
215+ manager . MultiDownloadStatistics += OnMultiDownloadStatistics ;
216+ foreach ( var versionInfo in _configinfo . UpdateVersions )
217+ {
218+ manager . Add ( new DownloadTask ( manager , versionInfo ) ) ;
219+ }
220+ await manager . LaunchTasksAsync ( ) ;
221+ }
222+
223+ private bool CheckUpgrade ( VersionRespDTO ? response )
224+ {
225+ if ( response == null )
226+ {
227+ return false ;
228+ }
194229
230+ if ( response . Code == HttpStatus . OK )
231+ {
232+ return response . Body . Count > 0 ;
233+ }
234+
235+ return false ;
236+ }
237+
195238 /// <summary>
196- /// User decides if update is required.
239+ /// User decides if update is required.
197240 /// </summary>
198241 /// <returns>is false to continue execution.</returns>
199242 private bool CanSkip ( bool isForcibly )
@@ -221,7 +264,7 @@ private void ExecuteCustomOptions()
221264 }
222265
223266 /// <summary>
224- /// Clear the environment variable information needed to start the upgrade assistant process.
267+ /// Clear the environment variable information needed to start the upgrade assistant process.
225268 /// </summary>
226269 private void ClearEnvironmentVariable ( )
227270 {
@@ -237,11 +280,25 @@ private void ClearEnvironmentVariable()
237280 }
238281 }
239282
240- protected override void ExecuteStrategy ( )
283+ private bool CheckForcibly ( List < VersionBodyDTO > ? versions )
241284 {
242- _strategy ? . Create ( _configinfo ! ) ;
243- _strategy ? . Execute ( ) ;
285+ if ( versions == null )
286+ return false ;
287+
288+ foreach ( var item in versions )
289+ {
290+ if ( item . IsForcibly == true )
291+ {
292+ return true ;
293+ }
294+ }
295+
296+ return false ;
244297 }
298+
299+ protected override void ExecuteStrategy ( ) => throw new NotImplementedException ( ) ;
300+
301+ protected override Task ExecuteStrategyAsync ( ) => throw new NotImplementedException ( ) ;
245302
246303 protected override GeneralClientBootstrap StrategyFactory ( )
247304 {
@@ -252,22 +309,10 @@ protected override GeneralClientBootstrap StrategyFactory()
252309 else
253310 throw new PlatformNotSupportedException ( "The current operating system is not supported!" ) ;
254311
312+ _strategy ? . Create ( _configinfo ! ) ;
255313 return this ;
256314 }
257315
258- private bool CheckForcibly ( List < VersionBodyDTO > versions )
259- {
260- foreach ( var item in versions )
261- {
262- if ( item . IsForcibly == true )
263- {
264- return true ;
265- }
266- }
267-
268- return false ;
269- }
270-
271316 private GeneralClientBootstrap AddListener < TArgs > ( Action < object , TArgs > callbackAction ) where TArgs : EventArgs
272317 {
273318 Debug . Assert ( callbackAction != null ) ;
@@ -288,11 +333,7 @@ private void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
288333 => EventManager . Instance . Dispatch ( sender , e ) ;
289334
290335 private void OnMultiAllDownloadCompleted ( object sender , MultiAllDownloadCompletedEventArgs e )
291- {
292- EventManager . Instance . Dispatch ( sender , e ) ;
293- StrategyFactory ( ) ;
294- ExecuteStrategy ( ) ;
295- }
336+ => EventManager . Instance . Dispatch ( sender , e ) ;
296337
297338 #endregion Private Methods
298339}
0 commit comments