## Summary - Adds proper Latin1 encoding support, replacing CP1252 usage throughout the codebase - Adds specialized, optimized string decoding methods with safe string filtering for each encoding type - Filters invalid Unicode characters (C0/C1 control codes, non-characters) by removal rather than replacement since the UO client renders nothing for these characters - Fixes UTF-16 null terminator position handling to correctly advance by 2 bytes ## Changes TextEncoding.cs - Added SearchValues-based invalid byte/char detection for efficient filtering - Added encoding-specific GetString methods: GetStringAscii, GetStringLatin1, GetStringUtf8, GetStringBigUni, GetStringLittleUni - Each method supports a safeString parameter for filtering invalid characters - Little-endian UTF-16 uses direct memory cast for zero-copy decoding on LE systems - Invalid characters are removed (not replaced with U+FFFD) since the client renders nothing for them SpanReader.cs - Added ReadLatin1() and ReadLatin1Safe() methods - Rewrote encoding-specific read methods to use optimized TextEncoding.GetString* methods - Fixed UTF-16 null terminator handling: position now correctly advances by byteLength (2) instead of 1 SpanWriter.cs - Added WriteLatin1 and WriteLatin1Null methods ## Packet Updates - Updated all packet code to use Latin1 encoding instead of CP1252 - Affected: account packets, equipment packets, menu packets, message packets, mobile packets, player packets, secure trade packets, vendor packets, gump packets, book packets, mahjong packets ## Filtering Behavior Invalid characters filtered in safe mode: ``` ┌───────────────┬────────────────────────┐ │ Range │ Description │ ├───────────────┼────────────────────────┤ │ 0x00-0x1F │ C0 control codes │ ├───────────────┼────────────────────────┤ │ 0x7F │ DEL │ ├───────────────┼────────────────────────┤ │ 0x80-0x9F │ C1 control codes │ ├───────────────┼────────────────────────┤ │ 0xFFFE-0xFFFF │ Unicode non-characters │ └───────────────┴────────────────────────┘ ``` Note: Surrogate pairs (0xD800-0xDFFF) are not filtered because proper validation requires context checking for paired vs unpaired surrogates. The UO client renders nothing for these anyway. ## Test Plan - All 631 Server.Tests pass - Verified client rendering behavior using TestUnicodeGump command (pages 1-5) - Confirmed U+FFFD, unpaired surrogates, and non-characters all render as blank in client - Verified Latin1 characters (0xA0-0xFF) display correctly - Verified C1 control codes (0x80-0x9F) are filtered and don't display
281 lines
7.8 KiB
C#
281 lines
7.8 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2023 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: OutgoingMessagePackets.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.Buffers;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
using Server.Prompts;
|
|
|
|
namespace Server.Network;
|
|
|
|
[Flags]
|
|
public enum AffixType : byte
|
|
{
|
|
Append = 0x00,
|
|
Prepend = 0x01,
|
|
System = 0x02
|
|
}
|
|
|
|
public static class OutgoingMessagePackets
|
|
{
|
|
public static void SendMessageLocalized(
|
|
this NetState ns,
|
|
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
|
|
)
|
|
{
|
|
if (ns.CannotSendPackets())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var buffer = stackalloc byte[GetMaxMessageLocalizedLength(args)].InitializePacket();
|
|
var length = CreateMessageLocalized(
|
|
buffer, serial, graphic, type, hue, font, number, name, args
|
|
);
|
|
|
|
ns.Send(buffer[..length]);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetMaxMessageLocalizedLength(string args) => 50 + (args?.Length ?? 0) * 2;
|
|
|
|
public static int CreateMessageLocalized(
|
|
Span<byte> buffer,
|
|
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
|
|
)
|
|
{
|
|
if (buffer[0] != 0)
|
|
{
|
|
return buffer.Length;
|
|
}
|
|
|
|
name ??= "";
|
|
args ??= "";
|
|
|
|
if (hue == 0)
|
|
{
|
|
hue = 0x3B2;
|
|
}
|
|
|
|
var writer = new SpanWriter(buffer);
|
|
writer.Write((byte)0xC1);
|
|
writer.Seek(2, SeekOrigin.Current);
|
|
writer.Write(serial);
|
|
writer.Write((short)graphic);
|
|
writer.Write((byte)type);
|
|
writer.Write((short)hue);
|
|
writer.Write((short)font);
|
|
writer.Write(number);
|
|
writer.WriteLatin1(name, 30);
|
|
writer.WriteLittleUniNull(args);
|
|
|
|
writer.WritePacketLength();
|
|
return writer.Position;
|
|
}
|
|
|
|
public static void SendMessageLocalizedAffix(
|
|
this NetState ns,
|
|
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name,
|
|
AffixType affixType, string affix = "", string args = ""
|
|
)
|
|
{
|
|
if (ns.CannotSendPackets())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)].InitializePacket();
|
|
var length = CreateMessageLocalizedAffix(
|
|
buffer, serial, graphic, type, hue, font, number, name, affixType, affix, args
|
|
);
|
|
|
|
ns.Send(buffer[..length]);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetMaxMessageLocalizedAffixLength(string affix, string args) =>
|
|
52 + (affix?.Length ?? 0) + (args?.Length ?? 0) * 2;
|
|
|
|
public static int CreateMessageLocalizedAffix(
|
|
Span<byte> buffer,
|
|
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name,
|
|
AffixType affixType, string affix = "", string args = ""
|
|
)
|
|
{
|
|
if (buffer[0] != 0)
|
|
{
|
|
return buffer.Length;
|
|
}
|
|
|
|
name ??= "";
|
|
affix ??= "";
|
|
args ??= "";
|
|
|
|
if (hue == 0)
|
|
{
|
|
hue = 0x3B2;
|
|
}
|
|
|
|
var writer = new SpanWriter(buffer);
|
|
writer.Write((byte)0xCC);
|
|
writer.Seek(2, SeekOrigin.Current);
|
|
writer.Write(serial);
|
|
writer.Write((short)graphic);
|
|
writer.Write((byte)type);
|
|
writer.Write((short)hue);
|
|
writer.Write((short)font);
|
|
writer.Write(number);
|
|
writer.Write((byte)affixType);
|
|
writer.WriteLatin1(name, 30);
|
|
writer.WriteLatin1Null(affix);
|
|
writer.WriteBigUniNull(args);
|
|
|
|
writer.WritePacketLength();
|
|
return writer.Position;
|
|
}
|
|
|
|
public static void SendMessage(
|
|
this NetState ns,
|
|
Serial serial, int graphic, MessageType type, int hue, int font, bool ascii, string lang, string name, string text
|
|
)
|
|
{
|
|
if (ns.CannotSendPackets())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var buffer = stackalloc byte[GetMaxMessageLength(text)].InitializePacket();
|
|
var length = CreateMessage(
|
|
buffer,
|
|
serial,
|
|
graphic,
|
|
type,
|
|
hue,
|
|
font,
|
|
ascii,
|
|
lang,
|
|
name,
|
|
text
|
|
);
|
|
|
|
ns.Send(buffer[..length]);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetMaxMessageLength(string text) => 50 + (text?.Length ?? 0) * 2;
|
|
|
|
public static int CreateMessage(
|
|
Span<byte> buffer,
|
|
Serial serial,
|
|
int graphic,
|
|
MessageType type,
|
|
int hue,
|
|
int font,
|
|
bool ascii,
|
|
string lang,
|
|
string name,
|
|
string text
|
|
)
|
|
{
|
|
if (buffer[0] != 0)
|
|
{
|
|
return buffer.Length;
|
|
}
|
|
|
|
name ??= "";
|
|
text ??= "";
|
|
lang ??= "ENU";
|
|
|
|
if (hue == 0)
|
|
{
|
|
hue = 0x3B2;
|
|
}
|
|
|
|
var writer = new SpanWriter(buffer);
|
|
writer.Write((byte)(ascii ? 0x1C : 0xAE)); // Packet ID
|
|
writer.Seek(2, SeekOrigin.Current);
|
|
writer.Write(serial);
|
|
writer.Write((short)graphic);
|
|
writer.Write((byte)type);
|
|
writer.Write((short)hue);
|
|
writer.Write((short)font);
|
|
if (ascii)
|
|
{
|
|
writer.WriteLatin1(name, 30);
|
|
writer.WriteLatin1Null(text);
|
|
}
|
|
else
|
|
{
|
|
writer.WriteAscii(lang, 4);
|
|
writer.WriteLatin1(name, 30);
|
|
writer.WriteBigUniNull(text);
|
|
}
|
|
|
|
writer.WritePacketLength();
|
|
return writer.Position;
|
|
}
|
|
|
|
public static void SendFollowMessage(this NetState ns, Serial s1, Serial s2)
|
|
{
|
|
if (ns.CannotSendPackets())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var writer = new SpanWriter(stackalloc byte[9]);
|
|
writer.Write((byte)0x15); // Packet ID
|
|
writer.Write(s1);
|
|
writer.Write(s2);
|
|
|
|
ns.Send(writer.Span);
|
|
}
|
|
|
|
public static void SendPrompt(this NetState ns, Prompt prompt)
|
|
{
|
|
if (ns == null || prompt == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var writer = new SpanWriter(stackalloc byte[21]);
|
|
writer.Write((byte)0xC2); // Packet ID
|
|
writer.Write((ushort)21);
|
|
writer.Write(prompt.Serial);
|
|
writer.Write(prompt.Serial);
|
|
writer.Clear(10);
|
|
|
|
ns.Send(writer.Span);
|
|
}
|
|
|
|
public static void SendHelpResponse(this NetState ns, Serial s, string text)
|
|
{
|
|
text = text?.Trim() ?? "";
|
|
|
|
if (ns == null || text.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var length = 9 + text.Length * 2;
|
|
var writer = new SpanWriter(stackalloc byte[length]);
|
|
writer.Write((byte)0xB7);
|
|
writer.Write((ushort)length);
|
|
writer.Write(s);
|
|
writer.WriteBigUniNull(text);
|
|
|
|
ns.Send(writer.Span);
|
|
}
|
|
}
|