Example #1
0
 public void Deserialize(uint tick, ref __GHOST_NAME__SnapshotData baseline, ref DataStreamReader reader,
                         NetworkCompressionModel compressionModel)
 {
     this.tick = tick;
     #region __GHOST_READ_CHANGE_MASK__
     changeMask__GHOST_MASK_BATCH__ = reader.ReadPackedUIntDelta(baseline.changeMask__GHOST_MASK_BATCH__, compressionModel);
     #endregion
     #region __GHOST_READ_IS_PREDICTED__
     bool isPredicted = reader.ReadPackedUInt(compressionModel) != 0;
     #endregion
     #region __GHOST_READ__
     #endregion
     #region __GHOST_BEGIN_READ_PREDICTED__
     if (isPredicted)
     {
         #endregion
         #region __GHOST_READ_PREDICTED__
         #endregion
         #region __GHOST_END_READ_PREDICTED__
     }
     #endregion
     #region __GHOST_BEGIN_READ_INTERPOLATED__
     if (!isPredicted)
     {
         #endregion
         #region __GHOST_READ_INTERPOLATED__
         #endregion
         #region __GHOST_END_READ_INTERPOLATED__
     }
     #endregion
 }
        public void ReadWritePackedUIntWithDeferred()
        {
            using (var dataStream = new DataStreamWriter(300 * 4, Allocator.Persistent))
                using (var compressionModel = new NetworkCompressionModel(Allocator.Persistent))
                {
                    uint base_val = 2000;
                    uint count    = 277;
                    var  def      = dataStream.Write((int)0);
                    for (uint i = 0; i < count; ++i)
                    {
                        dataStream.WritePackedUInt(base_val + i, compressionModel);
                    }

                    dataStream.Flush();
                    def.Update(1979);
                    def = dataStream.Write((int)0);
                    def.Update(1979);
                    dataStream.Flush();
                    var reader = new DataStreamReader(dataStream, 0, dataStream.Length);
                    var ctx    = default(DataStreamReader.Context);
                    Assert.AreEqual(1979, reader.ReadInt(ref ctx));
                    for (uint i = 0; i < count; ++i)
                    {
                        var val = reader.ReadPackedUInt(ref ctx, compressionModel);
                        Assert.AreEqual(base_val + i, val);
                    }
                    Assert.AreEqual(1979, reader.ReadInt(ref ctx));
                }
        }
Example #3
0
        public void ReadWritePackedUIntWithDeferred()
        {
            using (var compressionModel = new NetworkCompressionModel(Allocator.Persistent))
            {
                var  dataStream = new DataStreamWriter(300 * 4, Allocator.Temp);
                uint base_val   = 2000;
                uint count      = 277;
                var  def        = dataStream;
                dataStream.WriteInt((int)0);
                for (uint i = 0; i < count; ++i)
                {
                    dataStream.WritePackedUInt(base_val + i, compressionModel);
                }

                dataStream.Flush();
                def.WriteInt(1979);
                def = dataStream;
                dataStream.WriteInt((int)0);
                def.WriteInt(1979);
                dataStream.Flush();
                var reader = new DataStreamReader(dataStream.AsNativeArray());
                Assert.AreEqual(1979, reader.ReadInt());
                for (uint i = 0; i < count; ++i)
                {
                    var val = reader.ReadPackedUInt(compressionModel);
                    Assert.AreEqual(base_val + i, val);
                }
                Assert.AreEqual(1979, reader.ReadInt());
            }
        }
Example #4
0
    public void Deserialize(uint tick, ref ShipSnapshotData baseline, DataStreamReader reader, ref DataStreamReader.Context ctx,
                            NetworkCompressionModel compressionModel)
    {
        this.tick   = tick;
        changeMask0 = reader.ReadPackedUIntDelta(ref ctx, baseline.changeMask0, compressionModel);
        bool isPredicted = reader.ReadPackedUInt(ref ctx, compressionModel) != 0;

        if ((changeMask0 & (1 << 2)) != 0)
        {
            RotationValue = reader.ReadPackedIntDelta(ref ctx, baseline.RotationValue, compressionModel);
        }
        else
        {
            RotationValue = baseline.RotationValue;
        }
        if ((changeMask0 & (1 << 3)) != 0)
        {
            TranslationValueX = reader.ReadPackedIntDelta(ref ctx, baseline.TranslationValueX, compressionModel);
            TranslationValueY = reader.ReadPackedIntDelta(ref ctx, baseline.TranslationValueY, compressionModel);
        }
        else
        {
            TranslationValueX = baseline.TranslationValueX;
            TranslationValueY = baseline.TranslationValueY;
        }
        if (isPredicted)
        {
            if ((changeMask0 & (1 << 0)) != 0)
            {
                PlayerIdComponentDataPlayerId = reader.ReadPackedIntDelta(ref ctx, baseline.PlayerIdComponentDataPlayerId, compressionModel);
            }
            else
            {
                PlayerIdComponentDataPlayerId = baseline.PlayerIdComponentDataPlayerId;
            }
            if ((changeMask0 & (1 << 4)) != 0)
            {
                VelocityValueX = reader.ReadPackedIntDelta(ref ctx, baseline.VelocityValueX, compressionModel);
                VelocityValueY = reader.ReadPackedIntDelta(ref ctx, baseline.VelocityValueY, compressionModel);
            }
            else
            {
                VelocityValueX = baseline.VelocityValueX;
                VelocityValueY = baseline.VelocityValueY;
            }
        }
        if (!isPredicted)
        {
            if ((changeMask0 & (1 << 1)) != 0)
            {
                ShipStateComponentDataState = reader.ReadPackedIntDelta(ref ctx, baseline.ShipStateComponentDataState, compressionModel);
            }
            else
            {
                ShipStateComponentDataState = baseline.ShipStateComponentDataState;
            }
        }
    }
Example #5
0
 public void Deserialize(uint tick, ref ShipSnapshotData baseline, DataStreamReader reader, ref DataStreamReader.Context ctx,
                         NetworkCompressionModel compressionModel)
 {
     this.tick = tick;
     playerId  = (int)reader.ReadPackedUIntDelta(ref ctx, (uint)baseline.playerId, compressionModel);
     posX      = reader.ReadPackedIntDelta(ref ctx, baseline.posX, compressionModel);
     posY      = reader.ReadPackedIntDelta(ref ctx, baseline.posY, compressionModel);
     rot       = reader.ReadPackedIntDelta(ref ctx, baseline.rot, compressionModel);
     state     = (int)reader.ReadPackedUInt(ref ctx, compressionModel);
 }
Example #6
0
            private bool DeserializeEntity(uint serverTick, ref DataStreamReader dataStream, ref DeserializeData data)
            {
                if (data.targetArchLen == 0)
                {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                    data.curPos = dataStream.GetBitsRead();
                    if (data.statCount > 0)
                    {
                        int statType = (int)data.targetArch;
                        netStats[statType * 3 + 4] = netStats[statType * 3 + 4] + data.statCount;
                        netStats[statType * 3 + 5] = netStats[statType * 3 + 5] + (uint)(data.curPos - data.startPos);
                        netStats[statType * 3 + 6] = netStats[statType * 3 + 6] + data.uncompressedCount;
                    }

                    data.startPos          = data.curPos;
                    data.statCount         = 0;
                    data.uncompressedCount = 0;
#endif
                    data.targetArch    = dataStream.ReadPackedUInt(compressionModel);
                    data.targetArchLen = dataStream.ReadPackedUInt(compressionModel);
                }

                --data.targetArchLen;

                if (data.baselineLen == 0)
                {
                    data.baselineTick  = serverTick - dataStream.ReadPackedUInt(compressionModel);
                    data.baselineTick2 = serverTick - dataStream.ReadPackedUInt(compressionModel);
                    data.baselineTick3 = serverTick - dataStream.ReadPackedUInt(compressionModel);
                    data.baselineLen   = dataStream.ReadPackedUInt(compressionModel);
                    if (data.baselineTick3 != serverTick &&
                        (data.baselineTick3 == data.baselineTick2 || data.baselineTick2 == data.baselineTick))
                    {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                        UnityEngine.Debug.LogError("Received invalid snapshot baseline from server");
#endif
                        return(false);
                    }
                }

                --data.baselineLen;

                int  ghostId         = (int)dataStream.ReadPackedUInt(compressionModel);
                uint serverSpawnTick = 0;
                if (data.baselineTick == serverTick)
                {
                    serverSpawnTick = dataStream.ReadPackedUInt(compressionModel);
                }
                var   typeData        = GhostTypeCollection[(int)data.targetArch];
                int   changeMaskUints = GhostCollectionSystem.ChangeMaskArraySizeInUInts(typeData.ChangeMaskBits);
                int   snapshotOffset;
                int   snapshotSize = typeData.SnapshotSize;
                byte *baselineData = (byte *)UnsafeUtility.Malloc(snapshotSize, 16, Allocator.Temp);
                UnsafeUtility.MemClear(baselineData, snapshotSize);
                Entity gent;
                DynamicBuffer <SnapshotDataBuffer> snapshotDataBuffer;
                SnapshotData snapshotDataComponent;
                byte *       snapshotData;
                bool         existingGhost = ghostEntityMap.TryGetValue(ghostId, out gent);
                if (existingGhost && snapshotDataBufferFromEntity.HasComponent(gent) && ghostFromEntity[gent].ghostType == data.targetArch)
                {
                    snapshotDataBuffer = snapshotDataBufferFromEntity[gent];
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    if (snapshotDataBuffer.Length != snapshotSize * GhostSystemConstants.SnapshotHistorySize)
                    {
                        throw new InvalidOperationException($"Invalid snapshot buffer size");
                    }
#endif
                    snapshotData                      = (byte *)snapshotDataBuffer.GetUnsafePtr();
                    snapshotDataComponent             = snapshotDataFromEntity[gent];
                    snapshotDataComponent.LatestIndex = (snapshotDataComponent.LatestIndex + 1) % GhostSystemConstants.SnapshotHistorySize;
                    snapshotDataFromEntity[gent]      = snapshotDataComponent;
                    if (data.baselineTick != serverTick)
                    {
                        for (int bi = 0; bi < snapshotDataBuffer.Length; bi += snapshotSize)
                        {
                            if (*(uint *)(snapshotData + bi) == data.baselineTick)
                            {
                                UnsafeUtility.MemCpy(baselineData, snapshotData + bi, snapshotSize);
                                break;
                            }
                        }

                        if (*(uint *)baselineData == 0)
                        {
                            return(false); // Ack desync detected
                        }
                    }

                    if (data.baselineTick3 != serverTick)
                    {
                        byte *baselineData2 = null;
                        byte *baselineData3 = null;
                        for (int bi = 0; bi < snapshotDataBuffer.Length; bi += snapshotSize)
                        {
                            if (*(uint *)(snapshotData + bi) == data.baselineTick2)
                            {
                                baselineData2 = snapshotData + bi;
                            }

                            if (*(uint *)(snapshotData + bi) == data.baselineTick3)
                            {
                                baselineData3 = snapshotData + bi;
                            }
                        }

                        if (baselineData2 == null || baselineData3 == null)
                        {
                            return(false); // Ack desync detected
                        }
                        snapshotOffset = GhostCollectionSystem.SnapshotSizeAligned(4 + changeMaskUints * 4);
                        var predictor = new GhostDeltaPredictor(serverTick, data.baselineTick, data.baselineTick2, data.baselineTick3);
                        for (int comp = 0; comp < typeData.NumComponents; ++comp)
                        {
                            int compIdx = GhostComponentIndex[typeData.FirstComponent + comp].ComponentIndex;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                            if (snapshotOffset + GhostComponentCollection[compIdx].SnapshotSize > snapshotSize)
                            {
                                throw new InvalidOperationException("Snapshot buffer overflow during predict");
                            }
#endif
                            GhostComponentCollection[compIdx].PredictDelta.Ptr.Invoke((IntPtr)(baselineData + snapshotOffset), (IntPtr)(baselineData2 + snapshotOffset), (IntPtr)(baselineData3 + snapshotOffset), ref predictor);
                            snapshotOffset += GhostCollectionSystem.SnapshotSizeAligned(GhostComponentCollection[compIdx].SnapshotSize);
                        }
                    }
                }
                else
                {
                    if (existingGhost)
                    {
                        // The ghost entity map is out of date, clean it up
                        ghostEntityMap.Remove(ghostId);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                        if (ghostFromEntity.HasComponent(gent) && ghostFromEntity[gent].ghostType != data.targetArch)
                        {
                            UnityEngine.Debug.LogError("Received a ghost with an invalid ghost type");
                        }
                        UnityEngine.Debug.LogError("Found a ghost in the ghost map which does not have an entity connected to it. This can happen if you delete ghost entities on the client.");
#endif
                    }
                    if (data.baselineTick != serverTick)
                    {
                        // If the server specifies a baseline for a ghost we do not have that is an error
                        return(false);
                    }
                    ++data.newGhosts;

                    var ghostSpawnBuffer = ghostSpawnBufferFromEntity[ghostSpawnEntity];
                    snapshotDataBuffer = snapshotDataBufferFromEntity[ghostSpawnEntity];
                    var snapshotDataBufferOffset = snapshotDataBuffer.Length;
                    ghostSpawnBuffer.Add(new GhostSpawnBuffer {
                        GhostType = (int)data.targetArch, GhostID = ghostId, DataOffset = snapshotDataBufferOffset, ClientSpawnTick = serverTick, ServerSpawnTick = serverSpawnTick
                    });
                    snapshotDataBuffer.ResizeUninitialized(snapshotDataBufferOffset + snapshotSize);
                    snapshotData = (byte *)snapshotDataBuffer.GetUnsafePtr() + snapshotDataBufferOffset;
                    UnsafeUtility.MemClear(snapshotData, snapshotSize);
                    snapshotDataComponent = new SnapshotData {
                        SnapshotSize = snapshotSize, LatestIndex = 0
                    };
                }
                int maskOffset = 0;
                snapshotOffset          = GhostCollectionSystem.SnapshotSizeAligned(4 + changeMaskUints * 4);
                snapshotData           += snapshotSize * snapshotDataComponent.LatestIndex;
                *(uint *)(snapshotData) = serverTick;
                uint *changeMask = (uint *)(snapshotData + 4);
                for (int cm = 0; cm < changeMaskUints; ++cm)
                {
                    changeMask[cm] = dataStream.ReadPackedUIntDelta(((uint *)(baselineData + 4))[cm], compressionModel);
                }
                for (int comp = 0; comp < typeData.NumComponents; ++comp)
                {
                    int compIdx = GhostComponentIndex[typeData.FirstComponent + comp].ComponentIndex;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    if (maskOffset + GhostComponentCollection[compIdx].ChangeMaskBits > typeData.ChangeMaskBits || snapshotOffset + GhostComponentCollection[compIdx].SnapshotSize > snapshotSize)
                    {
                        throw new InvalidOperationException("Snapshot buffer overflow during deserialize");
                    }
#endif
                    GhostComponentCollection[compIdx].Deserialize.Ptr.Invoke((IntPtr)(snapshotData + snapshotOffset), (IntPtr)(baselineData + snapshotOffset), ref dataStream, ref compressionModel, (IntPtr)changeMask, maskOffset);
                    snapshotOffset += GhostCollectionSystem.SnapshotSizeAligned(GhostComponentCollection[compIdx].SnapshotSize);
                    maskOffset     += GhostComponentCollection[compIdx].ChangeMaskBits;
                }

#if UNITY_EDITOR || DEVELOPMENT_BUILD
                ++data.statCount;
                if (data.baselineTick == serverTick)
                {
                    ++data.uncompressedCount;
                }
#endif

                if (typeData.IsGhostGroup != 0)
                {
                    var groupLen = dataStream.ReadPackedUInt(compressionModel);
                    for (var i = 0; i < groupLen; ++i)
                    {
                        if (!DeserializeEntity(serverTick, ref dataStream, ref data))
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
 public void ReadFrom(ref DataStreamReader.Context ctx, DataStreamReader reader, ref CurrentWeapon baseline, DeserializeClientData jobData)
 {
     jobData.GhostToEntityMap.TryGetValue(reader.ReadPackedUInt(ref ctx, jobData.NetworkCompressionModel), out Target);
 }
Example #8
0
    public void Deserialize(uint tick, ref PilotSnapshotData baseline, ref DataStreamReader reader,
                            NetworkCompressionModel compressionModel)
    {
        this.tick   = tick;
        changeMask0 = reader.ReadPackedUIntDelta(baseline.changeMask0, compressionModel);
        bool isPredicted = reader.ReadPackedUInt(compressionModel) != 0;

        if ((changeMask0 & (1 << 0)) != 0)
        {
            PilotDataPlayerId = reader.ReadPackedIntDelta(baseline.PilotDataPlayerId, compressionModel);
        }
        else
        {
            PilotDataPlayerId = baseline.PilotDataPlayerId;
        }
        if ((changeMask0 & (1 << 5)) != 0)
        {
            RotationValueX = reader.ReadPackedIntDelta(baseline.RotationValueX, compressionModel);
            RotationValueY = reader.ReadPackedIntDelta(baseline.RotationValueY, compressionModel);
            RotationValueZ = reader.ReadPackedIntDelta(baseline.RotationValueZ, compressionModel);
            RotationValueW = reader.ReadPackedIntDelta(baseline.RotationValueW, compressionModel);
        }
        else
        {
            RotationValueX = baseline.RotationValueX;
            RotationValueY = baseline.RotationValueY;
            RotationValueZ = baseline.RotationValueZ;
            RotationValueW = baseline.RotationValueW;
        }
        if ((changeMask0 & (1 << 6)) != 0)
        {
            TranslationValueX = reader.ReadPackedIntDelta(baseline.TranslationValueX, compressionModel);
            TranslationValueY = reader.ReadPackedIntDelta(baseline.TranslationValueY, compressionModel);
            TranslationValueZ = reader.ReadPackedIntDelta(baseline.TranslationValueZ, compressionModel);
        }
        else
        {
            TranslationValueX = baseline.TranslationValueX;
            TranslationValueY = baseline.TranslationValueY;
            TranslationValueZ = baseline.TranslationValueZ;
        }
        if (isPredicted)
        {
            if ((changeMask0 & (1 << 1)) != 0)
            {
                PilotMovementSystemDatavelocityX = reader.ReadPackedIntDelta(baseline.PilotMovementSystemDatavelocityX, compressionModel);
                PilotMovementSystemDatavelocityY = reader.ReadPackedIntDelta(baseline.PilotMovementSystemDatavelocityY, compressionModel);
                PilotMovementSystemDatavelocityZ = reader.ReadPackedIntDelta(baseline.PilotMovementSystemDatavelocityZ, compressionModel);
            }
            else
            {
                PilotMovementSystemDatavelocityX = baseline.PilotMovementSystemDatavelocityX;
                PilotMovementSystemDatavelocityY = baseline.PilotMovementSystemDatavelocityY;
                PilotMovementSystemDatavelocityZ = baseline.PilotMovementSystemDatavelocityZ;
            }
            if ((changeMask0 & (1 << 2)) != 0)
            {
                PilotMovementSystemDataonGround = reader.ReadPackedUIntDelta(baseline.PilotMovementSystemDataonGround, compressionModel);
            }
            else
            {
                PilotMovementSystemDataonGround = baseline.PilotMovementSystemDataonGround;
            }
            if ((changeMask0 & (1 << 3)) != 0)
            {
                PilotMovementSystemDatajumpCooldown = reader.ReadPackedUIntDelta(baseline.PilotMovementSystemDatajumpCooldown, compressionModel);
            }
            else
            {
                PilotMovementSystemDatajumpCooldown = baseline.PilotMovementSystemDatajumpCooldown;
            }
            if ((changeMask0 & (1 << 4)) != 0)
            {
                PilotMovementSystemDatamovementMode = reader.ReadPackedIntDelta(baseline.PilotMovementSystemDatamovementMode, compressionModel);
            }
            else
            {
                PilotMovementSystemDatamovementMode = baseline.PilotMovementSystemDatamovementMode;
            }
        }
    }