Refactor Item collections for thread safety and performance

Replaces static Item.ItemList and related collections with thread-safe data structures using ConcurrentDictionary and ImmutableHashSet. Adds thread-safe helpers for marking items for deconstruction and managing item lists. Updates all usages of Item.ItemList and DeconstructItems to use new APIs, improving performance and safety in multi-threaded contexts. Also refactors MeleeWeapon and Projectile impact queues to use ConcurrentQueue, and updates related logic throughout the codebase.
This commit is contained in:
Eero
2025-12-28 03:57:04 +08:00
parent edd50ef181
commit 90962b2328
22 changed files with 232 additions and 92 deletions
@@ -261,7 +261,7 @@ namespace Barotrauma
DebugConsole.ThrowError($"Error while removing item \"{item}\"", exception);
}
}
Item.ItemList.Clear();
Item.ClearAllItemCollections();
}
if (Character.CharacterList.Count > 0)
{
@@ -4775,7 +4775,7 @@ namespace Barotrauma
// BeaconStation.FlipX();
// }
Item sonarItem = Item.ItemList.Find(it => it.Submarine == BeaconStation && it.GetComponent<Sonar>() != null);
Item sonarItem = Item.ItemList.FirstOrDefault(it => it.Submarine == BeaconStation && it.GetComponent<Sonar>() != null);
if (sonarItem == null)
{
DebugConsole.ThrowError($"No sonar found in the beacon station \"{beaconStationName}\"!");
@@ -4794,7 +4794,7 @@ namespace Barotrauma
throw new InvalidOperationException("Failed to prepare beacon station (no beacon station in the level).");
}
List<Item> beaconItems = Item.ItemList.FindAll(it => it.Submarine == BeaconStation);
List<Item> beaconItems = Item.ItemList.Where(it => it.Submarine == BeaconStation).ToList();
Item reactorItem = beaconItems.Find(it => it.GetComponent<Reactor>() != null);
Reactor reactorComponent = null;
@@ -4840,7 +4840,7 @@ namespace Barotrauma
if (BeaconStation?.Info?.BeaconStationInfo is { AllowDisconnectedWires: false }) { return; }
if (disconnectWireProbability <= 0.0f) { return; }
List<Item> beaconItems = Item.ItemList.FindAll(it => it.Submarine == BeaconStation);
List<Item> beaconItems = Item.ItemList.Where(it => it.Submarine == BeaconStation).ToList();
foreach (Item item in beaconItems.Where(it => it.GetComponent<Wire>() != null).ToList())
{
if (item.NonInteractable || item.InvulnerableToDamage) { continue; }
@@ -4878,7 +4878,7 @@ namespace Barotrauma
if (breakDeviceProbability <= 0.0f) { return; }
//break powered items
List<Item> beaconItems = Item.ItemList.FindAll(it => it.Submarine == BeaconStation);
List<Item> beaconItems = Item.ItemList.Where(it => it.Submarine == BeaconStation).ToList();
foreach (Item item in beaconItems.Where(it => it.Components.Any(c => c is Powered) && it.Components.Any(c => c is Repairable)))
{
if (item.NonInteractable || item.InvulnerableToDamage) { continue; }
@@ -1371,7 +1371,7 @@ namespace Barotrauma
{
foreach (TakenItem takenItem in takenItems)
{
Item item = Item.ItemList.Find(it => takenItem.Matches(it));
Item item = Item.ItemList.FirstOrDefault(it => takenItem.Matches(it));
item?.Remove();
}
}
@@ -1077,7 +1077,7 @@ namespace Barotrauma
Item.UpdateHulls();
List<Item> bodyItems = Item.ItemList.FindAll(it => it.Submarine == this && it.body != null);
List<Item> bodyItems = Item.ItemList.Where(it => it.Submarine == this && it.body != null).ToList();
List<MapEntity> subEntities = MapEntity.MapEntityList.FindAll(me => me.Submarine == this);
foreach (MapEntity e in subEntities)
@@ -1507,7 +1507,7 @@ namespace Barotrauma
public List<Hull> GetHulls(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, Hull.HullList);
public List<Gap> GetGaps(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, Gap.GapList);
public List<Item> GetItems(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, Item.ItemList);
public List<Item> GetItems(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, Item.ItemList).ToList();
public List<WayPoint> GetWaypoints(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, WayPoint.WayPointList);
public List<Structure> GetWalls(bool alsoFromConnectedSubs) => GetEntities(alsoFromConnectedSubs, Structure.WallList);
@@ -2148,7 +2148,7 @@ namespace Barotrauma
DebugConsole.ThrowError("Error while removing \"" + item.Name + "\"!", e);
}
}
Item.ItemList.Clear();
Item.ClearAllItemCollections();
}
Ragdoll.RemoveAll();