Skip to content

Commit

Permalink
opt: Switch Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Joker committed Jun 25, 2024
1 parent fd16f69 commit 2f7678d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/Starward.Language/Lang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Starward.Language/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1413,4 +1413,10 @@ Do you accept the risk and continue to use it?</value>
<data name="HoyolabToolboxPage_ApocalypticShadow" xml:space="preserve">
<value>Apocalyptic Shadow</value>
</data>
<data name="SwitchClientPage_FileVerificationFailedNeedToDownloadResources" xml:space="preserve">
<value>File verification failed, need to download resources. Click on Confirm to download.</value>
</data>
<data name="SwitchClientPage_Switching" xml:space="preserve">
<value>Switching</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Starward.Language/Lang.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1413,4 +1413,10 @@
<data name="HoyolabToolboxPage_ApocalypticShadow" xml:space="preserve">
<value>末日幻影</value>
</data>
<data name="SwitchClientPage_FileVerificationFailedNeedToDownloadResources" xml:space="preserve">
<value>文件校验失败,需要下载资源。点击确认以下载。</value>
</data>
<data name="SwitchClientPage_Switching" xml:space="preserve">
<value>切换中</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Starward/Pages/LauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
HorizontalAlignment="Stretch"
BorderThickness="0"
Command="{x:Bind SwitchClientCommand}"
CommandParameter="true"
CornerRadius="20,0,0,20"
FontSize="14"
Style="{ThemeResource AccentButtonStyle}"
Expand Down
6 changes: 3 additions & 3 deletions src/Starward/Pages/LauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ private async Task ReinstallGameAsync()


[RelayCommand]
private void SwitchClient()
private void SwitchClient(string autorun = "false")
{
if (IsUpdateGameButtonEnable)
{
Expand All @@ -1345,10 +1345,10 @@ private void SwitchClient()
}
else
{
MainWindow.Current.OverlayFrameNavigateTo(typeof(SwitchClientPage), CurrentGameBiz);
MainWindow.Current.OverlayFrameNavigateTo(typeof(SwitchClientPage), new Tuple<GameBiz,bool>(CurrentGameBiz, autorun == "true"));
}
}



[RelayCommand]
Expand Down
3 changes: 2 additions & 1 deletion src/Starward/Pages/SwitchClientPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
Command="{x:Bind StartSwitchClientCommand}"
Content="{x:Bind lang:Lang.SwitchClientPage_StartSwitching}"
IsEnabled="{x:Bind IsPrepared}"
Style="{ThemeResource AccentButtonStyle}" />
Style="{ThemeResource AccentButtonStyle}"
Visibility="{x:Bind Autorun, Converter={StaticResource BoolToVisibilityReversedConverter}}" />
<Button MinWidth="100"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Expand Down
40 changes: 37 additions & 3 deletions src/Starward/Pages/SwitchClientPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Navigation;
using Starward.Core;
using Starward.Core.Launcher;
using Starward.Messages;
Expand Down Expand Up @@ -60,6 +61,18 @@ public SwitchClientPage()
}


[ObservableProperty]
private bool autorun = false;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter is Tuple<GameBiz, bool> tuple)
{
CurrentGameBiz = tuple.Item1;
Autorun = tuple.Item2;
}
}


private string gameFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Starward\game");

Expand Down Expand Up @@ -302,6 +315,10 @@ private void UpdateTargetGameBizs()
list.Remove(from);
}
TargetGameBizs = list;
if (Autorun && FromGameBiz != CurrentGameBiz)
{
SelectedTargetGameBiz = list[list.FindIndex(item => item.GameBiz == CurrentGameBiz)];
}
}


Expand Down Expand Up @@ -340,7 +357,10 @@ private async Task PrepareForSwitchClientAsync()
tokenSource = new CancellationTokenSource();
await GetDownloadFilesAsync(tokenSource.Token);
await MoveExistFilesAsync(tokenSource.Token);
await DownloadFilesAsync(tokenSource.Token);
if (!Autorun)
{
await DownloadFilesAsync(tokenSource.Token);
}
CanCancel = false;
await VerifyDownloadFilesAsync();
await WriteConfigFileAsync();
Expand All @@ -354,7 +374,14 @@ private async Task PrepareForSwitchClientAsync()
}
catch (Exception ex)
{
_logger.LogInformation(ex, "Prepare for switch client");
if (ex.Message == Lang.SwitchClientPage_FileVerificationFailedNeedToDownloadResources)
{
Autorun = false;
}
else
{
_logger.LogInformation(ex, "Prepare for switch client");
}
StateText = Lang.DownloadGamePage_SomethingError;
ErrorText = ex.Message;
}
Expand All @@ -363,6 +390,11 @@ private async Task PrepareForSwitchClientAsync()
CanCancel = true;
StopTimer();
}
if (Autorun)
{
await StartSwitchClientAsync();
Close();
}
}


Expand Down Expand Up @@ -518,7 +550,7 @@ await Parallel.ForEachAsync(addFiles, async (task, _) =>
});
if (failed)
{
throw new Exception(Lang.SwitchClientPage_FileVerificationFailedPleaseTryAgain);
throw new Exception(Autorun ? Lang.SwitchClientPage_FileVerificationFailedNeedToDownloadResources : Lang.SwitchClientPage_FileVerificationFailedPleaseTryAgain);
}
}

Expand Down Expand Up @@ -605,6 +637,7 @@ private async Task StartSwitchClientAsync()
}
try
{
StateText = Lang.SwitchClientPage_Switching;
if (_gameService.GetGameProcess(FromGameBiz) is not null)
{
ErrorText = Lang.LauncherPage_GameIsRunning;
Expand Down Expand Up @@ -689,6 +722,7 @@ private async Task StartSwitchClientAsync()

await VerifyMovedFilesAsync();

StateText = Lang.SettingPage_Completed;
isCompleted = true;
Button_StartSwitch.Content = Lang.SettingPage_Completed;
AppConfig.SetGameInstallPath(ToGameBiz, installPath);
Expand Down

0 comments on commit 2f7678d

Please sign in to comment.