55using System . IO ;
66using System . Linq ;
77using System . Net ;
8+ using System . Runtime . CompilerServices ;
89using System . Text . Json ;
910using System . Threading ;
1011using System . Threading . Tasks ;
@@ -17,15 +18,12 @@ namespace GeneralUpdate.ClientCore;
1718
1819public sealed class GeneralClientOSS
1920{
20- private GeneralClientOSS ( )
21- {
22- }
21+ private GeneralClientOSS ( ) { }
2322
2423 /// <summary>
25- /// Starting an OSS update for windows,Linux,mac platform.
24+ /// Starting an OSS update for windows platform.
2625 /// </summary>
27- /// <param name="configInfo"></param>
28- public static async Task Start ( ParamsOSS configParams , string upgradeAppName = "GeneralUpdate.Upgrade" )
26+ public static async Task Start ( ParamsOSS configParams , string upgradeAppName = "GeneralUpdate.Upgrade.exe" )
2927 {
3028 await Task . Run ( ( ) =>
3129 {
@@ -34,24 +32,29 @@ await Task.Run(() =>
3432 var basePath = Thread . GetDomain ( ) . BaseDirectory ;
3533 //Download the version information file from OSS to be updated.(JSON)
3634 var versionsFilePath = Path . Combine ( basePath , configParams . VersionFileName ) ;
37- DownloadFile ( configParams . Url + "/" + configParams . VersionFileName , versionsFilePath ) ;
35+ DownloadFile ( configParams . Url , versionsFilePath ) ;
3836 if ( ! File . Exists ( versionsFilePath ) ) return ;
3937 var versions = GeneralFileManager . GetJson < List < VersionPO > > ( versionsFilePath ) ;
4038 if ( versions == null || versions . Count == 0 ) return ;
4139 versions = versions . OrderByDescending ( x => x . PubTime ) . ToList ( ) ;
4240 var newVersion = versions . First ( ) ;
4341 //Determine whether the current client version needs to be upgraded.
44- if ( ! IsUpgrade ( configParams . CurrentVersion , newVersion . Version ) ) return ;
45- var appPath = Path . Combine ( basePath , $ "{ upgradeAppName } .exe") ;
46- if ( ! File . Exists ( appPath ) ) throw new Exception ( $ "The application does not exist { upgradeAppName } !") ;
47- //If you confirm that an update is required, start the upgrade application.
42+ if ( ! IsUpgrade ( configParams . CurrentVersion , newVersion . Version ) )
43+ return ;
44+
4845 var json = JsonSerializer . Serialize ( configParams ) ;
49- //TODO: set environment variable
50- Process . Start ( appPath , json ) ;
46+ Environment . SetEnvironmentVariable ( "ParamsOSS" , json , EnvironmentVariableTarget . User ) ;
47+
48+ //If you confirm that an update is required, start the upgrade application.
49+ var appPath = Path . Combine ( basePath , $ "{ upgradeAppName } ") ;
50+ if ( ! File . Exists ( appPath ) )
51+ throw new Exception ( $ "The application does not exist { upgradeAppName } !") ;
52+
53+ Process . Start ( appPath ) ;
5154 }
5255 catch ( Exception ex )
5356 {
54- throw new Exception ( $ " GeneralClientOSS update exception ! { ex . Message } " , ex . InnerException ) ;
57+ EventManager . Instance . Dispatch ( new GeneralClientOSS ( ) , new ExceptionEventArgs ( ex ) ) ;
5558 }
5659 finally
5760 {
@@ -61,18 +64,18 @@ await Task.Run(() =>
6164 }
6265
6366 /// <summary>
64- /// Determine whether the current client version needs to be upgraded.
67+ /// Determine whether the current client version needs to be upgraded.
6568 /// </summary>
6669 /// <param name="clientVersion"></param>
6770 /// <param name="serverVersion"></param>
6871 /// <returns>true: Upgrade required , false: No upgrade is required</returns>
6972 private static bool IsUpgrade ( string clientVersion , string serverVersion )
7073 {
71- if ( string . IsNullOrWhiteSpace ( clientVersion ) || string . IsNullOrWhiteSpace ( serverVersion ) ) return false ;
72- Version currentClientVersion = null ;
73- Version currentServerVersion = null ;
74- var isParseClientVersion = Version . TryParse ( clientVersion , out currentClientVersion ) ;
75- var isParseServerVersion = Version . TryParse ( serverVersion , out currentServerVersion ) ;
74+ if ( string . IsNullOrWhiteSpace ( clientVersion ) || string . IsNullOrWhiteSpace ( serverVersion ) )
75+ return false ;
76+
77+ var isParseClientVersion = Version . TryParse ( clientVersion , out var currentClientVersion ) ;
78+ var isParseServerVersion = Version . TryParse ( serverVersion , out var currentServerVersion ) ;
7679 if ( ! isParseClientVersion || ! isParseServerVersion ) return false ;
7780 if ( currentClientVersion < currentServerVersion ) return true ;
7881 return false ;
@@ -89,7 +92,7 @@ public static void AddListenerException(Action<object, ExceptionEventArgs> callb
8992
9093 private static void AddListener < TArgs > ( Action < object , TArgs > callbackAction ) where TArgs : EventArgs
9194 {
92- Contract . Requires ( callbackAction != null ) ;
95+ Debug . Assert ( callbackAction != null ) ;
9396 EventManager . Instance . AddListener ( callbackAction ) ;
9497 }
9598}
0 commit comments