Skip to content

Commit

Permalink
✨ Add a protobuf serializer for UInt128.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexawyz committed Feb 1, 2025
1 parent 268c062 commit 8613d97
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Update="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageReference Update="Microsoft.Management.Infrastructure" Version="3.0.0" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Update="protobuf-net" Version="3.2.46" />
<PackageReference Update="protobuf-net.Grpc" Version="1.2.2" />
<PackageReference Update="protobuf-net.Grpc.AspNetCore" Version="1.2.2" />
<PackageReference Update="Serilog.AspNetCore" Version="9.0.0" />
Expand Down
1 change: 1 addition & 0 deletions src/Exo/Service/Exo.Service.Grpc/GrpcConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public static GrpcMonitorShape ToGrpc(this MonitorShape shape)
public static GrpcImageInformation ToGrpc(this ImageInformation imageInformation)
=> new()
{
ImageId = imageInformation.ImageId,
ImageName = imageInformation.ImageName,
FileName = imageInformation.FileName,
Width = imageInformation.Width,
Expand Down
3 changes: 3 additions & 0 deletions src/Exo/Service/Exo.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO.Pipes;
using System.Security.AccessControl;
using System.Security.Principal;
using Exo.Contracts.Ui;
using Exo.Programming;
using Exo.Utils;
using Microsoft.AspNetCore.Server.Kestrel.Core;
Expand Down Expand Up @@ -28,6 +29,8 @@ public static void Main(string[] args)
metaType.Add(3, nameof(NamedElement.Comment));
}

RuntimeTypeModel.Default.Add<UInt128>(false).SerializerType = typeof(UInt128Serializer);

CreateHostBuilder(args).Build().Run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="protobuf-net" />
<PackageReference Include="protobuf-net.Grpc" />
</ItemGroup>

Expand Down
15 changes: 9 additions & 6 deletions src/Exo/Ui/Exo.Contracts.Ui.Settings/ImageInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ namespace Exo.Contracts.Ui.Settings;
[DataContract]
public sealed class ImageInformation
{
// NB: Image name is used as a public-facing unique key, but the images will internally use another index.
// The ImageId is used to reference the image internally.
[DataMember(Order = 1)]
public required string ImageName { get; init; }
public required UInt128 ImageId { get; init; }
// NB: Image name is used as a public-facing unique key, but the images will internally use ImageId.
[DataMember(Order = 2)]
public required string FileName { get; init; }
public required string ImageName { get; init; }
[DataMember(Order = 3)]
public required ushort Width { get; init; }
public required string FileName { get; init; }
[DataMember(Order = 4)]
public required ushort Height { get; init; }
public required ushort Width { get; init; }
[DataMember(Order = 5)]
public required ImageFormat Format { get; init; }
public required ushort Height { get; init; }
[DataMember(Order = 6)]
public required ImageFormat Format { get; init; }
[DataMember(Order = 7)]
public bool IsAnimated { get; init; }
}
26 changes: 26 additions & 0 deletions src/Exo/Ui/Exo.Contracts.Ui.Settings/UInt128Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ProtoBuf;
using ProtoBuf.Serializers;

namespace Exo.Contracts.Ui;

internal class UInt128Serializer : ISerializer<UInt128>
{
public SerializerFeatures Features => SerializerFeatures.CategoryRepeated | SerializerFeatures.CategoryScalar | SerializerFeatures.WireTypeString;

public UInt128 Read(ref ProtoReader.State state, UInt128 value)
{
Unsafe.SkipInit(out value);
var result = state.ReadBytes(MemoryMarshal.Cast<UInt128, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
if (result.Length != Unsafe.SizeOf<UInt128>()) throw new InvalidDataException();
return BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);
}

public void Write(ref ProtoWriter.State state, UInt128 value)
{
if (!BitConverter.IsLittleEndian) value = BinaryPrimitives.ReverseEndianness(value);
state.WriteBytes(MemoryMarshal.Cast<UInt128, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
}
}
3 changes: 3 additions & 0 deletions src/Exo/Ui/Exo.Settings.Ui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Windows.Storage;
using System.Collections.Immutable;
using Exo.Ui;
using Exo.Contracts.Ui;

namespace Exo.Settings.Ui;

Expand Down Expand Up @@ -56,6 +57,8 @@ public App()
metaType.Add(3, nameof(NamedElement.Comment));
}

RuntimeTypeModel.Default.Add<UInt128>(false).SerializerType = typeof(UInt128Serializer);

GrpcClientFactory.AllowUnencryptedHttp2 = true;

_rasterizationScaleController = new();
Expand Down

0 comments on commit 8613d97

Please sign in to comment.