Revert "OBT1.1.0 Merge branch 'dev_pte' into dev"
This reverts commit177cf89756, reversing changes made to42ba733cd4.
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -64,77 +62,17 @@ namespace Barotrauma.Items.Components
|
||||
protected const float UpdateInterval = (float)Timing.Step;
|
||||
|
||||
/// <summary>
|
||||
/// List of all powered ItemComponents (thread-safe)
|
||||
/// List of all powered ItemComponents
|
||||
/// </summary>
|
||||
private static readonly ConcurrentDictionary<Powered, byte> _poweredDict = new ConcurrentDictionary<Powered, byte>();
|
||||
|
||||
/// <summary>
|
||||
/// Cached list for iteration - updated when collection changes
|
||||
/// </summary>
|
||||
private static volatile List<Powered> _cachedPoweredList;
|
||||
private static int _poweredListVersion;
|
||||
|
||||
private static readonly List<Powered> poweredList = new List<Powered>();
|
||||
public static IEnumerable<Powered> PoweredList
|
||||
{
|
||||
get
|
||||
{
|
||||
var cached = _cachedPoweredList;
|
||||
if (cached != null) return cached;
|
||||
return GetCachedPoweredList();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Powered> GetCachedPoweredList()
|
||||
{
|
||||
var newList = _poweredDict.Keys.ToList();
|
||||
_cachedPoweredList = newList;
|
||||
return newList;
|
||||
}
|
||||
|
||||
private static void InvalidatePoweredListCache()
|
||||
{
|
||||
_cachedPoweredList = null;
|
||||
Interlocked.Increment(ref _poweredListVersion);
|
||||
get { return poweredList; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe set of changed connections
|
||||
/// </summary>
|
||||
private static readonly ConcurrentDictionary<Connection, byte> _changedConnections = new ConcurrentDictionary<Connection, byte>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets all changed connections (snapshot)
|
||||
/// </summary>
|
||||
public static ICollection<Connection> ChangedConnections => _changedConnections.Keys;
|
||||
|
||||
/// <summary>
|
||||
/// Add a connection to the changed set
|
||||
/// </summary>
|
||||
public static void MarkConnectionChanged(Connection c)
|
||||
{
|
||||
_changedConnections.TryAdd(c, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear all changed connections
|
||||
/// </summary>
|
||||
public static void ClearChangedConnections()
|
||||
{
|
||||
_changedConnections.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a connection from the changed set
|
||||
/// </summary>
|
||||
public static void UnmarkConnectionChanged(Connection c)
|
||||
{
|
||||
_changedConnections.TryRemove(c, out _);
|
||||
}
|
||||
public static readonly HashSet<Connection> ChangedConnections = new HashSet<Connection>();
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe grid dictionary
|
||||
/// </summary>
|
||||
public readonly static ConcurrentDictionary<int, GridInfo> Grids = new ConcurrentDictionary<int, GridInfo>();
|
||||
public readonly static Dictionary<int, GridInfo> Grids = new Dictionary<int, GridInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// The amount of power currently consumed by the item. Negative values mean that the item is providing power to connected items
|
||||
@@ -271,8 +209,7 @@ namespace Barotrauma.Items.Components
|
||||
public Powered(Item item, ContentXElement element)
|
||||
: base(item, element)
|
||||
{
|
||||
_poweredDict.TryAdd(this, 0);
|
||||
InvalidatePoweredListCache();
|
||||
poweredList.Add(this);
|
||||
InitProjectSpecific(element);
|
||||
}
|
||||
|
||||
@@ -385,20 +322,17 @@ namespace Barotrauma.Items.Components
|
||||
//don't use cache if there are no existing grids
|
||||
if (Grids.Count > 0 && useCache)
|
||||
{
|
||||
// Take a snapshot of changed connections for iteration
|
||||
var changedSnapshot = ChangedConnections.ToList();
|
||||
|
||||
//delete all grids that were affected
|
||||
foreach (Connection c in changedSnapshot)
|
||||
foreach (Connection c in ChangedConnections)
|
||||
{
|
||||
if (c.Grid != null)
|
||||
{
|
||||
Grids.TryRemove(c.Grid.ID, out _);
|
||||
Grids.Remove(c.Grid.ID);
|
||||
c.Grid = null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Connection c in changedSnapshot)
|
||||
foreach (Connection c in ChangedConnections)
|
||||
{
|
||||
//Make sure the connection grid hasn't been resolved by another connection update
|
||||
//Ensure the connection has other connections
|
||||
@@ -412,7 +346,7 @@ namespace Barotrauma.Items.Components
|
||||
else
|
||||
{
|
||||
//Clear all grid IDs from connections
|
||||
foreach (Powered powered in PoweredList)
|
||||
foreach (Powered powered in poweredList)
|
||||
{
|
||||
//Only check devices with connectors
|
||||
if (powered.powerIn != null)
|
||||
@@ -427,7 +361,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
Grids.Clear();
|
||||
|
||||
foreach (Powered powered in PoweredList)
|
||||
foreach (Powered powered in poweredList)
|
||||
{
|
||||
if (powered.Item.Condition <= 0f) { continue; }
|
||||
|
||||
@@ -458,7 +392,7 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
|
||||
//Clear changed connections after each update
|
||||
ClearChangedConnections();
|
||||
ChangedConnections.Clear();
|
||||
}
|
||||
|
||||
private static GridInfo PropagateGrid(Connection conn)
|
||||
@@ -488,8 +422,8 @@ namespace Barotrauma.Items.Components
|
||||
c.Grid = grid;
|
||||
grid.AddConnection(c);
|
||||
|
||||
//Add on recipients - use ToList() snapshot for thread-safe iteration
|
||||
foreach (Connection otherC in c.Recipients.ToList())
|
||||
//Add on recipients
|
||||
foreach (Connection otherC in c.Recipients)
|
||||
{
|
||||
//Only add valid connections
|
||||
if (otherC.Grid != grid && (otherC.Grid == null || !Grids.ContainsKey(otherC.Grid.ID)) && ValidPowerConnection(c, otherC))
|
||||
@@ -560,7 +494,7 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
|
||||
//Determine if devices are adding a load or providing power, also resolve solo nodes
|
||||
foreach (Powered powered in PoweredList)
|
||||
foreach (Powered powered in poweredList)
|
||||
{
|
||||
//Make voltage decay to ensure the device powers down.
|
||||
//This only effects devices with no power input (whose voltage is set by other means, e.g. status effects from a contained battery)
|
||||
@@ -796,8 +730,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (item.Connections != null && powerIn != null)
|
||||
{
|
||||
// Use ToList() snapshot for thread-safe iteration
|
||||
foreach (Connection recipient in powerIn.Recipients.ToList())
|
||||
foreach (Connection recipient in powerIn.Recipients)
|
||||
{
|
||||
if (!recipient.IsPower || !recipient.IsOutput) { continue; }
|
||||
if (recipient.Item?.GetComponent<PowerContainer>() is PowerContainer battery)
|
||||
@@ -817,14 +750,13 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (c.IsPower && c.Grid != null)
|
||||
{
|
||||
MarkConnectionChanged(c);
|
||||
ChangedConnections.Add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.RemoveComponentSpecific();
|
||||
_poweredDict.TryRemove(this, out _);
|
||||
InvalidatePoweredListCache();
|
||||
poweredList.Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -848,9 +780,9 @@ namespace Barotrauma.Items.Components
|
||||
Connections.Remove(c);
|
||||
|
||||
//Remove the grid if it has no devices
|
||||
if (Connections.Count == 0)
|
||||
if (Connections.Count == 0 && Powered.Grids.ContainsKey(ID))
|
||||
{
|
||||
Powered.Grids.TryRemove(ID, out _);
|
||||
Powered.Grids.Remove(ID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user