@@ -26,13 +26,15 @@ private struct GithubResponse
2626 }
2727
2828 private static readonly HttpClient WebClient = new ( ) ;
29- private static readonly string GithubReleaseURL = "https://api.github.com/repos/ducng99/EasyERPMod/releases/latest " ;
29+ private static readonly string GithubReleaseURL = "https://api.github.com/repos/ducng99/EasyERPMod/releases/tags/v1.4.0 " ;
3030
3131 public string ChangeLog { get ; private set ; }
3232 public string Version { get ; private set ; }
3333 public string DownloadURL { get ; private set ; }
3434 public uint Size { get ; private set ; }
3535
36+ private static readonly string DownloadedFilePath = Path . Combine ( Path . GetTempPath ( ) , "EasyERPMod_Temp.zip" ) ;
37+ private static readonly string FolderExtracted = Path . Combine ( Path . GetTempPath ( ) , "EasyERPMod_TempEx" ) ;
3638 private static readonly string SelfUpdateFilePath = Path . Combine ( Path . GetTempPath ( ) , "EasySelfUpdater.bat" ) ;
3739 private static readonly string SelfUpdateBatContent = $@ "
3840@ECHO off
@@ -99,11 +101,10 @@ public async void DownloadAndInstall(MainWindow window)
99101 {
100102 try
101103 {
102- string filePath = Path . Combine ( Path . GetTempPath ( ) , "EasyERPMod_Temp.zip" ) ;
103104 using ( var httpStream = await WebClient . GetStreamAsync ( DownloadURL ) )
104105 {
105- if ( File . Exists ( filePath ) ) File . Delete ( filePath ) ;
106- using var fileStream = new FileStream ( filePath , FileMode . Create , FileAccess . Write , FileShare . Read ) ;
106+ if ( File . Exists ( DownloadedFilePath ) ) File . Delete ( DownloadedFilePath ) ;
107+ using var fileStream = new FileStream ( DownloadedFilePath , FileMode . Create , FileAccess . Write , FileShare . Read ) ;
107108 await httpStream . CopyToAsync ( fileStream ) ;
108109 }
109110
@@ -122,56 +123,67 @@ public async void DownloadAndInstall(MainWindow window)
122123 }
123124
124125 // Install
125- string folderExtracted = Path . Combine ( Path . GetTempPath ( ) , "EasyERPMod_TempEx" ) ;
126- if ( Directory . Exists ( folderExtracted ) )
127- Directory . Delete ( folderExtracted , true ) ;
128- ZipFile . ExtractToDirectory ( filePath , folderExtracted ) ;
126+ if ( Directory . Exists ( FolderExtracted ) )
127+ Directory . Delete ( FolderExtracted , true ) ;
128+ ZipFile . ExtractToDirectory ( DownloadedFilePath , FolderExtracted ) ;
129129
130- string realFolderPath = Path . Combine ( folderExtracted , "EasyERPMod" ) ;
131- var filesInFolder = Directory . EnumerateFiles ( realFolderPath , "*" , SearchOption . AllDirectories ) ;
130+ string realFolderPath = Path . Combine ( FolderExtracted , "EasyERPMod" ) ;
131+ var filesInExtractedFolder = Directory . EnumerateFiles ( realFolderPath , "*" , SearchOption . AllDirectories ) ;
132132
133133 ProcessStartInfo psi = null ;
134134 Regex modsFolderCheck = new ( @"^_MODS.+" , RegexOptions . Compiled ) ;
135135
136- foreach ( var file in filesInFolder )
136+ foreach ( var file in filesInExtractedFolder )
137137 {
138138 string relativePath = Path . GetRelativePath ( realFolderPath , file ) ;
139139
140140 if ( ! modsFolderCheck . IsMatch ( relativePath ) )
141141 {
142- if ( Path . GetFileName ( relativePath ) . Equals ( Process . GetCurrentProcess ( ) . ProcessName + ".exe" ) )
142+ try
143143 {
144- string newUpdaterPath = relativePath + "_new" ;
145- File . Move ( file , newUpdaterPath , true ) ;
146- File . WriteAllText ( SelfUpdateFilePath , SelfUpdateBatContent ) ;
147-
148- psi = new ( )
144+ if ( Path . GetFileName ( relativePath ) . Equals ( Process . GetCurrentProcess ( ) . ProcessName + ".exe" ) )
145+ {
146+ string newUpdaterPath = relativePath + "_new" ;
147+ File . Move ( file , newUpdaterPath , true ) ;
148+ File . WriteAllText ( SelfUpdateFilePath , SelfUpdateBatContent ) ;
149+
150+ psi = new ( )
151+ {
152+ FileName = SelfUpdateFilePath ,
153+ Arguments = $ "\" { newUpdaterPath } \" \" { relativePath } \" ",
154+ WorkingDirectory = Directory . GetCurrentDirectory ( ) ,
155+ CreateNoWindow = true ,
156+ WindowStyle = ProcessWindowStyle . Hidden
157+ } ;
158+ }
159+ else
149160 {
150- FileName = SelfUpdateFilePath ,
151- Arguments = $ "\" { newUpdaterPath } \" \" { relativePath } \" ",
152- WorkingDirectory = Directory . GetCurrentDirectory ( ) ,
153- CreateNoWindow = true ,
154- WindowStyle = ProcessWindowStyle . Hidden
155- } ;
161+ string directoryPath = Path . GetDirectoryName ( relativePath ) ;
162+ if ( ! string . IsNullOrEmpty ( directoryPath ) )
163+ new DirectoryInfo ( directoryPath ) . Create ( ) ;
164+ File . Move ( file , relativePath , true ) ;
165+ }
156166 }
157- else
167+ catch ( IOException ex )
158168 {
159- File . Move ( file , relativePath , true ) ;
169+ throw new Exception ( $ "Failed when installing \" { file } \" \n " , ex ) ;
160170 }
161171 }
162172 }
163173
164- // Cleanup
165- if ( Directory . Exists ( folderExtracted ) )
166- Directory . Delete ( folderExtracted , true ) ;
167- if ( File . Exists ( filePath ) )
168- File . Delete ( filePath ) ;
169-
170174 window . OnInstallCompleted ( psi ) ;
171175 }
172176 catch ( Exception ex )
173177 {
174- window . OnInstallFailed ( ex . ToString ( ) ) ;
178+ window . OnInstallFailed ( $ "{ ex } ") ;
179+ }
180+ finally
181+ {
182+ // Cleanup
183+ if ( Directory . Exists ( FolderExtracted ) )
184+ Directory . Delete ( FolderExtracted , true ) ;
185+ if ( File . Exists ( DownloadedFilePath ) )
186+ File . Delete ( DownloadedFilePath ) ;
175187 }
176188 }
177189 }
0 commit comments