@@ -151,12 +151,8 @@ private async void OnSetupBepInExButtonClick(object sender, RoutedEventArgs e)
151
151
) ;
152
152
}
153
153
154
- private void OnRemoveBepInExButtonClick ( object sender , RoutedEventArgs e )
154
+ private void RemoveBepInExFromGameExecutableDirectory ( )
155
155
{
156
- this . removeBepInExButton . IsEnabled = false ;
157
-
158
- this . removeBepInExButton . Content = "Removing BepInEx. Please wait..." ;
159
-
160
156
string gameExecutableParentDirectoryAbsolutePath = new FileInfo (
161
157
MainWindow . instance . mainConfig . gameExecutablePath
162
158
) . Directory . FullName ;
@@ -188,6 +184,15 @@ private void OnRemoveBepInExButtonClick(object sender, RoutedEventArgs e)
188
184
{
189
185
File . Delete ( bepInExWinHttpDllFileAbsolutePath ) ;
190
186
}
187
+ }
188
+
189
+ private void OnRemoveBepInExButtonClick ( object sender , RoutedEventArgs e )
190
+ {
191
+ this . removeBepInExButton . IsEnabled = false ;
192
+
193
+ this . removeBepInExButton . Content = "Removing BepInEx. Please wait..." ;
194
+
195
+ this . RemoveBepInExFromGameExecutableDirectory ( ) ;
191
196
192
197
this . removeBepInExButton . IsEnabled = true ;
193
198
@@ -196,6 +201,49 @@ private void OnRemoveBepInExButtonClick(object sender, RoutedEventArgs e)
196
201
this . OnBepInExSetupStatusChanged ( ) ;
197
202
}
198
203
204
+ /// <summary>
205
+ /// Downloads the latest stable version of BepInEx, places it in the mod
206
+ /// launcher's base directory, decompresses the folder, places the
207
+ /// decompressed folder in the mod launcher's base directory and then deletes the
208
+ /// compressed folder.
209
+ /// </summary>
210
+ /// <returns>
211
+ /// The DirectoryInfo object associated with the decompressed folder containing
212
+ /// the latest stable version of BepInEx.
213
+ /// </returns>
214
+ private DirectoryInfo DownloadAndDecompressBepInExLatestStableVersionInBaseDirectory ( )
215
+ {
216
+ string [ ] bepInExLatestReleaseDownloadUriStringComponents = this . bepInExLatestReleaseDownloadUri . OriginalString . Split ( '/' ) ;
217
+
218
+ string bepInExLatestReleaseZippedFileFullPath = Path . Combine (
219
+ MainWindow . instance . mainConfig . baseDirectoryPath ,
220
+ bepInExLatestReleaseDownloadUriStringComponents [ bepInExLatestReleaseDownloadUriStringComponents . Length - 1 ]
221
+ ) ;
222
+
223
+ this . webClient . DownloadFile (
224
+ this . bepInExLatestReleaseDownloadUri ,
225
+ bepInExLatestReleaseZippedFileFullPath
226
+ ) ;
227
+
228
+ string bepInExLatestReleaseUnzippedFolderFullPath = Path . Combine (
229
+ MainWindow . instance . mainConfig . baseDirectoryPath ,
230
+ "unzipped_" + bepInExLatestReleaseDownloadUriStringComponents [ bepInExLatestReleaseDownloadUriStringComponents . Length - 1 ]
231
+ ) ;
232
+
233
+ FileInfo gameExecutableFileInfo = new FileInfo (
234
+ MainWindow . instance . mainConfig . gameExecutablePath
235
+ ) ;
236
+
237
+ Directory . CreateDirectory ( bepInExLatestReleaseUnzippedFolderFullPath ) ;
238
+
239
+ ZipFile . ExtractToDirectory (
240
+ bepInExLatestReleaseZippedFileFullPath ,
241
+ bepInExLatestReleaseUnzippedFolderFullPath
242
+ ) ;
243
+
244
+ return new DirectoryInfo ( bepInExLatestReleaseUnzippedFolderFullPath ) ;
245
+ }
246
+
199
247
private void OnWebClientDownloadFileCompleted ( object sender , AsyncCompletedEventArgs e )
200
248
{
201
249
string [ ] bepInExLatestReleaseDownloadUriStringComponents = this . bepInExLatestReleaseDownloadUri . OriginalString . Split ( '/' ) ;
@@ -313,25 +361,67 @@ private bool IsBepInExAlreadySetup()
313
361
return true ;
314
362
}
315
363
316
- private void OnUpdateBepInExButtonClick ( object sender , RoutedEventArgs e )
364
+ private async void OnUpdateBepInExButtonClick ( object sender , RoutedEventArgs e )
317
365
{
318
- if ( this . IsBepInExLatestStableVersionInstalled ( ) == true )
366
+ if ( await this . IsBepInExLatestStableVersionInstalled ( ) == true )
319
367
{
320
368
return ;
321
369
}
322
370
371
+ this . updateBepInExButton . IsEnabled = false ;
372
+
373
+ this . updateBepInExButton . Content = "Updating BepInEx to the latest stable version. Please wait..." ;
374
+
375
+ /*
376
+ Steps for updating BepInEx to latest version automatically:
377
+ 1. Install latest version of BepInEx in base folder.
378
+ 2. Move all folders in BepInEx folder in game executable parent
379
+ folder that are not named "core" to the folder of the latest version of
380
+ BepInEx that was installed in Step 1.
381
+ 3. Delete BepInEx folder and its related files that are currently in the
382
+ game executable parent folder.
383
+ 4. Move the folder and related files of the latest version of BepInEx that was
384
+ installed in Step 1 to the game executable parent folder.
385
+ */
386
+ await this . UpdateBepInExLatestReleaseDownloadUri ( ) ;
387
+
388
+ DirectoryInfo bepInExLatestStableVersionDecompressedDirectoryInfo = this . DownloadAndDecompressBepInExLatestStableVersionInBaseDirectory ( ) ;
389
+
390
+ string gameExecutableBepInExDirectoryPath = Path . Combine (
391
+ new FileInfo ( MainWindow . instance . mainConfig . gameExecutablePath ) . Directory . FullName ,
392
+ "BepInEx"
393
+ ) ;
394
+
395
+ foreach ( string subDirectoryAbsolutePath in Directory . GetDirectories ( gameExecutableBepInExDirectoryPath ) )
396
+ {
397
+ if ( Path . GetDirectoryName ( subDirectoryAbsolutePath ) != "core" )
398
+ {
399
+ Directory . Move (
400
+ subDirectoryAbsolutePath ,
401
+ Path . Combine (
402
+ bepInExLatestStableVersionDecompressedDirectoryInfo . FullName ,
403
+ "BepInEx"
404
+ )
405
+ ) ;
406
+ }
407
+ }
408
+
409
+ this . RemoveBepInExFromGameExecutableDirectory ( ) ;
410
+
323
411
this . OnBepInExSetupStatusChanged ( ) ;
324
412
}
325
413
326
- private void UpdateBepInExUpdateButtonStatus ( )
414
+ private async void UpdateBepInExUpdateButtonStatus ( )
327
415
{
328
416
this . updateBepInExButton . IsEnabled = this . IsBepInExAlreadySetup ( ) ;
329
417
330
418
if ( this . updateBepInExButton . IsEnabled == true )
331
419
{
332
- if ( this . IsBepInExLatestStableVersionInstalled ( ) == true )
420
+ if ( await this . IsBepInExLatestStableVersionInstalled ( ) == true )
333
421
{
334
422
this . updateBepInExButton . Content = "Your version of BepInEx is currently up to date." ;
423
+
424
+ this . updateBepInExButton . IsEnabled = false ;
335
425
}
336
426
else
337
427
{
@@ -353,8 +443,12 @@ private void OnBepInExSetupStatusChanged()
353
443
this . UpdateBepInExUpdateButtonStatus ( ) ;
354
444
}
355
445
356
- private bool IsBepInExLatestStableVersionInstalled ( )
446
+ private async Task < bool > IsBepInExLatestStableVersionInstalled ( )
357
447
{
448
+ bool prevUpdateBepInExButtonIsEnabledValue = this . updateBepInExButton . IsEnabled ;
449
+
450
+ this . updateBepInExButton . IsEnabled = false ;
451
+
358
452
FileVersionInfo bepInExDllFileVersionInfo = FileVersionInfo . GetVersionInfo (
359
453
Path . Combine (
360
454
new FileInfo ( MainWindow . instance . mainConfig . gameExecutablePath ) . Directory . FullName ,
@@ -364,7 +458,24 @@ private bool IsBepInExLatestStableVersionInstalled()
364
458
)
365
459
) ;
366
460
367
- MessageBox . Show ( bepInExDllFileVersionInfo . FileVersion ) ;
461
+ string bepInExDllFileVersion = $ "{ bepInExDllFileVersionInfo . FileMajorPart } .{ bepInExDllFileVersionInfo . FileMinorPart } .{ bepInExDllFileVersionInfo . FileBuildPart } ";
462
+
463
+ foreach ( Release bepInExRelease in await this . gitHubClient . Repository . Release . GetAll ( "BepInEx" , "BepInEx" ) )
464
+ {
465
+ if ( bepInExRelease . Prerelease == false )
466
+ {
467
+ if ( bepInExDllFileVersion == bepInExRelease . TagName . Replace ( "v" , "" ) )
468
+ {
469
+ this . updateBepInExButton . IsEnabled = prevUpdateBepInExButtonIsEnabledValue ;
470
+
471
+ return true ;
472
+ }
473
+
474
+ break ;
475
+ }
476
+ }
477
+
478
+ this . updateBepInExButton . IsEnabled = prevUpdateBepInExButtonIsEnabledValue ;
368
479
369
480
return false ;
370
481
}
0 commit comments