From c283dc3abf41a5ddf06bebc439349c533b41a0dd Mon Sep 17 00:00:00 2001 From: DarkRRb <177549718+DarkRRb@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:32:16 +0800 Subject: [PATCH] fix stack overflow --- Lagrange.OneBot/LagrangeAppBuilder.cs | 5 ++++- Lagrange.OneBot/Utility/LiteDbUtility.cs | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Lagrange.OneBot/LagrangeAppBuilder.cs b/Lagrange.OneBot/LagrangeAppBuilder.cs index dee93b2b1..5f8ac37ea 100644 --- a/Lagrange.OneBot/LagrangeAppBuilder.cs +++ b/Lagrange.OneBot/LagrangeAppBuilder.cs @@ -119,7 +119,10 @@ public LagrangeAppBuilder ConfigureOneBot() BsonMapper.Global.EmptyStringToNull = false; // Specify ctor for some classes - BsonMapper.Global.RegisterType(BsonMapper.Global.Serialize, LiteDbUtility.IMessageEntityDeserialize); + BsonMapper.Global.RegisterType( + LiteDbUtility.IMessageEntitySerialize, + LiteDbUtility.IMessageEntityDeserialize + ); string path = Configuration["ConfigPath:Database"] ?? $"lagrange-{Configuration["Account:Uin"]}.db"; diff --git a/Lagrange.OneBot/Utility/LiteDbUtility.cs b/Lagrange.OneBot/Utility/LiteDbUtility.cs index 094ab1a0e..006e566b5 100644 --- a/Lagrange.OneBot/Utility/LiteDbUtility.cs +++ b/Lagrange.OneBot/Utility/LiteDbUtility.cs @@ -6,6 +6,14 @@ namespace Lagrange.OneBot.Utility; public static class LiteDbUtility { + public static BsonValue IMessageEntitySerialize(IMessageEntity entity) + { + var type = entity.GetType(); + var result = BsonMapper.Global.Serialize(type, entity); + result["_type"] = new BsonValue(DefaultTypeNameBinder.Instance.GetName(type)); + return result; + } + public static IMessageEntity IMessageEntityDeserialize(BsonValue bson) { if (!bson.IsDocument) throw new Exception("bson not BsonDocument"); @@ -17,6 +25,7 @@ public static IMessageEntity IMessageEntityDeserialize(BsonValue bson) } var type = DefaultTypeNameBinder.Instance.GetType(typeBson.AsString); + if (type == typeof(MarkdownEntity)) return MarkdownEntityDeserialize(doc); return (IMessageEntity)BsonMapper.Global.Deserialize(type, bson);