## What `Dictionary<K,V>.Remove` and `HashSet<T>.Remove` do not bump the collection's version, so removing an entry during a `foreach` does not invalidate the enumerator. A number of loops were still paying for a `PooledRefQueue`/`PooledRefList` to collect keys and drain them in a second pass. This drops those guards. ## Why it's safe Verified against .NET 10.0.10 rather than taken on trust, since the documented guarantee covers only `Dictionary<TKey,TValue>.Remove` while several of these call sites are `HashSet<T>` or enumerate `.Keys`/`.Values`: | Case | Result | |---|---| | `Dictionary` foreach + `Remove` | safe, all entries visited | | `Dictionary.Keys` / `.Values` foreach + `Remove` | safe, all entries visited | | `HashSet` foreach + `Remove` | safe, all entries visited | | `Dictionary` foreach + `Remove` **then `Add`** | throws `InvalidOperationException` | Reflection on `_version` confirms the mechanism: neither `Dictionary.Remove` nor `HashSet.Remove` touches it. Because `Remove` never bumps the version, the `Keys` and `Values` enumerators are just as safe as the dictionary's own, even though only `Dictionary.Remove` documents the behaviour. No entries were skipped in any case. The `HashSet` half is confirmed by [stephentoub on dotnet/dotnet-api-docs#8177](https://github.com/dotnet/dotnet-api-docs/issues/8177#issuecomment-1167251052): *"Both HashSet and Dictionary have been improved to support removal during enumeration. The docs may just benefit from updating."* The gap is in the documentation, not the runtime. `Remove` followed by `Add` in the same enumeration still throws. That is the line this PR does not cross. ## Guards removed `VisibilityList`, `ChampionTitleSystem`, `Channel`, `BombingRun`, `Ruleset`, `PuzzleChest`, `RaceChangeGump`, `StepCache`, `PlayerMurderSystem`, `VirtueSystem`, `ProjectedItem`, `StaminaSystem`, `AIGroupMovement`, `PromotedGuard`, `AutoDenylist`, `LoginAllowlist`, `AntiMacroSystem`, `DetectHidden`. Both collection kinds are covered: `Dictionary` (including loops over `.Keys` and `.Values`) and `HashSet` (`ProjectedItem._active`, `PlayerMurderSystem._contextTerms`, `StaminaSystem._resetHash`). In `StaminaSystem.ResetTimer` the `Count == queue.Count → Clear()` branch goes away with the queue — it only existed to avoid paying for N individual removes. Where the collection supports it, `Contains` + `Remove` and `TryGetValue` + `Remove` also collapse into a single lookup (`if (list.Remove(x))`, `if (m_Pending.Remove(ns, out var state))`). `Utility.Tidy<K,V>` keeps its two branches: when `K` is serializable the value is not inspected, otherwise the value is. Only the serializable side may be cast, so `Dictionary<Mobile, int>` and `Dictionary<Mobile, string>` stay valid. ## Deliberately unchanged **`BaseCreature.LoyaltyTimer.OnTick`** keeps its deferred-delete queue. Removing from `World.Mobiles` while enumerating it is safe, but `Mobile.Delete()` is not a `Remove` — it runs `OnDelete`/`OnAfterDelete`, the `OnParentDeleted` cascade over the creature's pack, `DropHolding()`, and region and guild callbacks. Anything in that surface that constructs a `Mobile` is an `Add` into the dictionary being enumerated, which does invalidate it. `BaseHire.PayTimer.OnTick` has the same shape and is likewise untouched. **Spatial-query buffers** — `GuardedRegion.CallGuards`, `Thunderstorm`, `Exorcism`, `LeverPuzzleController`, `BaseCreature.TeleportPets` — are a different hazard. They buffer the result of a range query because the drain moves or harms mobiles, which mutates sectors mid-enumeration. **Re-entrant drains.** The `_users` sets in `Firebomb` and the explosion, conflagration and confusion-blast potions look like this pattern but are not: the loop collects, `Clear()`s, and only then runs `Target.Cancel` on each, which can re-enter. `AnimalTrainer` enumerates `pm.Stabled` and drains through `RemoveStabled`, which nulls the `Stabled` field once it empties — safe for an in-flight enumerator, which holds the set reference rather than the field, but subtle enough not to be worth inlining on a cold path. ## Verification `dotnet build` clean with 0 warnings; 810 Server and 684 UOContent tests pass.
269 lines
9.4 KiB
C#
269 lines
9.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Server.Collections;
|
|
using Server.Engines.PartySystem;
|
|
using Server.Factions;
|
|
using Server.Guilds;
|
|
using Server.Items;
|
|
using Server.Mobiles;
|
|
using Server.Multis;
|
|
using Server.Network;
|
|
using Server.Systems.FeatureFlags;
|
|
using Server.Targeting;
|
|
|
|
namespace Server.SkillHandlers;
|
|
|
|
public static class DetectHidden
|
|
{
|
|
// Debounce tracking: (stealther, detector) -> last detection time
|
|
private static readonly Dictionary<(Mobile, Mobile), long> PassiveDetectDebounce = [];
|
|
|
|
private const int PassiveDetectDebounceMs = 3000; // 3 seconds
|
|
private const int DebounceCleanupIntervalMs = 10000; // Run cleanup every 10 seconds
|
|
private const int DebounceExpiryMs = 10000; // Remove entries older than 10 seconds
|
|
private static long _lastCleanupTime;
|
|
|
|
public static void Initialize()
|
|
{
|
|
SkillInfo.Table[(int)SkillName.DetectHidden].Callback = OnUse;
|
|
}
|
|
|
|
public static TimeSpan OnUse(Mobile src)
|
|
{
|
|
src.SendLocalizedMessage(500819); // Where will you search?
|
|
src.Target = new InternalTarget();
|
|
|
|
return TimeSpan.FromSeconds(30.0);
|
|
}
|
|
|
|
// Clean up old debounce entries to prevent memory bloat
|
|
private static void CleanupDebounceCache(long now)
|
|
{
|
|
foreach (var entry in PassiveDetectDebounce)
|
|
{
|
|
if (now - entry.Value > DebounceExpiryMs)
|
|
{
|
|
PassiveDetectDebounce.Remove(entry.Key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// For testing: clear the debounce cache to prevent cross-test contamination
|
|
internal static void ClearDebounceCache()
|
|
{
|
|
PassiveDetectDebounce.Clear();
|
|
}
|
|
|
|
// Passive detection: check if a detector can passively detect a stealther.
|
|
// Called via OnMovement when either party moves within range.
|
|
// Returns true if detection was successful (and the stealther was revealed).
|
|
// NOTE: OSI uncertain - The exact chance calculation and distance dropoff is unknown.
|
|
// We use a ±10 variance on both skills, matching active detection mechanics.
|
|
public static bool TryDetectStealther(Mobile detector, Mobile stealther)
|
|
{
|
|
if (!ContentFeatureFlags.PassiveDetectHidden || stealther == detector || !stealther.Hidden || !detector.Alive)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Felucca PvP only
|
|
if (stealther.Map != Map.Felucca)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var detectSkill = detector.Skills.DetectHidden.Value;
|
|
if (detectSkill <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var now = Core.TickCount;
|
|
|
|
// Debounce: skip pairs already checked recently (cheap dictionary lookup before expensive checks)
|
|
var key = (stealther, detector);
|
|
if (PassiveDetectDebounce.TryGetValue(key, out var lastDetect) && now - lastDetect < PassiveDetectDebounceMs)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Periodic cleanup (amortized, not every call)
|
|
if (now - _lastCleanupTime > DebounceCleanupIntervalMs)
|
|
{
|
|
_lastCleanupTime = now;
|
|
CleanupDebounceCache(now);
|
|
}
|
|
|
|
// Excludes blessed, dead, bonded pets, and region-based PvP rules
|
|
if (stealther.AccessLevel > AccessLevel.Player || !detector.CanBeHarmful(stealther, false))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Exclude party members
|
|
var stealtherParty = Party.Get(stealther);
|
|
var detectorParty = Party.Get(detector);
|
|
if (stealtherParty != null && stealtherParty == detectorParty)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Exclude guild members and allies
|
|
if (stealther.Guild is Guild sg && detector.Guild is Guild dg && (sg == dg || sg.IsAlly(dg)))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var ss = detectSkill + Utility.Random(21) - 10;
|
|
var ts = stealther.Skills.Hiding.Value + Utility.Random(21) - 10;
|
|
|
|
if (ss >= ts)
|
|
{
|
|
stealther.RevealingAction();
|
|
stealther.SendLocalizedMessage(500814); // You have been revealed!
|
|
PassiveDetectDebounce[key] = now;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private class InternalTarget : Target
|
|
{
|
|
public InternalTarget() : base(12, true, TargetFlags.None)
|
|
{
|
|
}
|
|
|
|
protected override void OnTargetCancel(Mobile from, TargetCancelType cancelType)
|
|
{
|
|
from.NextSkillTime = Core.TickCount;
|
|
}
|
|
|
|
protected override void OnTarget(Mobile src, object targ)
|
|
{
|
|
var foundAnyone = false;
|
|
var srcSkill = src.Skills.DetectHidden.Value;
|
|
var range = (int)(srcSkill / 10.0);
|
|
|
|
if (targ is TrappableContainer container && container.TrapType != TrapType.None)
|
|
{
|
|
// Direct container targeting: show [trapped] if within detection range and skill check passes
|
|
if (src.InRange(container.GetWorldLocation(), range) &&
|
|
src.CheckSkill(SkillName.DetectHidden, 0.0, 100.0))
|
|
{
|
|
src.NetState.SendMessageLocalized(
|
|
container.Serial,
|
|
container.ItemID,
|
|
MessageType.Regular,
|
|
0x3B2,
|
|
3,
|
|
500813 // [trapped]
|
|
);
|
|
|
|
foundAnyone = true;
|
|
}
|
|
}
|
|
else if (targ is not Item && (targ is not Mobile m || m == src))
|
|
{
|
|
// Area scan only when targeting self or the ground
|
|
var p = targ switch
|
|
{
|
|
Mobile mobile => mobile.Location,
|
|
IPoint3D d => new Point3D(d),
|
|
_ => src.Location
|
|
};
|
|
|
|
if (!src.CheckSkill(SkillName.DetectHidden, 0.0, 100.0))
|
|
{
|
|
range /= 2;
|
|
}
|
|
|
|
var house = BaseHouse.FindHouseAt(p, src.Map, 16);
|
|
var inHouse = house?.IsFriend(src) == true;
|
|
|
|
if (inHouse)
|
|
{
|
|
range = 22;
|
|
}
|
|
|
|
if (range > 0)
|
|
{
|
|
foreach (var trg in src.Map.GetMobilesInRange(p, range))
|
|
{
|
|
if (!trg.Hidden || src == trg)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var ss = srcSkill + Utility.Random(21) - 10;
|
|
var ts = trg.Skills.Hiding.Value + Utility.Random(21) - 10;
|
|
|
|
if (src.AccessLevel < trg.AccessLevel || ss < ts && (!inHouse || !house.IsInside(trg)))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (trg is ShadowKnight && (trg.X != p.X || trg.Y != p.Y))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
trg.RevealingAction();
|
|
trg.SendLocalizedMessage(500814); // You have been revealed!
|
|
foundAnyone = true;
|
|
}
|
|
|
|
foreach (var item in src.Map.GetItemsInRange(p, range))
|
|
{
|
|
if (item is BaseFactionTrap factionTrap)
|
|
{
|
|
if (Faction.Find(src) != null &&
|
|
src.CheckTargetSkill(SkillName.DetectHidden, factionTrap, 80.0, 100.0))
|
|
{
|
|
src.SendLocalizedMessage(
|
|
1042712, // You reveal a trap placed by a faction:
|
|
true,
|
|
$" {(factionTrap.Faction == null ? "" : factionTrap.Faction.Definition.FriendlyName)}"
|
|
);
|
|
|
|
factionTrap.Visible = true;
|
|
factionTrap.BeginConceal();
|
|
|
|
foundAnyone = true;
|
|
}
|
|
}
|
|
else if (item is BaseTrap { Visible: false } trap)
|
|
{
|
|
// High Seas (Publish 79): Requires 75 Detect Hidden to detect dungeon traps
|
|
if (Core.HS && srcSkill < 75.0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
trap.Visible = true;
|
|
Timer.StartTimer(TimeSpan.FromSeconds(10.0), () =>
|
|
{
|
|
if (!trap.Deleted)
|
|
{
|
|
trap.Visible = false;
|
|
}
|
|
});
|
|
|
|
foundAnyone = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!foundAnyone)
|
|
{
|
|
src.SendLocalizedMessage(500817); // You can see nothing hidden there.
|
|
}
|
|
|
|
// Calculate how much time has passed since the targeter was opened
|
|
var ticksSinceTargeter = (int)(Core.TickCount - (src.NextSkillTime - 30000));
|
|
var remainingCooldown = Math.Max(0, 10000 - ticksSinceTargeter);
|
|
src.NextSkillTime = Core.TickCount + remainingCooldown;
|
|
}
|
|
}
|
|
}
|