Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*, variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos: CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly. Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.2 KiB
C#
108 lines
3.2 KiB
C#
using System.Buffers.Binary;
|
|
using System.Text;
|
|
using RailEnterprise.Core.Common;
|
|
|
|
namespace RailEnterprise.Protocol;
|
|
|
|
/// <summary>
|
|
/// Scrittore binario little endian su un buffer che cresce. Le lunghezze e i conteggi sono
|
|
/// varuint (LEB128), le stringhe UTF-8 con lunghezza in byte. Nessuna reflection.
|
|
/// </summary>
|
|
public ref struct SpanWriter
|
|
{
|
|
private byte[] _buffer;
|
|
private int _position;
|
|
|
|
public SpanWriter(int capacity)
|
|
{
|
|
_buffer = new byte[Math.Max(16, capacity)];
|
|
_position = 0;
|
|
}
|
|
|
|
public SpanWriter(byte[] buffer)
|
|
{
|
|
_buffer = buffer;
|
|
_position = 0;
|
|
}
|
|
|
|
public readonly int Length => _position;
|
|
|
|
public readonly ReadOnlySpan<byte> Written => _buffer.AsSpan(0, _position);
|
|
|
|
public readonly byte[] ToArray() => _buffer.AsSpan(0, _position).ToArray();
|
|
|
|
/// <summary>Riserva n byte e restituisce lo spazio da riempire.</summary>
|
|
public Span<byte> Reserve(int n)
|
|
{
|
|
Ensure(n);
|
|
var span = _buffer.AsSpan(_position, n);
|
|
_position += n;
|
|
return span;
|
|
}
|
|
|
|
/// <summary>Riscrive un u32 in una posizione già scritta (lunghezze dei frame).</summary>
|
|
public readonly void PatchU32(int offset, uint value) => BinaryPrimitives.WriteUInt32LittleEndian(_buffer.AsSpan(offset, 4), value);
|
|
|
|
private void Ensure(int n)
|
|
{
|
|
if (_position + n <= _buffer.Length) return;
|
|
int size = Math.Max(_buffer.Length * 2, _position + n);
|
|
Array.Resize(ref _buffer, size);
|
|
}
|
|
|
|
public void Bool(bool v) => U8(v ? (byte)1 : (byte)0);
|
|
|
|
public void U8(byte v)
|
|
{
|
|
Ensure(1);
|
|
_buffer[_position++] = v;
|
|
}
|
|
|
|
public void U16(ushort v) => BinaryPrimitives.WriteUInt16LittleEndian(Reserve(2), v);
|
|
public void U32(uint v) => BinaryPrimitives.WriteUInt32LittleEndian(Reserve(4), v);
|
|
public void U64(ulong v) => BinaryPrimitives.WriteUInt64LittleEndian(Reserve(8), v);
|
|
public void I16(short v) => BinaryPrimitives.WriteInt16LittleEndian(Reserve(2), v);
|
|
public void I32(int v) => BinaryPrimitives.WriteInt32LittleEndian(Reserve(4), v);
|
|
public void I64(long v) => BinaryPrimitives.WriteInt64LittleEndian(Reserve(8), v);
|
|
public void F32(float v) => BinaryPrimitives.WriteInt32LittleEndian(Reserve(4), BitConverter.SingleToInt32Bits(v));
|
|
public void F64(double v) => BinaryPrimitives.WriteInt64LittleEndian(Reserve(8), BitConverter.DoubleToInt64Bits(v));
|
|
|
|
public void VarUInt(ulong v)
|
|
{
|
|
while (v >= 0x80)
|
|
{
|
|
U8((byte)(v | 0x80));
|
|
v >>= 7;
|
|
}
|
|
U8((byte)v);
|
|
}
|
|
|
|
/// <summary>Conteggio di una lista (varuint).</summary>
|
|
public void Count(int n) => VarUInt((ulong)n);
|
|
|
|
public void String(string? s)
|
|
{
|
|
s ??= "";
|
|
int n = Encoding.UTF8.GetByteCount(s);
|
|
Count(n);
|
|
Encoding.UTF8.GetBytes(s, Reserve(n));
|
|
}
|
|
|
|
public void Bytes(ReadOnlySpan<byte> bytes)
|
|
{
|
|
Count(bytes.Length);
|
|
bytes.CopyTo(Reserve(bytes.Length));
|
|
}
|
|
|
|
public void Money(Money m) => I64(m.Cents);
|
|
|
|
public void Vec2(Vec2 v)
|
|
{
|
|
F64(v.X);
|
|
F64(v.Y);
|
|
}
|
|
|
|
/// <summary>Byte di estensione in coda al messaggio: i campi futuri seguono i bit impostati.</summary>
|
|
public void Flags(byte flags) => U8(flags);
|
|
}
|