From 416c8f21652c842f3cbc5f98aaf7ea55e498cec0 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 19 Oct 2024 16:07:41 +0200 Subject: [PATCH] update servicepoint to branch with better codegen --- tanks-backend/TanksServer/GameLogic/MapService.cs | 2 +- .../TanksServer/Graphics/GeneratePixelsTickStep.cs | 12 +++++++++--- .../Interactivity/SendToServicePointDisplay.cs | 11 ++++++++--- tanks-backend/TanksServer/Program.cs | 7 +++++-- tanks-backend/servicepoint | 2 +- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/tanks-backend/TanksServer/GameLogic/MapService.cs b/tanks-backend/TanksServer/GameLogic/MapService.cs index 3a89567..816cb0f 100644 --- a/tanks-backend/TanksServer/GameLogic/MapService.cs +++ b/tanks-backend/TanksServer/GameLogic/MapService.cs @@ -42,7 +42,7 @@ internal sealed class MapService if (!_mapPrototypes.TryGetValue(name, out var prototype)) return false; // name not found - pixelGrid = Bitmap.New(PixelsPerRow, PixelsPerColumn); + pixelGrid = new Bitmap(PixelsPerRow, PixelsPerColumn); DrawMapStep.Draw(pixelGrid, prototype.CreateInstance()); _mapPreviews.TryAdd(name, pixelGrid); // another thread may have added the map already diff --git a/tanks-backend/TanksServer/Graphics/GeneratePixelsTickStep.cs b/tanks-backend/TanksServer/Graphics/GeneratePixelsTickStep.cs index f5ec8ad..1db7259 100644 --- a/tanks-backend/TanksServer/Graphics/GeneratePixelsTickStep.cs +++ b/tanks-backend/TanksServer/Graphics/GeneratePixelsTickStep.cs @@ -7,12 +7,12 @@ namespace TanksServer.Graphics; internal sealed class GeneratePixelsTickStep( IEnumerable drawSteps, IEnumerable consumers -) : ITickStep +) : ITickStep, IDisposable { private GamePixelGrid _lastGamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn); - private Bitmap _lastObserverPixelGrid = Bitmap.New(MapService.PixelsPerRow, MapService.PixelsPerColumn); + private Bitmap _lastObserverPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn); private GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn); - private Bitmap _observerPixelGrid = Bitmap.New(MapService.PixelsPerRow, MapService.PixelsPerColumn); + private Bitmap _observerPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn); private readonly List _drawSteps = drawSteps.ToList(); private readonly List _consumers = consumers.ToList(); @@ -44,4 +44,10 @@ internal sealed class GeneratePixelsTickStep( observerPixelGrid[(ushort)x, (ushort)y] = true; } } + + public void Dispose() + { + _lastObserverPixelGrid.Dispose(); + _observerPixelGrid.Dispose(); + } } diff --git a/tanks-backend/TanksServer/Interactivity/SendToServicePointDisplay.cs b/tanks-backend/TanksServer/Interactivity/SendToServicePointDisplay.cs index e7f8065..de055de 100644 --- a/tanks-backend/TanksServer/Interactivity/SendToServicePointDisplay.cs +++ b/tanks-backend/TanksServer/Interactivity/SendToServicePointDisplay.cs @@ -4,11 +4,10 @@ using System.Net.Sockets; using ServicePoint; using TanksServer.GameLogic; using TanksServer.Graphics; -using CompressionCode = ServicePoint.BindGen.CompressionCode; namespace TanksServer.Interactivity; -internal sealed class SendToServicePointDisplay : IFrameConsumer +internal sealed class SendToServicePointDisplay : IFrameConsumer, IDisposable { private const int ScoresWidth = 12; private const int ScoresHeight = 20; @@ -43,7 +42,7 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer var localIp = GetLocalIPv4(displayConfig.Value).Split('.'); Debug.Assert(localIp.Length == 4); - _scoresBuffer = Cp437Grid.New(12, 20); + _scoresBuffer = new Cp437Grid(12, 20); _scoresBuffer[00] = "== TANKS! =="; _scoresBuffer[01] = "-- scores --"; @@ -113,4 +112,10 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer var endPoint = socket.LocalEndPoint as IPEndPoint ?? throw new NotSupportedException(); return endPoint.Address.ToString(); } + + public void Dispose() + { + _displayConnection.Dispose(); + _scoresBuffer.Dispose(); + } } diff --git a/tanks-backend/TanksServer/Program.cs b/tanks-backend/TanksServer/Program.cs index d0599fb..56cc6ad 100644 --- a/tanks-backend/TanksServer/Program.cs +++ b/tanks-backend/TanksServer/Program.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using ServicePoint; -using SixLabors.ImageSharp; using TanksServer.GameLogic; using TanksServer.Graphics; using TanksServer.Interactivity; @@ -101,7 +100,11 @@ public static class Program builder.Services.AddSingleton(sp => { var config = sp.GetRequiredService>().Value; - return Connection.Open($"{config.Hostname}:{config.Port}"); + var connection = Connection.Open($"{config.Hostname}:{config.Port}"); + if (connection == null) + throw new IOException($"Could not open connection to {config.Hostname}:{config.Port}"); + + return connection; }); var app = builder.Build(); diff --git a/tanks-backend/servicepoint b/tanks-backend/servicepoint index 9193cfe..f968f92 160000 --- a/tanks-backend/servicepoint +++ b/tanks-backend/servicepoint @@ -1 +1 @@ -Subproject commit 9193cfec10c72d99ad01b1ae09a935007588d7d9 +Subproject commit f968f929173c6646acb7d9a3ed19112e626732b3