Skip to content

Commit

Permalink
optimize game account switcher #561
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 15, 2024
1 parent 9fe1b50 commit e40f269
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/Starward.Language/Lang.Designer.cs

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

3 changes: 3 additions & 0 deletions src/Starward.Language/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1296,4 +1296,7 @@ Do you accept the risk and continue to use it?</value>
<data name="LauncherPage_PleaseDoNotSelectTheRootDirectoryOfADrive" xml:space="preserve">
<value>Please do not select the root directory of a drive.</value>
</data>
<data name="LauncherPage_SavedAccount" xml:space="preserve">
<value>Saved account</value>
</data>
</root>
29 changes: 29 additions & 0 deletions src/Starward/Converters/GameAccountUidStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.UI.Xaml.Data;
using System;

namespace Starward.Converters;

public class GameAccountUidStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value switch
{
int uid when uid > 0 => uid.ToString(),
_ => string.Empty
};
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (int.TryParse(value as string, out int uid))
{
return uid;
}
else
{
return 0;
}
}
}

10 changes: 9 additions & 1 deletion src/Starward/Models/GameAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ public bool IsLogin
set => SetProperty(ref _IsLogin, value);
}

private bool _IsSaved;
public bool IsSaved
{
get => _IsSaved;
set => SetProperty(ref _IsSaved, value);
}

public static string LoginAccountText => Lang.LauncherPage_CurrentlyLoggedInAccount;

public static string LoginAccountText => Lang.LauncherPage_CurrentAccount;
public static string SaveAccountText => Lang.LauncherPage_SavedAccount;


}
62 changes: 45 additions & 17 deletions src/Starward/Pages/LauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
xmlns:local="using:Starward.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Controls"
xmlns:scv="using:Starward.Converters"
xmlns:sh="using:Starward.Helpers"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">


<Page.Resources>
<scv:GameAccountUidStringConverter x:Key="GameAccountUidStringConverter" />
</Page.Resources>

<SplitView Name="SplitView_Content"
BorderThickness="0"
Expand Down Expand Up @@ -448,10 +451,11 @@
SelectedItem="{x:Bind SelectGameAccount, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid Padding="8,0,0,0" ColumnSpacing="8">
<Grid Padding="8,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center">
<Run Text="{Binding Name}" />
Expand All @@ -461,12 +465,21 @@
Text="{Binding Uid}" />
</TextBlock>
<FontIcon Grid.Column="1"
Margin="8,0,0,0"
VerticalAlignment="Bottom"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Glyph="&#xE930;"
ToolTipService.ToolTip="{Binding LoginAccountText}"
Visibility="{Binding IsLogin}" />
<FontIcon Grid.Column="2"
Margin="8,0,0,0"
VerticalAlignment="Bottom"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Glyph="&#xE74E;"
ToolTipService.ToolTip="{Binding SaveAccountText}"
Visibility="{Binding IsSaved}" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
Expand Down Expand Up @@ -499,7 +512,7 @@
Glyph="&#xE70F;" />
<Button.Flyout>
<Flyout>
<StackPanel MinWidth="200" Spacing="8">
<StackPanel MinWidth="200" Spacing="4">
<TextBlock FontSize="16"
FontWeight="SemiBold"
Text="{x:Bind lang:Lang.LauncherPage_AccountInformation}" />
Expand All @@ -511,8 +524,34 @@
IsSpellCheckEnabled="False"
Text="{x:Bind SelectGameAccount.Name, Mode=TwoWay}" />
<TextBlock Margin="0,8,0,0" Text="Uid" />
<NumberBox Minimum="0" Value="{x:Bind SelectGameAccount.Uid, Mode=TwoWay}" />
<Grid Margin="0,16,0,0"
<AutoSuggestBox x:Name="AutoSuggestBox_Uid"
GotFocus="AutoSuggestBox_Uid_GotFocus"
Loaded="AutoSuggestBox_Uid_Loaded"
Text="{x:Bind SelectGameAccount.Uid, Converter={StaticResource GameAccountUidStringConverter}, Mode=TwoWay}"
TextChanged="AutoSuggestBox_Uid_TextChanged" />
<StackPanel Margin="0,4,0,0"
Orientation="Horizontal"
Visibility="{x:Bind SelectGameAccount.IsLogin}">
<FontIcon Margin="4,0,0,1"
VerticalAlignment="Bottom"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Glyph="&#xE930;" />
<TextBlock Margin="8,0,0,0"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind lang:Lang.LauncherPage_CurrentlyLoggedInAccount}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{x:Bind SelectGameAccount.IsSaved}">
<FontIcon Margin="4,0,0,1"
VerticalAlignment="Bottom"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Glyph="&#xE74E;" />
<TextBlock Margin="8,0,0,0"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind lang:Lang.LauncherPage_SavedAccount}" />
</StackPanel>
<Grid Margin="0,4,0,0"
HorizontalAlignment="Stretch"
Background="{ThemeResource CustomAcrylicBrush}"
CornerRadius="20">
Expand Down Expand Up @@ -550,18 +589,7 @@
</StackPanel>
</Button>
</Grid>
<StackPanel Margin="0,4,0,-4"
Orientation="Horizontal"
Visibility="{x:Bind SelectGameAccount.IsLogin}">
<FontIcon Margin="4,0,0,1"
VerticalAlignment="Bottom"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Glyph="&#xE930;" />
<TextBlock Margin="8,0,0,0"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind lang:Lang.LauncherPage_CurrentlyLoggedInAccount}" />
</StackPanel>

</StackPanel>
</Flyout>
</Button.Flyout>
Expand Down
83 changes: 83 additions & 0 deletions src/Starward/Pages/LauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ private void UpdatePlayTime()



private TextBox TextBox_AccountUid;


[ObservableProperty]
private List<GameAccount> gameAccountList;

Expand All @@ -765,6 +768,8 @@ partial void OnSelectGameAccountChanged(GameAccount? value)
private bool canChangeGameAccount;


private List<string> suggestionUids;


private void GetGameAccount()
{
Expand Down Expand Up @@ -876,9 +881,87 @@ private void DeleteGameAccount()



private void AutoSuggestBox_Uid_Loaded(object sender, RoutedEventArgs e)
{
try
{
if (TextBox_AccountUid is null)
{
var ele1 = VisualTreeHelper.GetChild(AutoSuggestBox_Uid, 0);
var ele = VisualTreeHelper.GetChild(ele1, 0);
if (ele is TextBox textBox)
{
TextBox_AccountUid = textBox;
TextBox_AccountUid.InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Number } } };
TextBox_AccountUid.BeforeTextChanging += (s, e) =>
{
e.Cancel = !e.NewText.All(x => char.IsDigit(x));
};
}
}
}
catch { }
}


private void AutoSuggestBox_Uid_GotFocus(object sender, RoutedEventArgs e)
{
try
{
suggestionUids = _gameService.GetSuggestionUids(CurrentGameBiz).Select(x => x.ToString()).ToList();
UpdateSuggestionUids(AutoSuggestBox_Uid.Text);
}
catch (Exception ex)
{
_logger.LogError(ex, "Get suggestion uids");
}
}


private void AutoSuggestBox_Uid_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
try
{
if (args.Reason is AutoSuggestionBoxTextChangeReason.UserInput)
{
UpdateSuggestionUids(sender.Text);
}
}
catch { }
}


private void UpdateSuggestionUids(string text)
{
try
{
if (suggestionUids != null && suggestionUids.Count > 0)
{
if (string.IsNullOrWhiteSpace(text))
{
AutoSuggestBox_Uid.ItemsSource = suggestionUids;
AutoSuggestBox_Uid.IsSuggestionListOpen = true;
}
else
{
var list = suggestionUids.Where(x => x != text && x.StartsWith(text)).ToList();
if (list.Count == 0)
{
AutoSuggestBox_Uid.IsSuggestionListOpen = false;
}
else
{
if (!(AutoSuggestBox_Uid.ItemsSource is List<string> source && source.SequenceEqual(list)))
{
AutoSuggestBox_Uid.ItemsSource = list;
}
AutoSuggestBox_Uid.IsSuggestionListOpen = true;
}
}
}
}
catch { }
}



Expand Down
51 changes: 49 additions & 2 deletions src/Starward/Services/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,56 @@ 12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h115894
{
account.Uid = (int)(Registry.GetValue(key, GameRegistry.App_LastUserID_h2841727341, 0) ?? 0);
}
if (biz.ToGame() is GameBiz.Honkai3rd)
else if (biz.ToGame() is GameBiz.Honkai3rd)
{
account.Uid = (int)(Registry.GetValue(key, GameRegistry.GENERAL_DATA_V2_LastLoginUserId_h47158221, 0) ?? 0);
}
else if (biz is GameBiz.hk4e_cn or GameBiz.hk4e_global)
{
List<int> uids = GetUidsFromRegistry(biz).ToList();
if (uids.Count == 1)
{
account.Uid = uids[0];
}
}
return account;
}
return null;
}



private IEnumerable<int> GetUidsFromRegistry(GameBiz biz)
{
if (biz is GameBiz.hk4e_cn or GameBiz.hk4e_global)
{
string key = biz.GetGameRegistryKey().Replace(@"HKEY_CURRENT_USER\", "");
List<string> usds = Registry.CurrentUser.OpenSubKey(key)?.GetValueNames()?.Where(x => x.StartsWith("USD_"))?.ToList() ?? [];
foreach (var usd in usds)
{
string uidstr = Regex.Match(usd, @"USD_(\d+)_h").Groups[1].Value;
if (int.TryParse(uidstr, out int uid))
{
if (uid != 0)
{
yield return uid;
}
}
}
}
}



public IEnumerable<GameAccount> GetGameAccountsFromDatabase(GameBiz biz)
{
using var dapper = _database.CreateConnection();
return dapper.Query<GameAccount>("SELECT * FROM GameAccount WHERE GameBiz = @biz;", new { biz });
var list = dapper.Query<GameAccount>("SELECT * FROM GameAccount WHERE GameBiz = @biz;", new { biz });
foreach (var item in list)
{
item.IsSaved = true;
yield return item;
}
}


Expand All @@ -187,11 +222,23 @@ public List<GameAccount> GetGameAccounts(GameBiz biz)



public IEnumerable<int> GetSuggestionUids(GameBiz biz)
{
using var dapper = _database.CreateConnection();
List<int> uids = dapper.Query<int>("SELECT DISTINCT Uid FROM GameAccount WHERE GameBiz = @biz AND Uid > 0;", new { biz }).ToList();
if (biz is GameBiz.hk4e_cn or GameBiz.hk4e_global)
{
uids.AddRange(GetUidsFromRegistry(biz));
}
return uids.Distinct().Order();
}



public void SaveGameAccount(GameAccount account)
{
account.Name ??= "";
account.IsSaved = true;
using var dapper = _database.CreateConnection();
dapper.Execute("INSERT OR REPLACE INTO GameAccount (SHA256, GameBiz, Uid, Name, Value, Time) VALUES (@SHA256, @GameBiz, @Uid, @Name, @Value, @Time);", account);
_logger.LogInformation("Save account {name} ({biz}) successfully!", account.Name, account.GameBiz);
Expand Down

0 comments on commit e40f269

Please sign in to comment.