ModernUO/Projects/Server/Network/Packets/OutgoingPlayerPackets.cs
Kamron Batman 0404251638
feat: Adds Latin1 text support (#2317)
## 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
2026-01-22 15:51:00 -08:00

351 lines
11 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: OutgoingPlayerPackets.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.Runtime.CompilerServices;
namespace Server.Network;
public enum LRReason : byte
{
CannotLift,
OutOfRange,
OutOfSight,
TryToSteal,
AreHolding,
Inspecific
}
public static class OutgoingPlayerPackets
{
public const int DragEffectPacketLength = 26;
public static void SendStatLockInfo(this NetState ns, Mobile m)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[12]);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)12);
writer.Write((short)0x19);
writer.Write((byte)2);
writer.Write(m.Serial);
writer.Write((byte)0);
var lockBits = ((int)m.StrLock << 4) | ((int)m.DexLock << 2) | (int)m.IntLock;
writer.Write((byte)lockBits);
ns.Send(writer.Span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendChangeUpdateRange(this NetState ns, byte range) => ns?.Send(stackalloc byte[] { 0xC8, range });
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendDeathStatus(this NetState ns) => ns?.Send(stackalloc byte[] { 0x2C, 2 });
public static void SendDisplayProfile(this NetState ns, Serial m, string header, string body, string footer)
{
if (ns.CannotSendPackets())
{
return;
}
header ??= "";
body ??= "";
footer ??= "";
var length = 12 + header.Length + footer.Length * 2 + body.Length * 2;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xB8); // Packet ID
writer.Write((ushort)length);
writer.Write(m);
writer.WriteLatin1Null(header);
writer.WriteBigUniNull(footer);
writer.WriteBigUniNull(body);
ns.Send(writer.Span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendLiftReject(this NetState ns, LRReason reason) =>
ns?.Send(stackalloc byte[] { 0x27, (byte)reason });
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendLogoutAck(this NetState ns) => ns?.Send(stackalloc byte[] { 0xD1, 0x01 });
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendWeather(this NetState ns, byte type, byte density, byte temp) =>
ns?.Send(stackalloc byte[] { 0x65, type, density, temp });
public static void SendServerChange(this NetState ns, Point3D p, Map map)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[16]);
writer.Write((byte)0x76); // Packet ID
writer.Write((short)p.X);
writer.Write((short)p.Y);
writer.Write((short)p.Z);
writer.Write((byte)0);
writer.Write((short)0);
writer.Write((short)0);
writer.Write((short)map.Width);
writer.Write((short)map.Height);
ns.Send(writer.Span);
}
public static void SendSkillsUpdate(this NetState ns, Skills skills)
{
if (ns.CannotSendPackets())
{
return;
}
var length = 6 + 9 * skills.Length;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0x3A); // Packet ID
writer.Write((ushort)length);
writer.Write((byte)0x02); // type: absolute, capped
for (var i = 0; i < skills.Length; ++i)
{
var s = skills[i];
var v = s.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
writer.Write((ushort)(s.Info.SkillID + 1));
writer.Write((ushort)uv);
writer.Write((ushort)s.BaseFixedPoint);
writer.Write((byte)s.Lock);
writer.Write((ushort)s.CapFixedPoint);
}
writer.Write((short)0); // terminate
ns.Send(writer.Span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendSequence(this NetState ns, byte sequence) => ns?.Send(stackalloc byte[] { 0x7B, sequence });
public static void SendSkillChange(this NetState ns, Skill skill)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[13]);
writer.Write((byte)0x3A); // Packet ID
writer.Write((ushort)13);
var v = skill.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
writer.Write((byte)0xDF); // type: delta, capped
writer.Write((ushort)skill.Info.SkillID);
writer.Write((ushort)uv);
writer.Write((ushort)skill.BaseFixedPoint);
writer.Write((byte)skill.Lock);
writer.Write((ushort)skill.CapFixedPoint);
ns.Send(writer.Span);
}
public static void SendLaunchBrowser(this NetState ns, string uri)
{
if (ns.CannotSendPackets())
{
return;
}
uri ??= "";
var length = 4 + uri.Length;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xA5); // Packet ID
writer.Write((ushort)length);
writer.WriteAsciiNull(uri);
ns.Send(writer.Span);
}
public static void CreateDragEffect(
Span<byte> buffer,
Serial srcSerial, Point3D srcLocation,
Serial trgSerial, Point3D trgLocation,
int itemID, int hue, int amount
)
{
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0x23); // Packet ID
writer.Write((short)itemID);
writer.Write((byte)0);
writer.Write((short)hue);
writer.Write((short)amount);
writer.Write(srcSerial);
writer.Write((short)srcLocation.X);
writer.Write((short)srcLocation.Y);
writer.Write((sbyte)srcLocation.Z);
writer.Write(trgSerial);
writer.Write((short)trgLocation.X);
writer.Write((short)trgLocation.Y);
writer.Write((sbyte)trgLocation.Z);
}
public static void SendDragEffect(
this NetState ns,
Serial srcSerial, Point3D srcLocation,
Serial trgSerial, Point3D trgLocation,
int itemID, int hue, int amount
)
{
if (ns.CannotSendPackets())
{
return;
}
var buffer = stackalloc byte[DragEffectPacketLength].InitializePacket();
CreateDragEffect(buffer, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount);
ns.Send(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SendSeasonChange(this NetState ns, byte season, bool playSound) =>
ns?.Send(stackalloc byte[]{ 0xBC, season, *(byte*)&playSound });
public static void SendDisplayPaperdoll(this NetState ns, Serial m, string title, bool warmode, bool canLift)
{
if (ns.CannotSendPackets())
{
return;
}
byte flags = 0x00;
if (warmode)
{
flags |= 0x01;
}
if (canLift)
{
flags |= 0x02;
}
var writer = new SpanWriter(stackalloc byte[66]);
writer.Write((byte)0x88); // Packet ID
writer.Write(m);
writer.WriteLatin1(title, 60);
writer.Write(flags);
ns.Send(writer.Span);
}
public static void SendPlayMusic(this NetState ns, MusicName music)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[3]);
writer.Write((byte)0x6D); // Packet ID
writer.Write((short)music);
ns.Send(writer.Span);
}
public static void SendStopMusic(this NetState ns) => ns?.Send(stackalloc byte[] { 0x6D, 0x1F, 0xFF });
public static void SendScrollMessage(this NetState ns, int type, int tip, string text)
{
if (ns.CannotSendPackets())
{
return;
}
text ??= "";
var length = 10 + text.Length;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xA6); // Packet ID
writer.Write((ushort)length);
writer.Write((byte)type);
writer.Write(tip);
writer.Write((ushort)text.Length);
writer.WriteLatin1(text);
ns.Send(writer.Span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendCurrentTime(this NetState ns) => ns.SendCurrentTime(Core.Now);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendCurrentTime(this NetState ns, DateTime date) =>
ns?.Send(stackalloc byte[] { 0x5B, (byte)date.Hour, (byte)date.Minute, (byte)date.Second });
public static void SendPathfindMessage(this NetState ns, Point3D p)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[7]);
writer.Write((byte)0x38); // Packet ID
writer.Write((short)p.X);
writer.Write((short)p.Y);
writer.Write((short)p.Z);
ns.Send(writer.Span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendPingAck(this NetState ns, byte ping) => ns?.Send(stackalloc byte[] { 0x73, ping });
public static void SendDisplayHuePicker(this NetState ns, Serial huePickerSerial, int huePickerItemID)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[9]);
writer.Write((byte)0x95); // Packet ID
writer.Write(huePickerSerial);
writer.Write((short)0);
writer.Write((short)huePickerItemID);
ns.Send(writer.Span);
}
}