Skip to content

Commit

Permalink
Ensure all renders are cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Aug 15, 2024
1 parent d960a94 commit 40758ea
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
79 changes: 50 additions & 29 deletions RLBotCS/ManagerTools/Rendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ public class Rendering(TcpMessenger tcpMessenger)
private static int MaxClearsPerTick = 1024;

private readonly RenderingSender _renderingSender = new(tcpMessenger);
private readonly Dictionary<int, Dictionary<int, List<ushort>>> _clientRenderTracker = [];
private readonly Dictionary<
int,
Dictionary<int, List<(ushort, bool)>>
> _clientRenderTracker = [];

private readonly Queue<ushort> _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
{
Expand All @@ -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
{
Expand All @@ -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
};
Expand All @@ -84,7 +99,7 @@ GameState gameState
// Clear the previous render group, if any
RemoveRenderGroup(clientId, renderId);

List<ushort> renderGroup = [];
List<(ushort, bool)> renderGroup = [];
foreach (RenderMessageT renderItem in renderItems)
if (RenderItem(renderItem.Variety, gameState) is { } renderItemId)
renderGroup.Add(renderItemId);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}

Expand Down
12 changes: 11 additions & 1 deletion RLBotCS/Server/BridgeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 10 additions & 0 deletions RLBotCS/Server/BridgeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
1 change: 1 addition & 0 deletions RLBotCS/Server/FlatbuffersMessage/StartMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions RLBotCS/Server/FlatbuffersMessage/StopMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 40758ea

Please sign in to comment.