Exemple #1
0
 protected Package(ClientWrapper client, DataBuffer buffer)
 {
     if (client == null) return;
     if (!client.TcpClient.Connected) return;
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = buffer;
 }
Exemple #2
0
        public PackageFactory(ClientWrapper client, DataBuffer buffer)
        {
            #region Ping

            PingPackages.Add(new Handshake(client, buffer));

            #endregion

            #region Login

            LoginPackages.Add(new EncryptionResponse(client, buffer));
            LoginPackages.Add(new LoginStart(client, buffer));

            #endregion

            #region Status

            StatusPackages.Add(new Request(client, buffer));
            StatusPackages.Add(new Ping(client, buffer));

            #endregion

            #region Play

            PlayPackages.Add(new ChatMessage(client, buffer));
            PlayPackages.Add(new Animation(client, buffer));
            PlayPackages.Add(new PlayerBlockPlacement(client, buffer));
            PlayPackages.Add(new HeldItemChange(client, buffer));
            PlayPackages.Add(new EntityAction(client, buffer));
            PlayPackages.Add(new PlayerAbilities(client, buffer));
            PlayPackages.Add(new PluginMessage(client, buffer));
            PlayPackages.Add(new KeepAlive(client, buffer));
            PlayPackages.Add(new PlayerPositionAndLook(client, buffer));
            PlayPackages.Add(new PlayerPosition(client, buffer));
            PlayPackages.Add(new PlayerLook(client, buffer));
            PlayPackages.Add(new OnGround(client, buffer));
            PlayPackages.Add(new ClientSettings(client, buffer));
            PlayPackages.Add(new PlayerDigging(client, buffer));
            PlayPackages.Add(new ClientStatus(client, buffer));
            PlayPackages.Add(new ClickWindow(client, buffer));
            PlayPackages.Add(new UseEntity(client, buffer));
            PlayPackages.Add(new CloseWindow(client, buffer));
            PlayPackages.Add(new UseItem(client, buffer));
            PlayPackages.Add(new CreativeInventoryAction(client, buffer));
            PlayPackages.Add(new UpdateSign(client, buffer));

            #endregion

            _client = client;
            _buffer = buffer;
        }
Exemple #3
0
        public bool HandleMessage(ClientWrapper client, DataBuffer buffer)
        {
            string raw = buffer.ReadString();
            string channel = raw.Split('|')[0];
            string command = raw.Split('|')[1];

            foreach (var msg in Messages)
            {
                if (msg.Key == channel)
                {
                    if (msg.Value.Command == command)
                    {
                        msg.Value.HandleData(client, buffer);
                    }
                    return true;
                }
            }
            return false;
        }
        public override ChunkColumn LoadChunk(int x, int z)
        {
            var u = Globals.Decompress(File.ReadAllBytes(_folder + "/" + x + "." + z + ".cfile"));
            var reader = new DataBuffer(u);

            var blockLength = reader.ReadInt();
            var block = reader.ReadUShortLocal(blockLength);

            var metalength = reader.ReadInt();
            var blockmeta = reader.ReadUShortLocal(metalength);

            var skyLength = reader.ReadInt();
            var skylight = reader.Read(skyLength);

            var lightLength = reader.ReadInt();
            var blocklight = reader.Read(lightLength);

            var biomeIdLength = reader.ReadInt();
            var biomeId = reader.Read(biomeIdLength);

            var cc = new ChunkColumn
            {
                Blocks = block,
                Metadata = blockmeta,
                Blocklight = {Data = blocklight},
                Skylight = {Data = skylight},
                BiomeId = biomeId,
                X = x,
                Z = z
            };
            Debug.WriteLine("We should have loaded " + x + ", " + z);
            return cc;
        }
Exemple #5
0
 public void SavePlayer()
 {
     byte[] health = HealthManager.Export();
     byte[] inv = Inventory.GetBytes();
     DataBuffer buffer = new DataBuffer(new byte[0]);
     buffer.WriteDouble(KnownPosition.X);
     buffer.WriteDouble(KnownPosition.Y);
     buffer.WriteDouble(KnownPosition.Z);
     buffer.WriteFloat(KnownPosition.Yaw);
     buffer.WriteFloat(KnownPosition.Pitch);
     buffer.WriteBool(KnownPosition.OnGround);
     buffer.WriteVarInt((int)Gamemode);
     buffer.WriteVarInt(health.Length);
     foreach (byte b in health)
     {
         buffer.WriteByte(b);
     }
     buffer.WriteVarInt(inv.Length);
     foreach (byte b in inv)
     {
         buffer.WriteByte(b);
     }
     byte[] data = buffer.ExportWriter;
     data = Globals.Compress(data);
     string savename = ServerSettings.OnlineMode ? Uuid : Username;
     File.WriteAllBytes("Players/" + savename + ".pdata", data);
 }
Exemple #6
0
 public void LoadPlayer()
 {
     string savename = ServerSettings.OnlineMode ? Uuid : Username;
     if (File.Exists("Players/" + savename + ".pdata"))
     {
         byte[] data = File.ReadAllBytes("Players/" + savename + ".pdata");
         data = Globals.Decompress(data);
         DataBuffer reader = new DataBuffer(data);
         double x = reader.ReadDouble();
         double y = reader.ReadDouble();
         double z = reader.ReadDouble();
         float yaw = reader.ReadFloat();
         float pitch = reader.ReadFloat();
         bool onGround = reader.ReadBool();
         KnownPosition = new PlayerLocation(x, y, z) {Yaw = yaw, Pitch = pitch, OnGround = onGround};
         Gamemode = (Gamemode) reader.ReadVarInt();
         int healthLength = reader.ReadVarInt();
         byte[] healthData = reader.Read(healthLength);
         int inventoryLength = reader.ReadVarInt();
         byte[] inventoryData = reader.Read(inventoryLength);
         HealthManager.Import(healthData);
         Inventory.Import(inventoryData);
     }
     else
     {
         KnownPosition = Level.GetSpawnPoint();
     }
     Loaded = true;
 }
Exemple #7
0
        private bool ReadUncompressed(ClientWrapper client, NetworkStream clientStream, int dlength)
        {
            var buffie = new byte[dlength];
            int receivedData;
            receivedData = clientStream.Read(buffie, 0, buffie.Length);
            if (receivedData > 0)
            {
                var buf = new DataBuffer(client);

                if (client.Decrypter != null)
                {
                    var date = new byte[4096];
                    client.Decrypter.TransformBlock(buffie, 0, buffie.Length, date, 0);
                    buf.BufferedData = date;
                }
                else
                {
                    buf.BufferedData = buffie;
                }

                buf.BufferedData = buffie;

                buf.Size = dlength;
                var packid = buf.ReadVarInt();

                if (!new PackageFactory(client, buf).Handle(packid))
                {
                    ConsoleFunctions.WriteWarningLine("Unknown packet received! \"0x" + packid.ToString("X2") + "\"");
                }

                buf.Dispose();
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Import(byte[] data)
        {
            DataBuffer buffer = new DataBuffer(data);

            for (int i = 0; i <= 45; i++)
            {
                int slotId = buffer.ReadInt();
                short itemId = buffer.ReadShort();
                byte metaData = (byte)buffer.ReadByte();
                byte itemCount = (byte)buffer.ReadByte();

                _slots[slotId] = new ItemStack(itemId, itemCount, metaData);
                UpdateHandItems();
            }
        }
 public byte[] GetBytes()
 {
     DataBuffer buffer = new DataBuffer(new byte[0]);
     for (int i = 0; i <= 45; i++)
     {
         var slot = _slots[i];
         buffer.WriteInt(i); //Write the SlotID
         buffer.WriteShort(slot.ItemId); //Write the ItemID
         buffer.WriteByte(slot.MetaData);
         buffer.WriteByte(slot.ItemCount);
     }
     return buffer.ExportWriter;
 }
Exemple #10
0
 public virtual void HandleData(ClientWrapper client, DataBuffer buffer)
 {
 }
Exemple #11
0
 public void Broadcast(Level level, bool self = true, Player source = null)
 {
     foreach (var i in level.GetOnlinePlayers)
     {
         try
         {
             if (i != null && i.Wrapper != null & i.Wrapper.TcpClient != null)
             {
                 if (!self && i == source)
                 {
                     continue;
                 }
                 Client = i.Wrapper;
                 Buffer = new DataBuffer(i.Wrapper);
                 Stream = i.Wrapper.TcpClient.GetStream(); //Exception here. (sometimes)
                 Write();
             }
         }
         catch
         {
             //Catch any exception just to be sure the broadcast works.
             //TODO: Fix the exception.
         }
     }
 }
Exemple #12
0
 internal void SetTarget(ClientWrapper client)
 {
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = new DataBuffer(client);
 }
Exemple #13
0
 public override void HandleData(ClientWrapper client, DataBuffer buffer)
 {
     string c = buffer.ReadString();
     ConsoleFunctions.WriteInfoLine(client.Player.Username + "'s client: " + c);
 }
Exemple #14
0
        /// <summary>
        ///     Flush all data to the TCPClient NetworkStream.
        /// </summary>
        public void FlushData(bool quee = false)
        {
            try
            {
                var allData = _bffr.ToArray();
                _bffr.Clear();

                if (ServerSettings.UseCompression && _client.PacketMode == PacketMode.Play && _client.SetCompressionSend)
                {
                    bool isOver = (allData.Length >= ServerSettings.CompressionThreshold);
                    int dataLength = isOver ? allData.Length : 0;

                    //Calculate length of 'Data Length'
                    byte[] dLength = GetVarIntBytes(dataLength);

                    //Create all data
                    var compressedBytes = ZlibStream.CompressBuffer(allData);
                    int packetlength = compressedBytes.Length + dLength.Length;
                    var dataToSend = isOver ? compressedBytes : allData;

                    var compressed = new DataBuffer(_client);
                    compressed.WriteVarInt(packetlength);
                    compressed.WriteVarInt(dataLength);
                    compressed.Write(dataToSend);

                    Console.WriteLine();

                    Console.WriteLine("Packet bigger than Threshold: " + isOver);
                    Console.WriteLine("Packet info: ");

                    Console.WriteLine("(Header) Packet Length: " + packetlength);
                    Console.WriteLine("(Header) Data Length: " + dataLength);
                    Console.WriteLine("Data Length " + dataToSend.Length);
                    Console.WriteLine("Length difference: " + (packetlength - dataToSend.Length));

                    Console.WriteLine();

                    _client.AddToQuee(compressed.ExportWriter, quee);
                }
                else
                {
                    WriteVarInt(allData.Length);
                    var buffer = _bffr.ToArray();

                    var data = new List<byte>();
                    foreach (var i in buffer)
                    {
                        data.Add(i);
                    }
                    foreach (var i in allData)
                    {
                        data.Add(i);
                    }
                    _client.AddToQuee(data.ToArray(), quee);
                }
                _bffr.Clear();
            }
            catch (Exception ex)
            {
            //	ConsoleFunctions.WriteErrorLine("Failed to send a packet!\n" + ex);
                Globals.ClientManager.PacketError(_client, ex);
            }
        }
Exemple #15
0
        public byte[] GetBytes(bool unloader = false)
        {
            var writer = new DataBuffer(new byte[0]);
            if (!unloader)
            {
                writer.WriteInt(X);
                writer.WriteInt(Z);
                writer.WriteBool(true);
                writer.WriteUShort(0xffff); // bitmap
                writer.WriteVarInt((Blocks.Length*2) + Skylight.Data.Length + Blocklight.Data.Length + BiomeId.Length);

                for (var i = 0; i < Blocks.Length; i++)
                {
                    writer.WriteUShort((ushort) ((Blocks[i] << 4) | Metadata[i]));
                }

                writer.Write(Blocklight.Data);
                writer.Write(Skylight.Data);

                writer.Write(BiomeId);
            }
            else
            {
                writer.WriteInt(X);
                writer.WriteInt(Z);
                writer.WriteBool(true);
                writer.WriteUShort(0);
                writer.WriteVarInt(0);
            }
            return writer.ExportWriter;
        }
Exemple #16
0
        public byte[] Export()
        {
            var buffer = new DataBuffer(new byte[0]);

            buffer.WriteInt(Blocks.Length);

            for (var i = 0; i < Blocks.Length; i++)
                buffer.WriteUShort(Blocks[i]);

            buffer.WriteInt(Blocks.Length);
            for (var i = 0; i < Blocks.Length; i++)
                buffer.WriteUShort((ushort) Metadata[i]);

            buffer.WriteInt(Blocklight.Data.Length);
            buffer.Write(Blocklight.Data);

            buffer.WriteInt(Skylight.Data.Length);
            buffer.Write(Skylight.Data);

            buffer.WriteInt(BiomeId.Length);
            buffer.Write(BiomeId);

            return buffer.ExportWriter;
        }