feat: validate chat input

This commit is contained in:
Anna 2022-02-18 00:34:39 -05:00
parent b5617a4179
commit a57f7d4904
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
2 changed files with 25 additions and 1 deletions

View File

@ -58,6 +58,9 @@ internal sealed unsafe class Chat : IDisposable {
[Signature("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 8B F2 48 8D B9")]
private readonly delegate* unmanaged<IntPtr, uint, IntPtr> _getColourInfo = null!;
[Signature("E8 ?? ?? ?? ?? EB 0A 48 8D 4C 24 ?? E8 ?? ?? ?? ?? 48 8D 8D")]
private readonly delegate* unmanaged<Utf8String*, int, IntPtr, void> _sanitiseString = null!;
// Hooks
private delegate byte ChatLogRefreshDelegate(IntPtr log, ushort eventId, AtkValue* value);
@ -646,4 +649,16 @@ internal sealed unsafe class Chat : IDisposable {
uMessage->Dtor();
IMemorySpace.Free(uMessage);
}
internal bool IsCharValid(char c) {
var uC = Utf8String.FromString(c.ToString());
this._sanitiseString(uC, 0x27F, IntPtr.Zero);
var wasValid = uC->ToString().Length > 0;
uC->Dtor();
IMemorySpace.Free(uC);
return wasValid;
}
}

View File

@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Numerics;
using System.Text;
using ChatTwo.Code;
using ChatTwo.GameFunctions.Types;
using ChatTwo.Resources;
@ -490,6 +491,7 @@ internal sealed class ChatLog : IUiComponent {
ImGui.SetNextItemWidth(inputWidth);
const ImGuiInputTextFlags inputFlags = ImGuiInputTextFlags.EnterReturnsTrue
| ImGuiInputTextFlags.CallbackAlways
| ImGuiInputTextFlags.CallbackCharFilter
| ImGuiInputTextFlags.CallbackHistory;
if (ImGui.InputText("##chat2-input", ref this.Chat, 500, inputFlags, this.Callback)) {
if (!string.IsNullOrWhiteSpace(this.Chat)) {
@ -525,7 +527,7 @@ internal sealed class ChatLog : IUiComponent {
}
}
this.Ui.Plugin.Common.Functions.Chat.SendMessage(trimmed);
this.Ui.Plugin.Common.Functions.Chat.SendMessageUnsafe(Encoding.UTF8.GetBytes(trimmed));
}
Skip:
@ -850,6 +852,13 @@ internal sealed class ChatLog : IUiComponent {
private unsafe int Callback(ImGuiInputTextCallbackData* data) {
var ptr = new ImGuiInputTextCallbackDataPtr(data);
if (data->EventFlag == ImGuiInputTextFlags.CallbackCharFilter) {
var valid = this.Ui.Plugin.Functions.Chat.IsCharValid((char) ptr.EventChar);
if (!valid) {
return 1;
}
}
if (this.Activate) {
this.Activate = false;
data->CursorPos = this.Chat.Length;