diff --git a/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs b/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs index c56428801..674c1dc15 100644 --- a/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs +++ b/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs @@ -7,22 +7,25 @@ using Lagrange.OneBot.Core.Entity.Message; using Lagrange.OneBot.Message; using LiteDB; +using Microsoft.Extensions.Logging; using JsonSerializer = System.Text.Json.JsonSerializer; namespace Lagrange.OneBot.Core.Operation.Message; public partial class MessageCommon { + private readonly ILogger _logger; + private readonly Dictionary _typeToSegment; - public MessageCommon(LiteDatabase database) + public MessageCommon(LiteDatabase database, ILogger logger) { + _logger = logger; _typeToSegment = new Dictionary(); foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { - var attribute = type.GetCustomAttribute(); - if (attribute != null) + if (type.GetCustomAttribute() is { } attribute) { var instance = (SegmentBase)type.CreateInstance(false); instance.Database = database; @@ -51,12 +54,20 @@ public MessageBuilder ParseChain(OneBotMessageSimple message) return builder; } - public static MessageBuilder ParseChain(OneBotMessageText message) + public MessageBuilder ParseChain(OneBotMessageText message) { var builder = message.MessageType == "private" ? MessageBuilder.Friend(message.UserId ?? 0) : MessageBuilder.Group(message.GroupId ?? 0); - builder.Text(message.Messages); + + if (message.AutoEscape == true) + { + builder.Text(message.Messages); + } + else + { + BuildMessages(builder, message.Messages); + } return builder; } @@ -152,8 +163,9 @@ private void BuildMessages(MessageBuilder builder, string message) var pair = capture.Value.Split('=', 2); if (pair.Length == 2) data[pair[0]] = UnescapeCQ(pair[1]); } - var cast = (SegmentBase)JsonSerializer.SerializeToElement(data).Deserialize(instance.GetType())!; - instance.Build(builder, cast); + + if (JsonSerializer.SerializeToElement(data).Deserialize(instance.GetType()) is SegmentBase cast) instance.Build(builder, cast); + else Log.LogCQFailed(_logger, type, string.Empty); } } @@ -166,8 +178,8 @@ private void BuildMessages(MessageBuilder builder, List segments) { if (_typeToSegment.TryGetValue(segment.Type, out var instance)) { - var cast = (SegmentBase)((JsonElement)segment.Data).Deserialize(instance.GetType())!; - instance.Build(builder, cast); + if (((JsonElement)segment.Data).Deserialize(instance.GetType()) is SegmentBase cast) instance.Build(builder, cast); + else Log.LogCQFailed(_logger, segment.Type, string.Empty); } } } @@ -176,8 +188,14 @@ private void BuildMessages(MessageBuilder builder, OneBotSegment segment) { if (_typeToSegment.TryGetValue(segment.Type, out var instance)) { - var cast = (SegmentBase)((JsonElement)segment.Data).Deserialize(instance.GetType())!; - instance.Build(builder, cast); + if (((JsonElement)segment.Data).Deserialize(instance.GetType()) is SegmentBase cast) instance.Build(builder, cast); + else Log.LogCQFailed(_logger, segment.Type, string.Empty); } } + + private static partial class Log + { + [LoggerMessage(EventId = 1, Level = LogLevel.Warning, Message = "Segment {type} Deserialization failed for code {code}")] + public static partial void LogCQFailed(ILogger logger, string type, string code); + } } diff --git a/Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.cs b/Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.cs index c1bed4875..bfd7ffd6a 100644 --- a/Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.cs +++ b/Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.cs @@ -16,7 +16,7 @@ public async Task HandleOperation(BotContext context, JsonObject? { OneBotMessage message => await context.SendMessage(common.ParseChain(message).Build()), OneBotMessageSimple messageSimple => await context.SendMessage(common.ParseChain(messageSimple).Build()), - OneBotMessageText messageText => await context.SendMessage(MessageCommon.ParseChain(messageText).Build()), + OneBotMessageText messageText => await context.SendMessage(common.ParseChain(messageText).Build()), _ => throw new Exception() }; diff --git a/Lagrange.OneBot/Core/Operation/OperationService.cs b/Lagrange.OneBot/Core/Operation/OperationService.cs index 8eb33605c..020774403 100644 --- a/Lagrange.OneBot/Core/Operation/OperationService.cs +++ b/Lagrange.OneBot/Core/Operation/OperationService.cs @@ -35,6 +35,7 @@ public OperationService(BotContext bot, ILogger logger, LiteDa service.AddSingleton(logger); service.AddSingleton(message); service.AddSingleton(); + service.AddLogging(); foreach (var (_, type) in _operations) service.AddScoped(type); _service = service.BuildServiceProvider();