ModernUO/Projects/UOContent/Items/Skill Items/Tailor Items/Dyetubs/DyeTub.cs
Kamron Batman b042edcf0b
refactor: fold hand-written serializable property setters into field hooks (#2587)
## Summary

Folds **113** hand-written `[SerializableProperty]` members into plain `[SerializableField]` declarations using the v4 setter hooks — value coercion/vetoes via `allowFieldChange`, post-change side effects via `fieldChanged` (whose `oldValue` parameter covers the old-house/old-sender unsubscribe patterns). Net **-450 lines** of setter boilerplate.

```cs
// before
[SerializableProperty(1)]
[CommandProperty(AccessLevel.GameMaster)]
public int Charges
{
    get => _charges;
    set
    {
        _charges = Math.Clamp(value, 0, MaxCharges);
        InvalidateProperties();
        this.MarkDirty();
    }
}

// after
[SerializableField(1, allowFieldChange: nameof(AllowChargesChange))]
[SerializedCommandProperty(AccessLevel.GameMaster)]
[InvalidateProperties]
private int _charges;

private bool AllowChargesChange(ref int value)
{
    value = Math.Clamp(value, 0, MaxCharges);
    return true;
}
```

## How sites were selected

A classifier parsed all 204 `[SerializableProperty]` sites and converted only those matching strict shapes: getter is exactly `get => _field;`, the assignment comes first (after at most an equality guard), and relocated side effects contain no `return`, no `value` mutation, and no field re-assignment. Everything else was left alone deliberately:

- **~34 custom getters** (fallback defaults like `_x == -1 ? Default : _x`, self-healing refs) — no setter hook can express these.
- **~35 pre-assignment logic** (durability Unscale/Scale sandwiches, old-state captures like PotionKeg's pile weight).
- **virtual/override members, name-mismatched backing fields (`m_`), exotic semantics** (guards' `Focus` does work on *equal* assignment; `ChampionSpawn.Active` never assigns its field).

Five sites the classifier refused were converted by hand where the hooks fit cleanly: `ReceiverCrystal.Sender`, `PlayerVendor.House`, `PlayerBarkeeper.House` (old-value unsubscribe via `oldValue`), `BaseSuit.AccessLevel` (its existing virtual `OnAccessLevelChanged` already had the exact callback shape), and `DyeTub.DyedHue` (a true veto: `AllowDyedHueChange(ref int value) => _redyable`).

## Verification

- Build: **0 errors, 0 warnings**.
- **Schema regeneration produces zero Migrations changes** — the conversion is wire- and schema-neutral by construction (same orders, types, and property names), and CI's schema diff check enforces it.
- **835 + 708 tests green.**

## Behavioral notes (all strict improvements, called out for review)

- Generated setters skip everything when the incoming value equals the current one; a few converted setters previously re-ran side effects on equal assignment (redundant `Update()`-style refreshes).
- Generated setters always `MarkDirty()` on change; several converted setters never did (e.g. `DyeTub.DyedHue`, `MorphItem` ranges) — their changes only persisted if something else dirtied the entity. Those latent persistence bugs are fixed by construction.
2026-08-22 18:20:39 -07:00

237 lines
9.2 KiB
C#

using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Gumps;
using Server.Multis;
using Server.Targeting;
namespace Server.Items
{
public interface IDyable
{
bool Dye(Mobile from, DyeTub sender);
}
[SerializationGenerator(2, false)]
public partial class DyeTub : Item, ISecurable
{
[SerializedIgnoreDupe]
[SerializableField(0)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private SecureLevel _level;
[SerializableField(1, isVirtual: true)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private bool _redyable;
[Constructible]
public DyeTub() : base(0xFAB) => _redyable = true;
public override double DefaultWeight => 10.0;
public virtual CustomHuePicker CustomHuePicker => null;
public virtual bool AllowRunebooks => false;
public virtual bool AllowFurniture => false;
public virtual bool AllowStatuettes => false;
public virtual bool AllowLeather => false;
public virtual bool AllowDyables => true;
[SerializableField(2, allowFieldChange: nameof(AllowDyedHueChange), fieldChanged: nameof(OnDyedHueChanged))]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private int _dyedHue;
private bool AllowDyedHueChange(ref int value) => _redyable;
private void OnDyedHueChanged(int oldValue, int newValue) => Hue = newValue;
// Three metallic tubs now.
public virtual bool MetallicHues => false;
// Select the clothing to dye.
public virtual int TargetMessage => 500859;
// You can not dye that.
public virtual int FailMessage => 1042083;
private void Deserialize(IGenericReader reader, int version)
{
switch (version)
{
case 1:
{
Level = (SecureLevel)reader.ReadInt();
goto case 0;
}
case 0:
{
_redyable = reader.ReadBool();
_dyedHue = reader.ReadInt();
break;
}
}
}
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, ref list);
SetSecureLevelEntry.AddTo(from, this, ref list);
}
public override void OnDoubleClick(Mobile from)
{
if (from.InRange(GetWorldLocation(), 1))
{
from.SendLocalizedMessage(TargetMessage);
from.Target = new InternalTarget(this);
}
else
{
from.SendLocalizedMessage(500446); // That is too far away.
}
}
private class InternalTarget : Target
{
private readonly DyeTub m_Tub;
public InternalTarget(DyeTub tub) : base(1, false, TargetFlags.None) => m_Tub = tub;
protected override void OnTarget(Mobile from, object targeted)
{
if (targeted is Item item)
{
if (item.QuestItem)
{
from.SendLocalizedMessage(1151836); // You may not dye toggled quest items.
}
else if (item is IDyable dyable && m_Tub.AllowDyables)
{
if (!from.InRange(m_Tub.GetWorldLocation(), 1) || !from.InRange(item.GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else if (item.Parent is Mobile)
{
from.SendLocalizedMessage(500861); // Can't Dye clothing that is being worn.
}
else if (dyable.Dye(from, m_Tub))
{
from.PlaySound(0x23E);
}
}
else if ((FurnitureAttribute.Check(item) || item is PotionKeg) && m_Tub.AllowFurniture)
{
if (!from.InRange(m_Tub.GetWorldLocation(), 1) || !from.InRange(item.GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else
{
var okay = item.IsChildOf(from.Backpack);
if (!okay)
{
if (item.Parent == null)
{
var house = BaseHouse.FindHouseAt(item);
if (house == null || !house.HasLockedDownItem(item) && !house.HasSecureItem(item))
{
from.SendLocalizedMessage(501022); // Furniture must be locked down to paint it.
}
else if (!house.IsCoOwner(from))
{
from.SendLocalizedMessage(501023); // You must be the owner to use this item.
}
else
{
okay = true;
}
}
else
{
from.SendLocalizedMessage(
1048135
); // The furniture must be in your backpack to be painted.
}
}
if (okay)
{
item.Hue = m_Tub.DyedHue;
from.PlaySound(0x23E);
}
}
}
else if (item is Runebook or RecallRune && m_Tub.AllowRunebooks)
{
if (!from.InRange(m_Tub.GetWorldLocation(), 1) || !from.InRange(item.GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else if (!item.Movable)
{
from.SendLocalizedMessage(1049776); // You cannot dye runes or runebooks that are locked down.
}
else
{
item.Hue = m_Tub.DyedHue;
from.PlaySound(0x23E);
}
}
else if (item is MonsterStatuette && m_Tub.AllowStatuettes)
{
if (!from.InRange(m_Tub.GetWorldLocation(), 1) || !from.InRange(item.GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else if (!item.Movable)
{
from.SendLocalizedMessage(1049779); // You cannot dye statuettes that are locked down.
}
else
{
item.Hue = m_Tub.DyedHue;
from.PlaySound(0x23E);
}
}
else if ((item is BaseArmor armor &&
armor.MaterialType is ArmorMaterialType.Leather or ArmorMaterialType.Studded || item is ElvenBoots or WoodlandBelt) && m_Tub.AllowLeather)
{
if (!from.InRange(m_Tub.GetWorldLocation(), 1) || !from.InRange(item.GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else if (!item.Movable)
{
from.SendLocalizedMessage(1042419); // You may not dye leather items which are locked down.
}
else if (item.Parent is Mobile)
{
from.SendLocalizedMessage(500861); // Can't Dye clothing that is being worn.
}
else
{
item.Hue = m_Tub.DyedHue;
from.PlaySound(0x23E);
}
}
else
{
from.SendLocalizedMessage(m_Tub.FailMessage);
}
}
else
{
from.SendLocalizedMessage(m_Tub.FailMessage);
}
}
}
}
}