Exemple #1
0
        public ServerMessage(StreamPeerTCP socket)
        {
            var _size = Utilites.Byte2IntBE((byte[])socket.GetData(4)[1]);

            cmd = (NET_CMD)Utilites.Byte2UShortBE((byte[])socket.GetData(2)[1]);
            seq = Utilites.Byte2UShortBE((byte[])socket.GetData(2)[1]);
            var _data = (byte[])socket.GetData(_size)[1];

            data = SimpleJSON.JSON.Parse(System.Text.Encoding.UTF8.GetString(_data, 0, _size));
        }
Exemple #2
0
        private Packet readPacketInternal(int len)
        {
            // read data
            byte[]      temp = client.GetData(len)[1] as byte[];
            List <byte> data = temp?.ToList();

            // compression is compressed, lets see if the packet was actually compressed
            if (compressionThreshold != -1)
            {
                int dataLength = dataTypes.ReadNextVarInt(data);
                if (dataLength != 0)
                {
                    // packet is compressed, lets decompress
                    GD.Print("packet is compressed :/");
                    return(null);
                }
            }

            // read id
            int packetId = dataTypes.ReadNextVarInt(data);
            // GD.Print($"Got packet with id 0x{packetId:X} and state {currentState}, len is {len}");
            // handle packet
            PacketType type;

            try {
                type = PacketType.of(packetId, currentState, PacketDirection.TO_CLIENT);
            }
            catch (KeyNotFoundException e) {
                // GD.Print($"Coulnt find a packet type for id 0x{packetId:X} and state {currentState}");
                return(null);
            }

            Packet packet = type.instance();

            try {
                packet.read(dataTypes, data);
            }
            catch (Exception ex) {
                GD.Print($"Error while reading packet {type}: \n{ex.GetType().Name}: {ex.Message}");
                GD.Print(ex.StackTrace);
            }

            if (data?.Count != 0)
            {
                GD.Print($"Error while reading packet {type}: Got {data?.Count} bytes too much");
            }

            if (countPacketSize)
            {
                Interlocked.Add(ref totalCount, 1);
                Interlocked.Add(ref totalSize, len);

                if (Interlocked.Read(ref totalCount) % 10 == 0)
                {
                    GD.Print($"Total count {Interlocked.Read(ref totalCount)}, total size {Interlocked.Read(ref totalSize)}");
                }
            }

            return(packet);
        }
Exemple #3
0
    public void getPackets()
    {
        //GD.Print("Get Packets");
        //GD.Print(wrapped_client.GetAvailablePacketCount());
        if (connected && client.GetAvailableBytes() > 0)
        {
            //byte[] packet_data = wrapped_client.GetPacket();
            //UInt32 packetLength = BitConverter.ToUInt32(packet_data, 0);
            //short packetId = BitConverter.ToInt16(packet_data, 4);
            UInt32 packetLength = (UInt32)client.GetU32();
            short  packetId     = (short)client.Get16();
            var    packetData   = client.GetData((int)packetLength - 2);
            //var data = new List<byte>(packet_data).GetRange(6, packet_data.Length -6).ToArray();
            //GD.Print(BitConverter.ToString( (byte[]) packetData[1]));
            var data   = (byte[])packetData[1];
            var packet = Packets.Packets.decode(packetId, data);

            if (GetParent().GetNodeOrNull("GUI") != null)
            {
                var gui = (Control)GetParent().GetNodeOrNull("GUI");
                gui.Call("recordPacket", packetLength + 4);
            }

            GD.Print(String.Format("Received packet {0}, ID: {1} Length: {2}", packet.name, packetId, packetLength));

            if (packet is ReadyPacket)
            {
                ReadyPacket parsed_packet = (ReadyPacket)packet;
                if (parsed_packet.code == 0)
                {
                    string token       = (string)GetParent().GetNode("Discord Integration").Call("getToken");
                    var    loginPacket = new LoginPacket(token);
                    GD.Print("Sending login");
                    sendPacket(loginPacket);
                }
                else if (parsed_packet.code == 1)
                {
                    var requestWorldPacket = new RequestWorldPacket();
                    sendPacket(requestWorldPacket);
                    if (!joined)
                    {
                        var loadingRes = GD.Load <PackedScene>("res://scenes/world.tscn");
                        var node       = loadingRes.Instance();
                        node.SetName("WorldScene");
                        var loadingGuiRes = GD.Load <PackedScene>("res://scenes/gui.tscn");
                        var gui           = (Control)loadingGuiRes.Instance();
                        gui.SetName("GUI");
                        GetParent().Call("setState", 2);
                        GetParent().AddChild(node);
                        //GetParent().AddChild(gui);
                        GetParent().AddChild(gui);
                        //node.AddChild(gui);
                        GetParent().GetNode("GameLoader").Free();
                        //var playerSpriteScene = (PackedScene) node.Call("getSprite", "rowan");
                        //var playerSprite = (AnimatedSprite) playerSpriteScene.Instance();
                        //playerSprite.SetName("PlayerSprite");
                        //node.GetNode("World/Player").AddChild(playerSprite);
                        //playerSprite.Position = ((KinematicBody2D) node.GetNode("Player")).Position;
                        //playerSprite.Visible = true;
                        //GD.Print(playerSprite);
                        joined = true;
                    }
                }
            }
            else if (packet is PongPacket)
            {
                PongPacket parsed_packet = (PongPacket)packet;
                GD.Print("Got pong of " + parsed_packet.message);
            }
            else if (packet is LoginResultPacket)
            {
                LoginResultPacket parsed_packet = (LoginResultPacket)packet;
                GD.Print("Login Result: " + parsed_packet.responseCode.ToString() + " Name: " + parsed_packet.userId);
                var joinGamePacket = new JoinGamePacket();
                sendPacket(joinGamePacket);
            }
            else if (packet is WorldPacket)
            {
                WorldPacket parsed_packet = (WorldPacket)packet;
                //GD.Print(parsed_packet.debug);
                GetParent().GetNode("WorldScene").Call("loadWorld", new object[] { parsed_packet.worldData, parsed_packet.bumpData, parsed_packet.height, parsed_packet.width });
            }
            else if (packet is PlayerPositionPacket)
            {
                PlayerPositionPacket parsed_packet = (PlayerPositionPacket)packet;
                GetParent().GetNode("WorldScene/World/Player").Call("move", new object[] { parsed_packet.x, parsed_packet.y });
            }
            else if (packet is ChatPacket)
            {
                ChatPacket parsed_packet = (ChatPacket)packet;
                GetNode("../GUI/Chat").Call("AddMessage", parsed_packet.author + ": " + parsed_packet.msg);
            }
            else if (packet is EntityPacket)
            {
                EntityPacket parsed_packet = (EntityPacket)packet;
                GD.Print("Got entity '", parsed_packet.sprite, "' at ", parsed_packet.x, ",", parsed_packet.y, " ID: ", parsed_packet.uuid);
                GetNode("../WorldScene").Call("addEntity", parsed_packet.x, parsed_packet.y, parsed_packet.type, parsed_packet.facing, parsed_packet.interactable, parsed_packet.sprite, parsed_packet.uuid, parsed_packet.type != 2);
            }
            else if (packet is EntityMovePacket)
            {
                EntityMovePacket parsed_packet = (EntityMovePacket)packet;
                GD.Print("Got entity moving to ", parsed_packet.x, ",", parsed_packet.y, " ID: ", parsed_packet.uuid);
                GetNode("../WorldScene").Call("moveEntity", parsed_packet.uuid, parsed_packet.x, parsed_packet.y, parsed_packet.facing);
            }
            else if (packet is InvalidateCachePacket)
            {
                InvalidateCachePacket parsed_packet = (InvalidateCachePacket)packet;
                GD.Print(parsed_packet.uuid, " Invalidated.");
                GetNode("../WorldScene").Call("hideEntity", parsed_packet.uuid);
            }
            else if (packet is DialoguePacket)
            {
                DialoguePacket parsed_packet = (DialoguePacket)packet;
                GD.Print("Got dialogue \"", parsed_packet.text, "\"");
                Window window = (Window)GetNode("../GUI/Window");
                window.OpenDialoguePanel();
                DialoguePanel dialoguePanel = window.OpenDialoguePanel(); //(DialoguePanel) GetNode("../GUI/Window/DialoguePanel");
                dialoguePanel.SetDialogue(parsed_packet.text, parsed_packet.author, parsed_packet.sprite, parsed_packet.substitutions, parsed_packet.optionViews);
            }
            else if (packet is CloseDialoguePacket)
            {
                CloseDialoguePacket parsed_packet = (CloseDialoguePacket)packet;
                DialoguePanel       dialoguePanel = (DialoguePanel)GetNode("../GUI/Window/DialoguePanel");
                dialoguePanel.CloseDialogue(parsed_packet.guid);
                //dialoguePanel.SetDialogue(parsed_packet.text, parsed_packet.author, parsed_packet.sprite, parsed_packet.substitutions, parsed_packet.optionViews);
            }
            else if (packet is PlayerDataPacket)
            {
                PlayerDataPacket parsed_packet = (PlayerDataPacket)packet;
                var player = (Player)GetNode("../WorldScene/World/Player");
                player.SetUuid(parsed_packet.guid);
                if (player.GetNodeOrNull("PlayerSprite") != null)
                {
                    player.GetNodeOrNull("PlayerSprite").Free();
                }
                var playerSpriteScene = (PackedScene)GetNode("../WorldScene").Call("getSprite", parsed_packet.sprite);
                var playerSprite      = (AnimatedSprite)playerSpriteScene.Instance();
                playerSprite.SetName("PlayerSprite");
                player.AddChild(playerSprite);
            }
            else if (packet is InventoryPacket)
            {
                InventoryPacket parsed_packet = (InventoryPacket)packet;
                var             player        = (Player)GetNode("../WorldScene/World/Player");
                if (player.guid == parsed_packet.inventory.guid)
                {
                    player.inventory = parsed_packet.inventory;
                    foreach (Item item in player.inventory.items)
                    {
                        GD.Print("Item: ", item.GetName(), " \"", item.GetDescription(), "\"");
                    }
                }
            }
            else if (packet is AddItemPacket)
            {
                AddItemPacket parsed_packet = (AddItemPacket)packet;
                var           player        = (Player)GetNode("../WorldScene/World/Player");
                if (player.guid == parsed_packet.guid)
                {
                    //TODO: Make use of indices.
                    player.inventory.AddItem(parsed_packet.item, true);
                }
            }
            else if (packet is ModifyItemPacket)
            {
                ModifyItemPacket parsed_packet = (ModifyItemPacket)packet;
                var player = (Player)GetNode("../WorldScene/World/Player");
                if (player.guid == parsed_packet.guid)
                {
                    //TODO: Make use of indices.
                    player.inventory.UpdateItem(parsed_packet.item, parsed_packet.index);
                }
            }
        }
        else
        {
            var testPacket = new Packets.PingPacket("Hello There!");
            //sendPacket(testPacket);
        }
    }