ModernUO/Projects/UOContent/Items/Skill Items/Misc/RepairDeed.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

225 lines
5.8 KiB
C#

using System;
using ModernUO.Serialization;
using Server.Engines.Craft;
using Server.Factions;
using Server.Mobiles;
using Server.Regions;
namespace Server.Items;
[SerializationGenerator(1, false)]
public partial class RepairDeed : Item
{
public enum RepairSkillType
{
Smithing,
Tailoring,
Tinkering,
Carpentry,
Fletching
}
[InvalidateProperties]
[SerializableField(0)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private RepairSkillType _skill;
[InvalidateProperties]
[SerializableField(2)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private string _crafter;
[Constructible]
public RepairDeed(RepairSkillType skill, double level, bool normalizeLevel) : this(
skill,
level,
null,
normalizeLevel
)
{
}
[Constructible]
public RepairDeed(
RepairSkillType skill = RepairSkillType.Smithing, double level = 100.0,
Mobile crafter = null, bool normalizeLevel = true
) : base(0x14F0)
{
if (normalizeLevel)
{
SkillLevel = (int)(level / 10) * 10;
}
else
{
SkillLevel = level;
}
_skill = skill;
_crafter = crafter?.RawName;
Hue = 0x1BC;
LootType = LootType.Blessed;
}
public override bool DisplayLootType => false;
[SerializableField(1, allowFieldChange: nameof(AllowSkillLevelChange))]
[SerializedCommandProperty(AccessLevel.GameMaster)]
[InvalidateProperties]
private double _skillLevel;
private bool AllowSkillLevelChange(ref double value)
{
value = Math.Clamp(value, 0, 120.0);
return true;
}
public override void AddNameProperty(IPropertyList list)
{
// A repair service contract from ~1_SKILL_TITLE~ ~2_SKILL_NAME~.
list.Add(1061133, $"{GetSkillTitle(_skillLevel)}\t{RepairSkillInfo.GetInfo(_skill).Name}");
}
public override void GetProperties(IPropertyList list)
{
base.GetProperties(list);
if (_crafter != null)
{
list.Add(1050043, _crafter); // crafted by ~1_NAME~
}
}
public override void OnSingleClick(Mobile from)
{
if (Deleted || !from.CanSee(this))
{
return;
}
// A repair service contract from ~1_SKILL_TITLE~ ~2_SKILL_NAME~.
LabelTo(from, 1061133, $"{GetSkillTitle(_skillLevel)}\t{RepairSkillInfo.GetInfo(_skill).Name}");
if (_crafter != null)
{
LabelTo(from, 1050043, _crafter); // crafted by ~1_NAME~
}
}
private static TextDefinition GetSkillTitle(double skillLevel)
{
var skill = (int)(skillLevel / 10);
if (skill >= 11)
{
return 1062008 + skill - 11;
}
if (skill >= 5)
{
return 1061123 + skill - 5;
}
return skill switch
{
4 => "a Novice",
3 => "a Neophyte",
_ => "a Newbie"
};
}
public static RepairSkillType GetTypeFor(CraftSystem s)
{
for (var i = 0; i < RepairSkillInfo.Table.Length; i++)
{
if (RepairSkillInfo.Table[i].System == s)
{
return (RepairSkillType)i;
}
}
return RepairSkillType.Smithing;
}
public override void OnDoubleClick(Mobile from)
{
if (Check(from))
{
Repair.Do(from, RepairSkillInfo.GetInfo(_skill).System, this);
}
}
public bool Check(Mobile from)
{
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1047012); // The contract must be in your backpack to use it.
return false;
}
if (!VerifyRegion(from))
{
RepairSkillInfo.GetInfo(_skill).NotNearbyMessage.SendMessageTo(from);
return false;
}
return true;
}
public bool VerifyRegion(Mobile m) => m.Region.IsPartOf<TownRegion>() &&
Faction.IsNearType(m, RepairSkillInfo.GetInfo(_skill).NearbyTypes, 6);
private void Deserialize(IGenericReader reader, int version)
{
_skill = (RepairSkillType)reader.ReadInt();
_skillLevel = reader.ReadDouble();
Timer.DelayCall((item, crafter) => item._crafter = crafter?.RawName, this, reader.ReadEntity<Mobile>());
}
private class RepairSkillInfo
{
public RepairSkillInfo(
CraftSystem system, Type[] nearbyTypes, TextDefinition notNearbyMessage,
TextDefinition name
)
{
System = system;
NearbyTypes = nearbyTypes;
NotNearbyMessage = notNearbyMessage;
Name = name;
}
public RepairSkillInfo(CraftSystem system, Type nearbyType, TextDefinition notNearbyMessage, TextDefinition name)
: this(system, new[] { nearbyType }, notNearbyMessage, name)
{
}
public TextDefinition NotNearbyMessage { get; }
public TextDefinition Name { get; }
public CraftSystem System { get; }
public Type[] NearbyTypes { get; }
public static RepairSkillInfo[] Table { get; } =
{
new(DefBlacksmithy.CraftSystem, typeof(Blacksmith), 1047013, 1023015),
new(DefTailoring.CraftSystem, typeof(Tailor), 1061132, 1022981),
new(DefTinkering.CraftSystem, typeof(Tinker), 1061166, 1022983),
new(DefCarpentry.CraftSystem, typeof(Carpenter), 1061135, 1060774),
new(DefBowFletching.CraftSystem, typeof(Bowyer), 1061134, 1023005)
};
public static RepairSkillInfo GetInfo(RepairSkillType type)
{
var v = (int)type;
if (v < 0 || v >= Table.Length)
{
v = 0;
}
return Table[v];
}
}
}