Skip to content

Commit

Permalink
fixup ds algorithm call
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Nov 18, 2022
1 parent f1dda02 commit a8bc0cf
Show file tree
Hide file tree
Showing 67 changed files with 784 additions and 539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public class HttpClientGenerator : ISourceGenerator
{
private const string DefaultName = "Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient.HttpClientConfigration.Default";
private const string XRpcName = "Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient.HttpClientConfigration.XRpc";
private const string XRpc2Name = "Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient.HttpClientConfigration.XRpc2";

private const string PrimaryHttpMessageHandlerAttributeName = "Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient.PrimaryHttpMessageHandlerAttribute";
private const string DynamicSecretAttributeName = "Snap.Hutao.Web.Hoyolab.DynamicSecret.UseDynamicSecretAttribute";

/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
Expand All @@ -50,6 +52,7 @@ public void Execute(GeneratorExecutionContext context)
// This class is generated by Snap.Hutao.SourceGeneration
using Microsoft.Extensions.DependencyInjection;
using Snap.Hutao.Web.Hoyolab.DynamicSecret;
using System.Net.Http;
namespace Snap.Hutao.Core.DependencyInjection;
Expand Down Expand Up @@ -100,6 +103,9 @@ private static void FillWithInjectionServices(HttpClientSyntaxContextReceiver re
case XRpcName:
lineBuilder.Append(@"XRpcConfiguration)");
break;
case XRpc2Name:
lineBuilder.Append(@"XRpc2Configuration)");
break;
default:
throw new InvalidOperationException($"非法的HttpClientConfigration值: [{injectAsName}]");
}
Expand All @@ -125,6 +131,11 @@ private static void FillWithInjectionServices(HttpClientSyntaxContextReceiver re
lineBuilder.Append(" })");
}

if (classSymbol.GetAttributes().Any(attr => attr.AttributeClass!.ToDisplayString() == DynamicSecretAttributeName))
{
lineBuilder.Append(".AddHttpMessageHandler<DynamicSecretHandler>()");
}

lineBuilder.Append(";");

lines.Add(lineBuilder.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using CommunityToolkit.WinUI.UI.Behaviors;
using Microsoft.UI.Xaml;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Behavior;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using CommunityToolkit.WinUI.UI.Behaviors;
using Microsoft.UI.Xaml;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Behavior;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using CommunityToolkit.WinUI.UI.Behaviors;
using Microsoft.UI.Xaml;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Behavior;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using CommunityToolkit.WinUI.UI.Behaviors;
using Microsoft.UI.Xaml;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Behavior;

Expand Down
1 change: 0 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Control/BindingProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

using Microsoft.UI.Xaml;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using Snap.Hutao.Core;
using Snap.Hutao.Core.Caching;
using Snap.Hutao.Extension;
using Snap.Hutao.Service.Abstraction;
Expand Down
1 change: 0 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Control/Markup/I18NHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Markup;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Core;

namespace Snap.Hutao.Control.Panel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Microsoft.UI.Xaml;

namespace Snap.Hutao.Core;
namespace Snap.Hutao.Control;

/// <summary>
/// 快速创建 <see cref="TOwner"/> 的 <see cref="DependencyProperty"/>
Expand Down
38 changes: 38 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/Convert/CastTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using System.Linq.Expressions;

namespace Snap.Hutao.Core.Convert;

/// <summary>
/// Class to cast to type <see cref="TTo"/>
/// </summary>
/// <typeparam name="TTo">Target type</typeparam>
public static class CastTo<TTo>
{
/// <summary>
/// Casts <see cref="s"/> to <see cref="TTo"/>.
/// This does not cause boxing for value types.
/// Useful in generic methods.
/// </summary>
/// <typeparam name="TFrom">Source type to cast from. Usually a generic type.</typeparam>
/// <param name="from">from value</param>
/// <returns>target value</returns>
public static TTo From<TFrom>(TFrom from)
{
return Cache<TFrom>.Caster(from);
}

private static class Cache<TCachedFrom>
{
public static readonly Func<TCachedFrom, TTo> Caster = Get();

private static Func<TCachedFrom, TTo> Get()
{
ParameterExpression param = Expression.Parameter(typeof(TCachedFrom));
UnaryExpression convert = Expression.ConvertChecked(param, typeof(TTo));
return Expression.Lambda<Func<TCachedFrom, TTo>>(convert, param).Compile();
}
}
}
25 changes: 10 additions & 15 deletions src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Snap.Hutao.Web.Hoyolab.DynamicSecret;
using System.Collections.Immutable;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization.Metadata;
using System.Text.Unicode;
using Windows.ApplicationModel;

Expand All @@ -26,20 +27,7 @@ internal static class CoreEnvironment
/// <summary>
/// 米游社 Rpc 版本
/// </summary>
public const string HoyolabXrpcVersion = "2.40.1";

/// <summary>
/// 动态密钥
/// https://github.com/UIGF-org/Hoyolab.Salt
/// </summary>
public static readonly ImmutableDictionary<SaltType, string> DynamicSecrets = new Dictionary<SaltType, string>()
{
[SaltType.K2] = "fdv0fY9My9eA7MR0NpjGP9RjueFvjUSQ",
[SaltType.LK2] = "jEpJb9rRARU2rXDA9qYbZ3selxkuct9a",
[SaltType.X4] = "xV8v4Qu54lUKrEYFZkJhB8cuOh9Asafs",
[SaltType.X6] = "t0qEgfub6cvueAPgR5m9aQWWVciEer7v",
[SaltType.PROD] = "JwYDpKvLj6MrMqqYU6jTKF17KNO2PXoS",
}.ToImmutableDictionary();
public const string HoyolabXrpcVersion = "2.41.0";

/// <summary>
/// 标准UA
Expand Down Expand Up @@ -72,9 +60,16 @@ internal static class CoreEnvironment
public static readonly JsonSerializerOptions JsonOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = new InboxTextEncoder(), //JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Encoder = new JsonTextEncoder(),
PropertyNameCaseInsensitive = true,
WriteIndented = true,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
{
Modifiers =
{
JsonTypeInfoResolvers.ResolveEnumType,
},
},
};

private const string CryptographyKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public enum HttpClientConfigration
/// 米游社请求配置
/// </summary>
XRpc,

/// <summary>
/// 米游社登录请求配置
/// </summary>
XRpc2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ private static void XRpcConfiguration(HttpClient client)
client.DefaultRequestHeaders.Add("x-rpc-client_type", "5");
client.DefaultRequestHeaders.Add("x-rpc-device_id", CoreEnvironment.HoyolabDeviceId);
}

/// <summary>
/// 对于需要添加动态密钥的客户端使用此配置
/// </summary>
/// <param name="client">配置后的客户端</param>
private static void XRpc2Configuration(HttpClient client)
{
client.Timeout = Timeout.InfiniteTimeSpan;
client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreEnvironment.HoyolabUA);
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
client.DefaultRequestHeaders.Add("x-rpc-aigis", string.Empty);
client.DefaultRequestHeaders.Add("x-rpc-app_id", "bll8iq97cem8");
client.DefaultRequestHeaders.Add("x-rpc-app_version", CoreEnvironment.HoyolabXrpcVersion);
client.DefaultRequestHeaders.Add("x-rpc-client_type", "2");
client.DefaultRequestHeaders.Add("x-rpc-device_id", CoreEnvironment.HoyolabDeviceId);
client.DefaultRequestHeaders.Add("x-rpc-game_biz", "bbs_cn");
client.DefaultRequestHeaders.Add("x-rpc-sdk_version", "1.3.1.2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Core.Json.Annotation;

/// <summary>
/// Json 枚举类型
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
internal class JsonEnumAttribute : Attribute
{
/// <summary>
/// 构造一个新的Json枚举声明
/// </summary>
/// <param name="readAs">读取</param>
/// <param name="writeAs">写入</param>
public JsonEnumAttribute(JsonSerializeType readAs, JsonSerializeType writeAs)
{
ReadAs = readAs;
WriteAs = writeAs;
}

/// <summary>
/// 读取形式
/// </summary>
public JsonSerializeType ReadAs { get; init; }

/// <summary>
/// 写入形式
/// </summary>
public JsonSerializeType WriteAs { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Core.Json.Annotation;

/// <summary>
/// Json 文本字符串序列化类型
/// </summary>
public enum JsonSerializeType
{
/// <summary>
/// 数字
/// </summary>
Int32,

/// <summary>
/// 字符串包裹的数字
/// </summary>
Int32AsString,

/// <summary>
/// 名称字符串
/// </summary>
String,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Core.Convert;
using Snap.Hutao.Core.Json.Annotation;

namespace Snap.Hutao.Core.Json.Converter;

/// <summary>
/// 枚举转换器
/// </summary>
/// <typeparam name="TEnum">枚举的类型</typeparam>
internal class ConfigurableEnumConverter<TEnum> : JsonConverter<TEnum>
where TEnum : struct, Enum
{
private readonly JsonSerializeType readAs;
private readonly JsonSerializeType writeAs;

/// <summary>
/// 构造一个新的枚举转换器
/// </summary>
/// <param name="readAs">读取</param>
/// <param name="writeAs">写入</param>
public ConfigurableEnumConverter(JsonSerializeType readAs, JsonSerializeType writeAs)
{
this.readAs = readAs;
this.writeAs = writeAs;
}

/// <inheritdoc/>
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (readAs == JsonSerializeType.Int32)
{
return CastTo<TEnum>.From(reader.GetInt32());
}

if (reader.GetString() is string str)
{
return Enum.Parse<TEnum>(str);
}

throw Must.NeverHappen();
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
{
switch (writeAs)
{
case JsonSerializeType.Int32:
writer.WriteNumberValue(CastTo<int>.From(value));
break;
case JsonSerializeType.Int32AsString:
writer.WriteStringValue(value.ToString("D"));
break;
default:
writer.WriteStringValue(value.ToString());
break;
}
}
}
Loading

0 comments on commit a8bc0cf

Please sign in to comment.