From 8aaaa684a2c1023e99cf71944145cde74f75d17b Mon Sep 17 00:00:00 2001 From: Infi Date: Sat, 25 May 2024 09:30:45 +0200 Subject: [PATCH] Remove reflection from InputPreview --- ChatTwo/Ui/InputPreview.cs | 19 +++++++------------ ChatTwo/Util/Payloads.cs | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/ChatTwo/Ui/InputPreview.cs b/ChatTwo/Ui/InputPreview.cs index de92e1a..f45d968 100644 --- a/ChatTwo/Ui/InputPreview.cs +++ b/ChatTwo/Ui/InputPreview.cs @@ -1,5 +1,4 @@ using System.Numerics; -using System.Reflection; using System.Text; using System.Text.RegularExpressions; using ChatTwo.Code; @@ -177,17 +176,13 @@ public partial class InputPreview : Window if (icon.Icon != BitmapFontIcon.AutoTranslateBegin) return; - try - { - NextChunkIsAutoTranslate = true; - var key = (uint)typeof(AutoTranslatePayload).GetField("key", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(chunk.Link)!; - var group = (uint)typeof(AutoTranslatePayload).GetField("group", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(chunk.Link)!; - CursorPosition += $"".Length; - } - catch - { - // Ignore - } + NextChunkIsAutoTranslate = true; + + // Skipping: StartByte, PayloadType, PayloadLength + using var reader = new BinaryReader(new MemoryStream(chunk.Link!.Encode().Skip(3).ToArray())); + var group = (uint) reader.ReadByte(); + var key = PayloadExt.GetInteger(reader); + CursorPosition += $"".Length; return; } diff --git a/ChatTwo/Util/Payloads.cs b/ChatTwo/Util/Payloads.cs index f8761ad..d075f2f 100755 --- a/ChatTwo/Util/Payloads.cs +++ b/ChatTwo/Util/Payloads.cs @@ -2,6 +2,33 @@ using Dalamud.Game.Text.SeStringHandling; namespace ChatTwo.Util; +internal static class PayloadExt +{ + // TODO: Remove after Key and Group in AutoTranslatePayload became public + // From: https://github.com/goatcorp/Dalamud/blob/master/Dalamud/Game/Text/SeStringHandling/Payload.cs#L366 + /// + /// Retrieve the packed integer from SE's native data format. + /// + /// The BinaryReader instance. + /// An integer. + internal static uint GetInteger(BinaryReader input) + { + uint marker = input.ReadByte(); + if (marker < 0xD0) + return marker - 1; + + marker = (marker + 1) & 0b1111; + + var ret = new byte[4]; + for (var i = 3; i >= 0; i--) + { + ret[i] = (marker & (1 << i)) == 0 ? (byte)0 : input.ReadByte(); + } + + return BitConverter.ToUInt32(ret, 0); + } +} + internal class PartyFinderPayload : Payload { public override PayloadType Type => (PayloadType) 0x50;