wip move to new uniffi language binding

This commit is contained in:
Vinzenz Schroeter 2024-11-12 18:27:04 +01:00
commit 53cbdd8440
30 changed files with 212 additions and 188 deletions

View file

@ -1,5 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TanksServer", "TanksServer\TanksServer.csproj", "{D88BF376-47A4-4C72-ADD1-983F9285C351}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{12DB7D48-1BB2-488B-B4D9-4126087D2F8C}"
@ -9,7 +10,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{12DB7D
..\.envrc = ..\.envrc
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint", "servicepoint/crates/servicepoint_binding_cs/ServicePoint/ServicePoint.csproj", "{DFCC69ED-E02B-4631-8A23-5D394BA01E03}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "servicepoint", "servicepoint", "{A10FB29A-9078-4E90-9CE1-E6C2B5209E19}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "crates", "crates", "{656A7CBA-9445-41CC-B1AF-A6897AAC9F17}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "servicepoint_binding_uniffi", "servicepoint_binding_uniffi", "{5FD9FAD7-07BA-4DF9-8C84-15A9558373F1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libraries", "libraries", "{6082A0DC-5345-48C8-BA2E-667754A2F0E9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "csharp", "csharp", "{D8A3290B-5DFB-43C6-99EE-56AB5F53F468}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint", "servicepoint\crates\servicepoint_binding_uniffi\libraries\csharp\ServicePoint\ServicePoint.csproj", "{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint.Tests", "servicepoint\crates\servicepoint_binding_uniffi\libraries\csharp\ServicePoint.Tests\ServicePoint.Tests.csproj", "{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,9 +34,21 @@ Global
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.Build.0 = Release|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Release|Any CPU.Build.0 = Release|Any CPU
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Release|Any CPU.Build.0 = Release|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{656A7CBA-9445-41CC-B1AF-A6897AAC9F17} = {A10FB29A-9078-4E90-9CE1-E6C2B5209E19}
{5FD9FAD7-07BA-4DF9-8C84-15A9558373F1} = {656A7CBA-9445-41CC-B1AF-A6897AAC9F17}
{6082A0DC-5345-48C8-BA2E-667754A2F0E9} = {5FD9FAD7-07BA-4DF9-8C84-15A9558373F1}
{D8A3290B-5DFB-43C6-99EE-56AB5F53F468} = {6082A0DC-5345-48C8-BA2E-667754A2F0E9}
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3} = {D8A3290B-5DFB-43C6-99EE-56AB5F53F468}
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C} = {D8A3290B-5DFB-43C6-99EE-56AB5F53F468}
EndGlobalSection
EndGlobal

View file

@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Text.Json;
@ -7,7 +8,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using ServicePoint;
using TanksServer.GameLogic;
using TanksServer.Interactivity;
@ -22,6 +22,8 @@ internal sealed class Endpoints(
Connection displayConnection
)
{
[RequiresUnreferencedCode("Calls Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapPost(String, Delegate)")]
[RequiresDynamicCode("Calls Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapPost(String, Delegate)")]
public void Map(WebApplication app)
{
app.MapPost("/player", PostPlayer);
@ -31,7 +33,7 @@ internal sealed class Endpoints(
app.Map("/controls", ConnectControlsAsync);
app.MapGet("/map", () => mapService.MapNames);
app.MapPost("/map", PostMap);
app.MapPost("/resetDisplay", () => displayConnection.Send(Command.HardReset().IntoPacket()));
app.MapPost("/resetDisplay", () => displayConnection.Send(Command.HardReset()));
app.MapGet("/map/{name}", GetMapByName);
app.MapHealthChecks("/health", new HealthCheckOptions
@ -117,7 +119,7 @@ internal sealed class Endpoints(
if (!mapService.TryGetPreview(name, out var preview))
return TypedResults.NotFound();
var mapInfo = new MapInfo(prototype.Name, prototype.GetType().Name, preview.Data.ToArray());
var mapInfo = new MapInfo(prototype.Name, prototype.GetType().Name, preview.CopyRaw());
return TypedResults.Ok(mapInfo);
}

View file

@ -4,7 +4,7 @@ internal sealed class Map(string name, bool[,] walls)
{
public string Name => name;
public bool IsWall(int x, int y) => walls[x, y];
public bool IsWall(ulong x, ulong y) => walls[x, y];
public bool IsWall(PixelPosition position) => walls[position.X, position.Y];
@ -12,8 +12,8 @@ internal sealed class Map(string name, bool[,] walls)
{
var pixel = position.ToPixelPosition();
for (short dx = 0; dx < MapService.TileSize; dx++)
for (short dy = 0; dy < MapService.TileSize; dy++)
for (long dx = 0; dx < (long)MapService.TileSize; dx++)
for (long dy = 0; dy < (long)MapService.TileSize; dy++)
{
if (IsWall(pixel.GetPixelRelative(dx, dy)))
return true;

View file

@ -1,18 +1,17 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using ServicePoint;
using TanksServer.Graphics;
namespace TanksServer.GameLogic;
internal sealed class MapService
{
public const ushort TilesPerRow = 44;
public const ushort TilesPerColumn = 20;
public const ushort TileSize = 8;
public const ushort PixelsPerRow = TilesPerRow * TileSize;
public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
public const ulong TilesPerRow = 44;
public const ulong TilesPerColumn = 20;
public const ulong TileSize = 8;
public const ulong PixelsPerRow = TilesPerRow * TileSize;
public const ulong PixelsPerColumn = TilesPerColumn * TileSize;
private readonly ConcurrentDictionary<string, MapPrototype> _mapPrototypes = new();
private readonly ConcurrentDictionary<string, Bitmap> _mapPreviews = new();

View file

@ -68,10 +68,10 @@ internal sealed class MoveTanks(
{
var (topLeft, _) = newPosition.GetBoundsForCenter(MapService.TileSize);
for (short y = 0; y < MapService.TileSize; y++)
for (short x = 0; x < MapService.TileSize; x++)
for (ulong y = 0; y < MapService.TileSize; y++)
for (ulong x = 0; x < MapService.TileSize; x++)
{
var pixelToCheck = topLeft.GetPixelRelative(x, y);
var pixelToCheck = topLeft.GetPixelRelative((long)x, (long)y);
if (map.Current.IsWall(pixelToCheck))
return true;
}

View file

@ -11,7 +11,7 @@ internal sealed class SpriteMapPrototype : MapPrototype
public SpriteMapPrototype(string name, Sprite sprite)
{
if (sprite.Width != MapService.PixelsPerRow || sprite.Height != MapService.PixelsPerColumn)
if ((ulong)sprite.Width != MapService.PixelsPerRow || (ulong)sprite.Height != MapService.PixelsPerColumn)
throw new FileLoadException($"invalid image size in file {Name}");
Name = name;

View file

@ -8,7 +8,7 @@ internal sealed class TextMapPrototype : MapPrototype
public TextMapPrototype(string name, string text)
{
if (text.Length != MapService.TilesPerColumn * MapService.TilesPerRow)
if ((ulong)text.Length != MapService.TilesPerColumn * MapService.TilesPerRow)
throw new ArgumentException($"cannot load map {name}: invalid length");
Name = name;
Text = text;
@ -19,11 +19,11 @@ internal sealed class TextMapPrototype : MapPrototype
{
var walls = new bool[MapService.PixelsPerRow, MapService.PixelsPerColumn];
for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++)
for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
for (ulong tileX = 0; tileX < MapService.TilesPerRow; tileX++)
for (ulong tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
{
var tile = new TilePosition(tileX, tileY);
if (Text[tileX + tileY * MapService.TilesPerRow] != '#')
if (Text[(int)(tileX + tileY * MapService.TilesPerRow)] != '#')
continue;
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)

View file

@ -7,3 +7,4 @@ global using System.Threading.Tasks;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using TanksServer.Models;
global using ServicePoint;

View file

@ -1,4 +1,3 @@
using ServicePoint;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
@ -9,8 +8,8 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
private static void Draw(GamePixelGrid pixels, Map map)
{
for (ushort y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++)
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
@ -21,12 +20,12 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
public static void Draw(Bitmap pixels, Map map)
{
for (ushort y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++)
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
pixels[x, y] = true;
pixels.Set(x, y, true);
}
}
}

View file

@ -5,7 +5,7 @@ namespace TanksServer.Graphics;
internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
{
private readonly SpriteSheet _tankSprites =
SpriteSheet.FromImageFile("assets/tank.png", MapService.TileSize, MapService.TileSize);
SpriteSheet.FromImageFile("assets/tank.png", (int)MapService.TileSize, (int)MapService.TileSize);
public void Draw(GamePixelGrid pixels)
{

View file

@ -5,27 +5,27 @@ namespace TanksServer.Graphics;
internal sealed class GamePixelGrid : IEnumerable<GamePixel>
{
public int Width { get; }
public int Height { get; }
public ulong Width { get; }
public ulong Height { get; }
private readonly GamePixel[,] _pixels;
public GamePixelGrid(int width, int height)
public GamePixelGrid(ulong width, ulong height)
{
Width = width;
Height = height;
_pixels = new GamePixel[width, height];
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
for (ulong y = 0; y < height; y++)
for (ulong x = 0; x < width; x++)
this[x, y] = new GamePixel();
}
public GamePixel this[int x, int y]
public GamePixel this[ulong x, ulong y]
{
get
{
Debug.Assert(y * Width + x < _pixels.Length);
Debug.Assert(y * Width + x < (ulong)_pixels.Length);
return _pixels[x, y];
}
set => _pixels[x, y] = value;
@ -41,8 +41,8 @@ internal sealed class GamePixelGrid : IEnumerable<GamePixel>
public IEnumerator<GamePixel> GetEnumerator()
{
for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++)
for (ulong y = 0; y < Height; y++)
for (ulong x = 0; x < Width; x++)
yield return this[x, y];
}
}

View file

@ -20,7 +20,7 @@ internal sealed class GeneratePixelsTickStep(
public async ValueTask TickAsync(TimeSpan _)
{
Draw(_gamePixelGrid, _observerPixelGrid);
if (_observerPixelGrid.Data.SequenceEqual(_lastObserverPixelGrid.Data))
if (_observerPixelGrid.Equals(_lastObserverPixelGrid))
return;
await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid))
@ -37,11 +37,11 @@ internal sealed class GeneratePixelsTickStep(
step.Draw(gamePixelGrid);
observerPixelGrid.Fill(false);
for (var y = 0; y < MapService.PixelsPerColumn; y++)
for (var x = 0; x < MapService.PixelsPerRow; x++)
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (gamePixelGrid[x, y].EntityType.HasValue)
observerPixelGrid[(ushort)x, (ushort)y] = true;
observerPixelGrid.Set(x, y, true);
}
}

View file

@ -36,7 +36,7 @@ internal sealed class ClientScreenServerConnection
private Package BuildNextPackage(Bitmap pixels, GamePixelGrid gamePixelGrid)
{
var pixelsData = pixels.Data;
var pixelsData = pixels.CopyRaw();
var nextPixels = _bufferPool.Rent(pixelsData.Length);
pixelsData.CopyTo(nextPixels.Memory.Span);

Some files were not shown because too many files have changed in this diff Show more