ModernUO/Projects/UOContent/Mobiles/Hireables/BaseHire.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

311 lines
7.6 KiB
C#

using Server.ContextMenus;
using Server.Items;
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
namespace Server.Mobiles;
[SerializationGenerator(0)]
public partial class BaseHire : BaseCreature
{
[SerializableField(0)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private DateTime _nextPay;
[SerializableField(2)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private int _holdGold;
[CommandProperty(AccessLevel.GameMaster)]
public int Pay => PerDayCost();
public int GoldOnDeath { get; set; }
[SerializableField(1, fieldChanged: nameof(OnIsHiredChanged))]
[SerializedCommandProperty(AccessLevel.GameMaster)]
[InvalidateProperties]
private bool _isHired;
private void OnIsHiredChanged(bool oldValue, bool newValue)
{
Delta(MobileDelta.Noto);
}
public BaseHire(AIType AI) : base(AI, FightMode.Aggressor)
{
ControlSlots = 2;
HoldGold = 8;
}
public BaseHire() : base(AIType.AI_Melee, FightMode.Aggressor) => ControlSlots = 2;
public override bool IsBondable => false;
public override bool KeepsItemsOnDeath => true;
// Ensure we cannot drop if we are a hireable
public override bool CanDrop => false;
[AfterDeserialization]
private void AfterDeserialization()
{
if (IsHired)
{
PayTimer.RegisterTimer(this);
}
}
public override bool OnBeforeDeath()
{
GoldOnDeath = Backpack?.GetAmount(typeof(Gold)) ?? 0;
return base.OnBeforeDeath();
}
public override void Delete()
{
base.Delete();
PayTimer.RemoveTimer(this);
}
public override void OnDeath(Container c)
{
if (GoldOnDeath > 0)
{
c.DropItem(new Gold(GoldOnDeath));
}
base.OnDeath(c);
}
public virtual Mobile GetOwner()
{
if (!Controlled)
{
return null;
}
var owner = ControlMaster;
IsHired = true;
if (owner == null)
{
return null;
}
if (owner.Deleted)
{
Say(1005653); // Hmmm. I seem to have lost my master.
SetControlMaster(null);
return null;
}
return owner;
}
public virtual bool AddHire(Mobile m)
{
var owner = GetOwner();
if (owner != null)
{
m.SendLocalizedMessage(1043283, owner.Name); // I am following ~1_NAME~.
return false;
}
if (SetControlMaster(m))
{
IsHired = true;
return true;
}
return false;
}
public int PerDayCost() =>
(int)(Skills[SkillName.Anatomy].Value +
Skills[SkillName.Tactics].Value +
Skills[SkillName.Macing].Value +
Skills[SkillName.Swords].Value +
Skills[SkillName.Fencing].Value +
Skills[SkillName.Archery].Value +
Skills[SkillName.MagicResist].Value +
Skills[SkillName.Healing].Value +
Skills[SkillName.Magery].Value +
Skills[SkillName.Parry].Value) / 35 + 1;
private bool OnHireDragDrop(Mobile from, Item item)
{
if (Pay == 0)
{
SayTo(from, 500200); // I have no need for that.
return false;
}
// Is the creature already hired
if (Controlled)
{
SayTo(from, 1042495); // I have already been hired.
return false;
}
// Is the item the payment in gold
if (item is not Gold)
{
SayTo(from, 1043268); // Tis crass of me, but I want gold
return false;
}
// Is the payment in gold sufficient
if (item.Amount < Pay)
{
SayHireCost();
return false;
}
if (from.Followers + ControlSlots > from.FollowersMax)
{
SayTo(from, 500896); // I see you already have an escort.
return false;
}
// Try to add the hireling as a follower
if (!AddHire(from))
{
return false;
}
// I thank thee for paying me. I will work for thee for ~1_NUMBER~ days.
SayTo(from, 1043258, $"{item.Amount / Pay}"); // Stupid that they don't have "day" cliloc
HoldGold += item.Amount;
NextPay = Core.Now + PayTimer.GetInterval();
PayTimer.RegisterTimer(this);
return true;
}
public override bool OnDragDrop(Mobile from, Item item) => OnHireDragDrop(from, item) || base.OnDragDrop(from, item);
internal void SayHireCost()
{
// I am available for hire for ~1_AMOUNT~ gold coins a day. If thou dost give me gold, I will work for thee.
Say(1043256, $"{Pay}");
}
public override void OnSpeech(SpeechEventArgs e)
{
// Check for a greeting, a 'hire', or a 'servant'
if (!e.Handled && e.Mobile.InRange(this, 6) && (e.HasKeyword(0x003B) || e.HasKeyword(0x0162) || e.HasKeyword(0x000C)))
{
if (Controlled)
{
Say(1042495); // I have already been hired.
}
else
{
e.Handled = true;
SayHireCost();
}
}
base.OnSpeech(e);
}
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
if (Deleted)
{
return;
}
if (!Controlled)
{
list.Add(new HireEntry());
}
else
{
base.GetContextMenuEntries(from, ref list);
}
}
public class PayTimer : Timer
{
private readonly HashSet<BaseHire> _hires = new();
public static PayTimer Instance { get; set; }
public PayTimer() : base(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1))
{
}
public static TimeSpan GetInterval() => TimeSpan.FromMinutes(30.0);
protected override void OnTick()
{
using var queue = PooledRefQueue<Mobile>.Create();
foreach (var hire in _hires)
{
if (hire.NextPay > Core.Now)
{
continue;
}
hire.NextPay = Core.Now + GetInterval();
var pay = hire.Pay;
if (hire.HoldGold <= pay)
{
queue.Enqueue(hire);
}
else
{
hire.HoldGold -= pay;
}
}
while (queue.Count > 0)
{
var hire = (BaseHire)queue.Dequeue();
hire.GetOwner(); // Sets owner to null
hire.Say(503235); // I regret nothing!
hire.Delete();
}
}
public static void RegisterTimer(BaseHire hire)
{
Instance ??= new PayTimer();
if (!Instance.Running)
{
Instance.Start();
}
Instance._hires.Add(hire);
}
public static void RemoveTimer(BaseHire hire)
{
if (Instance?._hires.Remove(hire) == true && Instance._hires.Count == 0)
{
Instance.Stop();
}
}
}
public class HireEntry : ContextMenuEntry
{
public HireEntry() : base(6120, 3)
{
}
public override void OnClick(Mobile from, IEntity target)
{
(target as BaseHire)?.SayHireCost();
}
}
}