binding/YGOLegacy.Engine (netstandard2.1, LangVersion 9, unsafe, no package, no Unity
reference) holds the Unity repo's Assets/YGOLegacy/Scripts/Protocol/ as
Protocol/{Contracts,Codecs,Framing,Routing}: 12 files copied byte-identical (LF), namespaces
YGOLegacy.Protocol.* unchanged. EngineVersion (default local) is stamped as the informational
version, so the DLL's ProductVersion is the tag it was built for.
binding/YGOLegacy.Engine.Tests (net8.0, NUnit 3.14) holds the six fixtures. 114 tests pass,
the Unity Edit Mode count. Two deviations from "as-is", lead-approved 2026-10-15:
LegacyReaders.cs is a frozen test-only copy of the legacy MDPro3 / YGOSharp readers and enums
the parity tests compare against (they live only in the Unity repo), and FrameCodecTests.cs
line 35 casts (short) instead of (byte): BitConverter.GetBytes((byte)) is ambiguous on
net8.0 (the Half overload), same bytes written.
The windows job builds and tests the binding after the native gates (both with
-p:EngineVersion so the test's project reference does not restamp the DLL as local), checks
the ProductVersion and stages ygol-engine-binding.zip (YGOLegacy.Engine.dll + .pdb) beside
ygol-engine-win64.zip; attach-windows attaches both. engine.json gains "binding": "<version>"
on every platform (windows.ps1 and the Dockerfile's export-gate, the only writers) and the
release job's agreement check includes it. README gains the Binding section and the asset row;
.gitignore ignores binding/**/bin and obj.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DJUFwdiGCHipsxLkgxssSR
601 lines
24 KiB
C#
601 lines
24 KiB
C#
using System.IO;
|
|
using YGOLegacy.Protocol.Contracts;
|
|
|
|
namespace YGOLegacy.Protocol.Codecs
|
|
{
|
|
public sealed class CapabilityCodec : ICtosCodec<Capability>
|
|
{
|
|
public const int Size = 6;
|
|
public const int StampedSize = Size + StampedCodec.SeatBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.Capability;
|
|
|
|
public void Write(BinaryWriter writer, Capability payload)
|
|
{
|
|
writer.Write(payload.Protocol);
|
|
writer.Write(payload.Flags);
|
|
}
|
|
|
|
public Capability Read(BinaryReader reader)
|
|
{
|
|
var protocol = reader.ReadUInt16();
|
|
var flags = reader.ReadUInt32();
|
|
return new Capability(protocol, flags);
|
|
}
|
|
}
|
|
|
|
public sealed class AppearanceCodec : ICtosCodec<AppearanceState>
|
|
{
|
|
public const int Size = AppearanceState.ItemCount * 4
|
|
+ (AppearanceState.DeckNameChars + AppearanceState.CharacterIdChars + AppearanceState.DeckGroupChars) * 2;
|
|
public const int StampedSize = Size + StampedCodec.SeatBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.Appearance;
|
|
|
|
public void Write(BinaryWriter writer, AppearanceState payload)
|
|
{
|
|
for (var i = 0; i < AppearanceState.ItemCount; i++)
|
|
writer.Write(payload.Items != null && i < payload.Items.Length ? payload.Items[i] : 0);
|
|
UnicodeCodec.WriteFixed(writer, payload.DeckName, AppearanceState.DeckNameChars);
|
|
UnicodeCodec.WriteFixed(writer, payload.CharacterId, AppearanceState.CharacterIdChars);
|
|
UnicodeCodec.WriteFixed(writer, payload.DeckGroup, AppearanceState.DeckGroupChars);
|
|
}
|
|
|
|
public AppearanceState Read(BinaryReader reader)
|
|
{
|
|
var items = new int[AppearanceState.ItemCount];
|
|
for (var i = 0; i < items.Length; i++)
|
|
items[i] = reader.ReadInt32();
|
|
var deckName = UnicodeCodec.ReadFixed(reader, AppearanceState.DeckNameChars);
|
|
var characterId = UnicodeCodec.ReadFixed(reader, AppearanceState.CharacterIdChars);
|
|
var deckGroup = UnicodeCodec.ReadFixed(reader, AppearanceState.DeckGroupChars);
|
|
return new AppearanceState(items, deckName, characterId, deckGroup);
|
|
}
|
|
}
|
|
|
|
public sealed class DialogueCodec : ICtosCodec<DialogueLine>
|
|
{
|
|
public const int Size = 1 + 1 + 2 + 4 + 4 + DialogueLine.KeyChars * 2 * 2;
|
|
public const int StampedSize = Size + StampedCodec.SeatBytes;
|
|
private const byte TargetFaceDownFlag = 0x1;
|
|
|
|
public CtosMessage Id => CtosMessage.Dialogue;
|
|
|
|
public void Write(BinaryWriter writer, DialogueLine payload)
|
|
{
|
|
writer.Write(payload.Version);
|
|
writer.Write((byte)(payload.TargetFaceDown ? TargetFaceDownFlag : 0));
|
|
writer.Write(payload.Index);
|
|
writer.Write(payload.CardCode);
|
|
writer.Write(payload.TargetCode);
|
|
UnicodeCodec.WriteFixed(writer, payload.CorpusKey, DialogueLine.KeyChars);
|
|
UnicodeCodec.WriteFixed(writer, payload.EventKey, DialogueLine.KeyChars);
|
|
}
|
|
|
|
public DialogueLine Read(BinaryReader reader)
|
|
{
|
|
var version = reader.ReadByte();
|
|
var flags = reader.ReadByte();
|
|
var index = reader.ReadInt16();
|
|
var cardCode = reader.ReadUInt32();
|
|
var targetCode = reader.ReadUInt32();
|
|
var corpusKey = UnicodeCodec.ReadFixed(reader, DialogueLine.KeyChars);
|
|
var eventKey = UnicodeCodec.ReadFixed(reader, DialogueLine.KeyChars);
|
|
return new DialogueLine(version, (flags & TargetFaceDownFlag) != 0, index, cardCode, targetCode, corpusKey, eventKey);
|
|
}
|
|
}
|
|
|
|
public sealed class SeatPlanCodec : ICtosCodec<SeatPlan>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes + 4 + 2 + 1 + SeatPlan.MaxSeats + SeatPlan.MaxSeats * SeatPlan.IdBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatPlan;
|
|
|
|
public void Write(BinaryWriter writer, SeatPlan payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write(payload.DeadlineMs);
|
|
writer.Write(payload.Revision);
|
|
writer.Write(payload.SeatCount);
|
|
for (var i = 0; i < SeatPlan.MaxSeats; i++)
|
|
writer.Write((byte)payload.Kind(i));
|
|
for (var i = 0; i < SeatPlan.MaxSeats; i++)
|
|
writer.Write(SeatIds.Fixed(payload.Tokens?[i]));
|
|
}
|
|
|
|
public SeatPlan Read(BinaryReader reader)
|
|
{
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var deadline = reader.ReadUInt32();
|
|
var revision = reader.ReadUInt16();
|
|
var count = reader.ReadByte();
|
|
var kinds = new SeatPlanKind[SeatPlan.MaxSeats];
|
|
for (var i = 0; i < kinds.Length; i++)
|
|
kinds[i] = (SeatPlanKind)reader.ReadByte();
|
|
var tokens = new byte[SeatPlan.MaxSeats][];
|
|
for (var i = 0; i < tokens.Length; i++)
|
|
tokens[i] = reader.ReadBytes(SeatPlan.IdBytes);
|
|
return new SeatPlan(restartId, deadline, revision, count, kinds, tokens);
|
|
}
|
|
}
|
|
|
|
public sealed class SeatRequestCodec : ICtosCodec<SeatRequest>
|
|
{
|
|
public const int Size = 2 + SeatPlan.IdBytes + 1 + SeatPlan.IdBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatRequest;
|
|
|
|
public void Write(BinaryWriter writer, SeatRequest payload)
|
|
{
|
|
writer.Write(payload.Protocol);
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write(payload.Seat);
|
|
writer.Write(SeatIds.Fixed(payload.Token));
|
|
}
|
|
|
|
public SeatRequest Read(BinaryReader reader)
|
|
{
|
|
var protocol = reader.ReadUInt16();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var seat = reader.ReadByte();
|
|
return new SeatRequest(protocol, restartId, seat, reader.ReadBytes(SeatPlan.IdBytes));
|
|
}
|
|
}
|
|
|
|
public sealed class SeatSwapCodec : ICtosCodec<SeatSwap>
|
|
{
|
|
public const int Size = 2;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatSwap;
|
|
|
|
public void Write(BinaryWriter writer, SeatSwap payload)
|
|
{
|
|
writer.Write(payload.First);
|
|
writer.Write(payload.Second);
|
|
}
|
|
|
|
public SeatSwap Read(BinaryReader reader) => new SeatSwap(reader.ReadByte(), reader.ReadByte());
|
|
}
|
|
|
|
public sealed class SeatMoveCodec : ICtosCodec<SeatMove>
|
|
{
|
|
public const int Size = 2;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatMove;
|
|
|
|
public void Write(BinaryWriter writer, SeatMove payload)
|
|
{
|
|
writer.Write(payload.From);
|
|
writer.Write(payload.To);
|
|
}
|
|
|
|
public SeatMove Read(BinaryReader reader) => new SeatMove(reader.ReadByte(), reader.ReadByte());
|
|
}
|
|
|
|
public sealed class SeatStateCodec : ICtosCodec<SeatState>
|
|
{
|
|
public const int Size = 4;
|
|
public const int StampedSize = Size + StampedCodec.SeatBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatState;
|
|
|
|
public void Write(BinaryWriter writer, SeatState payload)
|
|
{
|
|
writer.Write(payload.Version);
|
|
writer.Write((byte)payload.Kind);
|
|
writer.Write(payload.Value);
|
|
}
|
|
|
|
public SeatState Read(BinaryReader reader)
|
|
{
|
|
var version = reader.ReadByte();
|
|
var kind = (SeatStateKind)reader.ReadByte();
|
|
return new SeatState(version, kind, reader.ReadUInt16());
|
|
}
|
|
}
|
|
|
|
public sealed class ProfileCodec : ICtosCodec<ProfileState>
|
|
{
|
|
public const int Size = 1 + 1 + (ProfileState.AvatarIdChars + ProfileState.FrameIdChars) * 2;
|
|
public const int StampedSize = Size + StampedCodec.SeatBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.Profile;
|
|
|
|
public void Write(BinaryWriter writer, ProfileState payload)
|
|
{
|
|
writer.Write(payload.Version);
|
|
writer.Write((byte)0);
|
|
UnicodeCodec.WriteFixed(writer, payload.AvatarId, ProfileState.AvatarIdChars);
|
|
UnicodeCodec.WriteFixed(writer, payload.FrameId, ProfileState.FrameIdChars);
|
|
}
|
|
|
|
public ProfileState Read(BinaryReader reader)
|
|
{
|
|
var version = reader.ReadByte();
|
|
reader.ReadByte();
|
|
var avatarId = UnicodeCodec.ReadFixed(reader, ProfileState.AvatarIdChars);
|
|
var frameId = UnicodeCodec.ReadFixed(reader, ProfileState.FrameIdChars);
|
|
return new ProfileState(version, avatarId, frameId);
|
|
}
|
|
}
|
|
|
|
// Version, count, then count x 4 bytes (patch 0023).
|
|
public sealed class AttackPreviewQueryCodec : ICtosCodec<AttackPreviewQuery>
|
|
{
|
|
public const int HeaderSize = 2;
|
|
|
|
public CtosMessage Id => CtosMessage.AttackPreview;
|
|
|
|
public void Write(BinaryWriter writer, AttackPreviewQuery payload)
|
|
{
|
|
var count = payload.Pairs.Length > AttackPreviewQuery.MaxPairs ? AttackPreviewQuery.MaxPairs : payload.Pairs.Length;
|
|
writer.Write(payload.Version);
|
|
writer.Write((byte)count);
|
|
for (var i = 0; i < count; i++)
|
|
WritePair(writer, payload.Pairs[i]);
|
|
}
|
|
|
|
public AttackPreviewQuery Read(BinaryReader reader)
|
|
{
|
|
var version = reader.ReadByte();
|
|
int count = reader.ReadByte();
|
|
var pairs = new AttackPreviewPair[count];
|
|
for (var i = 0; i < count; i++)
|
|
pairs[i] = ReadPair(reader);
|
|
return new AttackPreviewQuery(version, pairs);
|
|
}
|
|
|
|
public static void WritePair(BinaryWriter writer, AttackPreviewPair pair)
|
|
{
|
|
writer.Write(pair.AttackerLocation);
|
|
writer.Write(pair.AttackerSequence);
|
|
writer.Write(pair.TargetLocation);
|
|
writer.Write(pair.TargetSequence);
|
|
}
|
|
|
|
public static AttackPreviewPair ReadPair(BinaryReader reader)
|
|
{
|
|
return new AttackPreviewPair(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
|
|
}
|
|
}
|
|
|
|
public sealed class RestartPrepareCodec : ICtosCodec<RestartPrepare>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes + 4 + 1 + SeatPlan.MaxSeats * SeatPlan.IdBytes + SeatPlan.IdBytes;
|
|
|
|
public CtosMessage Id => CtosMessage.RestartPrepare;
|
|
|
|
public void Write(BinaryWriter writer, RestartPrepare payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write(payload.TimeoutMs);
|
|
writer.Write(payload.SeatCount);
|
|
for (var i = 0; i < SeatPlan.MaxSeats; i++)
|
|
writer.Write(SeatIds.Fixed(payload.SeatTokens?[i]));
|
|
writer.Write(SeatIds.Fixed(payload.ObserverToken));
|
|
}
|
|
|
|
public RestartPrepare Read(BinaryReader reader)
|
|
{
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var timeout = reader.ReadUInt32();
|
|
var count = reader.ReadByte();
|
|
var tokens = new byte[SeatPlan.MaxSeats][];
|
|
for (var i = 0; i < tokens.Length; i++)
|
|
tokens[i] = reader.ReadBytes(SeatPlan.IdBytes);
|
|
return new RestartPrepare(restartId, timeout, count, tokens, reader.ReadBytes(SeatPlan.IdBytes));
|
|
}
|
|
}
|
|
|
|
public sealed class RestartAckCodec : ICtosCodec<RestartAck>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes + 1;
|
|
|
|
public CtosMessage Id => CtosMessage.RestartAck;
|
|
|
|
public void Write(BinaryWriter writer, RestartAck payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write((byte)payload.Phase);
|
|
}
|
|
|
|
public RestartAck Read(BinaryReader reader) => new RestartAck(reader.ReadBytes(SeatPlan.IdBytes), (RestartPhase)reader.ReadByte());
|
|
}
|
|
|
|
public sealed class RestartControlCodec : ICtosCodec<RestartControl>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes + 1;
|
|
|
|
public CtosMessage Id => CtosMessage.RestartControl;
|
|
|
|
public void Write(BinaryWriter writer, RestartControl payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write((byte)payload.Action);
|
|
}
|
|
|
|
public RestartControl Read(BinaryReader reader) => new RestartControl(reader.ReadBytes(SeatPlan.IdBytes), (RestartPhase)reader.ReadByte());
|
|
}
|
|
|
|
public sealed class SeatResumeCodec : ICtosCodec<SeatResume>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes + 2;
|
|
|
|
public CtosMessage Id => CtosMessage.SeatResume;
|
|
|
|
public void Write(BinaryWriter writer, SeatResume payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write((byte)payload.Action);
|
|
writer.Write(payload.Reason);
|
|
}
|
|
|
|
public SeatResume Read(BinaryReader reader)
|
|
=> new SeatResume(reader.ReadBytes(SeatPlan.IdBytes), (SeatResumeAction)reader.ReadByte(), reader.ReadByte());
|
|
}
|
|
|
|
public sealed class RestartOpenCodec : ICtosCodec<RestartOpen>
|
|
{
|
|
public const int Size = SeatPlan.IdBytes * 2;
|
|
|
|
public CtosMessage Id => CtosMessage.RestartOpen;
|
|
|
|
public void Write(BinaryWriter writer, RestartOpen payload)
|
|
{
|
|
writer.Write(SeatIds.Fixed(payload.RestartId));
|
|
writer.Write(SeatIds.Fixed(payload.ObserverToken));
|
|
}
|
|
|
|
public RestartOpen Read(BinaryReader reader) => new RestartOpen(reader.ReadBytes(SeatPlan.IdBytes), reader.ReadBytes(SeatPlan.IdBytes));
|
|
}
|
|
|
|
// STOC 0x54: uint16 sender (0xFF) + phase, restart id, the receiver's own seat, its token (Prepare only), timeout.
|
|
public sealed class RoomRestartingCodec : IStocCodec<Stamped<RoomRestarting>>
|
|
{
|
|
public const int PayloadSize = 1 + SeatPlan.IdBytes + 1 + SeatPlan.IdBytes + 4;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.RoomRestarting;
|
|
|
|
public Stamped<RoomRestarting> Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, Size, Id);
|
|
var seat = reader.ReadUInt16();
|
|
var phase = (RestartPhase)reader.ReadByte();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var own = reader.ReadByte();
|
|
var token = reader.ReadBytes(SeatPlan.IdBytes);
|
|
return new Stamped<RoomRestarting>(seat, new RoomRestarting(phase, restartId, own, token, reader.ReadUInt32()));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<RoomRestarting> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write((byte)payload.Payload.Phase);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.RestartId));
|
|
writer.Write(payload.Payload.Seat);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.Token));
|
|
writer.Write(payload.Payload.TimeoutMs);
|
|
}
|
|
}
|
|
|
|
// STOC 0x56, host only: uint16 sender (0xFF) + phase, restart id, acked / expected counts, complete flag, reason.
|
|
public sealed class RestartStatusCodec : IStocCodec<Stamped<RestartStatus>>
|
|
{
|
|
public const int PayloadSize = 1 + SeatPlan.IdBytes + 4;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.RestartStatus;
|
|
|
|
public Stamped<RestartStatus> Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, Size, Id);
|
|
var seat = reader.ReadUInt16();
|
|
var phase = (RestartPhase)reader.ReadByte();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var acked = reader.ReadByte();
|
|
var expected = reader.ReadByte();
|
|
var complete = reader.ReadByte() != 0;
|
|
return new Stamped<RestartStatus>(seat, new RestartStatus(phase, restartId, acked, expected, complete, (RestartReason)reader.ReadByte()));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<RestartStatus> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write((byte)payload.Payload.Phase);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.RestartId));
|
|
writer.Write(payload.Payload.Acked);
|
|
writer.Write(payload.Payload.Expected);
|
|
writer.Write((byte)(payload.Payload.Complete ? 1 : 0));
|
|
writer.Write((byte)payload.Payload.Reason);
|
|
}
|
|
}
|
|
|
|
// STOC 0x57, to the claimant: uint16 sender (0xFF) + restart id, accepted, seat, reason.
|
|
public sealed class SeatResumeResultCodec : IStocCodec<Stamped<SeatResumeResult>>
|
|
{
|
|
public const int PayloadSize = SeatPlan.IdBytes + 3;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.SeatResumeResult;
|
|
|
|
public Stamped<SeatResumeResult> Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, Size, Id);
|
|
var seat = reader.ReadUInt16();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var accepted = reader.ReadByte() != 0;
|
|
var own = reader.ReadByte();
|
|
return new Stamped<SeatResumeResult>(seat, new SeatResumeResult(restartId, accepted, own, reader.ReadByte()));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<SeatResumeResult> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.RestartId));
|
|
writer.Write((byte)(payload.Payload.Accepted ? 1 : 0));
|
|
writer.Write(payload.Payload.Seat);
|
|
writer.Write(payload.Payload.Reason);
|
|
}
|
|
}
|
|
|
|
// STOC 0x58, to the bootstrap host: uint16 sender (0xFF) + restart id, opened.
|
|
public sealed class RestartOpenedCodec : IStocCodec<Stamped<RestartOpened>>
|
|
{
|
|
public const int PayloadSize = SeatPlan.IdBytes + 1;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.RestartOpen;
|
|
|
|
public Stamped<RestartOpened> Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, Size, Id);
|
|
var seat = reader.ReadUInt16();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
return new Stamped<RestartOpened>(seat, new RestartOpened(restartId, reader.ReadByte() != 0));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<RestartOpened> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.RestartId));
|
|
writer.Write((byte)(payload.Payload.Opened ? 1 : 0));
|
|
}
|
|
}
|
|
|
|
// uint16 sender (0xFF) + version, seat count, occupied per seat (patch 0014).
|
|
public sealed class MatchOverCodec : IStocCodec<Stamped<MatchOver>>
|
|
{
|
|
public const int PayloadSize = 2 + MatchOver.MaxSeats;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.MatchOver;
|
|
|
|
public Stamped<MatchOver> Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, Size, Id);
|
|
var seat = reader.ReadUInt16();
|
|
var version = reader.ReadByte();
|
|
var seatCount = reader.ReadByte();
|
|
var occupied = new bool[MatchOver.MaxSeats];
|
|
for (var i = 0; i < MatchOver.MaxSeats; i++)
|
|
occupied[i] = reader.ReadByte() != 0;
|
|
return new Stamped<MatchOver>(seat, new MatchOver(version, seatCount, occupied));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<MatchOver> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write(payload.Payload.Version);
|
|
writer.Write(payload.Payload.SeatCount);
|
|
for (var i = 0; i < MatchOver.MaxSeats; i++)
|
|
writer.Write((byte)(payload.Payload.Occupied(i) ? 1 : 0));
|
|
}
|
|
}
|
|
|
|
// The server's echo: uint16 sender (0xFF) + restart id, revision, seat count and kinds; never tokens.
|
|
public sealed class SeatPlanStateCodec : IStocCodec<Stamped<SeatPlan>>
|
|
{
|
|
public const int PayloadSize = SeatPlan.IdBytes + 2 + 1 + SeatPlan.MaxSeats;
|
|
public const int Size = StampedCodec.SeatBytes + PayloadSize;
|
|
|
|
public StocMessage Id => StocMessage.SeatPlan;
|
|
|
|
public Stamped<SeatPlan> Read(BinaryReader reader)
|
|
{
|
|
var remaining = StocCodecs.Remaining(reader);
|
|
if (remaining < Size)
|
|
throw new EndOfStreamException("STOC " + Id + ": " + remaining + " bytes, expected " + Size);
|
|
var seat = reader.ReadUInt16();
|
|
var restartId = reader.ReadBytes(SeatPlan.IdBytes);
|
|
var revision = reader.ReadUInt16();
|
|
var count = reader.ReadByte();
|
|
var kinds = new SeatPlanKind[SeatPlan.MaxSeats];
|
|
for (var i = 0; i < kinds.Length; i++)
|
|
kinds[i] = (SeatPlanKind)reader.ReadByte();
|
|
return new Stamped<SeatPlan>(seat, new SeatPlan(restartId, 0, revision, count, kinds, null));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<SeatPlan> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
writer.Write(SeatIds.Fixed(payload.Payload.RestartId));
|
|
writer.Write(payload.Payload.Revision);
|
|
writer.Write(payload.Payload.SeatCount);
|
|
for (var i = 0; i < SeatPlan.MaxSeats; i++)
|
|
writer.Write((byte)payload.Payload.Kind(i));
|
|
}
|
|
}
|
|
|
|
// Version, count, then count x (the 4 request bytes echoed + 24 answer bytes); no sender stamp (patch 0023).
|
|
public sealed class AttackPreviewResultCodec : IStocCodec<AttackPreviewResult>
|
|
{
|
|
public const int HeaderSize = 2;
|
|
|
|
public StocMessage Id => StocMessage.AttackPreview;
|
|
|
|
public AttackPreviewResult Read(BinaryReader reader)
|
|
{
|
|
StocCodecs.Require(reader, HeaderSize, Id);
|
|
var version = reader.ReadByte();
|
|
int count = reader.ReadByte();
|
|
StocCodecs.Require(reader, count * AttackPreviewEntry.Size, Id);
|
|
var entries = new AttackPreviewEntry[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
var pair = AttackPreviewQueryCodec.ReadPair(reader);
|
|
entries[i] = new AttackPreviewEntry(pair, reader.ReadBytes(AttackPreviewEntry.AnswerBytes));
|
|
}
|
|
return new AttackPreviewResult(version, entries);
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, AttackPreviewResult payload)
|
|
{
|
|
var count = payload.Entries.Length > AttackPreviewQuery.MaxPairs ? AttackPreviewQuery.MaxPairs : payload.Entries.Length;
|
|
writer.Write(payload.Version);
|
|
writer.Write((byte)count);
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
AttackPreviewQueryCodec.WritePair(writer, payload.Entries[i].Pair);
|
|
writer.Write(payload.Entries[i].Answer, 0, AttackPreviewEntry.AnswerBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class StampedCodec
|
|
{
|
|
public const int SeatBytes = 2;
|
|
}
|
|
|
|
// The server prefixes a relayed feature message with the sender's seat (uint16); the payload is the CTOS bytes.
|
|
public sealed class StampedCodec<T> : IStocCodec<Stamped<T>>
|
|
{
|
|
private readonly ICtosCodec<T> _payload;
|
|
|
|
public StocMessage Id { get; }
|
|
public int Size { get; }
|
|
|
|
public StampedCodec(StocMessage id, ICtosCodec<T> payload, int payloadSize)
|
|
{
|
|
Id = id;
|
|
_payload = payload;
|
|
Size = StampedCodec.SeatBytes + payloadSize;
|
|
}
|
|
|
|
public Stamped<T> Read(BinaryReader reader)
|
|
{
|
|
var remaining = StocCodecs.Remaining(reader);
|
|
if (remaining < Size)
|
|
throw new EndOfStreamException("STOC " + Id + ": " + remaining + " bytes, expected " + Size);
|
|
var seat = reader.ReadUInt16();
|
|
return new Stamped<T>(seat, _payload.Read(reader));
|
|
}
|
|
|
|
public void Write(BinaryWriter writer, Stamped<T> payload)
|
|
{
|
|
writer.Write((ushort)payload.Seat);
|
|
_payload.Write(writer, payload.Payload);
|
|
}
|
|
}
|
|
}
|