Skip to content

Commit

Permalink
[Core] MessagePacker.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Mar 30, 2024
1 parent 764584f commit c1f5a48
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 13 deletions.
2 changes: 1 addition & 1 deletion KonataNT/BotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<string> FetchClientKey()
throw new Exception("干什么!");
}

public async Task<List<BotGroupContext>> GetGroups(bool refreshCache = false)
public async Task<List<BotGroupContext>> GetGroup(uint groupUin, bool refreshCache = false)

Check warning on line 25 in KonataNT/BotClient.cs

View workflow job for this annotation

GitHub Actions / Build-Package

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotImplementedException();
}
Expand Down
50 changes: 49 additions & 1 deletion KonataNT/Core/Handlers/CacheHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
using KonataNT.Common.Context;
using KonataNT.Core.Packet.Oidb;
using KonataNT.Core.Packet.Service.Oidb;
using KonataNT.Utility;

namespace KonataNT.Core.Handlers;

/// <summary>
/// Caching Uid, <see cref="BotFriendContext"/>, <see cref="BotGroupContext"/>, <see cref="BotMemberContext"/>
/// </summary>
internal class CacheHandler(BaseClient client)
internal class CacheHandler
{
private readonly BaseClient _client;


/// <summary>
/// Caching Uid, <see cref="BotFriendContext"/>, <see cref="BotGroupContext"/>, <see cref="BotMemberContext"/>
/// </summary>
public CacheHandler(BaseClient client)
{
_client = client;

_client.EventEmitter.OnBotGroupMessageEvent += async (_, e) =>
{
if (!Members.ContainsKey(e.GroupUin)) await GetMembers(e.GroupUin);
};
}

private Dictionary<uint, BotFriendContext> Friends { get; } = new();

private Dictionary<uint, BotGroupContext> Groups { get; } = new();
Expand Down Expand Up @@ -43,7 +62,36 @@ public async Task<List<BotMemberContext>> GetMembers(uint groupUin, bool refresh
{
if (refreshCache || !Members.TryGetValue(groupUin, out var members))
{
var memberList = new List<BotMemberContext>();
string? token = null;

while (token != null)
{
var packet = new OidbSvcTrpcTcp0xFE7_3
{
GroupUin = groupUin,
Field2 = 5,
Field3 = 2,
Body = new OidbSvcTrpcScp0xFE7_3Body
{
MemberName = true,
MemberCard = true,
Level = true,
JoinTimestamp = true,
LastMsgTimestamp = true,
Permission = true,
},
Token = token
};

var response = await _client.PacketHandler.SendOidb(0xfe7, 3, packet.Serialize(), false);
var payload = response.Deserialize<OidbSvcBase>();
var body = payload.Body?.Deserialize<OidbSvcTrpcTcp0xFE7_2Response>();

if (body == null) break;

token = body.Token;
}
}

if (Members.TryGetValue(groupUin, out members)) return members;
Expand Down
3 changes: 2 additions & 1 deletion KonataNT/Core/Handlers/PushHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ private void ParseMsfPush(byte[] packet)
case 166: // private msg
case 82: // group msg
{
var message = MessagePacker.Parse(msgPush.Message);
var eventArg = MessagePacker.Parse(msgPush.Message);
_client.EventEmitter.PostEvent(eventArg);
break;
}
default:
Expand Down
63 changes: 63 additions & 0 deletions KonataNT/Core/Packet/Oidb/OidbSvcTrpcTcp0xFE7_2Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using ProtoBuf;

namespace KonataNT.Core.Packet.Oidb;

// ReSharper disable InconsistentNaming
#pragma warning disable CS8618

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_2Response
{
[ProtoMember(1)] public uint GroupUin { get; set; }

[ProtoMember(2)] public List<OidbSvcTrpcTcp0xFE7_2Member> Members { get; set; }

[ProtoMember(3)] public uint Field3 { get; set; }

[ProtoMember(5)] public uint MemberChangeSeq { get; set; }

[ProtoMember(6)] public uint MemberCardChangeSeq { get; set; }

[ProtoMember(15)] public string? Token { get; set; } // for the next page
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_2Member
{
[ProtoMember(1)] public OidbSvcTrpcTcp0xFE7_2Uin Uin { get; set; }

[ProtoMember(10)] public string MemberName { get; set; }

[ProtoMember(11)] public OidbSvcTrpcTcp0xFE7_2Card MemberCard { get; set; }

[ProtoMember(12)] public OidbSvcTrpcTcp0xFE7_2Level? Level { get; set; }

[ProtoMember(100)] public uint JoinTimestamp { get; set; }

[ProtoMember(101)] public uint LastMsgTimestamp { get; set; }

[ProtoMember(107)] public uint Permission { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_2Uin
{
[ProtoMember(2)] public string Uid { get; set; }

[ProtoMember(4)] public uint Uin { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_2Card
{
[ProtoMember(2)] public string? MemberCard { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_2Level
{
[ProtoMember(1)] public List<uint>? Infos { get; set; }

[ProtoMember(2)] public uint Level { get; set; }
}

50 changes: 50 additions & 0 deletions KonataNT/Core/Packet/Oidb/OidbSvcTrpcTcp0xFE7_3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using ProtoBuf;

#pragma warning disable CS8618
// Resharper disable InconsistentNaming

namespace KonataNT.Core.Packet.Oidb;

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE7_3
{
[ProtoMember(1)] public uint GroupUin { get; set; }

[ProtoMember(2)] public uint Field2 { get; set; } // 5

[ProtoMember(3)] public uint Field3 { get; set; } // 2

[ProtoMember(4)] public OidbSvcTrpcScp0xFE7_3Body Body { get; set; }

[ProtoMember(15)] public string? Token { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcScp0xFE7_3Body
{
[ProtoMember(10)] public bool MemberName { get; set; } // 1

[ProtoMember(11)] public bool MemberCard { get; set; } // 1

[ProtoMember(12)] public bool Level { get; set; } // 1

[ProtoMember(20)] public bool Field4 { get; set; } // 1

[ProtoMember(21)] public bool Field5 { get; set; } // 1

[ProtoMember(100)] public bool JoinTimestamp { get; set; } // 1

[ProtoMember(101)] public bool LastMsgTimestamp { get; set; } // 1

[ProtoMember(102)] public bool Field8 { get; set; } // 1

[ProtoMember(103)] public bool Field9 { get; set; } // 1

[ProtoMember(104)] public bool Field10 { get; set; } // 1

[ProtoMember(105)] public bool Field11 { get; set; } // 1

[ProtoMember(106)] public bool Field12 { get; set; } // 1

[ProtoMember(107)] public bool Permission { get; set; } // 1
}
21 changes: 21 additions & 0 deletions KonataNT/Events/EventArgs/BotGroupMessageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using KonataNT.Message;

namespace KonataNT.Events.EventArgs;

public class BotGroupMessageEvent : EventBase
{
public BotGroupMessageEvent(uint groupUin, uint memberUin, MessageStruct message)
{
GroupUin = groupUin;
MemberUin = memberUin;
Message = message;

EventMessage = $"[{nameof(BotGroupMessageEvent)}] {groupUin} {memberUin} {message}";
}

public uint GroupUin { get; }

public uint MemberUin { get; }

public MessageStruct Message { get; }
}
18 changes: 18 additions & 0 deletions KonataNT/Events/EventArgs/BotPrivateMessageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using KonataNT.Message;

namespace KonataNT.Events.EventArgs;

public class BotPrivateMessageEvent : EventBase
{
public uint FriendUin { get; }

public MessageStruct Message { get; }

public BotPrivateMessageEvent(uint friendUin, MessageStruct message)
{
FriendUin = friendUin;
Message = message;

EventMessage = $"[{nameof(BotPrivateMessageEvent)}] {friendUin} {message}";
}
}
6 changes: 6 additions & 0 deletions KonataNT/Events/EventArgs/BotStrangerMessageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace KonataNT.Events.EventArgs;

public class BotStrangerMessageEvent : EventBase
{

}
6 changes: 6 additions & 0 deletions KonataNT/Events/EventEmitter.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public partial class EventEmitter
public event KonataEvent<BotOnlineEvent>? OnBotOnlineEvent;

public event KonataEvent<BotLogEvent>? OnBotLogEvent;

public event KonataEvent<BotPrivateMessageEvent>? OnBotPrivateMessageEvent;

public event KonataEvent<BotGroupMessageEvent>? OnBotGroupMessageEvent;

public event KonataEvent<BotStrangerMessageEvent>? OnBotStrangerMessageEvent;
}
3 changes: 3 additions & 0 deletions KonataNT/Events/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public EventEmitter(BaseClient client)
{
RegisterEvent((BotOnlineEvent e) => OnBotOnlineEvent?.Invoke(client, e));
RegisterEvent((BotLogEvent e) => OnBotLogEvent?.Invoke(client, e));
RegisterEvent((BotGroupMessageEvent e) => OnBotGroupMessageEvent?.Invoke(client, e));
RegisterEvent((BotPrivateMessageEvent e) => OnBotPrivateMessageEvent?.Invoke(client, e));
RegisterEvent((BotStrangerMessageEvent e) => OnBotStrangerMessageEvent?.Invoke(client, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
14 changes: 11 additions & 3 deletions KonataNT/Message/MessagePacker.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
using KonataNT.Core.Packet.Message;
using KonataNT.Events;
using KonataNT.Events.EventArgs;

namespace KonataNT.Message;

internal static class MessagePacker
{
public static MessageStruct Parse(PushMsgBody msg)
public static EventBase Parse(PushMsgBody msg)
{
string name = msg.ResponseHead.Grp?.MemberCard ?? "";
uint timestamp = msg.ContentHead.Timestamp;
var @struct = new MessageStruct(msg.ResponseHead.FromUin, name, DateTime.Now)
{
Time = DateTime.UnixEpoch.AddSeconds(timestamp),
Sequence = msg.ContentHead.Sequence
Sequence = msg.ContentHead.Sequence,
SourceType = msg.ResponseHead.Grp is not null ? MessageStruct.Source.Group : MessageStruct.Source.Friend
};

return @struct;
return @struct.SourceType switch
{
MessageStruct.Source.Group => new BotGroupMessageEvent(msg.ResponseHead.Grp?.GroupUin ?? 0, msg.ResponseHead.FromUin, @struct),
MessageStruct.Source.Friend => new BotPrivateMessageEvent(msg.ResponseHead.FromUin, @struct),
_ => throw new NotImplementedException()
};
}

public static MessageChain Build(BotClient client, MessageStruct @struct)
Expand Down
16 changes: 9 additions & 7 deletions KonataNT/Message/MessageStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ public class MessageStruct
public uint Sequence { get; internal set; }

public MessageChain Chain { get; internal set; }

public enum Source
{
Group,
Friend,
Stranger
}

public Source SourceType { get; internal set; }

/// <summary>
/// <b>[In] [Out]</b> <br/>
Expand All @@ -33,4 +28,11 @@ public MessageStruct(uint uin, string name, DateTime messageTime)
Time = messageTime.ToUniversalTime();
Chain = new MessageChain();
}

public enum Source
{
Group,
Friend,
Stranger
}
}

0 comments on commit c1f5a48

Please sign in to comment.