Skip to content

Commit d7a8404

Browse files
committed
Update to internal collection.
- Changed the internal ownership collection from MtObservableSortedDictionary to ObservableDictionary which resolved casting errors. - Added a try/catch to determine the casting error, will leave it for now but should be safe to remove. - Added a check if the functional blocks IDModule is not null, which should never be but it's safer.
1 parent 42df5b7 commit d7a8404

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

‎Torch.Server/ViewModels/Entities/GridViewModel.cs‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Sandbox.Game.Entities;
66
using Sandbox.Game.Entities.Cube;
77
using Sandbox.Game.World;
8+
using SharpDX.Toolkit.Collections;
89
using Torch.Collections;
910
using Torch.Server.ViewModels.Blocks;
1011
using VRage.Game;
@@ -76,7 +77,7 @@ private void RemoveBlock(MyCubeBlock block)
7677
Blocks.Remove(block.BlockDefinition);
7778

7879
long ownerId = block.OwnerId;
79-
if (block.OwnerId == 0)
80+
if (block.OwnerId == 0 && block.IDModule != null) // IDModule shouldn't be null on a terminal block but it has happened.
8081
ownerId = block.IDModule.Owner;
8182

8283
if (ownerId != 0)
@@ -95,26 +96,35 @@ private void RemoveBlock(MyCubeBlock block)
9596

9697
private void AddBlock(MyTerminalBlock block)
9798
{
98-
if (!Blocks.TryGetValue(block.BlockDefinition, out var group))
99-
group = Blocks[block.BlockDefinition] = new MtObservableSortedDictionary<long, BlockViewModel>();
100-
group.Add(block.EntityId, new BlockViewModel(block, Tree));
99+
try
100+
{
101+
if (!Blocks.TryGetValue(block.BlockDefinition, out var group))
102+
group = Blocks[block.BlockDefinition] = new MtObservableSortedDictionary<long, BlockViewModel>();
103+
group.Add(block.EntityId, new BlockViewModel(block, Tree));
101104

102-
long ownerId = block.OwnerId;
103-
if (block.OwnerId == 0)
104-
ownerId = block.IDModule.Owner;
105+
long ownerId = block.OwnerId;
106+
if (block.OwnerId == 0 && block.IDModule != null)
107+
ownerId = block.IDModule.Owner;
105108

106-
if (ownerId != 0)
107-
{
108-
var playerIdent = MySession.Static.Players.TryGetIdentity(ownerId);
109-
if (playerIdent != null)
109+
if (ownerId != 0)
110110
{
111-
var name = playerIdent.DisplayName;
112-
if (!Owners.TryGetValue(name, out int count))
113-
Owners[name] = 1;
114-
else
115-
Owners[name] = count + 1;
111+
var playerIdent = MySession.Static.Players.TryGetIdentity(ownerId);
112+
if (playerIdent != null)
113+
{
114+
var name = playerIdent.DisplayName;
115+
if (!Owners.TryGetValue(name, out int count))
116+
Owners[name] = 1;
117+
else
118+
Owners[name] = count + 1;
119+
}
116120
}
117121
}
122+
catch (Exception)
123+
{
124+
// Used MtObservableSortedDictionary for Owners collection, which threw errors on some blocks.
125+
// Switched to ObservableDictionary and left the try/catch just in case...
126+
}
127+
118128
}
119129

120130
public int BlockCount
@@ -127,7 +137,7 @@ public int BlockCount
127137
}
128138
}
129139

130-
public MtObservableSortedDictionary<string,int> Owners { get; } = new MtObservableSortedDictionary<string, int>();
140+
public ObservableDictionary<string,int> Owners { get; } = new ObservableDictionary<string, int>();
131141

132142
private void Grid_OnBlockAdded(MySlimBlock obj)
133143
{

‎Torch.Server/Views/Entities/GridView.xaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
<TextBox Text="{Binding BlockCount, Mode=OneWay}" Margin="3" />
5050
</StackPanel>
5151
<StackPanel Grid.Row="7" Grid.ColumnSpan="2" Orientation="Horizontal">
52-
<TextBlock Text="Owners and Blocks" Width="125" ToolTip="Expand the blocklist on the grid entry to populate."/>
52+
<TextBlock Text="Owners and Blocks" Width="125" ToolTip="Expand the blocklist on the grid entry to populate." Margin="3"/>
5353

5454
<DataGrid x:Name="DictGrid" AutoGenerateColumns="False" CanUserAddRows="False"
55-
IsReadOnly="True" ItemsSource="{Binding Owners}"
55+
IsReadOnly="True" ItemsSource="{Binding Owners, Mode=OneWay}"
5656
Margin="3" ToolTip="Expand the blocklist on the grid entry to populate.">
5757
<DataGrid.Columns>
5858
<DataGridTextColumn Header="Name" Binding="{Binding Key, Mode=OneWay}"/>

0 commit comments

Comments
 (0)