Skip to content

Commit

Permalink
Merge branch 'main' into streamdownload
Browse files Browse the repository at this point in the history
  • Loading branch information
iamscottxu committed Aug 27, 2024
2 parents f33702f + 0f8b2be commit 101e7b1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 46 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
10 changes: 9 additions & 1 deletion src/Starward/Pages/SelfQueryPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,17 @@ private void LoadMonthQueryItems(int type, string month)
}
if (gameBiz.ToGame() is GameBiz.ZZZ)
{
ZZZQueryItemList = dapper.Query<ZZZQueryItem>("""
var list = ZZZQueryItemList = dapper.Query<ZZZQueryItem>("""
SELECT * FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type AND DateTime LIKE @month ORDER BY DateTime DESC;
""", new { uid, type, month = month + "%" }).ToList();
if (type is (int)ZZZQueryType.PurchaseGift)
{
foreach (var item in list)
{
item.Reason = item.ItemName;
}
}
ZZZQueryItemList = list;
MonthAddNum = ZZZQueryItemList.Where(x => x.AddNum > 0).Sum(x => x.AddNum);
MonthSubNum = ZZZQueryItemList.Where(x => x.AddNum < 0).Sum(x => x.AddNum);
}
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
22 changes: 16 additions & 6 deletions src/Starward/Services/SelfQueryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,22 @@ public List<long> GetZZZUids()
public (long Add, long Sub) GetZZZQueryItemsNumSum(long uid, ZZZQueryType type)
{
using var dapper = _databaseService.CreateConnection();
long add = dapper.QueryFirstOrDefault<long>("""
SELECT IFNULL(SUM(AddNum), 0) FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type AND AddNum>0;
""", new { uid, type });
long sub = dapper.QueryFirstOrDefault<long>("""
SELECT IFNULL(SUM(AddNum), 0) FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type AND AddNum<0;
""", new { uid, type });
long add = 0, sub = 0;
if (type is ZZZQueryType.PurchaseGift)
{
add = dapper.QueryFirstOrDefault<long>("""
SELECT COUNT(*) FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type;
""", new { uid, type });
}
else
{
add = dapper.QueryFirstOrDefault<long>("""
SELECT IFNULL(SUM(AddNum), 0) FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type AND AddNum>0;
""", new { uid, type });
sub = dapper.QueryFirstOrDefault<long>("""
SELECT IFNULL(SUM(AddNum), 0) FROM ZZZQueryItem WHERE Uid=@uid AND Type=@type AND AddNum<0;
""", new { uid, type });
}
return (add, sub);
}

Expand Down

0 comments on commit 101e7b1

Please sign in to comment.