@@ -6,24 +6,44 @@ namespace AmazTool
66{
77 internal class UpgradeApp
88 {
9- public static bool Upgrade ( string fileName )
9+ public static void Upgrade ( string fileName )
1010 {
11- Utils . WriteLine ( $ "{ Resx . Resource . StartUnzipping } \n { fileName } ") ;
11+ Console . WriteLine ( $ "{ Resx . Resource . StartUnzipping } \n { fileName } ") ;
12+
13+ Utils . Waiting ( 5 ) ;
1214
1315 if ( ! File . Exists ( fileName ) )
1416 {
15- Utils . WriteLine ( Resx . Resource . UpgradeFileNotFound ) ;
16- return false ;
17+ Console . WriteLine ( Resx . Resource . UpgradeFileNotFound ) ;
18+ return ;
1719 }
1820
19- Utils . Waiting ( 5 ) ;
20-
21- KillV2rayN ( ) ;
21+ Console . WriteLine ( Resx . Resource . TryTerminateProcess ) ;
22+ try
23+ {
24+ var existing = Process . GetProcessesByName ( Utils . V2rayN ) ;
25+ foreach ( var pp in existing )
26+ {
27+ var path = pp . MainModule ? . FileName ?? "" ;
28+ if ( path . StartsWith ( Utils . GetPath ( Utils . V2rayN ) ) )
29+ {
30+ pp ? . Kill ( ) ;
31+ pp ? . WaitForExit ( 1000 ) ;
32+ }
33+ }
34+ }
35+ catch ( Exception ex )
36+ {
37+ // Access may be denied without admin right. The user may not be an administrator.
38+ Console . WriteLine ( Resx . Resource . FailedTerminateProcess + ex . StackTrace ) ;
39+ }
2240
23- Utils . WriteLine ( Resx . Resource . StartUnzipping ) ;
41+ Console . WriteLine ( Resx . Resource . StartUnzipping ) ;
2442 StringBuilder sb = new ( ) ;
2543 try
2644 {
45+ var thisAppOldFile = $ "{ Utils . GetExePath ( ) } .tmp";
46+ File . Delete ( thisAppOldFile ) ;
2747 var splitKey = "/" ;
2848
2949 using var archive = ZipFile . OpenRead ( fileName ) ;
@@ -36,7 +56,7 @@ public static bool Upgrade(string fileName)
3656 continue ;
3757 }
3858
39- Utils . WriteLine ( entry . FullName ) ;
59+ Console . WriteLine ( entry . FullName ) ;
4060
4161 var lst = entry . FullName . Split ( splitKey ) ;
4262 if ( lst . Length == 1 )
@@ -45,80 +65,53 @@ public static bool Upgrade(string fileName)
4565 }
4666
4767 var fullName = string . Join ( splitKey , lst [ 1 ..lst . Length ] ) ;
68+
69+ if ( string . Equals ( Utils . GetExePath ( ) , Utils . GetPath ( fullName ) , StringComparison . OrdinalIgnoreCase ) )
70+ {
71+ File . Move ( Utils . GetExePath ( ) , thisAppOldFile ) ;
72+ }
73+
4874 var entryOutputPath = Utils . GetPath ( fullName ) ;
4975 Directory . CreateDirectory ( Path . GetDirectoryName ( entryOutputPath ) ! ) ;
5076 //In the bin folder, if the file already exists, it will be skipped
5177 if ( fullName . StartsWith ( "bin" ) && File . Exists ( entryOutputPath ) )
5278 {
5379 continue ;
5480 }
55- entry . ExtractToFile ( entryOutputPath , true ) ;
5681
57- Utils . WriteLine ( entryOutputPath ) ;
82+ try
83+ {
84+ entry . ExtractToFile ( entryOutputPath , true ) ;
85+ }
86+ catch
87+ {
88+ Thread . Sleep ( 1000 ) ;
89+ entry . ExtractToFile ( entryOutputPath , true ) ;
90+ }
91+
92+ Console . WriteLine ( entryOutputPath ) ;
5893 }
5994 catch ( Exception ex )
6095 {
61- sb . Append ( ex . Message ) ;
6296 sb . Append ( ex . StackTrace ) ;
6397 }
6498 }
6599 }
66100 catch ( Exception ex )
67101 {
68- sb . Append ( Resx . Resource . FailedUpgrade + ex . StackTrace ) ;
102+ Console . WriteLine ( Resx . Resource . FailedUpgrade + ex . StackTrace ) ;
103+ //return;
69104 }
70-
71- if ( sb . Length <= 0 )
105+ if ( sb . Length > 0 )
72106 {
73- return true ;
107+ Console . WriteLine ( Resx . Resource . FailedUpgrade + sb . ToString ( ) ) ;
108+ //return;
74109 }
75110
76- Utils . WriteLine ( sb . ToString ( ) ) ;
77- Utils . WriteLine ( Resx . Resource . FailedUpgrade ) ;
78- return false ;
79- }
80-
81- public static bool Init ( )
82- {
83- //Process temporary files generated by the last update
84- var files = Directory . GetFiles ( Utils . GetPath ( "" ) , "*.tmp" ) ;
85- foreach ( var file in files )
86- {
87- if ( file . Contains ( Utils . AmazTool ) )
88- {
89- File . Delete ( file ) ;
90- }
91- }
92-
93- var destFileName = $ "{ Utils . GetExePath ( ) } { Guid . NewGuid ( ) . ToString ( "N" ) [ ..8 ] } .tmp";
94- File . Move ( Utils . GetExePath ( ) , destFileName ) ;
95-
96- return true ;
97- }
98-
99- private static bool KillV2rayN ( )
100- {
101- Utils . WriteLine ( Resx . Resource . TryTerminateProcess ) ;
102- try
103- {
104- var existing = Process . GetProcessesByName ( Utils . V2rayN ) ;
105- foreach ( var pp in existing )
106- {
107- var path = pp . MainModule ? . FileName ?? "" ;
108- if ( path . StartsWith ( Utils . GetPath ( Utils . V2rayN ) ) )
109- {
110- pp ? . Kill ( ) ;
111- pp ? . WaitForExit ( 1000 ) ;
112- }
113- }
114- }
115- catch ( Exception ex )
116- {
117- // Access may be denied without admin right. The user may not be an administrator.
118- Utils . WriteLine ( Resx . Resource . FailedTerminateProcess + ex . StackTrace ) ;
119- }
111+ Console . WriteLine ( Resx . Resource . Restartv2rayN ) ;
112+ Utils . Waiting ( 2 ) ;
120113
121- return true ;
114+ Utils . StartV2RayN ( ) ;
122115 }
123116 }
124117}
0 commit comments