-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Implemented LightAppEntity.cs
Linwenxuan
authored and
Linwenxuan
committed
Feb 9, 2024
1 parent
d643fe2
commit 578c82a
Showing
4 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters