ModernUO/Projects/UOContent/Items/Special/Solen Items/PowderOfTranslocation.cs
Kamron Batman 5f9fa88220
perf: Zero-alloc interpolation for SendMessage/Overhead APIs (#2434)
## Summary

Phase 1 of a multi-phase optimization to eliminate intermediate string allocations between `$"..."` interpolation and the packet text region for ModernUO's player-facing message APIs.

- Adds `[InterpolatedStringHandler]` overloads to every `Send*`/`Public/Local/Private/NonlocalOverheadMessage`/`Say`/`Emote`/`Whisper`/`Yell`/`SendLocalizedMessageTo` API in `OutgoingMessagePackets`, `Mobile`, and `Item`. Each overload is a 3-line shim that forwards `handler.Text` to the existing span-based path then calls `handler.Clear()` to return the rented `STArrayPool<char>` buffer (matches the established `SpanWriter.WriteAscii(ref RawInterpolatedStringHandler)` precedent).
- Converts `string text/args/affix/name` parameters to `ReadOnlySpan<char>` for consistency with the handler path. `lang` intentionally stays `string` (it's never interpolated and the `??= "ENU"` fallback stays cleaner).
- Adds `int charCount` overloads of the three `GetMaxMessage*Length` helpers so stackalloc sizing can avoid the redundant `ROS<char>` round-trip.
- Moves `Mobile` (17 methods) and `Item` (4 methods) message methods into new partial-class files (`Mobile.Messages.cs`, `Item.Messages.cs`) for organization.

No UOContent call sites change in this PR — existing `string`/`ROS<char>` calls compile unchanged via implicit conversion. Phase 2 (intermediate-string audit) and Phase 3 (cleanup PRs) follow.

## Files

- `Projects/Server/Network/Packets/OutgoingMessagePackets.cs` — `string` → `ROS<char>` for text params, `int charCount` length helpers added, class made `partial`
- `Projects/Server/Network/Packets/OutgoingMessagePackets.Interpolated.cs` (new) — 3 `ref RawInterpolatedStringHandler` extension overloads
- `Projects/Server/Mobiles/Mobile.cs` — message methods extracted (-262 lines)
- `Projects/Server/Mobiles/Mobile.Messages.cs` (new, 463 lines) — moved + ROS-converted methods + 25 handler overloads
- `Projects/Server/Items/Item.cs` — message methods extracted (-93 lines)
- `Projects/Server/Items/Item.Messages.cs` (new, 142 lines) — moved + ROS-converted methods + 4 handler overloads
- `Projects/Server.Tests/Tests/Network/Packets/Outgoing/MessagePacketTests.cs` — 3 new regression tests verifying byte-equivalence for the handler overloads
2026-05-03 18:04:02 -07:00

107 lines
3.6 KiB
C#

using ModernUO.Serialization;
using Server.Targeting;
namespace Server.Items;
public interface TranslocationItem
{
int Charges { get; set; }
int Recharges { get; set; }
int MaxCharges { get; }
int MaxRecharges { get; }
TextDefinition TranslocationItemName { get; }
}
[SerializationGenerator(0)]
public partial class PowderOfTranslocation : Item
{
[Constructible]
public PowderOfTranslocation(int amount = 1) : base(0x26B8)
{
Stackable = true;
Amount = amount;
}
public override double DefaultWeight => 0.1;
public override void OnDoubleClick(Mobile from)
{
if (from.InRange(GetWorldLocation(), 2))
{
from.Target = new InternalTarget(this);
}
else
{
from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that.
}
}
private class InternalTarget : Target
{
private readonly PowderOfTranslocation _powder;
public InternalTarget(PowderOfTranslocation powder) : base(-1, false, TargetFlags.None) => _powder = powder;
protected override void OnTarget(Mobile from, object targeted)
{
if (_powder.Deleted)
{
return;
}
if (!from.InRange(_powder.GetWorldLocation(), 2))
{
from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that.
}
else if (targeted is TranslocationItem transItem)
{
if (transItem.Charges >= transItem.MaxCharges)
{
// This item cannot absorb any more powder of translocation.
_powder.SendLocalizedMessageTo(from, 1054137, 0x59);
}
else if (transItem.Recharges >= transItem.MaxRecharges)
{
// This item has been oversaturated with powder of translocation and can no longer be recharged.
_powder.SendLocalizedMessageTo(from, 1054138, 0x59);
}
else
{
if (transItem.Charges + _powder.Amount > transItem.MaxCharges)
{
var delta = transItem.MaxCharges - transItem.Charges;
_powder.Amount -= delta;
transItem.Charges = transItem.MaxCharges;
transItem.Recharges += delta;
}
else
{
transItem.Charges += _powder.Amount;
transItem.Recharges += _powder.Amount;
_powder.Delete();
}
if (transItem is Item item)
{
var _transItemName = transItem.TranslocationItemName;
// The ~1_translocationItem~ glows with green energy and absorbs magical power from the powder.
if (_transItemName.Number > 0)
{
item.SendLocalizedMessageTo(from, 1054139, 0x43, $"#{_transItemName.Number}");
}
else if (_transItemName.String != null)
{
item.SendLocalizedMessageTo(from, 1054139, 0x43, _transItemName.String);
}
}
}
}
else
{
// Powder of translocation has no effect on this item.
_powder.SendLocalizedMessageTo(from, 1054140, 0x59);
}
}
}
}