Skip to content

Commit c55ed11

Browse files
authored
Modernize concurrent collections and UI bindings (#628)
1 parent 4fd5dfa commit c55ed11

35 files changed

Lines changed: 1727 additions & 132 deletions

‎Torch.API/Torch.API.csproj‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
<HintPath>..\GameBinaries\HavokWrapper.dll</HintPath>
4545
<Private>False</Private>
4646
</Reference>
47-
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
48-
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
49-
<Private>True</Private>
47+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
5049
</Reference>
5150
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
5251
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>

‎Torch.API/packages.config‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Mono.TextTransform" version="1.0.0" targetFramework="net461" />
4-
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
4+
<package id="Newtonsoft.Json" version="13.0.4" targetFramework="net48" />
55
<package id="NLog" version="4.4.12" targetFramework="net461" />
66
</packages>

‎Torch.Server/Managers/EntityControlManager.cs‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Reflection;
53
using System.Runtime.CompilerServices;
6-
using System.Text;
7-
using System.Threading;
8-
using System.Threading.Tasks;
94
using System.Windows.Controls;
105
using NLog;
11-
using NLog.Fluent;
126
using Torch.API;
137
using Torch.Collections;
8+
using Torch.Collections.Concurrent;
149
using Torch.Managers;
1510
using Torch.Server.ViewModels.Entities;
1611
using Torch.Utils;
@@ -87,8 +82,8 @@ protected override EntityControlViewModel Create(EntityViewModel evm)
8782
private readonly List<ModelFactory> _modelFactories = new List<ModelFactory>();
8883
private readonly List<Delegate> _controlFactories = new List<Delegate>();
8984

90-
private readonly List<WeakReference<EntityViewModel>> _boundEntityViewModels = new List<WeakReference<EntityViewModel>>();
91-
private readonly ConditionalWeakTable<EntityViewModel, MtObservableList<EntityControlViewModel>> _boundViewModels = new ConditionalWeakTable<EntityViewModel, MtObservableList<EntityControlViewModel>>();
85+
private readonly ObservableConcurrentList<WeakReference<EntityViewModel>> _boundEntityViewModels = new ObservableConcurrentList<WeakReference<EntityViewModel>>();
86+
private readonly ConditionalWeakTable<EntityViewModel, ObservableConcurrentList<EntityControlViewModel>> _boundViewModels = new ConditionalWeakTable<EntityViewModel, ObservableConcurrentList<EntityControlViewModel>>();
9287

9388
/// <summary>
9489
/// This factory will be used to create component models for matching entity models.
@@ -109,14 +104,14 @@ public void RegisterModelFactory<TEntityBaseModel>(Func<TEntityBaseModel, Entity
109104
while (i < _boundEntityViewModels.Count)
110105
{
111106
if (_boundEntityViewModels[i].TryGetTarget(out EntityViewModel target) &&
112-
_boundViewModels.TryGetValue(target, out MtObservableList<EntityControlViewModel> components))
107+
_boundViewModels.TryGetValue(target, out ObservableConcurrentList<EntityControlViewModel> components))
113108
{
114109
if (target is TEntityBaseModel tent)
115110
UpdateBinding(target, components);
116111
i++;
117112
}
118113
else
119-
_boundEntityViewModels.RemoveAtFast(i);
114+
_boundEntityViewModels.RemoveAt(i);
120115
}
121116
}
122117
}
@@ -190,15 +185,15 @@ private void RefreshControls<TEntityComponentModel>() where TEntityComponentMode
190185
while (i < _boundEntityViewModels.Count)
191186
{
192187
if (_boundEntityViewModels[i].TryGetTarget(out EntityViewModel target) &&
193-
_boundViewModels.TryGetValue(target, out MtObservableList<EntityControlViewModel> components))
188+
_boundViewModels.TryGetValue(target, out ObservableConcurrentList<EntityControlViewModel> components))
194189
{
195190
foreach (EntityControlViewModel component in components)
196191
if (component is TEntityComponentModel)
197192
component.InvalidateControl();
198193
i++;
199194
}
200195
else
201-
_boundEntityViewModels.RemoveAtFast(i);
196+
_boundEntityViewModels.RemoveAt(i);
202197
}
203198
}
204199

@@ -207,7 +202,7 @@ private void RefreshControls<TEntityComponentModel>() where TEntityComponentMode
207202
/// </summary>
208203
/// <param name="entity">view model to query</param>
209204
/// <returns></returns>
210-
public MtObservableList<EntityControlViewModel> BoundModels(EntityViewModel entity)
205+
public ObservableConcurrentList<EntityControlViewModel> BoundModels(EntityViewModel entity)
211206
{
212207
return _boundViewModels.GetValue(entity, CreateFreshBinding);
213208
}
@@ -231,9 +226,9 @@ public Control CreateControl(EntityControlViewModel model)
231226
return null;
232227
}
233228

234-
private MtObservableList<EntityControlViewModel> CreateFreshBinding(EntityViewModel key)
229+
private ObservableConcurrentList<EntityControlViewModel> CreateFreshBinding(EntityViewModel key)
235230
{
236-
var binding = new MtObservableList<EntityControlViewModel>();
231+
var binding = new ObservableConcurrentList<EntityControlViewModel>();
237232
lock (this)
238233
{
239234
_boundEntityViewModels.Add(new WeakReference<EntityViewModel>(key));
@@ -246,7 +241,7 @@ private MtObservableList<EntityControlViewModel> CreateFreshBinding(EntityViewMo
246241
return binding;
247242
}
248243

249-
private void UpdateBinding(EntityViewModel key, MtObservableList<EntityControlViewModel> binding)
244+
private void UpdateBinding(EntityViewModel key, ObservableConcurrentList<EntityControlViewModel> binding)
250245
{
251246
if (!binding.IsObserved)
252247
return;

‎Torch.Server/Managers/InstanceManager.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Sandbox;
77
using Sandbox.Engine.Utils;
88
using Torch.API;
9-
using Torch.Collections;
9+
using Torch.Collections.Concurrent;
1010
using Torch.Managers;
1111
using Torch.Mod;
1212
using Torch.Server.ViewModels;
@@ -131,7 +131,8 @@ public void ImportSelectedWorldConfig()
131131

132132
private void ImportWorldConfig(WorldViewModel world, bool modsOnly = true)
133133
{
134-
var mods = new MtObservableList<ModItemInfo>();
134+
135+
var mods = new ObservableConcurrentList<ModItemInfo>();
135136
foreach (var mod in world.WorldConfiguration.Mods)
136137
mods.Add(new ModItemInfo(mod));
137138
DedicatedConfig.Mods = mods;
@@ -189,7 +190,7 @@ private void ImportWorldConfig(bool modsOnly = true)
189190
return;
190191
}
191192

192-
var mods = new MtObservableList<ModItemInfo>();
193+
var mods = new ObservableConcurrentList<ModItemInfo>();
193194
foreach (var mod in checkpoint.Mods)
194195
mods.Add(new ModItemInfo(mod));
195196
DedicatedConfig.Mods = mods;

‎Torch.Server/Torch.Server.csproj‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@
9292
<Reference Include="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
9393
<HintPath>..\GameBinaries\netstandard.dll</HintPath>
9494
</Reference>
95-
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
96-
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
97-
<Private>True</Private>
95+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
96+
<HintPath>..\packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
9897
</Reference>
9998
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
10099
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>

‎Torch.Server/ViewModels/ConfigDedicatedViewModel.cs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
43
using System.Linq;
5-
using System.Text;
64
using System.Threading.Tasks;
75
using NLog;
86
using Sandbox.Engine.Utils;
9-
using Torch.Collections;
107
using Torch.Server.Managers;
118
using VRage.Game;
12-
using VRage.Game.ModAPI;
139
using Torch.Utils.SteamWorkshopTools;
14-
using Torch.Collections;
10+
using Torch.Collections.Concurrent;
1511

1612
namespace Torch.Server.ViewModels
1713
{
@@ -64,7 +60,7 @@ public bool Validate()
6460
private SessionSettingsViewModel _sessionSettings;
6561
public SessionSettingsViewModel SessionSettings { get => _sessionSettings; set { _sessionSettings = value; OnPropertyChanged(); } }
6662

67-
public MtObservableList<WorldViewModel> Worlds { get; } = new MtObservableList<WorldViewModel>();
63+
public ObservableConcurrentList<WorldViewModel> Worlds { get; } = new ObservableConcurrentList<WorldViewModel>();
6864
private WorldViewModel _selectedWorld;
6965
public WorldViewModel SelectedWorld
7066
{
@@ -118,8 +114,8 @@ public async Task UpdateAllModInfosAsync(Action<string> messageHandler = null)
118114

119115
public List<ulong> Banned { get => _config.Banned; set => SetValue(x => _config.Banned = x, value); }
120116

121-
private MtObservableList<ModItemInfo> _mods = new MtObservableList<ModItemInfo>();
122-
public MtObservableList<ModItemInfo> Mods
117+
private ObservableConcurrentList<ModItemInfo> _mods = new ObservableConcurrentList<ModItemInfo>();
118+
public ObservableConcurrentList<ModItemInfo> Mods
123119
{
124120
get => _mods;
125121
set

‎Torch.Server/ViewModels/Entities/Blocks/BlockViewModel.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Sandbox.ModAPI;
1111
using Sandbox.ModAPI.Interfaces;
1212
using Torch.Collections;
13+
using Torch.Collections.Concurrent;
1314
using Torch.Server.ViewModels.Entities;
1415
using VRage.Game.ModAPI;
1516

@@ -18,7 +19,7 @@ namespace Torch.Server.ViewModels.Blocks
1819
public class BlockViewModel : EntityViewModel
1920
{
2021
public IMyTerminalBlock Block => (IMyTerminalBlock) Entity;
21-
public MtObservableList<PropertyViewModel> Properties { get; } = new MtObservableList<PropertyViewModel>();
22+
public ObservableConcurrentList<PropertyViewModel> Properties { get; } = new ObservableConcurrentList<PropertyViewModel>();
2223

2324
public string FullName => $"{Block?.CubeGrid.CustomName} - {Block?.CustomName}";
2425

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NLog;
44
using Sandbox.Game.Entities;
55
using Torch.API.Managers;
6-
using Torch.Collections;
6+
using Torch.Collections.Concurrent;
77
using Torch.Server.Managers;
88
using Torch.Utils;
99
using VRage.ModAPI;
@@ -33,7 +33,7 @@ private set
3333

3434
public long Id => Entity?.EntityId ?? 0; // Throws null then gives entity id
3535

36-
public MtObservableList<EntityControlViewModel> EntityControls { get; private set; }
36+
public ObservableConcurrentList<EntityControlViewModel> EntityControls { get; private set; }
3737

3838
public virtual string Name
3939
{

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Sandbox.Game.Entities.Cube;
77
using Sandbox.Game.World;
88
using SharpDX.Toolkit.Collections;
9-
using Torch.Collections;
9+
using Torch.Collections.Concurrent;
1010
using Torch.Server.ViewModels.Blocks;
1111
using VRage.Game;
1212

@@ -45,10 +45,9 @@ public int Compare(MyCubeBlockDefinition x, MyCubeBlockDefinition y)
4545

4646
private MyCubeGrid Grid => (MyCubeGrid) Entity;
4747

48-
public MtObservableSortedDictionary<MyCubeBlockDefinition, MtObservableSortedDictionary<long, BlockViewModel>>
48+
public ObservableConcurrentDictionary<MyCubeBlockDefinition, ObservableConcurrentDictionary<long, BlockViewModel>>
4949
Blocks { get; } =
50-
new MtObservableSortedDictionary<MyCubeBlockDefinition, MtObservableSortedDictionary<long, BlockViewModel>>(
51-
CubeBlockDefinitionComparer.Default);
50+
new ObservableConcurrentDictionary<MyCubeBlockDefinition, ObservableConcurrentDictionary<long, BlockViewModel>>();
5251

5352
public GridViewModel()
5453
{
@@ -58,7 +57,7 @@ public GridViewModel()
5857
public GridViewModel(MyCubeGrid grid, EntityTreeViewModel tree) : base(grid, tree)
5958
{
6059
//DescriptiveName = $"{grid.DisplayName} ({grid.BlocksCount} blocks)";
61-
Blocks.Add(_fillerDefinition, new MtObservableSortedDictionary<long, BlockViewModel>());
60+
Blocks.Add(_fillerDefinition, new ObservableConcurrentDictionary<long, BlockViewModel>());
6261
}
6362

6463
private void Grid_OnBlockRemoved(MySlimBlock obj)
@@ -99,7 +98,7 @@ private void AddBlock(MyTerminalBlock block)
9998
try
10099
{
101100
if (!Blocks.TryGetValue(block.BlockDefinition, out var group))
102-
group = Blocks[block.BlockDefinition] = new MtObservableSortedDictionary<long, BlockViewModel>();
101+
group = Blocks[block.BlockDefinition] = new ObservableConcurrentDictionary<long, BlockViewModel>();
103102
group.Add(block.EntityId, new BlockViewModel(block, Tree));
104103

105104
long ownerId = block.OwnerId;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using VRage.Game.ModAPI;
66
using System.Threading.Tasks;
77
using Torch.Collections;
8+
using Torch.Collections.Concurrent;
89

910
namespace Torch.Server.ViewModels.Entities
1011
{
@@ -16,7 +17,7 @@ public class VoxelMapViewModel : EntityViewModel
1617

1718
public override bool CanStop => false;
1819

19-
public MtObservableList<GridViewModel> AttachedGrids { get; } = new MtObservableList<GridViewModel>();
20+
public ObservableConcurrentList<GridViewModel> AttachedGrids { get; } = new ObservableConcurrentList<GridViewModel>();
2021

2122
public async Task UpdateAttachedGrids()
2223
{

0 commit comments

Comments
 (0)