public static void EncodeDelta( IRailStateConstruction stateCreator, RailBitBuffer buffer, RailStateDelta delta) { // Write: [EntityId] buffer.WriteEntityId(delta.EntityId); // Write: [IsFrozen] buffer.WriteBool(delta.IsFrozen); if (delta.IsFrozen == false) { // Write: [FactoryType] RailState state = delta.State; buffer.WriteInt(stateCreator.EntityTypeCompressor, state.FactoryType); // Write: [IsRemoved] buffer.WriteBool(delta.RemovedTick.IsValid); if (delta.RemovedTick.IsValid) // Write: [RemovedTick] { buffer.WriteTick(delta.RemovedTick); } // Write: [HasControllerData] buffer.WriteBool(state.HasControllerData); // Write: [HasImmutableData] buffer.WriteBool(state.HasImmutableData); if (state.HasImmutableData) // Write: [Immutable Data] { state.DataSerializer.EncodeImmutableData(buffer); } // Write: [Flags] buffer.Write(state.DataSerializer.FlagBits, state.Flags); // Write: [Mutable Data] state.DataSerializer.EncodeMutableData(buffer, state.Flags); if (state.HasControllerData) { // Write: [Controller Data] state.DataSerializer.EncodeControllerData(buffer); // Write: [Command Ack] buffer.WriteTick(delta.CommandAck); } } }
public static RailStateDelta DecodeDelta( IRailStateConstruction stateCreator, RailBitBuffer buffer, Tick packetTick) { RailStateDelta delta = stateCreator.CreateDelta(); RailState state = null; Tick commandAck = Tick.INVALID; Tick removedTick = Tick.INVALID; // Read: [EntityId] EntityId entityId = buffer.ReadEntityId(); // Read: [IsFrozen] bool isFrozen = buffer.ReadBool(); if (isFrozen == false) { // Read: [FactoryType] int factoryType = buffer.ReadInt(stateCreator.EntityTypeCompressor); state = stateCreator.CreateState(factoryType); // Read: [IsRemoved] bool isRemoved = buffer.ReadBool(); if (isRemoved) // Read: [RemovedTick] { removedTick = buffer.ReadTick(); } // Read: [HasControllerData] state.HasControllerData = buffer.ReadBool(); // Read: [HasImmutableData] state.HasImmutableData = buffer.ReadBool(); if (state.HasImmutableData) // Read: [Immutable Data] { state.DataSerializer.DecodeImmutableData(buffer); } // Read: [Flags] state.Flags = buffer.Read(state.DataSerializer.FlagBits); // Read: [Mutable Data] state.DataSerializer.DecodeMutableData(buffer, state.Flags); if (state.HasControllerData) { // Read: [Controller Data] state.DataSerializer.DecodeControllerData(buffer); // Read: [Command Ack] commandAck = buffer.ReadTick(); } } delta.Initialize(packetTick, entityId, state, removedTick, commandAck, isFrozen); return(delta); }
public static RailStateDelta Create( IRailStateConstruction stateCreator, EntityId entityId, RailState current, IEnumerable <RailStateRecord> basisStates, bool includeControllerData, bool includeImmutableData, Tick commandAck, Tick removedTick, bool forceAllMutable) { bool shouldReturn = forceAllMutable || includeControllerData && current.HasControllerData || includeImmutableData || removedTick.IsValid; // We don't know what the client has and hasn't received from us since // the acked state. As a result, we'll build diff flags across all // states sent *between* the latest and current. This accounts for // situations where a value changes and then quickly changes back, // while appearing as no change on just the current-latest diff. uint flags = 0; if (forceAllMutable == false && basisStates != null) { foreach (RailStateRecord record in basisStates) { flags |= current.DataSerializer.CompareMutableData(record.State.DataSerializer); } } else { flags = FLAGS_ALL; } if (flags == FLAGS_NONE && !shouldReturn) { return(null); } RailState deltaState = stateCreator.CreateState(current.FactoryType); deltaState.HasImmutableData = includeImmutableData; if (includeImmutableData) { deltaState.DataSerializer.ApplyImmutableFrom(current.DataSerializer); } deltaState.Flags = flags; deltaState.DataSerializer.ApplyMutableFrom(current.DataSerializer, deltaState.Flags); deltaState.HasControllerData = includeControllerData; if (includeControllerData) { deltaState.DataSerializer.ApplyControllerFrom(current.DataSerializer); } // We don't need to include a tick when sending -- it's in the packet RailStateDelta delta = stateCreator.CreateDelta(); delta.Initialize(Tick.INVALID, entityId, deltaState, removedTick, commandAck, false); return(delta); }