From 40758ea3b7cb2e5328e2ec9fe5b8f7da5abb3632 Mon Sep 17 00:00:00 2001 From: Virx Date: Thu, 15 Aug 2024 00:10:34 -0400 Subject: [PATCH] Ensure all renders are cleared --- RLBotCS/ManagerTools/Rendering.cs | 79 ++++++++++++------- RLBotCS/Server/BridgeHandler.cs | 12 ++- RLBotCS/Server/BridgeMessage.cs | 10 +++ .../Server/FlatbuffersMessage/StartMatch.cs | 1 + .../Server/FlatbuffersMessage/StopMatch.cs | 1 + 5 files changed, 73 insertions(+), 30 deletions(-) diff --git a/RLBotCS/ManagerTools/Rendering.cs b/RLBotCS/ManagerTools/Rendering.cs index 432bf54..02b26d9 100644 --- a/RLBotCS/ManagerTools/Rendering.cs +++ b/RLBotCS/ManagerTools/Rendering.cs @@ -11,23 +11,32 @@ public class Rendering(TcpMessenger tcpMessenger) private static int MaxClearsPerTick = 1024; private readonly RenderingSender _renderingSender = new(tcpMessenger); - private readonly Dictionary>> _clientRenderTracker = []; + private readonly Dictionary< + int, + Dictionary> + > _clientRenderTracker = []; private readonly Queue _RenderClearQueue = new(); - private ushort? RenderItem(RenderTypeUnion renderItem, GameState gameState) => + private (ushort, bool)? RenderItem(RenderTypeUnion renderItem, GameState gameState) => renderItem.Value switch { Line3DT { Start: var start, End: var end, Color: var color } - => _renderingSender.AddLine3D( - FlatToModel.ToRenderAnchor(start, gameState), - FlatToModel.ToRenderAnchor(end, gameState), - FlatToModel.ToColor(color) + => ( + _renderingSender.AddLine3D( + FlatToModel.ToRenderAnchor(start, gameState), + FlatToModel.ToRenderAnchor(end, gameState), + FlatToModel.ToColor(color) + ), + true ), PolyLine3DT { Points: var points, Color: var color } - => _renderingSender.AddLine3DSeries( - points.Select(FlatToModel.ToVectorFromT).ToList(), - FlatToModel.ToColor(color) + => ( + _renderingSender.AddLine3DSeries( + points.Select(FlatToModel.ToVectorFromT).ToList(), + FlatToModel.ToColor(color) + ), + true ), String2DT { @@ -40,15 +49,18 @@ public class Rendering(TcpMessenger tcpMessenger) VAlign: var vAlign, Scale: var scale } - => _renderingSender.AddText2D( - text, - x, - y, - FlatToModel.ToColor(foreground), - FlatToModel.ToColor(background), - (byte)hAlign, - (byte)vAlign, - scale + => ( + _renderingSender.AddText2D( + text, + x, + y, + FlatToModel.ToColor(foreground), + FlatToModel.ToColor(background), + (byte)hAlign, + (byte)vAlign, + scale + ), + false ), String3DT { @@ -60,14 +72,17 @@ public class Rendering(TcpMessenger tcpMessenger) VAlign: var vAlign, Scale: var scale } - => _renderingSender.AddText3D( - text, - FlatToModel.ToRenderAnchor(anchor, gameState), - FlatToModel.ToColor(foreground), - FlatToModel.ToColor(background), - (byte)hAlign, - (byte)vAlign, - scale + => ( + _renderingSender.AddText3D( + text, + FlatToModel.ToRenderAnchor(anchor, gameState), + FlatToModel.ToColor(foreground), + FlatToModel.ToColor(background), + (byte)hAlign, + (byte)vAlign, + scale + ), + false ), _ => null }; @@ -84,7 +99,7 @@ GameState gameState // Clear the previous render group, if any RemoveRenderGroup(clientId, renderId); - List renderGroup = []; + List<(ushort, bool)> renderGroup = []; foreach (RenderMessageT renderItem in renderItems) if (RenderItem(renderItem.Variety, gameState) is { } renderItemId) renderGroup.Add(renderItemId); @@ -104,7 +119,7 @@ public void RemoveRenderGroup(int clientId, int renderId) if (!clientRenders.TryGetValue(renderId, out var renderItems)) return; - foreach (ushort renderItem in renderItems) + foreach ((ushort renderItem, _) in renderItems) _RenderClearQueue.Enqueue(renderItem); // Remove the renderId from the client @@ -118,7 +133,7 @@ public void ClearClientRenders(int clientId) // Tell the game to remove all the renders foreach (int renderId in clientRenders.Keys) - foreach (ushort renderItem in clientRenders[renderId]) + foreach ((ushort renderItem, _) in clientRenders[renderId]) _RenderClearQueue.Enqueue(renderItem); // Remove the client from the tracker @@ -130,6 +145,12 @@ public void ClearAllRenders(MatchCommandSender matchCommandSender) matchCommandSender.AddConsoleCommand("FlushPersistentDebugLines"); matchCommandSender.Send(); + foreach (var clientRenders in _clientRenderTracker.Values) + foreach (int renderId in clientRenders.Keys) + foreach ((ushort renderItem, bool isDebugLine) in clientRenders[renderId]) + if (!isDebugLine) + _RenderClearQueue.Enqueue(renderItem); + _clientRenderTracker.Clear(); } diff --git a/RLBotCS/Server/BridgeHandler.cs b/RLBotCS/Server/BridgeHandler.cs index a20712a..5897be1 100644 --- a/RLBotCS/Server/BridgeHandler.cs +++ b/RLBotCS/Server/BridgeHandler.cs @@ -79,7 +79,6 @@ private async Task HandleServer() // reset everything _context.QuickChat.ClearChats(); _context.PerfMonitor.ClearAll(); - _context.RenderingMgmt.ClearAllRenders(_context.MatchCommandSender); } else if ( _context.GameState.GameStateType != GameStateType.Replay @@ -136,6 +135,17 @@ public void Cleanup() try { _context.RenderingMgmt.ClearAllRenders(_context.MatchCommandSender); + + // we can only clear so many renders each tick + // so we do this until we've cleared them all + // or rocket league has been closed + while (!_context.RenderingMgmt.SendRenderClears()) + { + if (!messenger.WaitForAnyMessageAsync().Result) + break; + + messenger.ResetByteCount(); + } } catch (Exception e) { diff --git a/RLBotCS/Server/BridgeMessage.cs b/RLBotCS/Server/BridgeMessage.cs index 314d92f..9aef823 100644 --- a/RLBotCS/Server/BridgeMessage.cs +++ b/RLBotCS/Server/BridgeMessage.cs @@ -312,6 +312,16 @@ public void HandleMessage(BridgeContext context) } } +internal record ClearRenders() : IBridgeMessage +{ + public void HandleMessage(BridgeContext context) + { + context.QuickChat.ClearChats(); + context.PerfMonitor.ClearAll(); + context.RenderingMgmt.ClearAllRenders(context.MatchCommandSender); + } +} + internal record ShowQuickChat(MatchCommT MatchComm) : IBridgeMessage { public void HandleMessage(BridgeContext context) => diff --git a/RLBotCS/Server/FlatbuffersMessage/StartMatch.cs b/RLBotCS/Server/FlatbuffersMessage/StartMatch.cs index 0a7ed16..4ac8dfb 100644 --- a/RLBotCS/Server/FlatbuffersMessage/StartMatch.cs +++ b/RLBotCS/Server/FlatbuffersMessage/StartMatch.cs @@ -8,6 +8,7 @@ internal record StartMatch(MatchSettingsT MatchSettings) : IServerMessage public ServerAction Execute(ServerContext context) { context.LastTickPacket = null; + context.Bridge.TryWrite(new ClearRenders()); foreach (var (writer, _, _) in context.Sessions.Values) writer.TryWrite(new SessionMessage.StopMatch(false)); diff --git a/RLBotCS/Server/FlatbuffersMessage/StopMatch.cs b/RLBotCS/Server/FlatbuffersMessage/StopMatch.cs index 0398455..527a378 100644 --- a/RLBotCS/Server/FlatbuffersMessage/StopMatch.cs +++ b/RLBotCS/Server/FlatbuffersMessage/StopMatch.cs @@ -17,6 +17,7 @@ public ServerAction Execute(ServerContext context) context.FieldInfo = null; context.ShouldUpdateFieldInfo = false; context.LastTickPacket = null; + context.Bridge.TryWrite(new ClearRenders()); context.Bridge.TryWrite(new EndMatch()); foreach (var (writer, _, _) in context.Sessions.Values)