Skip to content

Commit

Permalink
hutao api v2 page
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Oct 21, 2022
1 parent 76800de commit fda642b
Show file tree
Hide file tree
Showing 60 changed files with 1,745 additions and 84 deletions.
8 changes: 6 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Snap.Hutao.Core.Logging;
using Snap.Hutao.Core.Threading;
using Snap.Hutao.Extension;
using Snap.Hutao.Service.AppCenter;
using Snap.Hutao.Service.Metadata;
using System.Diagnostics;
using Windows.Storage;
Expand All @@ -27,13 +28,14 @@ public partial class App : Application
/// Initializes the singleton application object.
/// </summary>
/// <param name="logger">日志器</param>
public App(ILogger<App> logger)
/// <param name="appCenter">App Center</param>
public App(ILogger<App> logger, AppCenter appCenter)
{
// load app resource
InitializeComponent();
this.logger = logger;

_ = new ExceptionRecorder(this, logger);
_ = new ExceptionRecorder(this, logger, appCenter);
}

/// <inheritdoc/>
Expand All @@ -57,6 +59,8 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args)
.ImplictAs<IMetadataInitializer>()?
.InitializeInternalAsync()
.SafeForget(logger);

Ioc.Default.GetRequiredService<AppCenter>().Initialize();
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Snap.Hutao.Control;
/// <summary>
/// 表示支持取消加载的异步页面
/// 在被导航到其他页面前触发取消异步通知
/// <para/>
/// InitializeWith{T}();
/// InitializeComponent();
/// </summary>
public class ScopedPage : Page
{
Expand Down
21 changes: 21 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.Win32;
using Snap.Hutao.Extension;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using Windows.ApplicationModel;

Expand All @@ -12,6 +15,9 @@ namespace Snap.Hutao.Core;
/// </summary>
internal static class CoreEnvironment
{
private const string CryptographyKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\";
private const string MachineGuidValue = "MachineGuid";

// 计算过程:https://gist.github.com/Lightczx/373c5940b36e24b25362728b52dec4fd

/// <summary>
Expand Down Expand Up @@ -49,6 +55,11 @@ internal static class CoreEnvironment
/// </summary>
public static readonly string HoyolabDeviceId;

/// <summary>
/// AppCenter 设备Id
/// </summary>
public static readonly string AppCenterDeviceId;

/// <summary>
/// 默认的Json序列化选项
/// </summary>
Expand All @@ -67,5 +78,15 @@ static CoreEnvironment()

// simply assign a random guid
HoyolabDeviceId = Guid.NewGuid().ToString();
AppCenterDeviceId = GetUniqueUserID();
}

private static string GetUniqueUserID()
{
string userName = Environment.UserName;
object? machineGuid = Registry.GetValue(CryptographyKey, MachineGuidValue, userName);
byte[] bytes = Encoding.UTF8.GetBytes($"{userName}{machineGuid}");
byte[] hash = MD5.Create().ComputeHash(bytes);
return System.Convert.ToHexString(hash);
}
}
10 changes: 6 additions & 4 deletions src/Snap.Hutao/Snap.Hutao/Core/Exception/ExceptionRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.UI.Xaml;
using Snap.Hutao.Core.Logging;
using Snap.Hutao.Service.AppCenter;

namespace Snap.Hutao.Core.Exception;

Expand All @@ -12,25 +13,26 @@ namespace Snap.Hutao.Core.Exception;
internal class ExceptionRecorder
{
private readonly ILogger logger;
private readonly AppCenter appCenter;

/// <summary>
/// 构造一个新的异常记录器
/// </summary>
/// <param name="application">应用程序</param>
/// <param name="logger">日志器</param>
public ExceptionRecorder(Application application, ILogger logger)
/// <param name="appCenter">App Center</param>
public ExceptionRecorder(Application application, ILogger logger, AppCenter appCenter)
{
this.logger = logger;
this.appCenter = appCenter;

application.UnhandledException += OnAppUnhandledException;
application.DebugSettings.BindingFailed += OnXamlBindingFailed;
}

private void OnAppUnhandledException(object? sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// string fileName = $"ex-{DateTimeOffset.Now:yyyyMMddHHmmssffff}.txt";
// File.WriteAllText(Path.Combine(path, fileName), $"{e.Exception}\r\n{e.Exception.StackTrace}");
appCenter.TrackCrash(e.Exception);
logger.LogError(EventIds.UnhandledException, e.Exception, "未经处理的异常");

foreach (ILoggerProvider provider in Ioc.Default.GetRequiredService<IEnumerable<ILoggerProvider>>())
Expand Down
9 changes: 7 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/Factory/AsyncRelayCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
using CommunityToolkit.Mvvm.Input;
using Snap.Hutao.Core.Logging;
using Snap.Hutao.Factory.Abstraction;
using Snap.Hutao.Service.AppCenter;

namespace Snap.Hutao.Factory;

/// <inheritdoc cref="IAsyncRelayCommandFactory"/>
[Injection(InjectAs.Transient, typeof(IAsyncRelayCommandFactory))]
internal class AsyncRelayCommandFactory : IAsyncRelayCommandFactory
{
private readonly ILogger logger;
private readonly ILogger<AsyncRelayCommandFactory> logger;
private readonly AppCenter appCenter;

/// <summary>
/// 构造一个新的异步命令工厂
/// </summary>
/// <param name="logger">日志器</param>
public AsyncRelayCommandFactory(ILogger<AsyncRelayCommandFactory> logger)
/// <param name="appCenter">App Center</param>
public AsyncRelayCommandFactory(ILogger<AsyncRelayCommandFactory> logger, AppCenter appCenter)
{
this.logger = logger;
this.appCenter = appCenter;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -94,6 +98,7 @@ private void ReportException(IAsyncRelayCommand command)
{
Exception baseException = exception.GetBaseException();
logger.LogError(EventIds.AsyncCommandException, baseException, "{name} Exception", nameof(AsyncRelayCommand));
appCenter.TrackError(exception);
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Binding/Hutao/ComplexAvatar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Model.Intrinsic;
using Snap.Hutao.Model.Metadata.Avatar;
using Snap.Hutao.Model.Metadata.Converter;

namespace Snap.Hutao.Model.Binding.Hutao;

/// <summary>
/// 角色
/// </summary>
internal class ComplexAvatar
{
/// <summary>
/// 构造一个胡桃数据库角色
/// </summary>
/// <param name="avatar">元数据角色</param>
/// <param name="rate">率</param>
public ComplexAvatar(Avatar avatar, double rate)
{
Name = avatar.Name;
Icon = AvatarIconConverter.IconNameToUri(avatar.Icon);
Quality = avatar.Quality;
Rate = $"{rate:P3}";
}

/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = default!;

/// <summary>
/// 图标
/// </summary>
public Uri Icon { get; set; } = default!;

/// <summary>
/// 星级
/// </summary>
public ItemQuality Quality { get; set; }

/// <summary>
/// 比率
/// </summary>
public string Rate { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Model.Intrinsic;
using Snap.Hutao.Model.Metadata.Avatar;
using Snap.Hutao.Model.Metadata.Converter;

namespace Snap.Hutao.Model.Binding.Hutao;

/// <summary>
/// 角色搭配
/// </summary>
internal class ComplexAvatarCollocation : ComplexAvatar
{
/// <summary>
/// 构造一个新的角色搭配
/// </summary>
/// <param name="avatar">角色</param>
/// <param name="rate">比率</param>
public ComplexAvatarCollocation(Avatar avatar)
: base(avatar, 0)
{
}

/// <summary>
/// 角色
/// </summary>
public List<ComplexAvatar> Avatars { get; set; } = default!;

/// <summary>
/// 武器
/// </summary>
public List<ComplexWeapon> Weapons { get; set; } = default!;

/// <summary>
/// 圣遗物套装
/// </summary>
public List<ComplexReliquarySet> ReliquarySets { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Model.Metadata.Avatar;

namespace Snap.Hutao.Model.Binding.Hutao;

/// <summary>
/// 角色命座信息
/// </summary>
internal class ComplexAvatarConstellationInfo : ComplexAvatar
{
/// <summary>
/// 构造一个新的角色命座信息
/// </summary>
/// <param name="avatar">角色</param>
/// <param name="rate">持有率</param>
/// <param name="rates">命座比率</param>
public ComplexAvatarConstellationInfo(Avatar avatar, double rate, IEnumerable<double> rates)
: base(avatar, rate)
{
Rates = rates.Select(r => $"{r:P3}").ToList();
}

/// <summary>
/// 命座比率
/// </summary>
public List<string> Rates { get; set; }
}
20 changes: 20 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Binding/Hutao/ComplexAvatarRank.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Model.Binding.Hutao;

/// <summary>
/// 角色榜
/// </summary>
internal class ComplexAvatarRank
{
/// <summary>
/// 层数
/// </summary>
public string Floor { get; set; } = default!;

/// <summary>
/// 排行信息
/// </summary>
public List<ComplexAvatar> Avatars { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Model.Metadata.Converter;
using Snap.Hutao.Web.Hutao.Model;
using System.Text;

namespace Snap.Hutao.Model.Binding.Hutao;

/// <summary>
/// 圣遗物套装
/// </summary>
internal class ComplexReliquarySet
{
/// <summary>
/// 构造一个新的胡桃数据库圣遗物套装
/// </summary>
/// <param name="reliquarySetRate">圣遗物套装率</param>
/// <param name="idReliquarySetMap">圣遗物套装映射</param>
public ComplexReliquarySet(ItemRate<ReliquarySets, double> reliquarySetRate, Dictionary<int, Metadata.Reliquary.ReliquarySet> idReliquarySetMap)
{
ReliquarySets sets = reliquarySetRate.Item;

if (sets.Count >= 1)
{
StringBuilder setStringBuilder = new();
List<Uri> icons = new();
foreach (ReliquarySet set in sets)
{
Metadata.Reliquary.ReliquarySet metaSet = idReliquarySetMap[set.EquipAffixId / 10];

if (setStringBuilder.Length != 0)
{
setStringBuilder.Append(Environment.NewLine);
}

setStringBuilder.Append(set.Count).Append('×').Append(metaSet.Name);
icons.Add(RelicIconConverter.IconNameToUri(metaSet.Icon));
}

Name = setStringBuilder.ToString();
Icons = icons;
}
else
{
Name = "无圣遗物";
}

Rate = $"{reliquarySetRate.Rate:P3}";
}

/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = default!;

/// <summary>
/// 图标
/// </summary>
public List<Uri> Icons { get; set; } = default!;

/// <summary>
/// 比率
/// </summary>
public string Rate { get; set; } = default!;
}
Loading

0 comments on commit fda642b

Please sign in to comment.