Skip to content

Commit 52264f7

Browse files
committed
[Milky] Suppress the IL3050 Risk
1 parent e2ca787 commit 52264f7

1 file changed

Lines changed: 19 additions & 37 deletions

File tree

Lagrange.Milky/Extension/BotContextExtension.cs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Lagrange.Milky.Extension;
77

88
[UnconditionalSuppressMessage("Trimming", "IL2026")]
99
[UnconditionalSuppressMessage("Trimming", "IL2075")]
10+
[UnconditionalSuppressMessage("Trimming", "IL3050", Justification = "There is no generic method in this code.")]
1011
public static class BotContextExtension
1112
{
1213
private delegate ValueTask<(int RetCode, string Extra, ReadOnlyMemory<byte> Data)> SendPacketDelegate(BotContext bot, string cmd, int sequence, byte[] data);
@@ -16,21 +17,18 @@ public static class BotContextExtension
1617
private const string EncryptTypeFullName = "Lagrange.Core.Internal.Packets.Struct.EncryptType";
1718
private const string ServiceAttributeFullName = "Lagrange.Core.Internal.Services.ServiceAttribute";
1819

19-
private static readonly Lazy<SendPacketDelegate> _sendPacket = new(() =>
20+
private static readonly Lazy<SendPacketDelegate> SendPacketDelegateGenerator = new(() =>
2021
{
21-
var coreAssembly = Assembly.GetAssembly(typeof(BotContext));
22-
if (coreAssembly == null) throw new Exception("Assembly(Lagrange.Core) not found");
22+
var coreAssembly = Assembly.GetAssembly(typeof(BotContext)) ?? throw new Exception("Assembly(Lagrange.Core) not found");
2323
Type ssoPacketType;
2424
{
2525
var type = coreAssembly.GetType(SsoPacketFullName);
26-
if (type == null) throw new Exception($"Type({SsoPacketFullName}) not found");
27-
ssoPacketType = type;
26+
ssoPacketType = type ?? throw new Exception($"Type({SsoPacketFullName}) not found");
2827
}
2928
Type serviceAttributeType;
3029
{
3130
var type = coreAssembly.GetType(ServiceAttributeFullName);
32-
if (type == null) throw new Exception($"Type({ServiceAttributeFullName}) not found");
33-
serviceAttributeType = type;
31+
serviceAttributeType = type ?? throw new Exception($"Type({ServiceAttributeFullName}) not found");
3432
}
3533

3634
//
@@ -46,39 +44,26 @@ public static class BotContextExtension
4644
// Create SsoPacket
4745
NewExpression packet;
4846
{
49-
var ctor = ssoPacketType.GetConstructor(
50-
[typeof(string), typeof(ReadOnlyMemory<byte>), typeof(int)]
51-
);
52-
if (ctor == null)
53-
{
54-
throw new Exception($"Ctor({SsoPacketFullName}(string, ReadOnlyMemory<byte>, int)) not found");
55-
}
56-
packet = Expression.New(ctor, [cmd, dataInput, sequence]);
47+
var ctor = ssoPacketType.GetConstructor([typeof(string), typeof(ReadOnlyMemory<byte>), typeof(int)]) ?? throw new Exception($"Ctor({SsoPacketFullName}(string, ReadOnlyMemory<byte>, int)) not found");
48+
49+
packet = Expression.New(ctor, cmd, dataInput, sequence);
5750
}
5851

5952
// Create ServiceAttribute
6053
NewExpression options;
6154
{
62-
var requestTypeType = coreAssembly.GetType(RequestTypeFullName);
63-
if (requestTypeType == null) throw new Exception($"Type({RequestTypeFullName}) not found");
55+
var requestTypeType = coreAssembly.GetType(RequestTypeFullName) ?? throw new Exception($"Type({RequestTypeFullName}) not found");
6456
var requestType = Expression.Constant(Enum.Parse(requestTypeType, "D2Auth"));
6557

66-
var encryptTypeType = coreAssembly.GetType(EncryptTypeFullName);
67-
if (encryptTypeType == null) throw new Exception($"Type({EncryptTypeFullName}) not found");
58+
var encryptTypeType = coreAssembly.GetType(EncryptTypeFullName) ?? throw new Exception($"Type({EncryptTypeFullName}) not found");
6859
var encryptType = Expression.Constant(Enum.Parse(encryptTypeType, "EncryptD2Key"));
6960

70-
var ctor = serviceAttributeType.GetConstructor(
71-
[typeof(string), requestTypeType, encryptTypeType]
72-
);
73-
if (ctor == null)
74-
{
75-
throw new Exception($"Ctor({ServiceAttributeFullName}(string, RequestType, EncryptType)) not found");
76-
}
77-
options = Expression.New(ctor, [cmd, requestType, encryptType]);
61+
var ctor = serviceAttributeType.GetConstructor([typeof(string), requestTypeType, encryptTypeType]) ?? throw new Exception($"Ctor({ServiceAttributeFullName}(string, RequestType, EncryptType)) not found");
62+
options = Expression.New(ctor, cmd, requestType, encryptType);
7863
}
7964

8065
// Call SendPacket
81-
var vt = Expression.Call(context, "SendPacket", [], [packet, options]);
66+
var vt = Expression.Call(context, "SendPacket", [], packet, options);
8267

8368
var taskResult = Expression.Property(vt, "Result");
8469

@@ -89,22 +74,19 @@ public static class BotContextExtension
8974
NewExpression result;
9075
{
9176
var type = typeof((int, string, ReadOnlyMemory<byte>));
92-
var ctor = type.GetConstructor([typeof(int), typeof(string), typeof(ReadOnlyMemory<byte>)]);
93-
if (ctor == null) throw new Exception("Ctor((int, string, ReadOnlyMemory<byte>)) not found");
94-
var newResult = Expression.New(ctor, [retcode, extra, dataResult]);
77+
var ctor = type.GetConstructor([typeof(int), typeof(string), typeof(ReadOnlyMemory<byte>)]) ?? throw new Exception("Ctor((int, string, ReadOnlyMemory<byte>)) not found");
78+
var newResult = Expression.New(ctor, retcode, extra, dataResult);
79+
var method = typeof(ValueTask<(int, string, ReadOnlyMemory<byte>)>).GetConstructor([type]) ?? throw new Exception("Ctor(ValueTask((int, string, ReadOnlyMemory<byte>))) not found");
9580

96-
var method = typeof(ValueTask<(int, string, ReadOnlyMemory<byte>)>).GetConstructor([type]);
97-
if (method == null) throw new Exception("Ctor(ValueTask((int, string, ReadOnlyMemory<byte>))) not found");
98-
99-
result = Expression.New(method, [newResult]);
81+
result = Expression.New(method, newResult);
10082
}
10183

102-
return Expression.Lambda<SendPacketDelegate>(result, [bot, cmd, sequence, dataInput]).Compile();
84+
return Expression.Lambda<SendPacketDelegate>(result, bot, cmd, sequence, dataInput).Compile();
10385
}
10486
});
10587

10688
public static ValueTask<(int RetCode, string Extra, ReadOnlyMemory<byte> Data)> SendPacket(this BotContext bot, string cmd, int sequence, byte[] data)
10789
{
108-
return _sendPacket.Value(bot, cmd, sequence, data);
90+
return SendPacketDelegateGenerator.Value(bot, cmd, sequence, data);
10991
}
11092
}

0 commit comments

Comments
 (0)