Skip to content

Commit cee8d42

Browse files
authored
Improve MergeImplicitData exception tolerance (#5075)
1 parent 3330d96 commit cee8d42

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

‎Robust.Client/GameStates/ClientGameStateManager.cs‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public sealed class ClientGameStateManager : IClientGameStateManager
125125
#endif
126126

127127
private bool _resettingPredictedEntities;
128+
private readonly List<EntityUid> _brokenEnts = new();
129+
private readonly List<(EntityUid, NetEntity)> _toStart = new();
128130

129131
/// <inheritdoc />
130132
public void Initialize()
@@ -667,7 +669,16 @@ private void MergeImplicitData(IEnumerable<NetEntity> createdEntities)
667669

668670
foreach (var netEntity in createdEntities)
669671
{
672+
#if EXCEPTION_TOLERANCE
673+
if (!_entityManager.TryGetEntityData(netEntity, out _, out var meta))
674+
{
675+
_sawmill.Error($"Encountered deleted entity while merging implicit data! NetEntity: {netEntity}");
676+
continue;
677+
}
678+
#else
670679
var (_, meta) = _entityManager.GetEntityData(netEntity);
680+
#endif
681+
671682
var compData = _compDataPool.Get();
672683
_outputData.Add(netEntity, compData);
673684

@@ -1157,63 +1168,58 @@ private void Detach(GameTick maxTick,
11571168

11581169
private void InitializeAndStart(Dictionary<NetEntity, EntityState> toCreate)
11591170
{
1160-
var metaQuery = _entityManager.GetEntityQuery<MetaDataComponent>();
1171+
_toStart.Clear();
11611172

1162-
#if EXCEPTION_TOLERANCE
1163-
var brokenEnts = new List<EntityUid>();
1164-
#endif
11651173
using (_prof.Group("Initialize Entity"))
11661174
{
1175+
EntityUid entity = default;
11671176
foreach (var netEntity in toCreate.Keys)
11681177
{
1169-
var entity = _entityManager.GetEntity(netEntity);
1170-
#if EXCEPTION_TOLERANCE
11711178
try
11721179
{
1173-
#endif
1174-
_entities.InitializeEntity(entity, metaQuery.GetComponent(entity));
1175-
#if EXCEPTION_TOLERANCE
1180+
(entity, var meta) = _entityManager.GetEntityData(netEntity);
1181+
_entities.InitializeEntity(entity, meta);
1182+
_toStart.Add((entity, netEntity));
11761183
}
11771184
catch (Exception e)
11781185
{
1179-
_sawmill.Error($"Server entity threw in Init: ent={_entities.ToPrettyString(entity)}");
1186+
_sawmill.Error($"Server entity threw in Init: nent={netEntity}, ent={_entities.ToPrettyString(entity)}");
11801187
_runtimeLog.LogException(e, $"{nameof(ClientGameStateManager)}.{nameof(InitializeAndStart)}");
1181-
brokenEnts.Add(entity);
1182-
toCreate.Remove(netEntity);
1183-
}
1188+
_toCreate.Remove(netEntity);
1189+
_brokenEnts.Add(entity);
1190+
#if !EXCEPTION_TOLERANCE
1191+
throw;
11841192
#endif
1193+
}
11851194
}
11861195
}
11871196

11881197
using (_prof.Group("Start Entity"))
11891198
{
1190-
foreach (var netEntity in toCreate.Keys)
1199+
foreach (var (entity, netEntity) in _toStart)
11911200
{
1192-
var entity = _entityManager.GetEntity(netEntity);
1193-
#if EXCEPTION_TOLERANCE
11941201
try
11951202
{
1196-
#endif
1197-
_entities.StartEntity(entity);
1198-
#if EXCEPTION_TOLERANCE
1203+
_entities.StartEntity(entity);
11991204
}
12001205
catch (Exception e)
12011206
{
1202-
_sawmill.Error($"Server entity threw in Start: ent={_entityManager.ToPrettyString(entity)}");
1207+
_sawmill.Error($"Server entity threw in Start: nent={netEntity}, ent={_entityManager.ToPrettyString(entity)}");
12031208
_runtimeLog.LogException(e, $"{nameof(ClientGameStateManager)}.{nameof(InitializeAndStart)}");
1204-
brokenEnts.Add(entity);
1205-
toCreate.Remove(netEntity);
1206-
}
1209+
_toCreate.Remove(netEntity);
1210+
_brokenEnts.Add(entity);
1211+
#if !EXCEPTION_TOLERANCE
1212+
throw;
12071213
#endif
1214+
}
12081215
}
12091216
}
12101217

1211-
#if EXCEPTION_TOLERANCE
1212-
foreach (var entity in brokenEnts)
1218+
foreach (var entity in _brokenEnts)
12131219
{
12141220
_entityManager.DeleteEntity(entity);
12151221
}
1216-
#endif
1222+
_brokenEnts.Clear();
12171223
}
12181224

12191225
private void HandleEntityState(EntityUid uid, NetEntity netEntity, MetaDataComponent meta, IEventBus bus, EntityState? curState,

0 commit comments

Comments
 (0)