Content update for NetEntities (#18935)

This commit is contained in:
metalgearsloth
2023-09-11 09:42:41 +10:00
committed by GitHub
parent 389c8d1a2c
commit 5a0fc68be2
526 changed files with 3058 additions and 2215 deletions

View File

@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
@@ -14,7 +13,6 @@ using Robust.Server.Player;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Threading;
using Robust.Shared.Timing;
@@ -34,8 +32,8 @@ namespace Content.Server.Decals
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
private readonly Dictionary<EntityUid, HashSet<Vector2i>> _dirtyChunks = new();
private readonly Dictionary<IPlayerSession, Dictionary<EntityUid, HashSet<Vector2i>>> _previousSentChunks = new();
private readonly Dictionary<NetEntity, HashSet<Vector2i>> _dirtyChunks = new();
private readonly Dictionary<IPlayerSession, Dictionary<NetEntity, HashSet<Vector2i>>> _previousSentChunks = new();
private static readonly Vector2 _boundsMinExpansion = new(0.01f, 0.01f);
private static readonly Vector2 _boundsMaxExpansion = new(1.01f, 1.01f);
@@ -44,9 +42,9 @@ namespace Content.Server.Decals
new DefaultObjectPool<HashSet<Vector2i>>(
new DefaultPooledObjectPolicy<HashSet<Vector2i>>(), 64);
private ObjectPool<Dictionary<EntityUid, HashSet<Vector2i>>> _chunkViewerPool =
new DefaultObjectPool<Dictionary<EntityUid, HashSet<Vector2i>>>(
new DefaultPooledObjectPolicy<Dictionary<EntityUid, HashSet<Vector2i>>>(), 64);
private ObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>> _chunkViewerPool =
new DefaultObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>>(
new DefaultPooledObjectPolicy<Dictionary<NetEntity, HashSet<Vector2i>>>(), 64);
public override void Initialize()
{
@@ -203,10 +201,12 @@ namespace Content.Server.Decals
if (!_adminManager.HasAdminFlag(session, AdminFlags.Spawn))
return;
if (!ev.Coordinates.IsValid(EntityManager))
var coordinates = GetCoordinates(ev.Coordinates);
if (!coordinates.IsValid(EntityManager))
return;
if (!TryAddDecal(ev.Decal, ev.Coordinates, out _))
if (!TryAddDecal(ev.Decal, coordinates, out _))
return;
if (eventArgs.SenderSession.AttachedEntity != null)
@@ -230,10 +230,12 @@ namespace Content.Server.Decals
if (!_adminManager.HasAdminFlag(session, AdminFlags.Spawn))
return;
if (!ev.Coordinates.IsValid(EntityManager))
var coordinates = GetCoordinates(ev.Coordinates);
if (!coordinates.IsValid(EntityManager))
return;
var gridId = ev.Coordinates.GetGridUid(EntityManager);
var gridId = coordinates.GetGridUid(EntityManager);
if (gridId == null)
return;
@@ -256,8 +258,9 @@ namespace Content.Server.Decals
}
}
protected override void DirtyChunk(EntityUid id, Vector2i chunkIndices, DecalChunk chunk)
protected override void DirtyChunk(EntityUid uid, Vector2i chunkIndices, DecalChunk chunk)
{
var id = GetNetEntity(uid);
chunk.LastModified = _timing.CurTick;
if(!_dirtyChunks.ContainsKey(id))
_dirtyChunks[id] = new HashSet<Vector2i>();
@@ -409,8 +412,8 @@ namespace Content.Server.Decals
foreach (var ent in _dirtyChunks.Keys)
{
if (TryComp(ent, out DecalGridComponent? decals))
Dirty(decals);
if (TryGetEntity(ent, out var uid) && TryComp(uid, out DecalGridComponent? decals))
Dirty(uid.Value, decals);
}
if (!PvsEnabled)
@@ -431,8 +434,7 @@ namespace Content.Server.Decals
public void UpdatePlayer(IPlayerSession player)
{
var xformQuery = GetEntityQuery<TransformComponent>();
var chunksInRange = _chunking.GetChunksForSession(player, ChunkSize, xformQuery, _chunkIndexPool, _chunkViewerPool);
var chunksInRange = _chunking.GetChunksForSession(player, ChunkSize, _chunkIndexPool, _chunkViewerPool);
var staleChunks = _chunkViewerPool.Get();
var previouslySent = _previousSentChunks[player];
@@ -440,16 +442,16 @@ namespace Content.Server.Decals
// Then, remove them from previousSentChunks (for stuff like grids out of range)
// and also mark them as stale for networking.
foreach (var (gridId, oldIndices) in previouslySent)
foreach (var (netGrid, oldIndices) in previouslySent)
{
// Mark the whole grid as stale and flag for removal.
if (!chunksInRange.TryGetValue(gridId, out var chunks))
if (!chunksInRange.TryGetValue(netGrid, out var chunks))
{
previouslySent.Remove(gridId);
previouslySent.Remove(netGrid);
// Was the grid deleted?
if (MapManager.IsGrid(gridId))
staleChunks[gridId] = oldIndices;
if (!TryGetEntity(netGrid, out var gridId) || !MapManager.IsGrid(gridId.Value))
staleChunks[netGrid] = oldIndices;
else
{
// If grid was deleted then don't worry about telling the client to delete the chunk.
@@ -465,7 +467,9 @@ namespace Content.Server.Decals
// Get individual stale chunks.
foreach (var chunk in oldIndices)
{
if (chunks.Contains(chunk)) continue;
if (chunks.Contains(chunk))
continue;
elmo.Add(chunk);
}
@@ -475,16 +479,16 @@ namespace Content.Server.Decals
continue;
}
staleChunks.Add(gridId, elmo);
staleChunks.Add(netGrid, elmo);
}
var updatedChunks = _chunkViewerPool.Get();
foreach (var (gridId, gridChunks) in chunksInRange)
foreach (var (netGrid, gridChunks) in chunksInRange)
{
var newChunks = _chunkIndexPool.Get();
_dirtyChunks.TryGetValue(gridId, out var dirtyChunks);
_dirtyChunks.TryGetValue(netGrid, out var dirtyChunks);
if (!previouslySent.TryGetValue(gridId, out var previousChunks))
if (!previouslySent.TryGetValue(netGrid, out var previousChunks))
newChunks.UnionWith(gridChunks);
else
{
@@ -498,19 +502,19 @@ namespace Content.Server.Decals
_chunkIndexPool.Return(previousChunks);
}
previouslySent[gridId] = gridChunks;
previouslySent[netGrid] = gridChunks;
if (newChunks.Count == 0)
_chunkIndexPool.Return(newChunks);
else
updatedChunks[gridId] = newChunks;
updatedChunks[netGrid] = newChunks;
}
//send all gridChunks to client
SendChunkUpdates(player, updatedChunks, staleChunks);
}
private void ReturnToPool(Dictionary<EntityUid, HashSet<Vector2i>> chunks)
private void ReturnToPool(Dictionary<NetEntity, HashSet<Vector2i>> chunks)
{
foreach (var (_, previous) in chunks)
{
@@ -524,12 +528,14 @@ namespace Content.Server.Decals
private void SendChunkUpdates(
IPlayerSession session,
Dictionary<EntityUid, HashSet<Vector2i>> updatedChunks,
Dictionary<EntityUid, HashSet<Vector2i>> staleChunks)
Dictionary<NetEntity, HashSet<Vector2i>> updatedChunks,
Dictionary<NetEntity, HashSet<Vector2i>> staleChunks)
{
var updatedDecals = new Dictionary<EntityUid, Dictionary<Vector2i, DecalChunk>>();
foreach (var (gridId, chunks) in updatedChunks)
var updatedDecals = new Dictionary<NetEntity, Dictionary<Vector2i, DecalChunk>>();
foreach (var (netGrid, chunks) in updatedChunks)
{
var gridId = GetEntity(netGrid);
var collection = ChunkCollection(gridId);
if (collection == null)
continue;
@@ -542,7 +548,7 @@ namespace Content.Server.Decals
? chunk
: new());
}
updatedDecals[gridId] = gridChunks;
updatedDecals[netGrid] = gridChunks;
}
if (updatedDecals.Count != 0 || staleChunks.Count != 0)