Skip to content

Commit

Permalink
game auth login
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Nov 30, 2024
1 parent 47c8023 commit bb3c501
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 36 deletions.
63 changes: 32 additions & 31 deletions src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;

Expand Down Expand Up @@ -79,36 +80,17 @@ static AppConfig()
#region Ini Config


private static int windowSizeMode;
public static int WindowSizeMode
{
get => windowSizeMode;
set
{
windowSizeMode = value;
}
}
public static int WindowSizeMode { get; set; }

private static string? language;
public static string? Language
{
get => language;
set
{
language = value;
}
}
public static string? Language { get; set; }

private static string userDataFolder;
public static string UserDataFolder
{
get => userDataFolder;
set
{
userDataFolder = value;
}
}
public static string UserDataFolder { get; set; }

public static bool? EnableLoginAuthTicket { get; set; }

public static string? stoken { get; set; }

public static string? mid { get; set; }



Expand Down Expand Up @@ -139,8 +121,11 @@ private static void Initialize()
}
Configuration = builder.Build();

windowSizeMode = Configuration.GetValue<int>(nameof(WindowSizeMode));
language = Configuration.GetValue<string>(nameof(Language));
WindowSizeMode = Configuration.GetValue<int>(nameof(WindowSizeMode));
Language = Configuration.GetValue<string>(nameof(Language));
EnableLoginAuthTicket = Configuration.GetValue<bool?>(nameof(EnableLoginAuthTicket));
stoken = Configuration.GetValue<string>(nameof(stoken));
mid = Configuration.GetValue<string>(nameof(mid));
string? dir = Configuration.GetValue<string>(nameof(UserDataFolder));
if (!string.IsNullOrWhiteSpace(dir))
{
Expand All @@ -155,7 +140,7 @@ private static void Initialize()
}
if (Directory.Exists(folder))
{
userDataFolder = Path.GetFullPath(folder);
UserDataFolder = Path.GetFullPath(folder);
}
}
}
Expand Down Expand Up @@ -185,11 +170,25 @@ public static void SaveConfiguration()
{
dataFolder = Path.GetRelativePath(baseDir, dataFolder);
}
File.WriteAllText(Path.Combine(baseDir, "config.ini"), $"""
var sb = new StringBuilder();
sb.AppendLine($"""
{nameof(WindowSizeMode)}={WindowSizeMode}
{nameof(Language)}={Language}
{nameof(UserDataFolder)}={dataFolder}
""");
if (EnableLoginAuthTicket.HasValue)
{
sb.AppendLine($"{nameof(EnableLoginAuthTicket)}={EnableLoginAuthTicket}");
}
if (!string.IsNullOrWhiteSpace(stoken))
{
sb.AppendLine($"{nameof(stoken)}={stoken}");
}
if (!string.IsNullOrWhiteSpace(mid))
{
sb.AppendLine($"{nameof(mid)}={mid}");
}
File.WriteAllText(Path.Combine(baseDir, "config.ini"), sb.ToString());
}
catch { }
}
Expand Down Expand Up @@ -773,6 +772,8 @@ public static void DeleteAllSettings()
public static bool LauncherPageFirstLoaded { get; set; }


public static string? HyperionAid { get; set; }


#endregion

Expand Down
65 changes: 65 additions & 0 deletions src/Starward/Pages/GameLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,71 @@
Glyph="&#xF012;"
IsTextScaleFactorEnabled="False" />
</Button>
<!-- Game Auth Login -->
<Button Name="Button_GameAuthLogin"
Width="40"
Height="40"
Margin="4"
Padding="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="0"
CornerRadius="8"
Style="{ThemeResource DateTimePickerFlyoutButtonStyle}"
Visibility="Collapsed">
<Grid>
<Border Width="24"
Height="24"
CornerRadius="4">
<Image Width="24"
Height="24"
Stretch="UniformToFill">
<Image.Source>
<BitmapImage DecodePixelHeight="24"
DecodePixelType="Logical"
DecodePixelWidth="24"
UriSource="ms-appx:///Assets/Image/icon_hyperion.png" />
</Image.Source>
</Image>
</Border>
<Image Name="Image_GameAuthLoginErrorAlert"
Width="20"
Margin="0,-10,-10,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Source="ms-appx:///Assets/Image/alert.png"
Visibility="{x:Bind GameAuthLoginErrorMessage, Converter={StaticResource ObjectToVisibilityConverter}}" />
</Grid>
<Button.Flyout>
<Flyout Placement="LeftEdgeAlignedTop">
<StackPanel Spacing="12">
<Border Width="60"
Height="60"
HorizontalAlignment="Center"
CornerRadius="8">
<Image Source="ms-appx:///Assets/Image/icon_hyperion.png" Stretch="UniformToFill" />
</Border>
<TextBlock HorizontalAlignment="Center">
<Run Text="通行证 ID: " />
<Run Text="{x:Bind HyperionAid}" />
</TextBlock>
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind GameAuthLoginErrorMessage}"
TextWrapping="Wrap"
Visibility="{x:Bind GameAuthLoginErrorMessage, Converter={StaticResource ObjectToVisibilityConverter}}" />
<Button MinWidth="120"
HorizontalAlignment="Center"
Command="{x:Bind InitializeGameAuthLoginCommand}"
Content="重新认证"
Visibility="{x:Bind GameAuthLoginErrorMessage, Converter={StaticResource ObjectToVisibilityConverter}}" />
<ToggleSwitch HorizontalAlignment="Center"
IsOn="{x:Bind EnableLoginAuthTicket, Mode=TwoWay}"
OffContent="已禁用授权登录"
OnContent="已启用授权登录" />
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
<!-- Test -->
<Button Width="40"
Height="40"
Expand Down
47 changes: 46 additions & 1 deletion src/Starward/Pages/GameLauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected override async void OnLoaded()
}
await UpdateGameContentAsync();
await UpdateGameNoticesAlertAsync();
await InitializeGameAuthLoginAsync();
}
catch { }
}
Expand Down Expand Up @@ -442,7 +443,7 @@ private async Task StartGameAsync()
return;
}
}
var process1 = _gameLauncherService.StartGame(CurrentGameBiz, IgnoreRunningGame);
var process1 = await _gameLauncherService.StartGame(CurrentGameBiz, IgnoreRunningGame);
if (process1 == null)
{
CanStartGame = true;
Expand Down Expand Up @@ -1680,4 +1681,48 @@ private async Task DebugAsync()




#region Game Auth Login


[ObservableProperty]
private string? gameAuthLoginErrorMessage;

[ObservableProperty]
private long? hyperionAid;

[ObservableProperty]
private bool enableLoginAuthTicket = AppConfig.EnableLoginAuthTicket ?? false;
partial void OnEnableLoginAuthTicketChanged(bool value)
{
AppConfig.EnableLoginAuthTicket = value;
AppConfig.SaveConfiguration();
}


[RelayCommand]
private async Task InitializeGameAuthLoginAsync()
{
try
{
if (string.IsNullOrWhiteSpace(AppConfig.stoken) || string.IsNullOrWhiteSpace(AppConfig.mid) || !CurrentGameBiz.IsChinaOfficial())
{
return;
}
Button_GameAuthLogin.Visibility = Visibility.Visible;
GameAuthLoginErrorMessage = null;
HyperionAid = await _gameLauncherService.GetHyperionAidAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "InitializeGameAuthLogin");
GameAuthLoginErrorMessage = ex.Message;
}
}


#endregion



}
4 changes: 2 additions & 2 deletions src/Starward/Services/GameRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public GameRecordService(ILogger<GameRecordService> logger, DatabaseService data
/// </summary>
/// <param name="forceUpdate"></param>
/// <returns></returns>
public async Task UpdateDeviceFpAsync(bool forceUpdate = false)
public async Task UpdateDeviceFpAsync(bool forceUpdate = false, CancellationToken cancellationToken = default)
{
if (IsHoyolab)
{
Expand All @@ -93,7 +93,7 @@ public async Task UpdateDeviceFpAsync(bool forceUpdate = false)
}
if (forceUpdate || DateTimeOffset.Now - lastUpdateTime > TimeSpan.FromDays(3))
{
await _gameRecordClient.GetDeviceFpAsync();
await _gameRecordClient.GetDeviceFpAsync(cancellationToken);
AppConfig.HyperionDeviceId = _gameRecordClient.DeviceId;
AppConfig.HyperionDeviceFp = _gameRecordClient.DeviceFp;
AppConfig.HyperionDeviceFpLastUpdateTime = DateTimeOffset.Now;
Expand Down
Loading

0 comments on commit bb3c501

Please sign in to comment.