Beispiel #1
0
 public static void WriteColor32(this NetworkWriter writer, Color32 value)
 {
     writer.WriteByte(value.r);
     writer.WriteByte(value.g);
     writer.WriteByte(value.b);
     writer.WriteByte(value.a);
 }
        [EditorBrowsable(EditorBrowsableState.Never)] // public only for tests
        public static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Quaternion rotation, Compression compressRotation, Vector3 scale)
        {
            // serialize position
            writer.WriteVector3(position);

            // serialize rotation
            // writing quaternion = 16 byte
            // writing euler angles = 12 byte
            // -> quaternion->euler->quaternion always works.
            // -> gimbal lock only occurs when adding.
            Vector3 euler = rotation.eulerAngles;

            if (compressRotation == Compression.None)
            {
                // write 3 floats = 12 byte
                writer.WriteSingle(euler.x);
                writer.WriteSingle(euler.y);
                writer.WriteSingle(euler.z);
            }
            else if (compressRotation == Compression.Much)
            {
                // write 3 byte. scaling [0,360] to [0,255]
                writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.x, 0, 360, byte.MinValue, byte.MaxValue));
                writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.y, 0, 360, byte.MinValue, byte.MaxValue));
                writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.z, 0, 360, byte.MinValue, byte.MaxValue));
            }
            else if (compressRotation == Compression.Lots)
            {
                // write 2 byte, 5 bits for each float
                writer.WriteUInt16(FloatBytePacker.PackThreeFloatsIntoUShort(euler.x, euler.y, euler.z, 0, 360));
            }

            // serialize scale
            writer.WriteVector3(scale);
        }
Beispiel #3
0
 public static void WriteUInt(this NetworkWriter writer, uint value)
 {
     writer.WriteByte((byte)value);
     writer.WriteByte((byte)(value >> 8));
     writer.WriteByte((byte)(value >> 16));
     writer.WriteByte((byte)(value >> 24));
 }
        public override void OnSerializeDelta(NetworkWriter writer)
        {
            // write all the queued up changes
            writer.WriteUInt((uint)changes.Count);

            for (int i = 0; i < changes.Count; i++)
            {
                Change change = changes[i];
                writer.WriteByte((byte)change.operation);

                switch (change.operation)
                {
                case Operation.OP_ADD:
                    writer.Write(change.item);
                    break;

                case Operation.OP_CLEAR:
                    break;

                case Operation.OP_REMOVE:
                    writer.Write(change.item);
                    break;
                }
            }
        }
Beispiel #5
0
        public void OnSerializeDelta(NetworkWriter writer)
        {
            // write all the queued up changes
            writer.WritePackedUInt32((uint)changes.Count);

            for (int i = 0; i < changes.Count; i++)
            {
                Change change = changes[i];
                writer.WriteByte((byte)change.operation);

                switch (change.operation)
                {
                case Operation.OP_ADD:
                    SerializeItem(writer, change.item);
                    break;

                case Operation.OP_CLEAR:
                    break;

                case Operation.OP_REMOVEAT:
                    writer.WritePackedUInt32((uint)change.index);
                    break;

                case Operation.OP_INSERT:
                case Operation.OP_SET:
                    writer.WritePackedUInt32((uint)change.index);
                    SerializeItem(writer, change.item);
                    break;
                }
            }
        }
Beispiel #6
0
 public static void WriteByteNullable(this NetworkWriter writer, byte?value)
 {
     writer.WriteBool(value.HasValue);
     if (value.HasValue)
     {
         writer.WriteByte(value.Value);
     }
 }
Beispiel #7
0
 public static void WriteULong(this NetworkWriter writer, ulong value)
 {
     writer.WriteByte((byte)value);
     writer.WriteByte((byte)(value >> 8));
     writer.WriteByte((byte)(value >> 16));
     writer.WriteByte((byte)(value >> 24));
     writer.WriteByte((byte)(value >> 32));
     writer.WriteByte((byte)(value >> 40));
     writer.WriteByte((byte)(value >> 48));
     writer.WriteByte((byte)(value >> 56));
 }
Beispiel #8
0
 public static void WriteNetworkBehaviour(this NetworkWriter writer, NetworkBehaviour value)
 {
     if (value == null)
     {
         writer.WriteUInt(0);
         return;
     }
     writer.WriteUInt(value.netId);
     writer.WriteByte((byte)value.ComponentIndex);
 }
Beispiel #9
0
        public static void WritePackedUInt64(this NetworkWriter writer, ulong value)
        {
            if (value <= 240)
            {
                writer.WriteByte((byte)value);
                return;
            }
            if (value <= 2287)
            {
                writer.WriteByte((byte)(((value - 240) >> 8) + 241));
                writer.WriteByte((byte)((value - 240) & 0xFF));
                return;
            }
            if (value <= 67823)
            {
                writer.WriteByte((byte)249);
                writer.WriteByte((byte)((value - 2288) >> 8));
                writer.WriteByte((byte)((value - 2288) & 0xFF));
                return;
            }
            if (value <= 16777215)
            {
                writer.WriteByte((byte)250);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                return;
            }
            if (value <= 4294967295)
            {
                writer.WriteByte((byte)251);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                writer.WriteByte((byte)((value >> 24) & 0xFF));
                return;
            }
            if (value <= 1099511627775)
            {
                writer.WriteByte((byte)252);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                writer.WriteByte((byte)((value >> 24) & 0xFF));
                writer.WriteByte((byte)((value >> 32) & 0xFF));
                return;
            }
            if (value <= 281474976710655)
            {
                writer.WriteByte((byte)253);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                writer.WriteByte((byte)((value >> 24) & 0xFF));
                writer.WriteByte((byte)((value >> 32) & 0xFF));
                writer.WriteByte((byte)((value >> 40) & 0xFF));
                return;
            }
            if (value <= 72057594037927935)
            {
                writer.WriteByte((byte)254);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                writer.WriteByte((byte)((value >> 24) & 0xFF));
                writer.WriteByte((byte)((value >> 32) & 0xFF));
                writer.WriteByte((byte)((value >> 40) & 0xFF));
                writer.WriteByte((byte)((value >> 48) & 0xFF));
                return;
            }

            // all others
            {
                writer.WriteByte((byte)255);
                writer.WriteByte((byte)(value & 0xFF));
                writer.WriteByte((byte)((value >> 8) & 0xFF));
                writer.WriteByte((byte)((value >> 16) & 0xFF));
                writer.WriteByte((byte)((value >> 24) & 0xFF));
                writer.WriteByte((byte)((value >> 32) & 0xFF));
                writer.WriteByte((byte)((value >> 40) & 0xFF));
                writer.WriteByte((byte)((value >> 48) & 0xFF));
                writer.WriteByte((byte)((value >> 56) & 0xFF));
            }
        }
Beispiel #10
0
 public static void WriteUInt16(this NetworkWriter writer, ushort value)
 {
     writer.WriteByte((byte)(value & 0xFF));
     writer.WriteByte((byte)(value >> 8));
 }
Beispiel #11
0
 public static void WriteBoolean(this NetworkWriter writer, bool value) => writer.WriteByte((byte)(value ? 1 : 0));
Beispiel #12
0
 public static void WriteSByte(this NetworkWriter writer, sbyte value) => writer.WriteByte((byte)value);
Beispiel #13
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WriteByte(error);
 }
Beispiel #14
0
 public static void WriteUShort(this NetworkWriter writer, ushort value)
 {
     writer.WriteByte((byte)value);
     writer.WriteByte((byte)(value >> 8));
 }
Beispiel #15
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WriteString(sceneName);
     writer.WriteByte((byte)sceneMode);
     writer.WriteByte((byte)physicsMode);
 }
Beispiel #16
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WriteString(sceneName);
     writer.WriteByte((byte)sceneOperation);
     writer.WriteBoolean(customHandling);
 }
Beispiel #17
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WriteByte(value);
 }
Beispiel #18
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WriteString(sceneName);
     writer.WriteByte((byte)sceneOperation);
 }
Beispiel #19
0
 public static void WriteByte(this NetworkWriter writer, byte value) => writer.WriteByte(value);
Beispiel #20
0
        public override bool OnSerialize(NetworkWriter writer, bool initialState)
        {
            if (initialState)
            {
                // Write number of dirty variables, in this case all of them are considered dirty.
                writer.WriteByte((byte)hashToType.Count);

                // Write all.
                foreach (var pair in hashToType)
                {
                    int hash = pair.Key;
                    var type = pair.Value;

                    writer.WriteInt32(hash);

                    switch (type)
                    {
                    case AnimatorControllerParameterType.Float:
                        writer.WriteSingle(floats[hash]);
                        break;

                    case AnimatorControllerParameterType.Int:
                        writer.WriteInt32(ints[hash]);
                        break;

                    case AnimatorControllerParameterType.Bool:
                        writer.WriteBoolean(bools[hash]);
                        break;
                    }
                }
            }
            else
            {
                ulong dirtyMask = this.syncVarDirtyBits;

                // Count the number of dirty variables and write that number to stream.
                byte dirtyCount = 0;
                for (int i = 0; i < 64; i++)
                {
                    ulong mask  = 1UL << i;
                    bool  isOne = (dirtyMask & mask) == mask;

                    if (isOne)
                    {
                        dirtyCount++;
                    }
                }
                writer.WriteByte(dirtyCount);

                // Write only changed variables.
                for (int i = 0; i < hashToType.Count; i++)
                {
                    ulong mask = 1UL << i;

                    bool changed = (dirtyMask & mask) == mask;

                    if (changed)
                    {
                        int hash = maskToHash[mask];
                        var type = hashToType[hash];

                        writer.WriteInt32(hash);
                        switch (type)
                        {
                        case AnimatorControllerParameterType.Float:
                            writer.WriteSingle(floats[hash]);
                            break;

                        case AnimatorControllerParameterType.Int:
                            writer.WriteInt32(ints[hash]);
                            break;

                        case AnimatorControllerParameterType.Bool:
                            writer.WriteBoolean(bools[hash]);
                            break;
                        }
                    }
                }
            }

            return(true);
        }