Skip to content

Commit 5804c44

Browse files
committed
Changelog:
- Modified SettingsPage.xaml.cs.
1 parent 37f8405 commit 5804c44

File tree

1 file changed

+122
-11
lines changed

1 file changed

+122
-11
lines changed

SettingsPage.xaml.cs

Lines changed: 122 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,8 @@ private async void OnSetupBepInExButtonClick(object sender, RoutedEventArgs e)
151151
);
152152
}
153153

154-
private void OnRemoveBepInExButtonClick(object sender, RoutedEventArgs e)
154+
private void RemoveBepInExFromGameExecutableDirectory()
155155
{
156-
this.removeBepInExButton.IsEnabled = false;
157-
158-
this.removeBepInExButton.Content = "Removing BepInEx. Please wait...";
159-
160156
string gameExecutableParentDirectoryAbsolutePath = new FileInfo(
161157
MainWindow.instance.mainConfig.gameExecutablePath
162158
).Directory.FullName;
@@ -188,6 +184,15 @@ private void OnRemoveBepInExButtonClick(object sender, RoutedEventArgs e)
188184
{
189185
File.Delete(bepInExWinHttpDllFileAbsolutePath);
190186
}
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();
191196

192197
this.removeBepInExButton.IsEnabled = true;
193198

@@ -196,6 +201,49 @@ private void OnRemoveBepInExButtonClick(object sender, RoutedEventArgs e)
196201
this.OnBepInExSetupStatusChanged();
197202
}
198203

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+
199247
private void OnWebClientDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
200248
{
201249
string[] bepInExLatestReleaseDownloadUriStringComponents = this.bepInExLatestReleaseDownloadUri.OriginalString.Split('/');
@@ -313,25 +361,67 @@ private bool IsBepInExAlreadySetup()
313361
return true;
314362
}
315363

316-
private void OnUpdateBepInExButtonClick(object sender, RoutedEventArgs e)
364+
private async void OnUpdateBepInExButtonClick(object sender, RoutedEventArgs e)
317365
{
318-
if (this.IsBepInExLatestStableVersionInstalled() == true)
366+
if (await this.IsBepInExLatestStableVersionInstalled() == true)
319367
{
320368
return;
321369
}
322370

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+
323411
this.OnBepInExSetupStatusChanged();
324412
}
325413

326-
private void UpdateBepInExUpdateButtonStatus()
414+
private async void UpdateBepInExUpdateButtonStatus()
327415
{
328416
this.updateBepInExButton.IsEnabled = this.IsBepInExAlreadySetup();
329417

330418
if (this.updateBepInExButton.IsEnabled == true)
331419
{
332-
if (this.IsBepInExLatestStableVersionInstalled() == true)
420+
if (await this.IsBepInExLatestStableVersionInstalled() == true)
333421
{
334422
this.updateBepInExButton.Content = "Your version of BepInEx is currently up to date.";
423+
424+
this.updateBepInExButton.IsEnabled = false;
335425
}
336426
else
337427
{
@@ -353,8 +443,12 @@ private void OnBepInExSetupStatusChanged()
353443
this.UpdateBepInExUpdateButtonStatus();
354444
}
355445

356-
private bool IsBepInExLatestStableVersionInstalled()
446+
private async Task<bool> IsBepInExLatestStableVersionInstalled()
357447
{
448+
bool prevUpdateBepInExButtonIsEnabledValue = this.updateBepInExButton.IsEnabled;
449+
450+
this.updateBepInExButton.IsEnabled = false;
451+
358452
FileVersionInfo bepInExDllFileVersionInfo = FileVersionInfo.GetVersionInfo(
359453
Path.Combine(
360454
new FileInfo(MainWindow.instance.mainConfig.gameExecutablePath).Directory.FullName,
@@ -364,7 +458,24 @@ private bool IsBepInExLatestStableVersionInstalled()
364458
)
365459
);
366460

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;
368479

369480
return false;
370481
}

0 commit comments

Comments
 (0)