Skip to content

Commit cd88b8f

Browse files
authored
[Core & Milky] Implement GroupFileEntity and file Segment (#65)
1 parent e5b8180 commit cd88b8f

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lagrange.Core/Internal/Packets/Message/Elem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ internal partial class Elem
99
[ProtoMember(1)] public Text? Text { get; set; }
1010

1111
[ProtoMember(4)] public NotOnlineImage? NotOnlineImage { get; set; }
12+
13+
[ProtoMember(5)] public TransElem? TransElemInfo { get; set; }
1214

1315
[ProtoMember(8)] public CustomFace? CustomFace { get; set; }
1416

@@ -101,6 +103,14 @@ internal partial class NotOnlineImage
101103
[ProtoMember(29)] public byte[] PbReserve { get; set; }
102104
}
103105

106+
[ProtoPackable]
107+
internal partial class TransElem
108+
{
109+
[ProtoMember(1)] public uint ElemType { get; set; }
110+
111+
[ProtoMember(2)] public byte[] ElemValue { get; set; }
112+
}
113+
104114
[ProtoPackable]
105115
internal partial class CustomFace
106116
{

Lagrange.Core/Internal/Packets/Message/Extra.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,40 @@ internal partial class SourceMsgResvAttr
5353
[ProtoMember(6)] public string SenderUid { get; set; }
5454

5555
[ProtoMember(7)] public string ReceiverUid { get; set; }
56+
}
57+
58+
[ProtoPackable]
59+
internal partial class GroupFileExtra
60+
{
61+
[ProtoMember(1)] public uint Field1 { get; set; }
62+
63+
[ProtoMember(2)] public string FileName { get; set; }
64+
65+
[ProtoMember(3)] public string Display { get; set; }
66+
67+
[ProtoMember(7)] public GroupFileExtraInner Inner { get; set; }
68+
}
69+
70+
[ProtoPackable]
71+
internal partial class GroupFileExtraInner
72+
{
73+
[ProtoMember(2)] public GroupFileExtraInfo Info { get; set; }
74+
}
75+
76+
[ProtoPackable]
77+
internal partial class GroupFileExtraInfo
78+
{
79+
[ProtoMember(1)] public uint BusId { get; set; }
80+
81+
[ProtoMember(2)] public string FileId { get; set; }
82+
83+
[ProtoMember(3)] public long FileSize { get; set; }
84+
85+
[ProtoMember(4)] public string FileName { get; set; }
86+
87+
[ProtoMember(5)] public uint Field5 { get; set; }
88+
89+
[ProtoMember(7)] public string Field7 { get; set; }
90+
91+
[ProtoMember(8)] public string FileMd5 { get; set; } // hexed
5692
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Lagrange.Core.Internal.Packets.Message;
2+
using Lagrange.Core.Utility;
3+
using Lagrange.Core.Utility.Binary;
4+
5+
namespace Lagrange.Core.Message.Entities;
6+
7+
public class GroupFileEntity : IMessageEntity
8+
{
9+
public string FileId { get; internal init; } = string.Empty;
10+
11+
public string FileName { get; internal init; } = string.Empty;
12+
13+
public long FileSize { get; internal init; }
14+
15+
public byte[] FileMd5 { get; internal init; } = [];
16+
17+
public string FileUrl { get; set; } = string.Empty;
18+
19+
public Task Postprocess(BotContext context, BotMessage message)
20+
{
21+
return Task.CompletedTask; // TODO: implement group file download event
22+
}
23+
24+
Elem[] IMessageEntity.Build() => throw new NotSupportedException();
25+
26+
IMessageEntity? IMessageEntity.Parse(List<Elem> elements, Elem target)
27+
{
28+
if (target.TransElemInfo is { ElemType: 24 } trans)
29+
{
30+
var payload = new BinaryPacket(trans.ElemValue.AsSpan());
31+
payload.Skip(1);
32+
var data = payload.ReadBytes(Prefix.Int16 | Prefix.LengthOnly);
33+
var extra = ProtoHelper.Deserialize<GroupFileExtra>(data).Inner.Info;
34+
35+
return new GroupFileEntity
36+
{
37+
FileId = extra.FileId,
38+
FileName = extra.FileName,
39+
FileSize = extra.FileSize,
40+
FileMd5 = Convert.FromHexString(extra.FileMd5)
41+
};
42+
}
43+
44+
return null;
45+
}
46+
47+
public string ToPreviewString() => $"[群文件 {FileName}]";
48+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Lagrange.Milky.Entity.Segment;
4+
5+
[method: JsonConstructor]
6+
public class FileIncomingSegment(FileIncomingSegmentData data) : IncomingSegmentBase<FileIncomingSegmentData>(data)
7+
{
8+
public FileIncomingSegment(string fileId, string fileName, long fileSize) : this(new FileIncomingSegmentData(fileId, fileName, fileSize, null)) { }
9+
10+
public FileIncomingSegment(string fileId, string fileName, long fileSize, string fileHash) : this(new FileIncomingSegmentData(fileId, fileName, fileSize, fileHash)) { }
11+
}
12+
13+
public class FileIncomingSegmentData(string fileId, string fileName, long fileSize, string? fileHash)
14+
{
15+
[JsonPropertyName("file_id")]
16+
public string FileId { get; } = fileId;
17+
18+
[JsonPropertyName("file_name")]
19+
public string FileName { get; } = fileName;
20+
21+
[JsonPropertyName("file_size")]
22+
public long FileSize { get; } = fileSize;
23+
24+
[JsonPropertyName("file_hash")]
25+
public string? FileHash { get; } = fileHash;
26+
}

Lagrange.Milky/Utility/EntityConvert.Segment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public async Task<MessageChain> FakeSegmentsAsync(IReadOnlyList<IOutgoingSegment
7070
(int)video.VideoSize.Y,
7171
(int)video.VideoLength
7272
),
73+
GroupFileEntity groupFile => new FileIncomingSegment(groupFile.FileId, groupFile.FileName, groupFile.FileSize),
7374
MultiMsgEntity multiMsg => new ForwardIncomingSegment(multiMsg.ResId!),
7475
LightAppEntity lightApp => new LightAppIncomingSegment(lightApp.AppName, lightApp.Payload),
7576
// ? => new MarketFaceSegment(...),

0 commit comments

Comments
 (0)