public virtual void ReadAdditionalData(BitBuffer b)
 {
 }
 public NMFullGhostState(BitBuffer dat) => this.manager = BelongsToManager.GhostManager;
 public virtual void WriteAdditionalData(BitBuffer b)
 {
 }
 public NMGhostInputState(BitBuffer dat)
   : base(dat)
 {
 }
Exemple #5
0
 public override object ReadNetValue(object val) => (object)(float)((double)((float)(int)val / (float)(BitBuffer.GetMaxValue(this._bits) / 2L)) * (double)this._range);
        private bool DoDeserialize(BinaryClassChunk node)
        {
            System.Type type = Editor.GetType(node.GetProperty <string>("blockType"));
            if (type == (System.Type)null)
            {
                return(false);
            }
            bool flag1 = typeof(AutoBlock).IsAssignableFrom(type);

            this._things = new List <Thing>();
            BitBuffer property1 = node.GetProperty <BitBuffer>("data");

            if (!typeof(AutoBlock).IsAssignableFrom(type))
            {
                flag1 = false;
            }
            List <AutoBlock> autoBlockList = new List <AutoBlock>();
            int num1 = property1.ReadInt();

            for (int index = 0; index < num1; ++index)
            {
                float num2  = property1.ReadFloat();
                float num3  = property1.ReadFloat();
                int   num4  = (int)property1.ReadByte();
                bool  flag2 = Level.flipH;
                if (Level.loadingOppositeSymmetry)
                {
                    flag2 = !flag2;
                }
                if (flag2)
                {
                    num2 = (float)(192.0 - (double)num2 - 16.0);
                }
                Thing thing = Editor.CreateThing(type);
                if (flag2 && thing is AutoBlock)
                {
                    (thing as AutoBlock).needsRefresh = true;
                    (thing as AutoBlock).isFlipped    = true;
                }
                if (thing is BackgroundTile)
                {
                    if (flag2)
                    {
                        (thing as BackgroundTile).isFlipped = true;
                    }
                    (thing as BackgroundTile).oppositeSymmetry = !Level.loadingOppositeSymmetry;
                }
                if (flag2 && thing is AutoPlatform)
                {
                    (thing as AutoPlatform).needsRefresh = true;
                }
                thing.x      = num2;
                thing.y      = num3;
                thing.placed = true;
                if (thing.isStatic)
                {
                    this._isStatic = true;
                }
                if (flag1)
                {
                    short     num5      = property1.ReadShort();
                    short     num6      = property1.ReadShort();
                    short     num7      = property1.ReadShort();
                    short     num8      = property1.ReadShort();
                    AutoBlock autoBlock = thing as AutoBlock;
                    autoBlock.northIndex = (int)num5;
                    autoBlock.southIndex = (int)num6;
                    if (flag2)
                    {
                        autoBlock.westIndex = (int)num7;
                        autoBlock.eastIndex = (int)num8;
                    }
                    else
                    {
                        autoBlock.eastIndex = (int)num7;
                        autoBlock.westIndex = (int)num8;
                    }
                    autoBlockList.Add(autoBlock);
                }
                bool flag3 = true;
                if (Level.symmetry)
                {
                    if (Level.leftSymmetry && (double)num2 > 80.0)
                    {
                        flag3 = false;
                    }
                    if (!Level.leftSymmetry && (double)num2 < 96.0)
                    {
                        flag3 = false;
                    }
                }
                if (flag3)
                {
                    thing.frame = num4;
                    this._things.Add(thing);
                }
            }
            if (flag1 && !(Level.current is Editor))
            {
                foreach (AutoBlock autoBlock in autoBlockList)
                {
                    if (autoBlock.northIndex != -1)
                    {
                        autoBlock.upBlock = (Block)autoBlockList[autoBlock.northIndex];
                    }
                    if (autoBlock.southIndex != -1)
                    {
                        autoBlock.downBlock = (Block)autoBlockList[autoBlock.southIndex];
                    }
                    if (autoBlock.eastIndex != -1)
                    {
                        autoBlock.rightBlock = (Block)autoBlockList[autoBlock.eastIndex];
                    }
                    if (autoBlock.westIndex != -1)
                    {
                        autoBlock.leftBlock = (Block)autoBlockList[autoBlock.westIndex];
                    }
                    autoBlock.neighborsInitialized = true;
                }
                BitBuffer property2 = node.GetProperty <BitBuffer>("groupData");
                if (property2 != null)
                {
                    ushort num2 = property2.ReadUShort();
                    int    num3;
                    for (int index1 = 0; index1 < (int)num2; index1 = num3 + 1)
                    {
                        BlockGroup blockGroup = new BlockGroup();
                        blockGroup.position = new Vec2(property2.ReadFloat(), property2.ReadFloat());
                        bool flag2 = Level.flipH;
                        if (Level.loadingOppositeSymmetry)
                        {
                            flag2 = !flag2;
                        }
                        if (flag2)
                        {
                            blockGroup.position.x = (float)(192.0 - (double)blockGroup.position.x - 16.0);
                        }
                        blockGroup.collisionOffset = new Vec2(property2.ReadFloat(), property2.ReadFloat());
                        blockGroup.collisionSize   = new Vec2(property2.ReadFloat(), property2.ReadFloat());
                        float num4 = 88f;
                        if (Level.symmetry)
                        {
                            if (Level.leftSymmetry)
                            {
                                if ((double)blockGroup.left < (double)num4 && (double)blockGroup.right > (double)num4)
                                {
                                    float num5 = blockGroup.right - num4;
                                    float x    = blockGroup.collisionSize.x - num5;
                                    blockGroup.position.x     -= num5;
                                    blockGroup.position.x     += x / 2f;
                                    blockGroup.collisionSize   = new Vec2(x, blockGroup.collisionSize.y);
                                    blockGroup.collisionOffset = new Vec2((float)-((double)x / 2.0), blockGroup.collisionOffset.y);
                                    blockGroup.right           = num4;
                                }
                            }
                            else
                            {
                                num4 = 88f;
                                if ((double)blockGroup.right > (double)num4 && (double)blockGroup.left < (double)num4)
                                {
                                    float num5 = num4 - blockGroup.left;
                                    float x    = blockGroup.collisionSize.x - num5;
                                    blockGroup.position.x     += num5;
                                    blockGroup.position.x     -= x / 2f;
                                    blockGroup.collisionSize   = new Vec2(x, blockGroup.collisionSize.y);
                                    blockGroup.collisionOffset = new Vec2((float)-((double)x / 2.0), blockGroup.collisionOffset.y);
                                    blockGroup.left            = num4;
                                }
                            }
                        }
                        int num6 = property2.ReadInt();
                        for (int index2 = 0; index2 < num6; ++index2)
                        {
                            int index3 = (int)property2.ReadShort();
                            if (index3 >= 0)
                            {
                                AutoBlock autoBlock = autoBlockList[index3];
                                bool      flag3     = true;
                                if (Level.symmetry)
                                {
                                    if (Level.leftSymmetry && (double)autoBlock.x > 80.0)
                                    {
                                        flag3 = false;
                                    }
                                    if (!Level.leftSymmetry && (double)autoBlock.x < 96.0)
                                    {
                                        flag3 = false;
                                    }
                                }
                                if (flag3)
                                {
                                    autoBlock.groupedWithNeighbors = true;
                                    blockGroup.Add((Block)autoBlock);
                                    blockGroup.physicsMaterial = autoBlock.physicsMaterial;
                                    blockGroup.thickness       = autoBlock.thickness;
                                }
                                this._things.Remove((Thing)autoBlock);
                            }
                        }
                        num3 = index1 + num6;
                        if (flag2)
                        {
                            blockGroup.needsRefresh = true;
                        }
                        if (Level.symmetry)
                        {
                            if (Level.leftSymmetry && (double)blockGroup.left < (double)num4)
                            {
                                this._things.Add((Thing)blockGroup);
                            }
                            else if (!Level.leftSymmetry && (double)blockGroup.right > (double)num4)
                            {
                                this._things.Add((Thing)blockGroup);
                            }
                        }
                        else
                        {
                            this._things.Add((Thing)blockGroup);
                        }
                    }
                }
            }
            return(true);
        }
        public override BinaryClassChunk Serialize()
        {
            BinaryClassChunk binaryClassChunk = new BinaryClassChunk();

            binaryClassChunk.AddProperty("type", (object)ModLoader.SmallTypeName(this.GetType()));
            binaryClassChunk.AddProperty("blockType", (object)ModLoader.SmallTypeName(this._type));
            BitBuffer bitBuffer1 = new BitBuffer(false);

            bitBuffer1.Write(this._things.Count);
            if (typeof(AutoBlock).IsAssignableFrom(this._type))
            {
                foreach (Thing thing in this._things)
                {
                    AutoBlock autoBlock = thing as AutoBlock;
                    autoBlock.groupedWithNeighbors = false;
                    autoBlock.neighborsInitialized = false;
                }
                BitBuffer bitBuffer2 = new BitBuffer(false);
                bitBuffer2.Write((ushort)0);
                ushort val = 0;
                foreach (Thing thing in this._things)
                {
                    AutoBlock autoBlock = thing as AutoBlock;
                    autoBlock.InitializeNeighbors();
                    bitBuffer1.Write(thing.x);
                    bitBuffer1.Write(thing.y);
                    bitBuffer1.Write((byte)thing.frame);
                    bitBuffer1.Write(autoBlock.upBlock != null ? (short)this._things.IndexOf((Thing)autoBlock.upBlock) : (short)-1);
                    bitBuffer1.Write(autoBlock.downBlock != null ? (short)this._things.IndexOf((Thing)autoBlock.downBlock) : (short)-1);
                    bitBuffer1.Write(autoBlock.rightBlock != null ? (short)this._things.IndexOf((Thing)autoBlock.rightBlock) : (short)-1);
                    bitBuffer1.Write(autoBlock.leftBlock != null ? (short)this._things.IndexOf((Thing)autoBlock.leftBlock) : (short)-1);
                    BlockGroup blockGroup = autoBlock.GroupWithNeighbors(false);
                    if (blockGroup != null)
                    {
                        bitBuffer2.Write(blockGroup.x);
                        bitBuffer2.Write(blockGroup.y);
                        bitBuffer2.Write(blockGroup.collisionOffset.x);
                        bitBuffer2.Write(blockGroup.collisionOffset.y);
                        bitBuffer2.Write(blockGroup.collisionSize.x);
                        bitBuffer2.Write(blockGroup.collisionSize.y);
                        bitBuffer2.Write(blockGroup.blocks.Count <Block>());
                        foreach (Block block in blockGroup.blocks)
                        {
                            bitBuffer2.Write((short)this._things.IndexOf((Thing)block));
                        }
                        ++val;
                    }
                }
                bitBuffer2.position = 0;
                bitBuffer2.Write(val);
                foreach (Thing thing in this._things)
                {
                    AutoBlock autoBlock = thing as AutoBlock;
                    autoBlock.groupedWithNeighbors = false;
                    autoBlock.neighborsInitialized = false;
                }
                if (bitBuffer2.lengthInBytes > 2)
                {
                    binaryClassChunk.AddProperty("groupData", (object)bitBuffer2);
                }
            }
            else
            {
                foreach (Thing thing in this._things)
                {
                    bitBuffer1.Write(thing.x);
                    bitBuffer1.Write(thing.y);
                    bitBuffer1.Write((byte)thing.frame);
                }
            }
            binaryClassChunk.AddProperty("data", (object)bitBuffer1);
            return(binaryClassChunk);
        }
        public BitBuffer Serialize(BitBuffer data = null, bool root = true)
        {
            if (data == null)
            {
                data = new BitBuffer(false);
            }
            this._serializedData = data;
            if (data.allowPacking)
            {
                throw new Exception("This class does not support serialization with a packed bit buffer. Construct the buffer with allowPacking set to false.");
            }
            System.Type              type1   = this.GetType();
            List <ClassMember>       members = Editor.GetMembers(type1);
            List <BinaryClassMember> binaryClassMemberList = new List <BinaryClassMember>();

            foreach (ClassMember classMember in members)
            {
                if (!classMember.isPrivate && (classMember.type.IsEnum || classMember.type.IsPrimitive || (classMember.type.Equals(typeof(string)) || typeof(BinaryClassChunk).IsAssignableFrom(classMember.type)) || classMember.type.IsArray || classMember.type.IsGenericType && (classMember.type.GetGenericTypeDefinition() == typeof(List <>) || classMember.type.GetGenericTypeDefinition() == typeof(HashSet <>))))
                {
                    object obj = classMember.GetValue((object)this);
                    if (classMember.type.IsEnum)
                    {
                        obj = (object)(int)obj;
                    }
                    if (obj != null)
                    {
                        BinaryClassMember binaryClassMember = new BinaryClassMember()
                        {
                            name = classMember.name,
                            data = obj
                        };
                        binaryClassMemberList.Add(binaryClassMember);
                    }
                }
            }
            if (this._extraProperties != null)
            {
                foreach (KeyValuePair <string, List <object> > extraProperty in (MultiMap <string, object, List <object> >) this._extraProperties)
                {
                    if (extraProperty.Value != null)
                    {
                        foreach (object obj1 in extraProperty.Value)
                        {
                            object obj2 = obj1;
                            if (obj2.GetType().IsEnum)
                            {
                                obj2 = (object)(int)obj2;
                            }
                            BinaryClassMember binaryClassMember = new BinaryClassMember()
                            {
                                name  = "@" + extraProperty.Key,
                                data  = obj2,
                                extra = true
                            };
                            binaryClassMemberList.Add(binaryClassMember);
                        }
                    }
                }
            }
            if (root)
            {
                long val = BinaryClassChunk.MagicNumber(type1);
                data.Write(val);
                data.Write(0U);
            }
            data.Write(BinaryClassChunk.ChunkVersion(type1));
            int position1 = data.position;

            data.Write(0U);
            data.Write((ushort)binaryClassMemberList.Count);
            foreach (BinaryClassMember binaryClassMember in binaryClassMemberList)
            {
                data.Write(binaryClassMember.name);
                if (binaryClassMember.extra)
                {
                    byte        val   = 0;
                    System.Type type2 = binaryClassMember.data.GetType();
                    if (BinaryClassMember.typeMap.TryGetValue(type2, out val))
                    {
                        val = (byte)((int)val << 1 | 1);
                    }
                    data.Write(val);
                    if (val == (byte)0)
                    {
                        data.Write(ModLoader.SmallTypeName(type2));
                    }
                }
                int position2 = data.position;
                data.Write(0U);
                if (binaryClassMember.data is BinaryClassChunk)
                {
                    (binaryClassMember.data as BinaryClassChunk).Serialize(data, false);
                }
                else if (binaryClassMember.data is Array)
                {
                    this.SerializeArray(binaryClassMember.data as Array, binaryClassMember.data.GetType().GetElementType(), data);
                }
                else if (binaryClassMember.data.GetType().IsGenericType&& binaryClassMember.data.GetType().GetGenericTypeDefinition() == typeof(List <>))
                {
                    IList data1 = binaryClassMember.data as IList;
                    Array array = (Array) new object[data1.Count];
                    data1.CopyTo(array, 0);
                    this.SerializeArray(array, binaryClassMember.data.GetType().GetGenericArguments()[0], data);
                }
                else if (binaryClassMember.data.GetType().IsGenericType&& binaryClassMember.data.GetType().GetGenericTypeDefinition() == typeof(HashSet <>))
                {
                    IEnumerable   data1      = binaryClassMember.data as IEnumerable;
                    List <object> objectList = new List <object>();
                    foreach (object obj in data1)
                    {
                        objectList.Add(obj);
                    }
                    object[] array = new object[objectList.Count];
                    objectList.CopyTo(array, 0);
                    this.SerializeArray((Array)array, binaryClassMember.data.GetType().GetGenericArguments()[0], data);
                }
                else
                {
                    data.Write(binaryClassMember.data);
                }
                int position3 = data.position;
                data.position = position2;
                data.Write((uint)(position3 - position2 - 4));
                data.position = position3;
            }
            int position4 = data.position;

            data.position = position1;
            data.Write((uint)(position4 - position1 - 4));
            if (root)
            {
                this._checksum = Editor.Checksum(data.buffer);
                data.position  = 8;
                data.Write(this._checksum);
            }
            data.position = position4;
            return(data);
        }
Exemple #9
0
 public DataTransferSession(short session, byte index, BitBuffer data, bool last)
 {
     id = session;
     RecieveData(index, data, last);
 }
        private void WriteMinimalStateMask(NetworkConnection c, BitBuffer b)
        {
            long connectionStateMask = this.GetConnectionData(c).connectionStateMask;

            b.WriteBits((object)connectionStateMask, this._fields.Count);
        }
 public static long ReadMinimalStateMask(System.Type t, BitBuffer b) => b.ReadBits <long>(Editor.AllStateFields[t].Length);
 public void NetDeserialize(BitBuffer data)
 {
     RockWeather._timeOfDay = data.ReadFloat();
     RockWeather._weather   = (Weather)data.ReadByte();
 }
Exemple #13
0
        protected override NCError OnSpinServerThread()
        {
            if (this._socket == null)
            {
                return(new NCError("connection was lost.", NCErrorType.CriticalError));
            }
            Queue <NCBasicPacket> ncBasicPacketQueue = (Queue <NCBasicPacket>)null;

            lock (this._threadPackets)
            {
                ncBasicPacketQueue = new Queue <NCBasicPacket>((IEnumerable <NCBasicPacket>) this._threadPackets);
                this._threadPackets.Clear();
            }
            foreach (NCBasicPacket ncBasicPacket in ncBasicPacketQueue)
            {
                IPEndPoint sender  = ncBasicPacket.sender;
                byte[]     data    = ncBasicPacket.data;
                string     address = sender.ToString();
                lock (this._basicConnections)
                {
                    NCBasicConnection ncBasicConnection1 = this._basicConnections.FirstOrDefault <NCBasicConnection>((Func <NCBasicConnection, bool>)(x => x.address == address));
                    if (ncBasicConnection1 == null || ncBasicConnection1.status == NCBasicStatus.Disconnected)
                    {
                        if (data.Length > 8)
                        {
                            BitBuffer bitBuffer1 = new BitBuffer(data);
                            if (bitBuffer1.ReadLong() == 2449832521355936907L)
                            {
                                if (bitBuffer1.ReadString() == this._serverIdentifier)
                                {
                                    NCBasicConnection ncBasicConnection2 = new NCBasicConnection()
                                    {
                                        connection = sender,
                                        address    = address,
                                        status     = NCBasicStatus.Connecting
                                    };
                                    ncBasicConnection2.port = sender.Port;
                                    this._basicConnections.Add(ncBasicConnection2);
                                    this._pendingMessages.Enqueue(new NCError("connection attempt from " + ncBasicConnection2.address, NCErrorType.Message));
                                    ncBasicConnection2.heartbeat.Restart();
                                    ncBasicConnection2.timeout.Restart();
                                    BitBuffer bitBuffer2 = new BitBuffer();
                                    bitBuffer2.Write(2449832521355936907L);
                                    this._socket.Send(bitBuffer2.buffer, bitBuffer2.lengthInBytes, ncBasicConnection2.connection);
                                }
                            }
                        }
                    }
                    else if (ncBasicConnection1.status == NCBasicStatus.WaitingForAck || ncBasicConnection1.status == NCBasicStatus.TryingToConnect)
                    {
                        if (data.Length >= 8)
                        {
                            if (new BitBuffer(data).ReadLong() == 2449832521355936907L)
                            {
                                ncBasicConnection1.status = NCBasicStatus.Connecting;
                                ncBasicConnection1.timeout.Restart();
                                ncBasicConnection1.heartbeat.Restart();
                                this._pendingMessages.Enqueue(new NCError("received first acknowledgement...", NCErrorType.Success));
                                BitBuffer bitBuffer = new BitBuffer();
                                bitBuffer.Write(2449832521355936907L);
                                this._socket.Send(bitBuffer.buffer, bitBuffer.lengthInBytes, ncBasicConnection1.connection);
                            }
                        }
                    }
                    else
                    {
                        BitBuffer bitBuffer = new BitBuffer(data);
                        if (ncBasicConnection1.status != NCBasicStatus.Connected)
                        {
                            if (ncBasicConnection1.status != NCBasicStatus.Connecting)
                            {
                                continue;
                            }
                        }
                        bool flag = false;
                        if (data.Length >= 8)
                        {
                            if (bitBuffer.ReadLong() == 2449832521355936907L)
                            {
                                ncBasicConnection1.timeout.Restart();
                                ++ncBasicConnection1.beatsReceived;
                                flag = true;
                            }
                            else
                            {
                                bitBuffer.SeekToStart();
                            }
                        }
                        if (!flag)
                        {
                            this.OnPacket(data, (object)ncBasicConnection1);
                        }
                    }
                }
            }
            List <NCBasicConnection> ncBasicConnectionList = new List <NCBasicConnection>();

            for (int index = 0; index < this._basicConnections.Count; ++index)
            {
                NCBasicConnection basicConnection = this._basicConnections[index];
                if (basicConnection.status == NCBasicStatus.TryingToConnect)
                {
                    if (basicConnection.attempts > 5)
                    {
                        ncBasicConnectionList.Add(basicConnection);
                        this._pendingMessages.Enqueue(new NCError("could not connect to server.", NCErrorType.CriticalError));
                    }
                    BitBuffer bitBuffer = new BitBuffer();
                    bitBuffer.Write(2449832521355936907L);
                    bitBuffer.Write(this._serverIdentifier);
                    this._socket.Send(bitBuffer.buffer, bitBuffer.lengthInBytes, basicConnection.connection);
                    ++basicConnection.attempts;
                    basicConnection.timeout.Restart();
                    basicConnection.status = NCBasicStatus.WaitingForAck;
                }
                else if (basicConnection.status == NCBasicStatus.WaitingForAck && basicConnection.timeout.elapsed.TotalSeconds > 2.0)
                {
                    basicConnection.status = NCBasicStatus.TryingToConnect;
                    this._pendingMessages.Enqueue(new NCError("resending connection message to server.", NCErrorType.Message));
                }
                float num = 1f;
                if (basicConnection.status == NCBasicStatus.Connecting)
                {
                    num = 0.05f;
                }
                if (basicConnection.status == NCBasicStatus.Connecting || basicConnection.status == NCBasicStatus.Connected)
                {
                    if (basicConnection.heartbeat.elapsed.TotalSeconds >= (double)num)
                    {
                        BitBuffer bitBuffer = new BitBuffer();
                        bitBuffer.Write(2449832521355936907L);
                        this._socket.Send(bitBuffer.buffer, bitBuffer.lengthInBytes, basicConnection.connection);
                        basicConnection.heartbeat.Restart();
                    }
                    if (basicConnection.status == NCBasicStatus.Connecting && basicConnection.beatsReceived > 0)
                    {
                        this._pendingMessages.Enqueue(new NCError("connection to " + basicConnection.address + " succeeded!", NCErrorType.Success));
                        basicConnection.status = NCBasicStatus.Connected;
                        bool host = false;
                        if (!this._isAServer && this.connections.Count == 1 && this.connections[0].identifier == "waiting")
                        {
                            host = true;
                        }
                        this.AttemptConnection((object)basicConnection, host);
                    }
                }
            }
            foreach (NCBasicConnection ncBasicConnection in ncBasicConnectionList)
            {
                this._basicConnections.Remove(ncBasicConnection);
            }
            return((NCError)null);
        }
 public override void OnDeserialize(BitBuffer d)
 {
     this.count = (int)d.ReadUShort();
     this.data  = d.ReadBitBuffer();
 }
 public override void OnDeserialize(BitBuffer d)
 {
     this.address = new IPAddress(d.ReadBitBuffer().buffer);
     this.port    = d.ReadInt();
 }
Exemple #16
0
 public override void OnDeserialize(BitBuffer msg)
 {
     base.OnDeserialize(msg);
     DataTransferManager.OnDataRecieved(connection, index, msg.ReadBitBuffer(), transferSession, last);
 }