Fix showrays networking (#4701)

This commit is contained in:
Leon Friedrich
2023-12-12 04:17:36 -05:00
committed by GitHub
parent bfe8e687da
commit dc7a51e582
8 changed files with 73 additions and 71 deletions

View File

@@ -198,6 +198,7 @@ namespace Robust.Client.Console.Commands
}
}
#if DEBUG
internal sealed class ShowRayCommand : LocalizedCommands
{
[Dependency] private readonly IEntitySystemManager _entitySystems = default!;
@@ -224,6 +225,7 @@ namespace Robust.Client.Console.Commands
mgr.DebugRayLifetime = TimeSpan.FromSeconds(duration);
}
}
#endif
internal sealed class DisconnectCommand : LocalizedCommands
{

View File

@@ -4,18 +4,17 @@ using System.Numerics;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Shared.Debugging;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Physics;
using Robust.Shared.Timing;
namespace Robust.Client.Debugging
{
internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem
{
#if DEBUG
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IGameTiming _gameTimer = default!;
@@ -28,6 +27,8 @@ namespace Robust.Client.Debugging
public Vector2 RayHit;
public TimeSpan LifeTime;
public bool DidActuallyHit;
public bool Server;
public MapId Map;
}
public bool DebugDrawRays
@@ -73,7 +74,8 @@ namespace Robust.Client.Debugging
DidActuallyHit = ev.Results != null,
RayOrigin = ev.Ray.Position,
RayHit = ev.Results?.HitPos ?? ev.Ray.Direction * ev.MaxLength + ev.Ray.Position,
LifeTime = _gameTimer.RealTime + DebugRayLifetime
LifeTime = _gameTimer.RealTime + DebugRayLifetime,
Map = ev.Map
};
_raysWithLifeTime.Add(newRayWithLifetime);
@@ -93,7 +95,9 @@ namespace Robust.Client.Debugging
DidActuallyHit = msg.DidHit,
RayOrigin = msg.RayOrigin,
RayHit = msg.RayHit,
LifeTime = _gameTimer.RealTime + DebugRayLifetime
LifeTime = _gameTimer.RealTime + DebugRayLifetime,
Server = true,
Map = msg.Map
};
_raysWithLifeTime.Add(newRayWithLifetime);
@@ -114,10 +118,20 @@ namespace Robust.Client.Debugging
var handle = args.WorldHandle;
foreach (var ray in _owner._raysWithLifeTime)
{
if (args.MapId != ray.Map)
continue;
Color color;
if (ray.Server)
color = ray.DidActuallyHit ? Color.Cyan : Color.Orange;
else
color = ray.DidActuallyHit ? Color.Blue : Color.Red;
handle.DrawLine(
ray.RayOrigin,
ray.RayHit,
ray.DidActuallyHit ? Color.Yellow : Color.Magenta);
color
);
}
}
@@ -128,5 +142,6 @@ namespace Robust.Client.Debugging
_owner._raysWithLifeTime.RemoveAll(r => r.LifeTime < _owner._gameTimer.RealTime);
}
}
#endif
}
}

View File

@@ -1,8 +1,5 @@
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using Robust.Shared.Physics;
using Robust.Shared.Debugging;
namespace Robust.Server.Debugging;
@@ -10,11 +7,11 @@ namespace Robust.Server.Debugging;
[UsedImplicitly]
internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem
{
#if DEBUG
protected override void ReceiveLocalRayAtMainThread(DebugRayData data)
{
// This code won't be called on release - eliminate it anyway for good measure.
#if DEBUG
var msg = new MsgRay {RayOrigin = data.Ray.Position};
var msg = new MsgRay {RayOrigin = data.Ray.Position, Map = data.Map};
if (data.Results != null)
{
msg.DidHit = true;
@@ -25,8 +22,7 @@ internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem
msg.RayHit = data.Ray.Position + data.Ray.Direction * data.MaxLength;
}
EntityManager.EventBus.RaiseEvent(EventSource.Network, msg);
#endif
RaiseNetworkEvent(msg);
}
#endif
}

View File

@@ -1,36 +1,38 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Timing;
namespace Robust.Shared.Debugging;
[Virtual]
public abstract class SharedDebugRayDrawingSystem : EntitySystem
{
#if DEBUG
private ConcurrentBag<DebugRayData> _rayDataThreadShuntBag = new();
#endif
public override void FrameUpdate(float frameTime)
{
#if DEBUG
// Pull rays into main thread for distribution.
Process();
}
public override void Update(float frameTime)
{
Process();
}
private void Process()
{
if (_rayDataThreadShuntBag.Count == 0)
return;
var arr = _rayDataThreadShuntBag.ToArray();
_rayDataThreadShuntBag.Clear();
foreach (var drd in arr)
{
ReceiveLocalRayAtMainThread(drd);
}
#endif
}
/// <summary>
@@ -42,9 +44,7 @@ public abstract class SharedDebugRayDrawingSystem : EntitySystem
{
// If not on DEBUG, we're not going to use this anyway.
// Let the inlining DCE this away.
#if DEBUG
_rayDataThreadShuntBag.Add(drd);
#endif
}
/// <summary>
@@ -53,5 +53,7 @@ public abstract class SharedDebugRayDrawingSystem : EntitySystem
/// Note that on release builds (!DEBUG), this function is never called.
/// </summary>
protected abstract void ReceiveLocalRayAtMainThread(DebugRayData drd);
}
public readonly record struct DebugRayData(Ray Ray, float MaxLength, RayCastResults? Results, bool ServerSide, MapId Map);
#endif
}

View File

@@ -1,16 +1,19 @@
using System.Numerics;
using Lidgren.Network;
using System;
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
#nullable disable
namespace Robust.Shared.Network.Messages;
namespace Robust.Shared.Network.Messages
/// <summary>
/// Debug message contain physics collision ray data.
/// </summary>
[Serializable, NetSerializable]
public sealed class MsgRay : EntityEventArgs
{
public sealed class MsgRay : EntityEventArgs
{
public Vector2 RayOrigin { get; set; }
public Vector2 RayHit { get; set; }
public bool DidHit { get; set; }
}
public Vector2 RayOrigin;
public Vector2 RayHit;
public bool DidHit;
public MapId Map;
}

View File

@@ -1,25 +0,0 @@
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Robust.Shared.Maths;
namespace Robust.Shared.Physics
{
public struct DebugRayData
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DebugRayData(Ray ray, float maxLength, [CanBeNull] RayCastResults? results)
{
Ray = ray;
MaxLength = maxLength;
Results = results;
}
public Ray Ray
{
get;
}
public RayCastResults? Results { get; }
public float MaxLength { get; }
}
}

View File

@@ -1,6 +1,5 @@
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
namespace Robust.Shared.Physics
{

View File

@@ -6,6 +6,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Physics.Collision;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
@@ -20,6 +21,7 @@ namespace Robust.Shared.Physics.Systems
public partial class SharedPhysicsSystem
{
[Dependency] private readonly SharedDebugRayDrawingSystem _sharedDebugRaySystem = default!;
[Dependency] private readonly INetManager _netMan = default!;
/// <summary>
/// Checks to see if the specified collision rectangle collides with any of the physBodies under management.
@@ -300,7 +302,9 @@ namespace Robust.Shared.Physics.Systems
// Need to convert it back to world-space.
var result = new RayCastResults(distFromOrigin, matrix.Transform(point), proxy.Entity);
results.Add(result);
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, result));
#if DEBUG
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, result, _netMan.IsServer, mapId));
#endif
return true;
}, gridRay);
@@ -326,15 +330,19 @@ namespace Robust.Shared.Physics.Systems
// Need to convert it back to world-space.
var result = new RayCastResults(distFromOrigin, matrix.Transform(point), proxy.Entity);
results.Add(result);
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, result));
#if DEBUG
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, result, _netMan.IsServer, mapId));
#endif
return true;
}, gridRay);
}
#if DEBUG
if (results.Count == 0)
{
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, null));
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, null, _netMan.IsServer, mapId));
}
#endif
results.Sort((a, b) => a.Distance.CompareTo(b.Distance));
return results;
@@ -423,7 +431,9 @@ namespace Robust.Shared.Physics.Systems
}
// This hid rays that didn't penetrate something. Don't hide those because that causes rays to disappear that shouldn't.
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, null));
#if DEBUG
_sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, null, _netMan.IsServer, mapId));
#endif
return penetration;
}