Skip to content

Commit

Permalink
[Core] Implemented LightAppEntity.cs
Browse files Browse the repository at this point in the history
Linwenxuan authored and Linwenxuan committed Feb 9, 2024
1 parent d643fe2 commit 578c82a
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Lagrange.Core/Internal/Packets/Message/Element/Elem.cs
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ internal class Elem

[ProtoMember(45)] public SrcMsg? SrcMsg { get; set; } // Forward/ReplyEntity

[ProtoMember(51)] public LightAppElem? LightApp { get; set; }
[ProtoMember(51)] public LightAppElem? LightAppElem { get; set; }

[ProtoMember(53)] public CommonElem? CommonElem { get; set; }
}
Original file line number Diff line number Diff line change
@@ -8,5 +8,5 @@ internal class LightAppElem
{
[ProtoMember(1)] public byte[] Data { get; set; }

[ProtoMember(2)] public byte[] MsgResid { get; set; }
[ProtoMember(2)] public byte[]? MsgResid { get; set; }
}
70 changes: 70 additions & 0 deletions Lagrange.Core/Message/Entity/LightAppEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Text;
using System.Text.Json.Nodes;
using Lagrange.Core.Internal.Packets.Message.Element;
using Lagrange.Core.Internal.Packets.Message.Element.Implementation;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Binary.Compression;

namespace Lagrange.Core.Message.Entity;

[MessageElement(typeof(LightAppElem))]
public class LightAppEntity : IMessageEntity
{
public string AppName { get; set; } = string.Empty;

public string Payload { get; set; } = string.Empty;

public LightAppEntity() { }

public LightAppEntity(string payload)
{
Payload = payload;
string? app = JsonNode.Parse(payload)?["app"]?.ToString();
if (app != null) AppName = app;
}

IEnumerable<Elem> IMessageEntity.PackElement()
{
using var payload = new BinaryPacket()
.WriteByte(0x01)
.WriteBytes(ZCompression.ZDecompress(Encoding.UTF8.GetBytes(Payload)));

return new Elem[]
{
new()
{
LightAppElem = new LightAppElem
{
Data = payload.ToArray(),
MsgResid = null
}
}
};
}

IMessageEntity? IMessageEntity.UnpackElement(Elem elems)
{
if (elems.LightAppElem is { } lightApp)
{
var payload = ZCompression.ZDecompress(lightApp.Data[1..]);
string json = Encoding.UTF8.GetString(payload);
string? app = JsonNode.Parse(json)?["app"]?.ToString();

if (app != null)
{
return new LightAppEntity
{
AppName = app,
Payload = json
};
}
}

return null;
}

public string ToPreviewString()
{
return $"[{nameof(LightAppEntity)}: {AppName}]";
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
| | | | | Reply. | 🟢 | Group Request | 🟢 | GroupPromoteAdmin | 🟢 |
| | | | | File | 🟡 | ~~Voice Call~~ | 🔴 | GroupInvite | 🟢 |
| | | | | Poke | 🟢 | Client Key | 🟢 | GroupRequestJoin | 🔴 |
| | | | | | | Cookies | 🟢 | FriendRequest | 🟢 |
| | | | | LightApp | 🟢 | Cookies | 🟢 | FriendRequest | 🟢 |
| | | | | | | Send Message | 🟢 | ~~FriendTyping~~ | 🔴 |
| | | | | | | | | ~~FriendVoiceCall~~ | 🔴 |

0 comments on commit 578c82a

Please sign in to comment.