mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
Use SeString Expressions in DalamudLinkPayload (#1856)
This commit is contained in:
parent
2e3153c502
commit
1a91495159
1 changed files with 26 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
using Lumina.Text.Payloads;
|
||||
using Lumina.Text.ReadOnly;
|
||||
|
||||
namespace Dalamud.Game.Text.SeStringHandling.Payloads;
|
||||
|
||||
|
|
@ -31,27 +32,33 @@ public class DalamudLinkPayload : Payload
|
|||
/// <inheritdoc/>
|
||||
protected override byte[] EncodeImpl()
|
||||
{
|
||||
var pluginBytes = Encoding.UTF8.GetBytes(this.Plugin);
|
||||
var commandBytes = MakeInteger(this.CommandId);
|
||||
var chunkLen = 3 + pluginBytes.Length + commandBytes.Length;
|
||||
|
||||
if (chunkLen > 255)
|
||||
{
|
||||
throw new Exception("Chunk is too long. Plugin name exceeds limits for DalamudLinkPayload");
|
||||
}
|
||||
|
||||
var bytes = new List<byte> { START_BYTE, (byte)SeStringChunkType.Interactable, (byte)chunkLen, (byte)EmbeddedInfoType.DalamudLink };
|
||||
bytes.Add((byte)pluginBytes.Length);
|
||||
bytes.AddRange(pluginBytes);
|
||||
bytes.AddRange(commandBytes);
|
||||
bytes.Add(END_BYTE);
|
||||
return bytes.ToArray();
|
||||
return new Lumina.Text.SeStringBuilder()
|
||||
.BeginMacro(MacroCode.Link)
|
||||
.AppendIntExpression((int)EmbeddedInfoType.DalamudLink - 1)
|
||||
.AppendStringExpression(this.Plugin)
|
||||
.AppendUIntExpression(this.CommandId)
|
||||
.EndMacro()
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void DecodeImpl(BinaryReader reader, long endOfStream)
|
||||
{
|
||||
this.Plugin = Encoding.UTF8.GetString(reader.ReadBytes(reader.ReadByte()));
|
||||
this.CommandId = GetInteger(reader);
|
||||
// Note: Payload.DecodeChunk already took the first int expr (DalamudLink).
|
||||
|
||||
var body = reader.ReadBytes((int)(endOfStream - reader.BaseStream.Position));
|
||||
var rosps = new ReadOnlySePayloadSpan(ReadOnlySePayloadType.Macro, MacroCode.Link, body.AsSpan());
|
||||
|
||||
if (!rosps.TryGetExpression(out var pluginExpression, out var commandIdExpression))
|
||||
return;
|
||||
|
||||
if (!pluginExpression.TryGetString(out var pluginString))
|
||||
return;
|
||||
|
||||
if (!commandIdExpression.TryGetUInt(out var commandId))
|
||||
return;
|
||||
|
||||
this.Plugin = pluginString.ExtractText();
|
||||
this.CommandId = commandId;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue