-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMainWindow.xaml.cs
1586 lines (1528 loc) · 57 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Hi3Helper.Http;
using Microsoft.Win32;
using SevenZip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shell;
namespace BetterHI3Launcher
{
public partial class MainWindow : Window
{
public static readonly string miHoYoPath = Path.Combine(App.LocalLowPath, "miHoYo");
public static string GameInstallPath, GameCachePath, GameRegistryPath, GameArchivePath, GameArchiveTempPath, GameExePath;
public static string RegistryVersionInfo;
public static string GameWebProfileURL, GameFullName, GameArchiveName, GameExeName, GameInstallRegistryName;
public static bool DownloadPaused, PatchDownload, PreloadDownload, BackgroundImageDownloading, LegacyBoxActive, ActionAbort;
public static int PatchDownloadInt;
public static RoutedCommand DownloadCacheCommand = new RoutedCommand();
public static RoutedCommand RepairGameCommand = new RoutedCommand();
public static RoutedCommand MoveGameCommand = new RoutedCommand();
public static RoutedCommand UninstallGameCommand = new RoutedCommand();
public static RoutedCommand WebProfileCommand = new RoutedCommand();
public static RoutedCommand FeedbackCommand = new RoutedCommand();
public static RoutedCommand ChangelogCommand = new RoutedCommand();
public static RoutedCommand CustomBackgroundCommand = new RoutedCommand();
public static RoutedCommand ToggleLogCommand = new RoutedCommand();
public static RoutedCommand ToggleSoundsCommand = new RoutedCommand();
public static RoutedCommand AboutCommand = new RoutedCommand();
public dynamic LocalVersionInfo, OnlineVersionInfo, OnlineRepairInfo, miHoYoVersionInfo;
public dynamic GameGraphicSettings, GameScreenSettings;
LauncherStatus _status;
HI3Server _gameserver;
HI3Mirror _downloadmirror;
Http httpclient;
HttpProp httpprop;
CancellationTokenSource token;
DownloadProgressTracker tracker = new DownloadProgressTracker(50, TimeSpan.FromMilliseconds(500));
internal LauncherStatus Status
{
get => _status;
set => Dispatcher.Invoke(() =>
{
void ToggleUI(bool val)
{
LaunchButton.IsEnabled = val;
OptionsButton.IsEnabled = val;
ServerDropdown.IsEnabled = val;
MirrorDropdown.IsEnabled = val;
ToggleContextMenuItems(val);
DownloadProgressBarStackPanel.Visibility = Visibility.Collapsed;
DownloadETAText.Visibility = Visibility.Visible;
DownloadSpeedText.Visibility = Visibility.Visible;
DownloadPauseButton.Visibility = Visibility.Visible;
DownloadResumeButton.Visibility = Visibility.Collapsed;
}
void ToggleProgressBar(bool val)
{
ProgressText.Visibility = val ? Visibility.Visible : Visibility.Collapsed;
ProgressBar.Visibility = val ? Visibility.Visible : Visibility.Collapsed;
ProgressBar.IsIndeterminate = true;
TaskbarItemInfo.ProgressState = val ? TaskbarItemProgressState.Indeterminate : TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
}
_status = value;
switch(_status)
{
case LauncherStatus.Ready:
ProgressText.Text = string.Empty;
ToggleUI(true);
ToggleProgressBar(false);
ProgressBar.IsIndeterminate = false;
break;
case LauncherStatus.Error:
ProgressText.Text = App.TextStrings["progresstext_error"];
ToggleUI(false);
ToggleProgressBar(false);
ProgressBar.IsIndeterminate = false;
ToggleLog(true);
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Error;
FlashMainWindow();
break;
case LauncherStatus.CheckingUpdates:
ProgressText.Text = App.TextStrings["progresstext_checking_update"];
ToggleUI(false);
ToggleProgressBar(true);
break;
case LauncherStatus.Downloading:
DownloadPaused = false;
ProgressText.Text = App.TextStrings["progresstext_initiating_download"];
LaunchButton.Content = App.TextStrings["button_downloading"];
ToggleUI(false);
OptionsButton.IsEnabled = true;
ToggleProgressBar(true);
ProgressBar.IsIndeterminate = false;
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
break;
case LauncherStatus.DownloadPaused:
DownloadPaused = true;
ProgressText.Text = string.Empty;
ToggleUI(true);
ToggleProgressBar(false);
ToggleContextMenuItems(false);
ProgressBar.IsIndeterminate = false;
break;
case LauncherStatus.Preloading:
PreloadBottomText.Text = App.TextStrings["button_downloading"];
PreloadButton.Visibility = Visibility.Collapsed;
PreloadPauseButton.IsEnabled = false;
PreloadPauseButton.Visibility = Visibility.Visible;
PreloadPauseButton.Background = (ImageBrush)Resources["PreloadPauseButton"];
PreloadCircle.Visibility = Visibility.Visible;
PreloadCircleProgressBar.Visibility = Visibility.Visible;
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
ToggleContextMenuItems(false);
break;
case LauncherStatus.PreloadVerifying:
PreloadPauseButton.IsEnabled = false;
PreloadCircleProgressBar.Value = 0;
PreloadBottomText.Text = App.TextStrings["label_verifying"];
PreloadStatusMiddleRightText.Text = string.Empty;
PreloadStatusBottomRightText.Text = string.Empty;
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
break;
case LauncherStatus.Working:
ToggleUI(false);
ToggleProgressBar(true);
break;
case LauncherStatus.Running:
ProgressText.Text = string.Empty;
LaunchButton.Content = App.TextStrings["button_running"];
ToggleUI(false);
OptionsButton.IsEnabled = true;
ToggleProgressBar(false);
ToggleContextMenuItems(false);
ProgressBar.IsIndeterminate = false;
break;
case LauncherStatus.Verifying:
ProgressText.Text = App.TextStrings["progresstext_verifying_files"];
ToggleUI(false);
OptionsButton.IsEnabled = true;
ToggleProgressBar(true);
break;
case LauncherStatus.Unpacking:
ProgressText.Text = string.Empty;
ToggleProgressBar(false);
DownloadProgressBarStackPanel.Visibility = Visibility.Visible;
DownloadProgressText.Text = App.TextStrings["progresstext_unpacking_1"];
DownloadPauseButton.Visibility = Visibility.Collapsed;
DownloadETAText.Visibility = Visibility.Collapsed;
DownloadSpeedText.Visibility = Visibility.Collapsed;
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
break;
case LauncherStatus.UpdateAvailable:
ProgressText.Text = string.Empty;
ToggleUI(true);
ToggleProgressBar(false);
ToggleContextMenuItems(false, true);
ProgressBar.IsIndeterminate = false;
break;
case LauncherStatus.Uninstalling:
ProgressText.Text = App.TextStrings["progresstext_uninstalling"];
ToggleUI(false);
OptionsButton.IsEnabled = true;
ToggleProgressBar(true);
break;
}
});
}
internal HI3Server Server
{
get => _gameserver;
set
{
_gameserver = value;
switch(_gameserver)
{
case HI3Server.GLB:
RegistryVersionInfo = "VersionInfoGlobal";
GameFullName = "Honkai Impact 3rd";
GameInstallRegistryName = GameFullName;
GameWebProfileURL = "https://account.hoyoverse.com";
break;
case HI3Server.SEA:
RegistryVersionInfo = "VersionInfoSEA";
GameFullName = "Honkai Impact 3";
GameInstallRegistryName = GameFullName;
GameWebProfileURL = "https://account.hoyoverse.com";
break;
case HI3Server.CN:
RegistryVersionInfo = "VersionInfoCN";
GameFullName = "崩坏3";
GameInstallRegistryName = GameFullName;
GameWebProfileURL = "https://user.mihoyo.com";
break;
case HI3Server.TW:
RegistryVersionInfo = "VersionInfoTW";
GameFullName = "崩壊3rd";
GameInstallRegistryName = "崩壞3rd";
GameWebProfileURL = "https://account.hoyoverse.com";
break;
case HI3Server.KR:
RegistryVersionInfo = "VersionInfoKR";
GameFullName = "붕괴3rd";
GameInstallRegistryName = GameFullName;
GameWebProfileURL = "https://account.hoyoverse.com";
break;
case HI3Server.JP:
RegistryVersionInfo = "VersionInfoJP";
GameFullName = "崩壊3rd";
GameInstallRegistryName = GameFullName;
GameWebProfileURL = "https://account.hoyoverse.com";
break;
}
GameRegistryPath = $@"SOFTWARE\miHoYo\{GameFullName}";
GameCachePath = Path.Combine(miHoYoPath, GameFullName);
}
}
internal HI3Mirror Mirror
{
get => _downloadmirror;
set
{
_downloadmirror = value;
}
}
public MainWindow()
{
InitializeComponent();
var args = new List<string>();
for(int i = 1; i < App.CommandLineArgs.Length; i++)
{
args.Add(App.CommandLineArgs[i].ToUpper());
}
if(args.Contains("NOLOG"))
{
App.DisableLogging = true;
}
if(!App.DisableLogging)
{
try
{
if(File.Exists(App.LauncherLogFile))
{
string old_log_path_1 = Path.Combine(App.LauncherDataPath, "BetterHI3Launcher-old1.log");
for(int i = 9; i > 0; i--)
{
string old_log_path_2 = Path.Combine(App.LauncherDataPath, $"BetterHI3Launcher-old{i}.log");
if(File.Exists(old_log_path_2))
{
string old_log_path_3 = Path.Combine(App.LauncherDataPath, $"BetterHI3Launcher-old{i + 1}.log");
string old_log_path_4 = Path.Combine(App.LauncherDataPath, "BetterHI3Launcher-old10.log");
if(File.Exists(old_log_path_4))
{
File.Delete(old_log_path_4);
}
File.Move(old_log_path_2, old_log_path_3);
}
}
File.Move(App.LauncherLogFile, old_log_path_1);
}
}
catch
{
Log("Unable to rename log files", true, 2);
}
}
DeleteFile(App.LauncherLogFile, true);
try
{
Log($"BetterHI3Launcher v{App.LocalLauncherVersion}", false);
Log($"Working directory: {App.LauncherRootPath}");
Log($"OS version: {App.OSVersion}");
Log($"OS language: {App.OSLanguage}");
#if !DEBUG
if(args.Contains("NOUPDATE"))
{
App.DisableAutoUpdate = true;
App.UserAgentComment.Add("NOUPDATE");
Log("Auto-update disabled");
}
#endif
if(args.Contains("NOLOG"))
{
App.UserAgentComment.Add("NOLOG");
Log("Logging to file disabled");
}
if(args.Contains("NOTRANSLATIONS"))
{
App.DisableTranslations = true;
App.LauncherLanguage = "en";
App.UserAgentComment.Add("NOTRANSLATIONS");
Log("Translations disabled, only English will be available");
}
if(args.Contains("ADVANCED"))
{
App.AdvancedFeatures = true;
App.UserAgentComment.Add("ADVANCED");
Log("Advanced features enabled");
}
else
{
RepairBoxGenerateButton.Visibility = Visibility.Collapsed;
}
string language_log_msg = "Launcher language: {0}";
var language_reg = App.LauncherRegKey.GetValue("Language");
if(!App.DisableTranslations)
{
if(language_reg != null)
{
if(App.LauncherRegKey.GetValueKind("Language") == RegistryValueKind.String)
{
SetLanguage(language_reg.ToString());
}
}
else
{
SetLanguage(App.LauncherLanguage);
language_log_msg += " (autodetect)";
}
}
language_log_msg = string.Format(language_log_msg, App.LauncherLanguage);
Log(language_log_msg);
App.UserAgentComment.Add(App.LauncherLanguage);
App.UserAgentComment.Add(App.OSLanguage);
App.UserAgentComment.Add(App.OSVersion);
App.UserAgent += $" ({string.Join("; ", App.UserAgentComment)})";
LaunchButton.Content = App.TextStrings["button_download"];
ServerLabel.Text = $"{App.TextStrings["label_server"]}:";
MirrorLabel.Text = $"{App.TextStrings["label_mirror"]}:";
IntroBoxTitleTextBlock.Text = App.TextStrings["introbox_title"];
IntroBoxMessageTextBlock.Text = App.TextStrings["introbox_msg"];
IntroBoxOKButton.Content = App.TextStrings["button_ok"];
RepairBoxTitleTextBlock.Text = App.TextStrings["contextmenu_repair"];
RepairBoxYesButton.Content = App.TextStrings["button_yes"];
RepairBoxNoButton.Content = App.TextStrings["button_no"];
RepairBoxGenerateButton.Content = App.TextStrings["button_generate"];
FPSInputBoxTitleTextBlock.Text = App.TextStrings["fpsinputbox_title"];
CombatFPSInputBoxTextBlock.Text = App.TextStrings["fpsinputbox_label_combatfps"];
MenuFPSInputBoxTextBlock.Text = App.TextStrings["fpsinputbox_label_menufps"];
FPSInputBoxOKButton.Content = App.TextStrings["button_confirm"];
FPSInputBoxCancelButton.Content = App.TextStrings["button_cancel"];
ResolutionInputBoxTitleTextBlock.Text = App.TextStrings["resolutioninputbox_title"];
ResolutionInputBoxWidthTextBlock.Text = $"{App.TextStrings["resolutioninputbox_label_width"]}:";
ResolutionInputBoxHeightTextBlock.Text = $"{App.TextStrings["resolutioninputbox_label_height"]}:";
ResolutionInputBoxFullscreenTextBlock.Text = $"{App.TextStrings["resolutioninputbox_label_fullscreen"]}:";
ResolutionInputBoxOKButton.Content = App.TextStrings["button_confirm"];
ResolutionInputBoxCancelButton.Content = App.TextStrings["button_cancel"];
ChangelogBoxTitleTextBlock.Text = App.TextStrings["changelogbox_title"];
ChangelogBoxMessageTextBlock.Text = App.TextStrings["changelogbox_1_msg"];
ChangelogBoxOKButton.Content = App.TextStrings["button_ok"];
AboutBoxTitleTextBlock.Text = App.TextStrings["contextmenu_about"];
AboutBoxAppNameTextBlock.Text += $" v{App.LocalLauncherVersion}";
AboutBoxMessageTextBlock.Text = $"{App.TextStrings["aboutbox_msg"]}\n\nMade by Bp (BuIlDaLiBlE production).";
AboutBoxGitHubButton.Content = App.TextStrings["button_github"];
AboutBoxOKButton.Content = App.TextStrings["button_ok"];
AnnouncementBoxOKButton.Content = App.TextStrings["button_ok"];
AnnouncementBoxDoNotShowCheckbox.Content = App.TextStrings["announcementbox_do_not_show"];
PreloadTopText.Text = App.TextStrings["label_pre_install"];
PreloadStatusMiddleLeftText.Text = App.TextStrings["label_eta"];
TitleBar.MouseLeftButtonDown += delegate{DragMove();};
PreloadGrid.Visibility = Visibility.Collapsed;
DownloadProgressBarStackPanel.Visibility = Visibility.Collapsed;
LogBox.Visibility = Visibility.Collapsed;
LogBoxRichTextBox.Document.PageWidth = LogBox.Width;
IntroBox.Visibility = Visibility.Collapsed;
RepairBox.Visibility = Visibility.Collapsed;
FPSInputBox.Visibility = Visibility.Collapsed;
ResolutionInputBox.Visibility = Visibility.Collapsed;
ChangelogBox.Visibility = Visibility.Collapsed;
AboutBox.Visibility = Visibility.Collapsed;
AnnouncementBox.Visibility = Visibility.Collapsed;
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full");
if(key == null || (int)key.GetValue("Release") < 394802)
{
MessageBox.Show(App.TextStrings["msgbox_net_version_old_msg"], App.TextStrings["msgbox_start_error_title"], MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return;
}
Server = HI3Server.GLB;
var last_selected_server_reg = App.LauncherRegKey.GetValue("LastSelectedServer");
if(last_selected_server_reg != null)
{
if(App.LauncherRegKey.GetValueKind("LastSelectedServer") == RegistryValueKind.DWord)
{
switch((int)last_selected_server_reg)
{
case 0:
Server = HI3Server.GLB;
break;
case 1:
Server = HI3Server.SEA;
break;
case 2:
Server = HI3Server.CN;
break;
case 3:
Server = HI3Server.TW;
break;
case 4:
Server = HI3Server.KR;
break;
case 5:
Server = HI3Server.JP;
break;
}
}
}
ServerDropdown.SelectedIndex = (int)Server;
try
{
FetchOnlineVersionInfo();
}
catch(Exception ex)
{
if(Status == LauncherStatus.Error)
{
return;
}
Status = LauncherStatus.Error;
MessageBox.Show($"{App.TextStrings["msgbox_conn_bp_error_msg"]}\n{ex.Message}", App.TextStrings["msgbox_net_error_title"], MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return;
}
try
{
FetchmiHoYoVersionInfo();
}
catch(Exception ex)
{
if(Status == LauncherStatus.Error)
{
return;
}
Status = LauncherStatus.Error;
MessageBox.Show($"{App.TextStrings["msgbox_conn_mihoyo_error_msg"]}\n{ex.Message}", App.TextStrings["msgbox_net_error_title"], MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return;
}
OptionsContextMenu.Items.Clear();
var CM_Screenshots = new MenuItem {Header = App.TextStrings["contextmenu_open_screenshots_dir"], InputGestureText = "Ctrl+S"};
CM_Screenshots.Click += (sender, e) => CM_Screenshots_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Screenshots);
var CM_Download_Cache = new MenuItem{Header = App.TextStrings["contextmenu_download_cache"], InputGestureText = "Ctrl+D"};
CM_Download_Cache.Click += (sender, e) => CM_DownloadCache_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Download_Cache);
var CM_Repair = new MenuItem{Header = App.TextStrings["contextmenu_repair"], InputGestureText = "Ctrl+R"};
CM_Repair.Click += async (sender, e) => await CM_Repair_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Repair);
var CM_Move = new MenuItem{Header = App.TextStrings["contextmenu_move"], InputGestureText = "Ctrl+M"};
CM_Move.Click += async (sender, e) => await CM_Move_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Move);
var CM_Uninstall = new MenuItem{Header = App.TextStrings["contextmenu_uninstall"], InputGestureText = "Ctrl+U"};
CM_Uninstall.Click += async (sender, e) => await CM_Uninstall_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Uninstall);
var CM_Game_Settings = new MenuItem{Header = App.TextStrings["contextmenu_game_settings"]};
var CM_Custom_FPS = new MenuItem{Header = App.TextStrings["contextmenu_custom_fps"]};
CM_Custom_FPS.Click += (sender, e) => CM_CustomFPS_Click(sender, e);
CM_Game_Settings.Items.Add(CM_Custom_FPS);
var CM_Custom_Resolution = new MenuItem{Header = App.TextStrings["contextmenu_custom_resolution"]};
CM_Custom_Resolution.Click += (sender, e) => CM_CustomResolution_Click(sender, e);
CM_Game_Settings.Items.Add(CM_Custom_Resolution);
var CM_Custom_Launch_Options = new MenuItem{Header = App.TextStrings["contextmenu_custom_launch_options"]};
CM_Custom_Launch_Options.Click += (sender, e) => CM_CustomLaunchOptions_Click(sender, e);
CM_Game_Settings.Items.Add(CM_Custom_Launch_Options);
var CM_Download_Type = new MenuItem{Header = App.TextStrings["contextmenu_reset_download_type"]};
CM_Download_Type.Click += (sender, e) => CM_ResetDownloadType_Click(sender, e);
CM_Game_Settings.Items.Add(CM_Download_Type);
OptionsContextMenu.Items.Add(CM_Game_Settings);
var CM_Web_Profile = new MenuItem{Header = App.TextStrings["contextmenu_web_profile"], InputGestureText = "Ctrl+P"};
CM_Web_Profile.Click += (sender, e) => BpUtility.StartProcess(GameWebProfileURL, null, App.LauncherRootPath, true);
OptionsContextMenu.Items.Add(CM_Web_Profile);
OptionsContextMenu.Items.Add(new Separator());
var CM_Feedback = new MenuItem{Header = App.TextStrings["contextmenu_feedback"], InputGestureText = "Ctrl+F"};
CM_Feedback.Click += (sender, e) => BpUtility.StartProcess(OnlineVersionInfo.launcher_info.links.feedback.ToString(), null, App.LauncherRootPath, true);
OptionsContextMenu.Items.Add(CM_Feedback);
var CM_Changelog = new MenuItem{Header = App.TextStrings["contextmenu_changelog"], InputGestureText = "Ctrl+C"};
CM_Changelog.Click += (sender, e) => CM_Changelog_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Changelog);
var CM_Custom_Background = new MenuItem{Header = App.TextStrings["contextmenu_custom_background"], InputGestureText = "Ctrl+B"};
CM_Custom_Background.Click += (sender, e) => CM_CustomBackground_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Custom_Background);
var CM_ShowLog = new MenuItem{Header = App.TextStrings["contextmenu_show_log"], InputGestureText = "Ctrl+L"};
CM_ShowLog.Click += (sender, e) => CM_ShowLog_Click(sender, e);
OptionsContextMenu.Items.Add(CM_ShowLog);
var CM_Sounds = new MenuItem{Header = App.TextStrings["contextmenu_sounds"], InputGestureText = "Ctrl+S", IsChecked = true};
CM_Sounds.Click += (sender, e) => CM_Sounds_Click(sender, e);
OptionsContextMenu.Items.Add(CM_Sounds);
var CM_Language = new MenuItem{Header = App.TextStrings["contextmenu_language"]};
var CM_Language_System = new MenuItem{Header = App.TextStrings["contextmenu_language_system"]};
CM_Language_System.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_System);
var CM_Language_Chinese_Simplified = new MenuItem {Header = App.TextStrings["contextmenu_language_chinese_simplified"]};
CM_Language_Chinese_Simplified.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Chinese_Simplified);
var CM_Language_Czech = new MenuItem {Header = App.TextStrings["contextmenu_language_czech"]};
CM_Language_Czech.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Czech);
var CM_Language_English = new MenuItem{Header = App.TextStrings["contextmenu_language_english"]};
CM_Language_English.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_English);
var CM_Language_French = new MenuItem{Header = App.TextStrings["contextmenu_language_french"]};
CM_Language_French.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_French);
var CM_Language_German = new MenuItem{Header = App.TextStrings["contextmenu_language_german"]};
CM_Language_German.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_German);
var CM_Language_Indonesian = new MenuItem{Header = App.TextStrings["contextmenu_language_indonesian"]};
CM_Language_Indonesian.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Indonesian);
var CM_Language_Italian = new MenuItem {Header = App.TextStrings["contextmenu_language_italian"]};
CM_Language_Italian.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Italian);
var CM_Language_Japanese = new MenuItem {Header = App.TextStrings["contextmenu_language_japanese"]};
CM_Language_Japanese.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Japanese);
var CM_Language_Portuguese_Brazil = new MenuItem{Header = App.TextStrings["contextmenu_language_portuguese_brazil"]};
CM_Language_Portuguese_Brazil.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Portuguese_Brazil);
var CM_Language_Portuguese_Portugal = new MenuItem{Header = App.TextStrings["contextmenu_language_portuguese_portugal"]};
CM_Language_Portuguese_Portugal.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Portuguese_Portugal);
var CM_Language_Russian = new MenuItem{Header = App.TextStrings["contextmenu_language_russian"]};
CM_Language_Russian.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Russian);
var CM_Language_Serbian = new MenuItem{Header = App.TextStrings["contextmenu_language_serbian"]};
CM_Language_Serbian.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Serbian);
var CM_Language_Spanish = new MenuItem{Header = App.TextStrings["contextmenu_language_spanish"]};
CM_Language_Spanish.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Spanish);
var CM_Language_Thai = new MenuItem{Header = App.TextStrings["contextmenu_language_thai"]};
CM_Language_Thai.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Thai);
var CM_Language_Vietnamese = new MenuItem{Header = App.TextStrings["contextmenu_language_vietnamese"]};
CM_Language_Vietnamese.Click += (sender, e) => CM_Language_Click(sender, e);
CM_Language.Items.Add(CM_Language_Vietnamese);
CM_Language.Items.Add(new Separator());
var CM_Language_Contribute = new MenuItem{Header = App.TextStrings["contextmenu_language_contribute"]};
CM_Language_Contribute.Click += (sender, e) => BpUtility.StartProcess(OnlineVersionInfo.launcher_info.links.language_contribute.ToString(), null, App.LauncherRootPath, true);
CM_Language.Items.Add(CM_Language_Contribute);
OptionsContextMenu.Items.Add(CM_Language);
var CM_About = new MenuItem{Header = App.TextStrings["contextmenu_about"], InputGestureText = "Ctrl+A"};
CM_About.Click += (sender, e) => CM_About_Click(sender, e);
OptionsContextMenu.Items.Add(CM_About);
if(!App.DisableTranslations)
{
if(language_reg == null)
{
CM_Language_System.IsChecked = true;
}
else
{
switch(language_reg.ToString())
{
case "cs":
CM_Language_Czech.IsChecked = true;
break;
case "fr":
CM_Language_French.IsChecked = true;
break;
case "de":
CM_Language_German.IsChecked = true;
break;
case "id":
CM_Language_Indonesian.IsChecked = true;
break;
case "it":
CM_Language_Italian.IsChecked = true;
break;
case "ja":
CM_Language_Japanese.IsChecked = true;
break;
case "pt-BR":
CM_Language_Portuguese_Brazil.IsChecked = true;
break;
case "pt-PT":
CM_Language_Portuguese_Portugal.IsChecked = true;
break;
case "ru":
CM_Language_Russian.IsChecked = true;
break;
case "sr":
CM_Language_Serbian.IsChecked = true;
break;
case "es":
CM_Language_Spanish.IsChecked = true;
break;
case "th":
CM_Language_Thai.IsChecked = true;
break;
case "vi":
CM_Language_Vietnamese.IsChecked = true;
break;
case "zh-CN":
CM_Language_Chinese_Simplified.IsChecked = true;
break;
default:
CM_Language_English.IsChecked = true;
break;
}
}
}
else
{
CM_Language_English.IsChecked = true;
foreach(dynamic item in CM_Language.Items)
{
if(item.GetType() == typeof(MenuItem))
{
if(item.Header.ToString() == App.TextStrings["contextmenu_language_contribute"])
{
continue;
}
}
item.IsEnabled = false;
}
}
Mirror = HI3Mirror.miHoYo;
var last_selected_mirror_reg = App.LauncherRegKey.GetValue("LastSelectedMirror");
if(last_selected_mirror_reg != null)
{
if(App.LauncherRegKey.GetValueKind("LastSelectedMirror") == RegistryValueKind.DWord)
{
if((int)last_selected_mirror_reg == 0)
{
Mirror = HI3Mirror.miHoYo;
}
else if((int)last_selected_mirror_reg == 1)
{
Mirror = HI3Mirror.BpNetwork;
}
}
}
if(Server != HI3Server.GLB && Server != HI3Server.SEA)
{
Mirror = HI3Mirror.miHoYo;
}
MirrorDropdown.SelectedIndex = (int)Mirror;
var seen_announcements_reg = App.LauncherRegKey.GetValue("SeenAnnouncements");
if(seen_announcements_reg != null)
{
if(App.LauncherRegKey.GetValueKind("SeenAnnouncements") == RegistryValueKind.String)
{
App.SeenAnnouncements = seen_announcements_reg.ToString().Split(',').ToList();
}
}
var show_log_reg = App.LauncherRegKey.GetValue("ShowLog");
if(show_log_reg != null)
{
if(App.LauncherRegKey.GetValueKind("ShowLog") == RegistryValueKind.DWord)
{
if((int)show_log_reg == 1)
{
ToggleLog(true);
CM_ShowLog.IsChecked = true;
}
}
}
var sounds_reg = App.LauncherRegKey.GetValue("Sounds");
if(sounds_reg != null)
{
if(App.LauncherRegKey.GetValueKind("Sounds") == RegistryValueKind.DWord)
{
if((int)sounds_reg == 0)
{
App.DisableSounds = true;
CM_Sounds.IsChecked = false;
}
}
}
DownloadCacheCommand.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
RepairGameCommand.InputGestures.Add(new KeyGesture(Key.R, ModifierKeys.Control));
MoveGameCommand.InputGestures.Add(new KeyGesture(Key.M, ModifierKeys.Control));
UninstallGameCommand.InputGestures.Add(new KeyGesture(Key.U, ModifierKeys.Control));
WebProfileCommand.InputGestures.Add(new KeyGesture(Key.P, ModifierKeys.Control));
FeedbackCommand.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
ChangelogCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control));
CustomBackgroundCommand.InputGestures.Add(new KeyGesture(Key.B, ModifierKeys.Control));
ToggleLogCommand.InputGestures.Add(new KeyGesture(Key.L, ModifierKeys.Control));
ToggleSoundsCommand.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
AboutCommand.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));
App.NeedsUpdate = LauncherUpdateCheck();
if(!App.DisableTranslations)
{
if(!App.DisableAutoUpdate && !App.NeedsUpdate)
{
DownloadLauncherTranslations();
}
}
Log($"Using server: {((ComboBoxItem)ServerDropdown.SelectedItem).Content as string}");
Log($"Using mirror: {((ComboBoxItem)MirrorDropdown.SelectedItem).Content as string}");
DownloadBackgroundImage();
}
catch(Exception ex)
{
Status = LauncherStatus.Error;
MessageBox.Show(string.Format(App.TextStrings["msgbox_start_error_msg"], ex), App.TextStrings["msgbox_start_error_title"], MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return;
}
}
private async void Window_ContentRendered(object sender, EventArgs e)
{
#if DEBUG
App.DisableAutoUpdate = true;
#endif
try
{
string exe_name = Process.GetCurrentProcess().MainModule.ModuleName;
string old_exe_name = $"{Path.GetFileNameWithoutExtension(App.LauncherPath)}_old.exe";
if(Process.GetCurrentProcess().MainModule.ModuleName != App.LauncherExeName)
{
Status = LauncherStatus.Error;
DeleteFile(App.LauncherPath, true);
File.Move(Path.Combine(App.LauncherRootPath, exe_name), App.LauncherPath);
BpUtility.RestartApp();
return;
}
DeleteFile(Path.Combine(App.LauncherRootPath, old_exe_name), true);
await Task.Run(() =>
{
if(App.DisableAutoUpdate)
{
return;
}
if(!App.NeedsUpdate)
{
if(BpUtility.CalculateMD5(App.LauncherPath) != OnlineVersionInfo.launcher_info.exe_md5.ToString().ToUpper())
{
Log($"Launcher integrity error, attempting self-repair...", true, 1);
App.NeedsUpdate = true;
}
}
if(App.NeedsUpdate)
{
Log("A newer version of the launcher is available!");
Status = LauncherStatus.Working;
DownloadLauncherUpdate();
Log("Validating update...");
string md5 = OnlineVersionInfo.launcher_info.md5.ToString().ToUpper();
string actual_md5 = BpUtility.CalculateMD5(App.LauncherArchivePath);
if(actual_md5 != md5)
{
Status = LauncherStatus.Error;
Log($"Validation failed. Expected MD5: {md5}, got MD5: {actual_md5}", true, 1);
DeleteFile(App.LauncherArchivePath, true);
Dispatcher.Invoke(() => {new DialogWindow(App.TextStrings["msgbox_verify_error_title"], App.TextStrings["msgbox_verify_error_1_msg"]).ShowDialog();});
return;
}
Log("success!", false);
Log("Performing update...");
File.Move(Path.Combine(App.LauncherRootPath, exe_name), Path.Combine(App.LauncherRootPath, old_exe_name));
using(var archive = new SevenZipExtractor(App.LauncherArchivePath))
{
archive.ExtractArchive(App.LauncherRootPath);
}
Log("success!", false);
Dispatcher.Invoke(() => {BpUtility.RestartApp();});
return;
}
else
{
DeleteFile(App.LauncherArchivePath, true);
if(!File.Exists(App.LauncherPath))
{
File.Copy(Path.Combine(App.LauncherRootPath, exe_name), App.LauncherPath, true);
}
}
});
}
catch(Exception ex)
{
Status = LauncherStatus.Error;
Log($"Failed to start the launcher:\n{ex}", true, 1);
new DialogWindow(App.TextStrings["msgbox_start_error_title"], string.Format(App.TextStrings["msgbox_start_error_msg"], ex.Message)).ShowDialog();
Application.Current.Shutdown();
return;
}
if(App.FirstLaunch)
{
LegacyBoxActive = true;
IntroBox.Visibility = Visibility.Visible;
ProgressBar.Visibility = Visibility.Collapsed;
}
else
{
FetchAnnouncements();
}
var custom_background_name_reg = App.LauncherRegKey.GetValue("CustomBackgroundName");
if(custom_background_name_reg != null)
{
if(App.LauncherRegKey.GetValueKind("CustomBackgroundName") == RegistryValueKind.String)
{
string path = Path.Combine(App.LauncherBackgroundsPath, custom_background_name_reg.ToString());
if(File.Exists(path))
{
SetCustomBackgroundFile(path);
}
else
{
Log("Custom background file cannot be found, resetting to official...", true, 2);
BpUtility.DeleteFromRegistry("CustomBackgroundName");
}
}
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
if(LegacyBoxActive)
{
return;
}
Close();
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
if(LegacyBoxActive)
{
return;
}
WindowState = WindowState.Minimized;
}
private async void LaunchButton_Click(object sender, RoutedEventArgs e)
{
if(LegacyBoxActive)
{
return;
}
BpUtility.PlaySound(Properties.Resources.Click);
switch(Status)
{
case LauncherStatus.Ready:
case LauncherStatus.Preloading:
case LauncherStatus.PreloadVerifying:
{
if(DownloadPaused)
{
DownloadPaused = false;
await DownloadGameFile();
return;
}
if(LocalVersionInfo != null)
{
if(!File.Exists(GameExePath))
{
if(new DialogWindow(App.TextStrings["msgbox_no_game_exe_title"], App.TextStrings["msgbox_no_game_exe_msg"], DialogWindow.DialogType.Question).ShowDialog() == true)
{
ResetVersionInfo();
GameUpdateCheck();
}
return;
}
try
{
var processes = Process.GetProcessesByName("BH3");
if(processes.Length > 0)
{
processes[0].EnableRaisingEvents = true;
processes[0].Exited += new EventHandler((object s, EventArgs ea) => {OnGameExit();});
if(PreloadDownload)
{
Dispatcher.Invoke(() =>
{
LaunchButton.Content = App.TextStrings["button_running"];
LaunchButton.IsEnabled = false;
});
}
else
{
Status = LauncherStatus.Running;
}
return;
}
var start_info = new ProcessStartInfo(GameExePath);
start_info.WorkingDirectory = GameInstallPath;
start_info.UseShellExecute = true;
try
{
start_info.Arguments = LocalVersionInfo.launch_options.ToString();
}catch{}
var process = Process.Start(start_info);
process.EnableRaisingEvents = true;
process.Exited += new EventHandler((object s1, EventArgs ea1) =>
{
processes = Process.GetProcessesByName("BH3");
if(processes.Length > 0)
{
processes[0].EnableRaisingEvents = true;
processes[0].Exited += new EventHandler((object s2, EventArgs ea2) => {OnGameExit();});
}
else
{
OnGameExit();
}
});
if(PreloadDownload)
{
Dispatcher.Invoke(() =>
{
LaunchButton.Content = App.TextStrings["button_running"];
LaunchButton.IsEnabled = false;
});
}
else
{
Status = LauncherStatus.Running;
}
WindowState = WindowState.Minimized;
}
catch(Exception ex)
{
Status = LauncherStatus.Error;
Log($"Failed to start the game:\n{ex}", true, 1);
new DialogWindow(App.TextStrings["msgbox_start_error_title"], App.TextStrings["msgbox_process_start_error_msg"]).ShowDialog();
Status = LauncherStatus.Ready;
}
}
else
{
try
{
var possible_paths = new List<string>();
possible_paths.Add(App.LauncherRootPath);
possible_paths.Add(Environment.ExpandEnvironmentVariables("%ProgramW6432%"));
string[] game_reg_names = {"Honkai Impact 3rd", "Honkai Impact 3", "崩坏3", "崩壞3rd", "붕괴3rd", "崩壊3rd"};
foreach(string game_reg_name in game_reg_names)
{
try
{
var path = CheckForExistingGameDirectory(Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{game_reg_name}").GetValue("InstallPath").ToString());
if(!string.IsNullOrEmpty(path))
{
possible_paths.Add(path);
}
}catch{}
}
foreach(string path in possible_paths)
{
if(!string.IsNullOrEmpty(CheckForExistingGameDirectory(path)))
{
var server = CheckForExistingGameClientServer(path);
if(server >= 0)
{
if(new DialogWindow(App.TextStrings["msgbox_install_title"], string.Format(App.TextStrings["msgbox_install_existing_dir_msg"], path), DialogWindow.DialogType.Question).ShowDialog() == true)
{
Log($"Existing installation directory selected: {path}");
GameInstallPath = path;
if((int)Server != server)
{
ServerDropdown.SelectedIndex = server;
}
WriteVersionInfo(true, true);