Skip to content

Commit

Permalink
game account switcher supporting for ZZZ
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Aug 26, 2024
1 parent 4fdf5c3 commit 0f8b2be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/Starward.Core/GameRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class GameRegistry
public const string App_LastUserID_h2841727341 = "App_LastUserID_h2841727341";
public const string GENERAL_DATA_V2_LastLoginUserId_h47158221 = "GENERAL_DATA_V2_LastLoginUserId_h47158221";
public const string GraphicsSettings_Model_h2986158309 = "GraphicsSettings_Model_h2986158309";
public const string __LastUid___h2153286551 = "__LastUid___h2153286551";



Expand Down
4 changes: 2 additions & 2 deletions src/Starward/Converters/GameAccountUidStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public object Convert(object value, Type targetType, object parameter, string la
{
return value switch
{
int uid when uid > 0 => uid.ToString(),
long 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))
if (long.TryParse(value as string, out long uid))
{
return uid;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Starward/Models/GameAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class GameAccount : ObservableObject

public GameBiz GameBiz { get; set; }

private int _Uid;
public int Uid
private long _Uid;
public long Uid
{
get => _Uid;
set => SetProperty(ref _Uid, value);
Expand Down
2 changes: 1 addition & 1 deletion src/Starward/Pages/GameLauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private void GetGameAccount()
{
try
{
if (AppConfig.DisableGameAccountSwitcher || CurrentGameBiz.IsBilibiliServer() || CurrentGameBiz.ToGame() is GameBiz.ZZZ)
if (AppConfig.DisableGameAccountSwitcher || CurrentGameBiz.IsBilibiliServer())
{
StackPanel_Account.Visibility = Visibility.Collapsed;
return;
Expand Down
52 changes: 18 additions & 34 deletions src/Starward/Services/GameAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Text;

namespace Starward.Services;

Expand Down Expand Up @@ -37,9 +37,9 @@ public GameAccountService(ILogger<GameAccountService> logger, DatabaseService da
var key = biz.GetGameRegistryKey();
var keyName = (int)biz switch
{
11 or 21 or 31 or 14 or 24 => GameRegistry.MIHOYOSDK_ADL_PROD_CN_h3123967166,
11 or 21 or 31 or 14 or 24 or 41 or 44 => GameRegistry.MIHOYOSDK_ADL_PROD_CN_h3123967166,
13 => GameRegistry.MIHOYOSDK_ADL_0,
12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h1158948810,
12 or 22 or (>= 32 and <= 36) or 42 => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h1158948810,
_ => throw new ArgumentOutOfRangeException($"Unknown region {biz}"),
};

Expand All @@ -63,10 +63,14 @@ 12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h115894
}
else if (biz is GameBiz.hk4e_cn or GameBiz.hk4e_global)
{
List<int> uids = GetUidsFromRegistry(biz).ToList();
if (uids.Count == 1)
byte[]? uidBytes = Registry.GetValue(key, GameRegistry.__LastUid___h2153286551, 0) as byte[];
if (uidBytes is not null)
{
account.Uid = uids[0];
string uidStr = Encoding.UTF8.GetString(uidBytes).Trim();
if (long.TryParse(uidStr, out long uid))
{
account.Uid = uid;
}
}
}
return account;
Expand All @@ -76,26 +80,6 @@ 12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h115894



private IEnumerable<int> GetUidsFromRegistry(GameBiz biz)
{
if (biz is GameBiz.hk4e_cn or GameBiz.hk4e_global or GameBiz.hk4e_bilibili)
{
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)
Expand Down Expand Up @@ -137,10 +121,6 @@ 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();
}

Expand Down Expand Up @@ -172,19 +152,23 @@ public void ChangeGameAccount(GameAccount account)
var key = account.GameBiz.GetGameRegistryKey();
var keyName = (int)account.GameBiz switch
{
11 or 21 or 31 or 14 or 24 => GameRegistry.MIHOYOSDK_ADL_PROD_CN_h3123967166,
11 or 21 or 31 or 14 or 24 or 41 or 44 => GameRegistry.MIHOYOSDK_ADL_PROD_CN_h3123967166,
13 => GameRegistry.MIHOYOSDK_ADL_0,
12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h1158948810,
12 or 22 or (>= 32 and <= 36) or 42 => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h1158948810,
_ => throw new ArgumentOutOfRangeException($"Unknown region {account.GameBiz}"),
};
Registry.SetValue(key, keyName, account.Value);
if (account.GameBiz.ToGame() is GameBiz.StarRail)
{
Registry.SetValue(key, GameRegistry.App_LastUserID_h2841727341, account.Uid);
Registry.SetValue(key, GameRegistry.App_LastUserID_h2841727341, (int)account.Uid, RegistryValueKind.DWord);
}
if (account.GameBiz.ToGame() is GameBiz.Honkai3rd)
{
Registry.SetValue(key, GameRegistry.GENERAL_DATA_V2_LastLoginUserId_h47158221, account.Uid);
Registry.SetValue(key, GameRegistry.GENERAL_DATA_V2_LastLoginUserId_h47158221, (int)account.Uid, RegistryValueKind.DWord);
}
if (account.GameBiz.ToGame() is GameBiz.GenshinImpact)
{
Registry.SetValue(key, GameRegistry.__LastUid___h2153286551, Encoding.UTF8.GetBytes($"{account.Uid}\0"));
}
_logger.LogInformation("Change account {name} ({biz}) successfully!", account.Name, account.GameBiz);
}
Expand Down

0 comments on commit 0f8b2be

Please sign in to comment.