Skip to content

Commit

Permalink
Did a pass over the messaging to improve thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
behindcurtain3 committed Apr 19, 2024
1 parent f3329e8 commit 8930e25
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 60 deletions.
13 changes: 9 additions & 4 deletions Pulsar4X/GameEngine/DataStructures/SafeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public int Count
}
}

internal Dictionary<TKey, TValue>.KeyCollection Keys
public Dictionary<TKey, TValue>.KeyCollection Keys
{
get
{
lock(_lock) return _innerDictionary.Keys;
}
}

internal Dictionary<TKey, TValue>.ValueCollection Values
public Dictionary<TKey, TValue>.ValueCollection Values
{
get
{
Expand Down Expand Up @@ -102,7 +102,7 @@ public TValue this[TKey key]
}
}

internal void Add(TKey key, TValue value)
public void Add(TKey key, TValue value)
{
lock(_lock)
{
Expand All @@ -112,7 +112,7 @@ internal void Add(TKey key, TValue value)
}
}

internal bool Remove(TKey key)
public bool Remove(TKey key)
{
lock(_lock)
{
Expand All @@ -132,6 +132,11 @@ public bool ContainsKey(TKey key)
lock(_lock) return _innerDictionary.ContainsKey(key);
}

public void Clear()
{
lock(_lock) _innerDictionary.Clear();
}

public bool TryGetValue(TKey key, [NotNullWhen(true)] out TValue? value)
{
if(ContainsKey(key))
Expand Down
2 changes: 1 addition & 1 deletion Pulsar4X/Pulsar4X.Client/DebugAndPData/DebugWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ public override void OnSystemTickChange(DateTime newDate)

if(SystemState == null) return;

if (SystemState.EntitysToBin.Count > 0)
if (SystemState.EntitiesToBin.Count > 0)
{
RefreshFactionEntites();
}
Expand Down
56 changes: 28 additions & 28 deletions Pulsar4X/Pulsar4X.Client/EntityManagement/entityInfoPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal override void Display()
var _parentEntity = _selectedEntity.GetDataBlob<PositionDB>().Parent;
bool _hasParentEntity = false;
SystemState _StarSystemState = _uiState.StarSystemStates[_uiState.SelectedStarSysGuid];
Dictionary<int, EntityState> _NamedEntityStates = _StarSystemState.EntityStatesWithNames;
var _NamedEntityStates = _StarSystemState.EntityStatesWithNames;
if (_parentEntity != null)
{
//checks if parent exists in the selected star system and has a name
Expand Down Expand Up @@ -153,9 +153,9 @@ internal override void Display()

public static class ComponentsDisplay
{

/// <summary>
/// returns a 2d array[i][j] where i is the component design, and j is the componentInstance
/// returns a 2d array[i][j] where i is the component design, and j is the componentInstance
/// </summary>
/// <param name="selectedEntity"></param>
/// <returns></returns>
Expand Down Expand Up @@ -196,12 +196,12 @@ public static void Display(ComponentInstance[][] instancesArray)
var instance = instancesArray[i][j];
string name = instance.Name;
float health = 100 * instance.HealthPercent();
ImGui.Text(name);

ImGui.Text(name);
ImGui.NextColumn();

ImGui.Text(health + "%%");

ImGui.NextColumn();
if(instance.IsEnabled)
ImGui.Text("On");
Expand Down Expand Up @@ -234,14 +234,14 @@ public static void DisplayComplex(ComponentInstance[][] instancesArray)
flatInstances.Add(instance);
foreach (var atb in instance.GetAttributes().Values)
{

}

c++;
}
}


//BorderListOptions.Begin("Components", names.ToArray(), ref _selectedIndex, 256);
//left selectable box
if (BorderListOptions.Begin("Components", names.ToArray(), ref _selectedIndex, 188))
Expand All @@ -250,28 +250,28 @@ public static void DisplayComplex(ComponentInstance[][] instancesArray)
attrubutes = new List<IComponentDesignAttribute>();
}



Dictionary<Type, ComponentAbilityState> states = flatInstances[_selectedIndex].GetAllStates();
foreach (var state in states.Values)
{
ImGui.Text(state.Name);
}

foreach (var atb in flatInstances[_selectedIndex].GetAttributes().Values)
{
attrubutes.Add(atb);
}



for (int atbi = 0; atbi < attrubutes.Count; atbi++)
{
var atbName = attrubutes[atbi].AtbName();
ImGui.Text(atbName);

string[] atbAry = attrubutes[atbi].AtbDescription().Split("\n");

//we're assuming the first item is a title of sorts
var strline = atbAry[0];
ImGui.Text(strline);
Expand All @@ -281,40 +281,40 @@ public static void DisplayComplex(ComponentInstance[][] instancesArray)
for (int strlinei = 1; strlinei < atbAry.Length; strlinei++)
{
strline = atbAry[strlinei];

string[] tabSplit = strline.Split("\t"); //split if there are tabs.
var xpos = ImGui.GetCursorPosX();
for (int i = 0; i < tabSplit.Length; i++)
for (int i = 0; i < tabSplit.Length; i++)
{
if(i > 0) //after the tab char
ImGui.SetCursorPosX(xpos + _textwidth[i-1] + 12);//allign second row

ImGui.Text(tabSplit[i]); //display the text
if (_textwidth[i] < ImGui.GetItemRectSize().X) //check the size
_textwidth[i] = ImGui.GetItemRectSize().X; //expand the size for the next frame
if (i < tabSplit.Length - 1) //put the next item on the same line if there is another item in the array
ImGui.SameLine(); //at least this bit shouldnt break if there's more than one tab
}

}


//ImGui.Text(attrubutes[i].AtbDescription());
}

ImGui.Unindent(2);
float ycount = flatInstances.Count;
float yhight = ImGui.GetTextLineHeightWithSpacing() * ycount;

//float ycount = abilites[_selectedIndex].AbilityDescription().Split("\n").Length -1;
//float yhight = ImGui.GetTextLineHeightWithSpacing() * ycount;
if(xwid < _textwidth[0] + _textwidth[1] + 12)
xwid = _textwidth[0] + _textwidth[1] + 12;
ImGui.Columns(2);
BorderListOptions.End(new Vector2(xwid,yhight));

//BorderListOptions.End(new Vector2(184,yhight));

}

}
Expand Down Expand Up @@ -374,7 +374,7 @@ public static void Display(Entity entity)
strline = abilitiesAry[strlinei];
string[] tabSplit = strline.Split("\t"); //split if there are tabs.
var xpos = ImGui.GetCursorPosX();
for (int i = 0; i < tabSplit.Length; i++)
for (int i = 0; i < tabSplit.Length; i++)
{
if(i > 0) //after the tab char
ImGui.SetCursorPosX(xpos + _textwidth[i-1] + 12);//allign second row
Expand Down
6 changes: 3 additions & 3 deletions Pulsar4X/Pulsar4X.Client/GameManagment/GlobalUIState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class GlobalUIState
internal SystemMapRendering SelectedSysMapRender { get { return GalacticMap.SelectedSysMapRender; } }
internal DateTime PrimarySystemDateTime;
internal EntityContextMenu ContextMenu { get; set; }
internal Dictionary<string, SystemState> StarSystemStates = new ();
internal SafeDictionary<string, SystemState> StarSystemStates = new ();
internal Camera Camera;
internal ImGuiSDL2CSWindow ViewPort;
internal System.Numerics.Vector2 MainWinSize { get {return ViewPort.Size;}}
Expand Down Expand Up @@ -159,7 +159,7 @@ internal void SetFaction(Entity factionEntity, bool setAsPlayer = false)

Faction = factionEntity;
FactionInfoDB factionInfo = factionEntity.GetDataBlob<FactionInfoDB>();
StarSystemStates = new Dictionary<string, SystemState>();
StarSystemStates = new SafeDictionary<string, SystemState>();
foreach (var guid in factionInfo.KnownSystems)
{
var system = Game.Systems.First(s => s.Guid.Equals(guid));
Expand Down Expand Up @@ -252,7 +252,7 @@ internal void MapClicked(Orbital.Vector3 worldCoord, MouseButtons button)
if (LoadedWindows.ContainsKey(typeof(DistanceRuler)))
LoadedWindows[typeof(DistanceRuler)].MapClicked(worldCoord, button);

Dictionary<int, EntityState> allEntities = null;
SafeDictionary<int, EntityState> allEntities = null;
if(StarSystemStates.ContainsKey(SelectedStarSysGuid))
allEntities = StarSystemStates[SelectedStarSysGuid].EntityStatesWithNames;

Expand Down
2 changes: 1 addition & 1 deletion Pulsar4X/Pulsar4X.Client/MapRendering/Icons/ShipIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ShipIcon(Entity entity) : base(entity.GetDataBlob<PositionDB>())
_newtonMoveDB = entity.GetDataBlob<NewtonMoveDB>();
}

Func<Message, bool> filterById = msg => msg.EntityId == entity.Id;
Func<Message, bool> filterById = msg => msg.EntityId != null && msg.EntityId == entity.Id;

MessagePublisher.Instance.Subscribe(MessageTypes.DBAdded, OnDBAdded, filterById);
MessagePublisher.Instance.Subscribe(MessageTypes.DBRemoved, OnDBRemoved, filterById);
Expand Down
12 changes: 10 additions & 2 deletions Pulsar4X/Pulsar4X.Client/MapRendering/SystemMapRendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ internal void Initialize(StarSystem starSys)
_sensorMgr = starSys.GetSensorContacts(_faction.Id);
_sensorChanges = _sensorMgr.Changes.Subscribe();
_sysState.OnEntityAdded += OnSystemStateEntityAdded;
_sysState.OnEntityUpdated += OnSystemStateEntityUpdated;

foreach (var entityItem in _sysState.EntityStatesWithPosition.Values)
{
Expand Down Expand Up @@ -220,7 +221,7 @@ void HandleChanges(EntityState entityState)
entityState = new EntityState(retrievedEntity) { Name = "Unknown" };
}
}

OrbitIconBase orbit;
if (orbitDB.Eccentricity < 1)
{
Expand Down Expand Up @@ -357,6 +358,13 @@ private void OnSystemStateEntityAdded(SystemState systemState, Entity entity)
AddIconable(systemState.EntityStatesWithPosition[entity.Id]);
}

private void OnSystemStateEntityUpdated(SystemState systemState, int entityId, Message message)
{
// Refreseh the icons for the updated entity
RemoveIconable(entityId);
AddIconable(systemState.EntityStatesWithPosition[entityId]);
}

internal void Draw()
{

Expand All @@ -369,7 +377,7 @@ internal void Draw()
HandleChanges(item);
}
}
foreach (var item in _sysState.EntitysToBin)
foreach (var item in _sysState.EntitiesToBin)
{
if(_sysState.EntityStatesWithPosition.ContainsKey(item))
RemoveIconable(item);
Expand Down
Loading

0 comments on commit 8930e25

Please sign in to comment.