Skip to content

Commit

Permalink
[Core] Refactor MessagePacker to static class
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 28, 2023
1 parent 96369a5 commit 8ca7f25
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ protected override bool Build(MultiMsgUploadEvent input, BotKeystore keystore, B
{
if (input.Chains == null) throw new ArgumentNullException(nameof(input.Chains));

var msgPacker = new MessagePacker(keystore.Uid ?? "");
var msgBody = input.Chains.Select(msgPacker.BuildFake).ToList();
var msgBody = input.Chains.Select(x => MessagePacker.BuildFake(x, keystore.Uid ?? "")).ToList();
var longMsgResult = new LongMsgResult
{
Action = new LongMsgAction
Expand Down
3 changes: 1 addition & 2 deletions Lagrange.Core/Internal/Service/Message/SendMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ internal class SendMessageService : BaseService<SendMessageEvent>
protected override bool Build(SendMessageEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packer = new MessagePacker(keystore.Uid ?? throw new Exception("No UID found in keystore"));
var packet = packer.Build(input.Chain);
var packet = MessagePacker.Build(input.Chain, keystore.Uid ?? throw new Exception("No UID found in keystore"));

using var stream = new MemoryStream();
Serializer.Serialize(stream, packet);
Expand Down
19 changes: 6 additions & 13 deletions Lagrange.Core/Message/MessagePacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ namespace Lagrange.Core.Message;
/// <summary>
/// Pack up message into the Protobuf <see cref="Message"/>
/// </summary>
internal class MessagePacker
internal static class MessagePacker
{
private static readonly Dictionary<Type, List<PropertyInfo>> EntityToElem;
private static readonly Dictionary<Type, IMessageEntity> Factory;
private static readonly List<IMessageEntity> MsgFactory;

private readonly string _selfUid;


static MessagePacker()
{
EntityToElem = new Dictionary<Type, List<PropertyInfo>>();
Expand Down Expand Up @@ -56,19 +54,14 @@ static MessagePacker()

MsgFactory = assembly.GetImplementations<IMessageEntity>().Select(type => (IMessageEntity)type.CreateInstance()).ToList();
}

public MessagePacker(string selfUid)
{
_selfUid = selfUid;
}

public Internal.Packets.Message.Message Build(MessageChain chain)
public static Internal.Packets.Message.Message Build(MessageChain chain, string selfUid)
{
var message = BuildPacketBase(chain);

foreach (var entity in chain)
{
entity.SetSelfUid(_selfUid);
entity.SetSelfUid(selfUid);

if (message.Body != null)
{
Expand All @@ -86,13 +79,13 @@ public Internal.Packets.Message.Message Build(MessageChain chain)
return message;
}

public PushMsgBody BuildFake(MessageChain chain)
public static PushMsgBody BuildFake(MessageChain chain, string selfUid)
{
var message = BuildFakePacketBase(chain);

foreach (var entity in chain)
{
entity.SetSelfUid(_selfUid);
entity.SetSelfUid(selfUid);

if (message.Body != null)
{
Expand Down

0 comments on commit 8ca7f25

Please sign in to comment.