コード例 #1
0
    // Predict snapshot from baselines. Returns true if prediction is different from baseline 0 (if it need to be automatically predicted next frame).
    public static void PredictSnapshot(byte[] outputData, byte[] fieldsChangedPrediction, NetworkSchema schema, uint numBaselines, uint time0, byte[] baselineData0, uint time1, byte[] baselineData1, uint time2, byte[] baselineData2, uint time, byte fieldMask)
    {
        for (int i = 0, l = fieldsChangedPrediction.Length; i < l; ++i)
        {
            fieldsChangedPrediction[i] = 0;
        }

        if (numBaselines < 3)
        {
            System.Array.Copy(baselineData0, outputData, schema.GetByteSize());
            return;
        }

        var baselineStream0 = new ByteInputStream(baselineData0);
        var baselineStream1 = new ByteInputStream(baselineData1);
        var baselineStream2 = new ByteInputStream(baselineData2);
        var outputStream    = new ByteOutputStream(outputData);

        for (int i = 0; i < schema.fields.Count; ++i)
        {
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream0.GetBytePosition());
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream1.GetBytePosition());
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream2.GetBytePosition());

            var field = schema.fields[i];

            byte fieldByteOffset = (byte)((uint)i >> 3);
            byte fieldBitOffset  = (byte)((uint)i & 0x7);

            bool masked = (field.fieldMask & fieldMask) != 0;

            switch (field.fieldType)
            {
            case NetworkSchema.FieldType.Bool:
            {
                uint baseline0  = baselineStream0.ReadUInt8();
                uint baseline1  = baselineStream1.ReadUInt8();
                uint baseline2  = baselineStream2.ReadUInt8();
                uint prediction = baseline0;

                if (!masked)
                {
                    if (baseline0 != baseline1 && baseline1 != baseline2)
                    {
                        fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                    }
                }

                outputStream.WriteUInt8((byte)prediction);
                break;
            }

            case NetworkSchema.FieldType.UInt:
            case NetworkSchema.FieldType.Int:
            case NetworkSchema.FieldType.Float:
            {
                uint baseline0 = (uint)baselineStream0.ReadBits(field.bits);
                uint baseline1 = (uint)baselineStream1.ReadBits(field.bits);
                uint baseline2 = (uint)baselineStream2.ReadBits(field.bits);

                uint prediction = baseline0;
                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrong;
                        prediction = NetworkPrediction.PredictUint(numBaselines, time0, baseline0, time1, baseline1, time2, baseline2, time, out predictionLikelyWrong);
                        if (predictionLikelyWrong)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if (baseline0 != baseline1 && baseline1 != baseline2)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteBits(prediction, field.bits);          //RUTODO: fix this
                break;
            }

            case NetworkSchema.FieldType.Vector2:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        if (predictionLikelyWrongX || predictionLikelyWrongY)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1) && (bx1 != bx2 || by1 != by2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                break;
            }

            case NetworkSchema.FieldType.Vector3:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();
                uint bz0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();
                uint bz1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();
                uint bz2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                uint pz = bz0;

                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        bool predictionLikelyWrongZ;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        pz = NetworkPrediction.PredictUint(numBaselines, time0, bz0, time1, bz1, time2, bz2, time, out predictionLikelyWrongZ);

                        if (predictionLikelyWrongX || predictionLikelyWrongY || predictionLikelyWrongZ)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1 || bz0 != bz1) && (bx1 != bx2 || by1 != by2 || bz1 != bz2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                outputStream.WriteUInt32(pz);
                break;
            }

            case NetworkSchema.FieldType.Quaternion:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();
                uint bz0 = baselineStream0.ReadUInt32();
                uint bw0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();
                uint bz1 = baselineStream1.ReadUInt32();
                uint bw1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();
                uint bz2 = baselineStream2.ReadUInt32();
                uint bw2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                uint pz = bz0;
                uint pw = bw0;

                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        bool predictionLikelyWrongZ;
                        bool predictionLikelyWrongW;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        pz = NetworkPrediction.PredictUint(numBaselines, time0, bz0, time1, bz1, time2, bz2, time, out predictionLikelyWrongZ);
                        pw = NetworkPrediction.PredictUint(numBaselines, time0, bw0, time1, bw1, time2, bw2, time, out predictionLikelyWrongW);

                        if (predictionLikelyWrongX || predictionLikelyWrongY || predictionLikelyWrongZ || predictionLikelyWrongW)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1 || bz0 != bz1 || bw0 != bw1) && (bx1 != bx2 || by1 != by2 || bz1 != bz2 || bw1 != bw2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                outputStream.WriteUInt32(pz);
                outputStream.WriteUInt32(pw);
                break;
            }

            case NetworkSchema.FieldType.String:
            case NetworkSchema.FieldType.ByteArray:
            {
                baselineStream1.SkipByteArray(field.arraySize);
                baselineStream2.SkipByteArray(field.arraySize);
                outputStream.CopyByteArray(ref baselineStream0, field.arraySize);
                //TODO: predict me!
            }
            break;
            }
        }

        //fieldsChangedPrediction = 0;

        outputStream.Flush();
    }
コード例 #2
0
ファイル: DeltaReader.cs プロジェクト: JinKitamura/learn
    public static int Read <TInputStream>(ref TInputStream input, NetworkSchema schema, byte[] outputData, byte[] baselineData, byte[] fieldsChangedPrediction, byte fieldMask, ref uint hash) where TInputStream : NetworkCompression.IInputStream
    {
        GameDebug.Assert(baselineData != null);
        var outputStream   = new ByteOutputStream(outputData);
        var baselineStream = new ByteInputStream(baselineData);

        int numFields = schema.fields.Count;

        int skipContext = schema.id * NetworkConfig.maxContextsPerSchema + NetworkConfig.firstSchemaContext;

        for (int i = 0; i * 8 < numFields; i++)
        {
            uint value = input.ReadPackedNibble(skipContext + 2 * i + 0);
            value |= input.ReadPackedNibble(skipContext + 2 * i + 1) << 4;
            fieldsNotPredicted[i] = (byte)(value ^ fieldsChangedPrediction[i]);
        }

        for (int i = 0; i < numFields; ++i)
        {
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream.GetBytePosition());
            int fieldStartContext = schema.fields[i].startContext;

            var field = schema.fields[i];

            byte fieldByteOffset = (byte)((uint)i >> 3);
            byte fieldBitOffset  = (byte)((uint)i & 0x7);

            bool skip   = (fieldsNotPredicted[fieldByteOffset] & (1 << fieldBitOffset)) == 0;
            bool masked = ((field.fieldMask & fieldMask) != 0);

            skip = skip || masked;

            switch (field.fieldType)
            {
            case NetworkSchema.FieldType.Bool:
            {
                uint value = baselineStream.ReadUInt8();
                if (!skip)
                {
                    value = input.ReadRawBits(1);
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, value);
                }

                outputStream.WriteUInt8((byte)value);
                break;
            }

            case NetworkSchema.FieldType.UInt:
            case NetworkSchema.FieldType.Int:
            case NetworkSchema.FieldType.Float:
            {
                uint baseline = (uint)baselineStream.ReadBits(field.bits);

                uint value = baseline;
                if (!skip)
                {
                    if (field.delta)
                    {
                        value = input.ReadPackedUIntDelta(baseline, fieldStartContext);
                    }
                    else
                    {
                        value = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, value);
                }

                outputStream.WriteBits(value, field.bits);          //RUTODO: fix this
                break;
            }

            case NetworkSchema.FieldType.Vector2:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);

                break;
            }

            case NetworkSchema.FieldType.Vector3:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();
                uint bz = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                uint vz = bz;

                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                        vz = input.ReadPackedUIntDelta(bz, fieldStartContext + 2);
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                        vz = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vz);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);
                outputStream.WriteUInt32(vz);
                break;
            }

            case NetworkSchema.FieldType.Quaternion:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();
                uint bz = baselineStream.ReadUInt32();
                uint bw = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                uint vz = bz;
                uint vw = bw;

                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                        vz = input.ReadPackedUIntDelta(bz, fieldStartContext + 2);
                        vw = input.ReadPackedUIntDelta(bw, fieldStartContext + 3);
                        //RUTODO: normalize
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                        vz = input.ReadRawBits(field.bits);
                        vw = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vz);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vw);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);
                outputStream.WriteUInt32(vz);
                outputStream.WriteUInt32(vw);
                break;
            }

            case NetworkSchema.FieldType.String:
            case NetworkSchema.FieldType.ByteArray:
            {
                // TODO : Do a better job with deltaing strings and buffers
                if (!skip)
                {
                    baselineStream.SkipByteArray(field.arraySize);
                    outputStream.CopyByteArray <TInputStream>(ref input, field.arraySize, fieldStartContext);
                }
                else
                {
                    outputStream.CopyByteArray(ref baselineStream, field.arraySize);
                }

                if (!masked)
                {
                    hash += 0;         // TODO (hash strings and bytearrays as well)
                }
            }
            break;
            }
        }
        outputStream.Flush();
        return(outputStream.GetBytePosition());
    }