Skip to content

Commit

Permalink
Fix collection modified crashes on TreeHierarchyDB children
Browse files Browse the repository at this point in the history
  • Loading branch information
behindcurtain3 committed Mar 24, 2024
1 parent 8f5d211 commit 741b706
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Pulsar4X/GameEngine/DataStructures/SafeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public void RemoveAt(int index)
}
}

public int RemoveAll(Predicate<T> match)
{
lock(_lock)
{
return _innerList.RemoveAll(match);
}
}

public void Insert(int index, T item)
{
lock(_lock)
Expand Down Expand Up @@ -101,6 +109,14 @@ public void Clear()
}
}

public T[] ToArray()
{
lock(_lock)
{
return _innerList.ToArray();
}
}

public IEnumerator<T> GetEnumerator()
{
lock(_lock)
Expand Down
7 changes: 4 additions & 3 deletions Pulsar4X/GameEngine/Datablobs/TreeHierarchyDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using Pulsar4X.Engine;
using Pulsar4X.DataStructures;

namespace Pulsar4X.Datablobs
{
Expand Down Expand Up @@ -90,9 +91,9 @@ public TreeHierarchyDB? ParentDB
/// </summary>
[NotNull]
[PublicAPI]
public List<Entity> Children => _children;
public SafeList<Entity> Children => _children;
[JsonProperty]
private readonly List<Entity> _children;
private readonly SafeList<Entity> _children;

/// <summary>
/// All node nodeDB's to this node.
Expand All @@ -108,7 +109,7 @@ public TreeHierarchyDB? ParentDB
protected TreeHierarchyDB(Entity? parent)
{
Parent = parent;
_children = new List<Entity>();
_children = new SafeList<Entity>();
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Pulsar4X/Pulsar4X.Client/EmpireManagement/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Numerics;
using ImGuiNET;
using Pulsar4X.Datablobs;
using Pulsar4X.DataStructures;
using Pulsar4X.Engine;
using Pulsar4X.Extensions;

Expand Down Expand Up @@ -70,7 +71,7 @@ internal override void Display()
}
if(ImGui.CollapsingHeader("Fleets", ImGuiTreeNodeFlags.DefaultOpen))
{
var fleets = _uiState.Faction.GetDataBlob<FleetDB>().RootDB?.Children ?? new List<Entity>();
var fleets = _uiState.Faction.GetDataBlob<FleetDB>().RootDB?.Children ?? new SafeList<Entity>();

foreach(var fleet in fleets)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void TreeGen(Entity currentBody, Entity selectedBody, int depth = 0)
var children = positionDB.Children;
if (children.Count > 0)
{
children = children.OrderBy(x => x.GetDataBlob<PositionDB>().AbsolutePosition).ToList();
children = new(children.OrderBy(x => x.GetDataBlob<PositionDB>().AbsolutePosition).ToList());
foreach (var child in children)
{
TreeGen(child, selectedBody, depth + 1);
Expand Down

0 comments on commit 741b706

Please sign in to comment.