c4c85cf4b8
- Translated project documentation (LEARNING-JOURNEY, CONTRIBUTORS, AI_DISCLOSURE) to English for better accessibility. - Standardized internal code documentation by converting XML-doc blocks to standard comment format. - Cleaned up inline comments and removed redundant versioning metadata across the codebase. - Refactored non-functional text elements to improve readability and maintain a consistent style.
51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using System.Text;
|
|
using Dalamud.Memory;
|
|
using FFXIVClientStructs.FFXIV.Client.System.String;
|
|
using FFXIVClientStructs.FFXIV.Client.UI;
|
|
using HellionChat.Resources;
|
|
|
|
namespace HellionChat.GameFunctions;
|
|
|
|
public unsafe class ChatBox
|
|
{
|
|
public static void SendMessageUnsafe(byte[] message)
|
|
{
|
|
var mes = Utf8String.FromSequence(message.NullTerminate());
|
|
UIModule.Instance()->ProcessChatBoxEntry(mes);
|
|
mes->Dtor(true);
|
|
}
|
|
|
|
public static void SendMessage(string message) => SendMessageUnsafe(ValidateMessage(message));
|
|
|
|
// sanitiserOverride allows xUnit to bypass Utf8String->SanitizeString (game memory only).
|
|
// Returns encoded bytes so SendMessage avoids a second GetBytes call.
|
|
// TEST-MIRROR: ../../../Hellion Build test/GameFunctions/ChatBoxTests.cs
|
|
internal static byte[] ValidateMessage(
|
|
string message,
|
|
Func<string, string>? sanitiserOverride = null
|
|
)
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(message);
|
|
if (bytes.Length == 0)
|
|
throw new ArgumentException(Language.ChatBox_Error_Empty, nameof(message));
|
|
|
|
if (bytes.Length > 500)
|
|
throw new ArgumentException(Language.ChatBox_Error_Too_Long, nameof(message));
|
|
|
|
var sanitiser = sanitiserOverride ?? SanitiseText;
|
|
if (message.Length != sanitiser(message).Length)
|
|
throw new ArgumentException(Language.ChatBox_Error_Invalid, nameof(message));
|
|
|
|
return bytes;
|
|
}
|
|
|
|
private static string SanitiseText(string text)
|
|
{
|
|
var uText = Utf8String.FromString(text);
|
|
uText->SanitizeString((AllowedEntities)0x27F);
|
|
var sanitised = uText->ToString();
|
|
uText->Dtor(true);
|
|
return sanitised;
|
|
}
|
|
}
|