public void prepareVehicle(StreamedPed ped)
 {
     if (ped.gameReference != null)
     {
         preparePed(ped.gameReference);
     }
 }
 public PedAnimationManager(StreamedPed ped)
 {
     this.ped             = ped;
     currentAnimation     = PedAnimations.StandStill;
     animset              = new AnimationSet("move_m@casual");
     animset2             = new AnimationSet("gun@ak47");
     lastRunToCoord       = Vector3.Zero;
     lastWalkToCoord      = Vector3.Zero;
     lastAnimationStarted = DateTime.Now;
 }
 public PedAnimationManager(StreamedPed ped)
 {
     this.ped = ped;
     currentAnimation = PedAnimations.StandStill;
     animset = new AnimationSet("move_m@casual");
     animset2 = new AnimationSet("gun@ak47");
     lastRunToCoord = Vector3.Zero;
     lastWalkToCoord = Vector3.Zero;
     lastAnimationStarted = DateTime.Now;
 }
Beispiel #4
0
        public void updateAllPlayers()
        {
            for (int i = 0; i < serverConnection.playersdata.Keys.Count; i++)
            {
                var elemKey   = serverConnection.playersdata.Keys.ToArray()[i];
                var elemValue = serverConnection.playersdata[elemKey];
                if (elemValue.pos_x == 0)
                {
                    continue;
                }

                if (elemValue.client_has_been_set)
                {
                    continue;
                }
                else
                {
                    elemValue.client_has_been_set = true;
                }

                //if (!pedController.Exists(elemKey))
                //{
                //pedController.Add(new StreamedPed(pedStreamer, "F_Y_NURSE", "-", Vector3.Zero, 0, (BlipColor)(elemKey % 13)));
                //}

                StreamedPed ped = pedController.GetInstance(elemKey);
                ped.model = playerModels.ContainsKey(elemKey) ? playerModels[elemKey] : "F_Y_NURSE";
                if (ped.position == Vector3.Zero)
                {
                    ped.color       = (BlipColor)(elemKey % 14);
                    ped.networkname = playerNames.ContainsKey(elemKey) ? playerNames[elemKey] : "-";
                }
                try
                {
                    updateVehicle(elemKey, elemValue, ped);
                }
                catch (Exception ex)
                {
                    log("Failed updating streamed vehicle data for Player " + ex.Message);
                }
                try
                {
                    updatePed(elemKey, elemValue, ped);
                }
                catch (Exception ex)
                {
                    log("Failed updating streamed ped data for Player " + ex.Message);
                }
            }
        }
        public void updatePed(uint id, UpdateDataStruct data, StreamedPed ped)
        {
            var posnew = new Vector3(data.pos_x, data.pos_y, data.pos_z - 1.0f);

            ped.position        = posnew;
            ped.heading         = data.heading;
            ped.direction       = new Vector3(data.rot_x, data.rot_y, data.rot_z);
            ped.cameraDirection = new Vector3(data.camdir_x, data.camdir_y, data.camdir_z);
            if (ped.IsStreamedIn() && data.vehicle_id == 0)
            {
                if (ped.gameReference.isInVehicle())
                {
                    ped.gameReference.CurrentVehicle.PassengersLeaveVehicle(true);
                    //ped.gameReference.CurrentVehicle.Delete();
                }

                float   delta  = posnew.DistanceTo(ped.gameReference.Position);
                Vector3 vdelta = posnew - ped.gameReference.Position;
                //ped.gameReference.Weapons.MP5.Ammo = 999;

                int healthDelta = data.ped_health - ped.gameReference.Health;
                ped.gameReference.Health = data.ped_health;
                ped.last_game_health     = data.ped_health;

                if (data.weapon > 0)
                {
                    if (ped.gameReference.Weapons.Current != (Weapon)data.weapon)
                    {
                        ped.gameReference.Weapons.FromType((Weapon)data.weapon).Ammo       = 999;
                        ped.gameReference.Weapons.FromType((Weapon)data.weapon).AmmoInClip = 999;
                        ped.gameReference.Weapons.Select((Weapon)data.weapon);
                    }
                }
                else
                {
                    ped.gameReference.Weapons.RemoveAll();
                }

                if (healthDelta > 20 && healthDelta < 100)
                {
                    var bpf = new BinaryPacketFormatter(Commands.Player_damage);
                    bpf.Add(id);
                    //Client.instance.chatController.writeChat("damaged " + healthDelta) ;
                    Client.instance.serverConnection.write(bpf.getBytes());
                }

                bool cancelPositionUpdate = false;

                if ((data.state & PlayerState.IsShooting) != 0)
                {
                    ped.animator.playAnimation(PedAnimations.Shoot);
                    cancelPositionUpdate = true;
                }
                else
                if ((data.state & PlayerState.IsAiming) != 0)
                {
                    ped.animator.playAnimation(PedAnimations.Aim);
                    cancelPositionUpdate = true;
                }
                else
                if ((data.state & PlayerState.IsRagdoll) != 0 || data.ped_health <= 0)
                {
                    ped.animator.playAnimation(PedAnimations.Ragdoll);
                    ped.gameReference.Velocity = new Vector3(data.vel_x, data.vel_y, data.vel_z);
                    cancelPositionUpdate       = true;
                }
                else
                if ((data.vstate & VehicleState.IsEnteringVehicle) != 0)
                {
                    ped.animator.playAnimation(PedAnimations.EnterClosestVehicle);
                    cancelPositionUpdate = true;
                }
                else
                if ((data.state & PlayerState.IsCrouching) != 0)
                {
                    ped.animator.playAnimation(PedAnimations.Couch);
                }
                else
                {
                    if ((data.vstate & VehicleState.IsAccelerating) != 0)
                    {
                        if ((data.vstate & VehicleState.IsSprinting) != 0)
                        {
                            ped.animator.playAnimation(PedAnimations.Run);
                        }
                        else
                        {
                            ped.animator.playAnimation(PedAnimations.Walk);
                        }
                    }
                    else
                    {
                        ped.animator.playAnimation(PedAnimations.StandStill);
                    }
                }

                if (!cancelPositionUpdate)
                {
                    ped.gameReference.Position = posnew;
                }
                ped.gameReference.Heading = data.heading;

                //ped.gameReference.Velocity = new Vector3(elemValue.vel_x, elemValue.vel_y, elemValue.vel_z);
                //ped.gameReference.Task.ClearAllImmediately();
            }
        }
        public void updateVehicle(uint id, UpdateDataStruct data, StreamedPed ped)
        {
            if (data.vehicle_id > 0)
            {
                var             posnew = new Vector3(data.pos_x, data.pos_y, data.pos_z);
                StreamedVehicle veh    = vehicleController.GetInstance(data.vehicle_id);
                if (veh != null)
                {
                    if (veh.IsStreamedIn())
                    {
                        if (ped != null && ped.IsStreamedIn() && !ped.gameReference.isInVehicle())
                        {
                            if ((data.state & PlayerState.IsPassenger1) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.RightFront);
                            }
                            else if ((data.state & PlayerState.IsPassenger2) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.LeftRear);
                            }
                            else if ((data.state & PlayerState.IsPassenger3) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.RightFront);
                            }
                            else
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.Driver);
                            }
                        }

                        int healthDelta = data.ped_health - ped.gameReference.Health;
                        ped.gameReference.Health = data.ped_health;
                        ped.last_game_health     = data.ped_health;

                        if (healthDelta > 20 && healthDelta < 100)
                        {
                            var bpf = new BinaryPacketFormatter(Commands.Player_damage);
                            bpf.Add(id);
                            //Client.instance.chatController.writeChat("damaged " + healthDelta) ;
                            Client.instance.serverConnection.write(bpf.getBytes());
                        }

                        int vehicleHealthDelta = data.vehicle_health - veh.gameReference.Health;
                        veh.gameReference.Health = data.vehicle_health;
                        veh.last_game_health     = data.vehicle_health;

                        if (vehicleHealthDelta > 20 && vehicleHealthDelta < 2000 && data.vehicle_id > 0)
                        {
                            var bpf = new BinaryPacketFormatter(Commands.Vehicle_damage, id, data.vehicle_id, vehicleHealthDelta);
                            Client.instance.serverConnection.write(bpf.getBytes());
                        }

                        if ((data.vstate & VehicleState.IsAsPassenger) != 0)
                        {
                            return;
                        }
                        veh.position = posnew;
                        if (veh.gameReference.Position.DistanceTo(posnew) > 1.0f)
                        {
                            veh.gameReference.Position = posnew;
                        }
                        //veh.gameReference.Position = posnew;
                        veh.orientation = new Quaternion(data.rot_x, data.rot_y, data.rot_z, data.rot_a);
                        //veh.gameReference.ApplyForce(, Vector3.Zero);
                        veh.gameReference.RotationQuaternion = veh.orientation;
                        var vel = new Vector3(data.vel_x, data.vel_y, data.vel_z);
                        if (System.Math.Abs(veh.gameReference.Velocity.Length() - vel.Length()) > 6.0f)
                        {
                            veh.gameReference.ApplyForce(vel);
                        }
                        if ((data.vstate & VehicleState.IsBraking) == 0)
                        {
                            ped.gameReference.Task.DrivePointRoute(veh.gameReference, 999.0f, posnew - veh.gameReference.Velocity);
                        }
                        else
                        {
                            ped.gameReference.Task.DrivePointRoute(veh.gameReference, 999.0f, posnew + veh.gameReference.Velocity);
                        }
                        if ((data.state & PlayerState.IsShooting) != 0)
                        {
                            Vector3 pos = veh.gameReference.Position + veh.gameReference.Direction;
                            GTA.Native.Function.Call("FIRE_PED_WEAPON", ped.gameReference, pos.X, pos.Y, pos.Z);
                            GTA.Native.Function.Call("TASK_SHOOT_AT_COORD", ped.gameReference, pos.X, pos.Y, pos.Z, (Int32)4, 999992);
                        }
                    }
                }
            }
        }
        public void updatePed(uint id, UpdateDataStruct data, StreamedPed ped)
        {
            var posnew = new Vector3(data.pos_x, data.pos_y, data.pos_z - 1.0f);
            ped.position = posnew;
            ped.heading = data.heading;
            ped.direction = new Vector3(data.rot_x, data.rot_y, data.rot_z);
            ped.cameraDirection = new Vector3(data.camdir_x, data.camdir_y, data.camdir_z);
            if (ped.IsStreamedIn() && data.vehicle_id == 0)
            {
                if (ped.gameReference.isInVehicle())
                {
                    ped.gameReference.CurrentVehicle.PassengersLeaveVehicle(true);
                    //ped.gameReference.CurrentVehicle.Delete();
                }

                float delta = posnew.DistanceTo(ped.gameReference.Position);
                Vector3 vdelta = posnew - ped.gameReference.Position;
                //ped.gameReference.Weapons.MP5.Ammo = 999;

                int healthDelta = data.ped_health - ped.gameReference.Health;
                ped.gameReference.Health = data.ped_health;
                ped.last_game_health = data.ped_health;

                if (data.weapon > 0)
                {
                    if (ped.gameReference.Weapons.Current != (Weapon)data.weapon)
                    {
                        ped.gameReference.Weapons.FromType((Weapon)data.weapon).Ammo = 999;
                        ped.gameReference.Weapons.FromType((Weapon)data.weapon).AmmoInClip = 999;
                        ped.gameReference.Weapons.Select((Weapon)data.weapon);
                    }
                }
                else
                {
                    ped.gameReference.Weapons.RemoveAll();
                }

                if (healthDelta > 20 && healthDelta < 100)
                {
                    var bpf = new BinaryPacketFormatter(Commands.Player_damage);
                    bpf.Add(id);
                    //Client.instance.chatController.writeChat("damaged " + healthDelta) ;
                    Client.instance.serverConnection.write(bpf.getBytes());
                }

                bool cancelPositionUpdate = false;

                if ((data.state & PlayerState.IsShooting) != 0)
                {
                    ped.animator.playAnimation(PedAnimations.Shoot);
                    cancelPositionUpdate = true;
                }
                else
                    if ((data.state & PlayerState.IsAiming) != 0)
                    {
                        ped.animator.playAnimation(PedAnimations.Aim);
                        cancelPositionUpdate = true;
                    }
                    else
                        if ((data.state & PlayerState.IsRagdoll) != 0 || data.ped_health <= 0)
                        {
                            ped.animator.playAnimation(PedAnimations.Ragdoll);
                            ped.gameReference.Velocity = new Vector3(data.vel_x, data.vel_y, data.vel_z);
                            cancelPositionUpdate = true;
                        }
                        else
                            if ((data.vstate & VehicleState.IsEnteringVehicle) != 0)
                            {
                                ped.animator.playAnimation(PedAnimations.EnterClosestVehicle);
                                cancelPositionUpdate = true;
                            }
                            else
                                if ((data.state & PlayerState.IsCrouching) != 0)
                                {
                                    ped.animator.playAnimation(PedAnimations.Couch);
                                }
                                else
                                {
                                    if ((data.vstate & VehicleState.IsAccelerating) != 0)
                                    {
                                        if ((data.vstate & VehicleState.IsSprinting) != 0)
                                        {
                                            ped.animator.playAnimation(PedAnimations.Run);
                                        }
                                        else
                                        {
                                            ped.animator.playAnimation(PedAnimations.Walk);
                                        }
                                    }
                                    else
                                    {
                                        ped.animator.playAnimation(PedAnimations.StandStill);
                                    }
                                }

                if (!cancelPositionUpdate)
                {
                    ped.gameReference.Position = posnew;
                }
                ped.gameReference.Heading = data.heading;

                //ped.gameReference.Velocity = new Vector3(elemValue.vel_x, elemValue.vel_y, elemValue.vel_z);
                //ped.gameReference.Task.ClearAllImmediately();
            }
        }
 public void prepareVehicle(StreamedPed ped)
 {
     if (ped.gameReference != null) preparePed(ped.gameReference);
 }
        public void updateVehicle(uint id, UpdateDataStruct data, StreamedPed ped)
        {
            if (data.vehicle_id > 0)
            {
                var posnew = new Vector3(data.pos_x, data.pos_y, data.pos_z);
                StreamedVehicle veh = vehicleController.GetInstance(data.vehicle_id);
                if (veh != null)
                {
                    if (veh.IsStreamedIn())
                    {
                        if (ped != null && ped.IsStreamedIn() && !ped.gameReference.isInVehicle())
                        {
                            if ((data.state & PlayerState.IsPassenger1) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.RightFront);
                            }
                            else if ((data.state & PlayerState.IsPassenger2) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.LeftRear);
                            }
                            else if ((data.state & PlayerState.IsPassenger3) != 0)
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.RightFront);
                            }
                            else
                            {
                                ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.Driver);
                            }
                        }

                        int healthDelta = data.ped_health - ped.gameReference.Health;
                        ped.gameReference.Health = data.ped_health;
                        ped.last_game_health = data.ped_health;

                        if (healthDelta > 20 && healthDelta < 100)
                        {
                            var bpf = new BinaryPacketFormatter(Commands.Player_damage);
                            bpf.Add(id);
                            //Client.instance.chatController.writeChat("damaged " + healthDelta) ;
                            Client.instance.serverConnection.write(bpf.getBytes());
                        }

                        int vehicleHealthDelta = data.vehicle_health - veh.gameReference.Health;
                        veh.gameReference.Health = data.vehicle_health;
                        veh.last_game_health = data.vehicle_health;

                        if (vehicleHealthDelta > 20 && vehicleHealthDelta < 2000 && data.vehicle_id > 0)
                        {
                            var bpf = new BinaryPacketFormatter(Commands.Vehicle_damage, id, data.vehicle_id, vehicleHealthDelta);
                            Client.instance.serverConnection.write(bpf.getBytes());
                        }

                        if ((data.vstate & VehicleState.IsAsPassenger) != 0) return;
                        veh.position = posnew;
                        if (veh.gameReference.Position.DistanceTo(posnew) > 1.0f)
                        {
                            veh.gameReference.Position = posnew;
                        }
                        //veh.gameReference.Position = posnew;
                        veh.orientation = new Quaternion(data.rot_x, data.rot_y, data.rot_z, data.rot_a);
                        //veh.gameReference.ApplyForce(, Vector3.Zero);
                        veh.gameReference.RotationQuaternion = veh.orientation;
                        var vel = new Vector3(data.vel_x, data.vel_y, data.vel_z);
                        if (System.Math.Abs(veh.gameReference.Velocity.Length() - vel.Length()) > 6.0f)
                        {
                            veh.gameReference.ApplyForce(vel);
                        }
                        if ((data.vstate & VehicleState.IsBraking) == 0)
                        {
                            ped.gameReference.Task.DrivePointRoute(veh.gameReference, 999.0f, posnew - veh.gameReference.Velocity);
                        }
                        else
                        {
                            ped.gameReference.Task.DrivePointRoute(veh.gameReference, 999.0f, posnew + veh.gameReference.Velocity);
                        }
                        if ((data.state & PlayerState.IsShooting) != 0)
                        {
                            Vector3 pos = veh.gameReference.Position + veh.gameReference.Direction;
                            GTA.Native.Function.Call("FIRE_PED_WEAPON", ped.gameReference, pos.X, pos.Y, pos.Z);
                            GTA.Native.Function.Call("TASK_SHOOT_AT_COORD", ped.gameReference, pos.X, pos.Y, pos.Z, (Int32)4, 999992);
                        }
                    }
                }
            }
        }
        public override void UpdateGfx()
        {
            if (!client.getPlayerPed().Exists())
            {
                return;
            }
            var player_projected = (Vector2)World.WorldToScreenProject(client.getPlayerPed().Position);

            for (int i = 0; i < instances.Count; i++)
            {
                StreamedPed ped = (StreamedPed)instances[i];
                try
                {
                    if (ped.IsStreamedIn())
                    {
                        Vector2 projected = new Vector2();
                        if (client.getPlayerPed().Exists() && ped.gameReference.Exists())
                        {
                            projected = (Vector2)World.WorldToScreenProject(ped.gameReference.Position);
                        }
                        else
                        {
                            return;
                        }
                        Vector3 peddelta = Vector3.Zero;
                        if (client.getPlayerPed().Exists() && ped.gameReference.Exists())
                        {
                            peddelta = ped.gameReference.Position - client.getPlayerPed().Position;
                        }
                        else
                        {
                            return;
                        }
                        float distance = peddelta.Length();
                        //float distance_from_centerscreen = (projected - new Vector2(Game.Resolution.Width, Game.Resolution.Height)).Length();
                        int alpha = (int)Math.Round((255.0f * (distance / -DrawDistance + 1.0f)));
                        if (alpha > 255)
                        {
                            alpha = 255;
                        }
                        if (alpha < 0)
                        {
                            alpha = 0;
                        }
                        if (!ped.gameReference.Exists())
                        {
                            return;
                        }
                        if (projected.X < -120 || projected.X > Game.Resolution.Width || projected.Y < -50 || projected.Y > Game.Resolution.Height ||
                            (peddelta + Game.CurrentCamera.Direction).Length() < distance || (checkLOS && !Game.CurrentCamera.isSphereVisible(ped.gameReference.Position, 3.0f)))
                        {
                            if (!ped.gameReference.Exists())
                            {
                                return;
                            }
                            ped.nickDraw.destroy();
                            ped.healthDraw.destroy();
                            ped.healthDraw2.destroy();
                            if (ped.carHealthDraw != null)
                            {
                                ped.carHealthDraw.destroy();
                            }
                            if (ped.carHealthDraw2 != null)
                            {
                                ped.carHealthDraw2.destroy();
                            }
                            ped.chatDraw.destroy();
                            ped.nickDraw       = null;
                            ped.healthDraw     = null;
                            ped.healthDraw2    = null;
                            ped.carHealthDraw  = null;
                            ped.carHealthDraw2 = null;
                            ped.chatDraw       = null;
                        }
                        else
                        {
                            var rect = new System.Drawing.RectangleF(projected.X - 100, projected.Y - 50, 200, 30);

                            var rect2  = new System.Drawing.RectangleF(projected.X - 37, projected.Y - 22, 37 * 2, 11);
                            var rect22 = new System.Drawing.RectangleF(projected.X - 35, projected.Y - 20, (35.0f * 2.0f) * (ped.last_game_health < 0 ? 0 : ped.last_game_health / 100.0f), 7);

                            var rect3 = new System.Drawing.RectangleF(projected.X - 300, projected.Y - 10, 600, 130);

                            System.Drawing.RectangleF carrect1 = System.Drawing.RectangleF.Empty;
                            System.Drawing.RectangleF carrect2 = System.Drawing.RectangleF.Empty;
                            bool invehicle = ped.gameReference.isInVehicle();
                            if (invehicle)
                            {
                                carrect1 = new System.Drawing.RectangleF(projected.X - 37, projected.Y - 2, 37 * 2, 11);
                                carrect2 = new System.Drawing.RectangleF(projected.X - 35, projected.Y, (35.0f * 2.0f) * (ped.gameReference.CurrentVehicle.Health < 0 ? 0 : ped.gameReference.CurrentVehicle.Health / 1000.0f), 7);
                            }

                            var chaticonframe = new System.Drawing.RectangleF(projected.X - 30, projected.Y - 80, 60, 30);
                            if (ped.nickDraw == null)
                            {
                                ped.nickDraw    = new ClientTextView(rect, TextAlignment.Center, ped.networkname, nickfont, System.Drawing.Color.FromArgb(alpha, 255, 255, 255));
                                ped.healthDraw  = new ClientRectangleView(rect2, System.Drawing.Color.FromArgb(alpha, 0, 0, 0));
                                ped.healthDraw2 = new ClientRectangleView(rect22, System.Drawing.Color.FromArgb(alpha, 80, 80, 255));
                                ped.chatDraw    = new ClientTextView(rect3, TextAlignment.Center, ped.CurrentChatMessage, nickfont, System.Drawing.Color.FromArgb(alpha, 255, 255, 255));
                                if (invehicle)
                                {
                                    ped.carHealthDraw  = new ClientRectangleView(carrect1, System.Drawing.Color.FromArgb(alpha, 0, 0, 0));
                                    ped.carHealthDraw2 = new ClientRectangleView(carrect2, System.Drawing.Color.FromArgb(alpha, 255, 80, 80));
                                }
                            }
                            else
                            {
                                ped.nickDraw.Box      = rect;
                                ped.nickDraw.color    = System.Drawing.Color.FromArgb(alpha, 255, 255, 255);
                                ped.healthDraw.Box    = rect2;
                                ped.healthDraw.color  = System.Drawing.Color.FromArgb(alpha, 0, 0, 0);
                                ped.healthDraw2.Box   = rect22;
                                ped.healthDraw2.color = System.Drawing.Color.FromArgb(alpha, 80, 80, 255);
                                ped.chatDraw.Box      = rect3;
                                ped.chatDraw.text     = ped.CurrentChatMessage;
                                ped.chatDraw.color    = System.Drawing.Color.FromArgb(alpha, 255, 255, 255);
                                if (invehicle)
                                {
                                    if (ped.carHealthDraw != null)
                                    {
                                        ped.carHealthDraw.Box    = carrect1;
                                        ped.carHealthDraw.color  = System.Drawing.Color.FromArgb(alpha, 0, 0, 0);
                                        ped.carHealthDraw2.Box   = carrect2;
                                        ped.carHealthDraw2.color = System.Drawing.Color.FromArgb(alpha, 255, 80, 80);
                                    }
                                    else
                                    {
                                        ped.carHealthDraw  = new ClientRectangleView(carrect1, System.Drawing.Color.FromArgb(alpha, 0, 0, 0));
                                        ped.carHealthDraw2 = new ClientRectangleView(carrect2, System.Drawing.Color.FromArgb(alpha, 255, 80, 80));
                                    }
                                }
                                else
                                {
                                    if (ped.carHealthDraw != null)
                                    {
                                        ped.carHealthDraw.destroy();
                                    }
                                    if (ped.carHealthDraw2 != null)
                                    {
                                        ped.carHealthDraw2.destroy();
                                    }
                                    ped.carHealthDraw  = null;
                                    ped.carHealthDraw2 = null;
                                }
                            }
                        }
                    }
                }
                catch
                { }
            }
        }
Beispiel #11
0
        private void onReceive(IAsyncResult iar)
        {
            lock (client)
            {
                try
                {
                    client.client.Client.EndReceive(iar);
                    var bpr = new BinaryPacketReader(buffer);
                    while (bpr.canRead())
                    {
                        Commands command = bpr.readCommand();
                        if (command == Commands.Invalid)
                        {
                            break;
                        }
                        try
                        {
                            switch (command)
                            {
                            case Commands.Client_enableUDPTunnel:
                            {
                                int port = bpr.readInt32();
                                client.udpTunnel = new ClientUDPTunnel(port);
                            }
                            break;

                            case Commands.Client_ping:
                            {
                                Int64 timestamp = bpr.readInt64();
                                write(new BinaryPacketFormatter(Commands.Client_ping, timestamp).getBytes());
                            }
                            break;

                            case Commands.Player_setModel:
                            {
                                string model = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayer().Model = new Model(model);
                                    }));
                            }
                            break;

                            case Commands.Player_setHeading:
                            {
                                var g = bpr.readSingle();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayerPed().Heading = g;
                                    }));
                            }
                            break;


                            case Commands.Player_setVirtualWorld:
                            {
                                uint id     = bpr.readUInt32();
                                uint vworld = bpr.readUInt32();
                                if (client.pedController.Exists(id))
                                {
                                    var instance = client.pedController.GetInstance(id);
                                    instance.VirtualWorld = vworld;
                                }
                            }
                            break;

                            case Commands.Game_setGameTime:
                            {
                                var g = bpr.readInt64();
                                client.enqueueAction(new Action(delegate
                                    {
                                        World.CurrentDayTime = new TimeSpan(g);
                                    }));
                            }
                            break;

                            case Commands.Game_setWeather:
                            {
                                var g = bpr.readByte();
                                client.enqueueAction(new Action(delegate
                                    {
                                        World.Weather = (Weather)(int)g;
                                        GTA.Native.Function.Call("FORCE_WEATHER_NOW", (int)g);
                                    }));
                            }
                            break;

                            case Commands.Game_fadeScreenIn:
                            {
                                var data = bpr.readInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        Game.FadeScreenIn(data);
                                    }));
                            }
                            break;

                            case Commands.Game_fadeScreenOut:
                            {
                                var data = bpr.readInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        Game.FadeScreenOut(data);
                                    }));
                            }
                            break;

                            case Commands.Game_showLoadingScreen:
                            {
                                var data = bpr.readInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        GTA.Native.Function.Call("FORCE_LOADING_SCREEN");
                                    }));
                            }
                            break;

                            case Commands.Game_hideLoadingScreen:
                            {
                                var data = bpr.readInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        GTA.Native.Function.Call("DONT_DISPLAY_LOADING_ON_FADE_THIS_FRAME");
                                        Game.FadeScreenIn(1);
                                    }));
                            }
                            break;

                            case Commands.Game_setGravity:
                            {
                                var data = bpr.readSingle();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayerPed().GravityMultiplier = data;
                                    }));
                            }
                            break;

                            case Commands.Client_setVirtualWorld:
                            {
                                client.CurrentVirtualWorld = bpr.readUInt32();
                            }
                            break;

                            case Commands.Client_pauseBroadcast:
                            {
                                client.BroadcastingPaused = true;
                            }
                            break;

                            case Commands.Client_resumeBroadcast:
                            {
                                client.BroadcastingPaused = false;
                            }
                            break;

                            case Commands.Client_JSEval:
                            {
                                string script = bpr.readString();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.jsEngine.Execute(script);
                                    }));
                            }
                            break;

                            case Commands.Vehicle_removePeds:
                            {
                                uint id     = bpr.readUInt32();
                                uint vworld = bpr.readUInt32();
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.EveryoneLeaveVehicle();
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_repaint:
                            {
                                uint   id    = bpr.readUInt32();
                                ushort color = bpr.readUInt16();
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.Color = (ColorIndex)color;
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_repair:
                            {
                                uint id = bpr.readUInt32();
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    //instance.health = 100;
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.Repair();
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_setModel:
                            {
                                uint   id    = bpr.readUInt32();
                                string model = MIVSDK.ModelDictionary.getVehicleById(bpr.readUInt32());
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    instance.model = model;
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.Delete();
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_setOrientation:
                            {
                                uint       id   = bpr.readUInt32();
                                Quaternion quat = fromSharpDX(bpr.readQuaternion());
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    instance.orientation = quat;
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.RotationQuaternion = quat;
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_setPosition:
                            {
                                uint    id       = bpr.readUInt32();
                                Vector3 position = fromSharpDX(bpr.readVector3());
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    instance.position = position;
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.Position = position;
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_setVelocity:
                            {
                                uint    id       = bpr.readUInt32();
                                Vector3 velocity = fromSharpDX(bpr.readVector3());
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    if (instance.StreamedIn)
                                    {
                                        instance.gameReference.Velocity = velocity;
                                    }
                                }
                            }
                            break;

                            case Commands.Vehicle_setVirtualWorld:
                            {
                                uint id     = bpr.readUInt32();
                                uint vworld = bpr.readUInt32();
                                if (client.vehicleController.Exists(id))
                                {
                                    var instance = client.vehicleController.GetInstance(id);
                                    instance.VirtualWorld = vworld;
                                }
                            }
                            break;

                            case Commands.NPC_setVirtualWorld:
                            {
                                uint id     = bpr.readUInt32();
                                uint vworld = bpr.readUInt32();
                                if (client.npcPedController.Exists(id))
                                {
                                    var instance = client.npcPedController.GetInstance(id);
                                    instance.VirtualWorld = vworld;
                                }
                            }
                            break;

                            case Commands.Player_setHealth:
                            {
                                int h = bpr.readInt32();
                                //client.chatController.writeChat("setting healtcz " + h.ToString());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayerPed().Health = h;
                                    }));
                            }
                            break;

                            case Commands.Player_setVehicleHealth:
                            {
                                int h = bpr.readInt32();
                                //client.chatController.writeChat("setting healtcz " + h.ToString());
                                client.enqueueAction(new Action(delegate
                                    {
                                        if (client.getPlayerPed().isInVehicle())
                                        {
                                            if (h <= 0)
                                            {
                                                client.getPlayerPed().CurrentVehicle.Explode();
                                            }
                                            else
                                            {
                                                client.getPlayerPed().CurrentVehicle.Health = h;
                                            }
                                        }
                                    }));
                            }
                            break;

                            case Commands.Chat_writeLine:
                            {
                                client.chatController.writeChat(bpr.readString());
                            }
                            break;

                            case Commands.Player_setPosition:
                            {
                                Vector3 vec = fromSharpDX(bpr.readVector3());
                                //client.chatController.writeChat("OasK");
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayer().TeleportTo(vec);
                                    }));
                            }
                            break;

                            case Commands.Player_setVelocity:
                            {
                                Vector3 vec = fromSharpDX(bpr.readVector3());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayerPed().Velocity = vec;
                                    }));
                            }
                            break;

                            case Commands.InternalClient_finishSpawn:
                            {
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.finishSpawn();
                                    }));
                            }
                            break;

                            case Commands.Camera_setPosition:
                            {
                                var data = fromSharpDX(bpr.readVector3());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.Position = data;
                                    }));
                            }
                            break;

                            case Commands.Camera_setDirection:
                            {
                                var data = fromSharpDX(bpr.readVector3());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.Direction = data;
                                    }));
                            }
                            break;

                            case Commands.Camera_setOrientation:
                            {
                                var data = fromSharpDX(bpr.readVector3());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.Rotation = data;
                                    }));
                            }
                            break;

                            case Commands.Camera_setFOV:
                            {
                                var data = bpr.readSingle();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.FOV = data;
                                    }));
                            }
                            break;

                            case Commands.Camera_lookAt:
                            {
                                var data = fromSharpDX(bpr.readVector3());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.LookAt(data);
                                    }));
                            }
                            break;

                            case Commands.Camera_reset:
                            {
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.cameraController.Reset();
                                    }));
                            }
                            break;

                            case Commands.Player_freeze:
                            {
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayer().CanControlCharacter = false;
                                    }));
                            }
                            break;

                            case Commands.Player_unfreeze:
                            {
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayer().CanControlCharacter = true;
                                    }));
                            }
                            break;

                            case Commands.Player_giveWeapon:
                            {
                                Weapon weapon = (Weapon)bpr.readInt32();
                                int    ammo   = bpr.readInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.getPlayerPed().Weapons.FromType(weapon).Ammo = ammo;
                                    }));
                            }
                            break;

                            case Commands.Global_setPlayerPedText:
                            {
                                uint   playerid = bpr.readUInt32();
                                string text     = bpr.readString();
                                if (client.pedController.Exists(playerid))
                                {
                                    StreamedPed ped = client.pedController.GetInstance(playerid);
                                    ped.CurrentChatMessage = text;
                                }
                            }
                            break;

                            case Commands.Global_setPlayerModel:
                            {
                                uint   playerid = bpr.readUInt32();
                                string model    = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());
                                if (client.playerModels.ContainsKey(playerid))
                                {
                                    client.playerModels[playerid] = model;
                                }
                                else
                                {
                                    client.playerModels.Add(playerid, model);
                                }
                                if (client.pedController.Exists(playerid))
                                {
                                    var player = client.pedController.GetInstance(playerid);
                                    if (player.IsStreamedIn())
                                    {
                                        player.gameReference.Delete();
                                    }
                                }
                            }
                            break;

                            case Commands.Global_setPlayerName:
                            {
                                uint   playerid = bpr.readUInt32();
                                string name     = bpr.readString();
                                if (client.playerNames.ContainsKey(playerid))
                                {
                                    client.playerNames[playerid] = name;
                                }
                                else
                                {
                                    client.playerNames.Add(playerid, name);
                                }
                                if (client.pedController.Exists(playerid))
                                {
                                    var player = client.pedController.GetInstance(playerid);
                                    if (player.IsStreamedIn())
                                    {
                                        player.gameReference.Delete();
                                    }
                                }
                            }
                            break;

                            case Commands.Global_createPlayer:
                            {
                                uint   playerid = bpr.readUInt32();
                                string model    = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());
                                string name     = bpr.readString();

                                if (client.playerNames.ContainsKey(playerid))
                                {
                                    client.playerNames[playerid] = name;
                                }
                                else
                                {
                                    client.playerNames.Add(playerid, name);
                                }

                                if (client.playerModels.ContainsKey(playerid))
                                {
                                    client.playerModels[playerid] = model;
                                }
                                else
                                {
                                    client.playerModels.Add(playerid, model);
                                }

                                client.pedController.Add(playerid, new StreamedPed(client.pedStreamer, model, name, Vector3.Zero, 0, (BlipColor)(playerid % 13)));
                            }
                            break;

                            case Commands.Request_getSelectedPlayer:
                            {
                                uint requestid = bpr.readUInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        var peds        = World.GetPeds(client.getPlayerPed().Position, 80.0f);
                                        Ped selectedPed = null;
                                        foreach (var ped in peds)
                                        {
                                            var projected = (Vector2)World.WorldToScreenProject(ped.Position);
                                            if (Math.Abs((projected - new Vector2(Game.Resolution.Width / 2, Game.Resolution.Height / 2)).Length()) < 30.0)
                                            {
                                                selectedPed = ped;
                                                break;
                                            }
                                        }
                                        var bpf = new BinaryPacketFormatter(Commands.Request_getSelectedPlayer);
                                        bpf.Add(requestid);
                                        if (selectedPed != null)
                                        {
                                            bpf.Add(client.pedController.dict.Count(a => a.Value.IsStreamedIn() && a.Value.gameReference == selectedPed) > 0 ? client.pedController.dict.First(a => a.Value.IsStreamedIn() && a.Value.gameReference == selectedPed).Key : 0);
                                        }
                                        else
                                        {
                                            bpf.Add(0);
                                        }
                                        client.serverConnection.write(bpf.getBytes());
                                    }));
                            }
                            break;

                            case Commands.Request_getCameraPosition:
                            {
                                uint requestid = bpr.readUInt32();
                                client.enqueueAction(new Action(delegate
                                    {
                                        var bpf = new BinaryPacketFormatter(Commands.Request_getCameraPosition);
                                        bpf.Add(requestid);
                                        bpf.Add(new SharpDX.Vector3(Game.CurrentCamera.Position.X, Game.CurrentCamera.Position.Y, Game.CurrentCamera.Position.Z));
                                        client.serverConnection.write(bpf.getBytes());
                                    }));
                            }
                            break;

                            case Commands.Request_worldToScreen:
                            {
                                uint requestid = bpr.readUInt32();
                                var  world     = bpr.readVector3();
                                client.enqueueAction(new Action(delegate
                                    {
                                        var bpf = new BinaryPacketFormatter(Commands.Request_worldToScreen);
                                        bpf.Add(requestid);
                                        var screen = (Vector2)World.WorldToScreenProject(new Vector3(world.X, world.Y, world.Z));
                                        bpf.Add(screen.X);
                                        bpf.Add(screen.Y);
                                        client.serverConnection.write(bpf.getBytes());
                                    }));
                            }
                            break;

                            case Commands.Request_isObjectVisible:
                            {
                                uint requestid = bpr.readUInt32();
                                var  position  = bpr.readVector3();
                                client.enqueueAction(new Action(delegate
                                    {
                                        var bpf = new BinaryPacketFormatter(Commands.Request_isObjectVisible);
                                        bpf.Add(requestid);
                                        bpf.Add(new byte[1] {
                                            (byte)(Game.CurrentCamera.isSphereVisible(new Vector3(position.X, position.Y, position.Z), 1.0f) ? 1 : 0)
                                        });
                                        client.serverConnection.write(bpf.getBytes());
                                    }));
                            }
                            break;

                            case Commands.Vehicle_create:
                            {
                                uint       id    = bpr.readUInt32();
                                Vector3    pos   = fromSharpDX(bpr.readVector3());
                                Quaternion rot   = fromSharpDX(bpr.readQuaternion());
                                string     model = MIVSDK.ModelDictionary.getVehicleById(bpr.readUInt32());
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.vehicleController.Add(id, new StreamedVehicle(client.vehicleStreamer, model, pos, rot));
                                    }));
                            }
                            break;

                            case Commands.NPC_create:
                            {
                                //int count = bpr.readInt32();
                                uint    id      = bpr.readUInt32();
                                Vector3 pos     = fromSharpDX(bpr.readVector3());
                                float   heading = bpr.readSingle();
                                string  model   = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());

                                string str = bpr.readString();
                                client.enqueueAction(new Action(delegate
                                    {
                                        client.npcPedController.Add(id, new StreamedPed(client.pedStreamer, model, str, pos, heading, BlipColor.Grey));
                                    }));
                            }
                            break;

                            case Commands.NPC_update:
                            {
                                //int count = bpr.readInt32();
                                uint    id      = bpr.readUInt32();
                                Vector3 pos     = fromSharpDX(bpr.readVector3());
                                float   heading = bpr.readSingle();
                                string  model   = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());

                                string str = bpr.readString();
                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped         = client.npcPedController.GetInstance(id);
                                        ped.position    = pos;
                                        ped.heading     = heading;
                                        ped.model       = model;
                                        ped.networkname = str;
                                    }));
                            }
                            break;

                            case Commands.NPC_setPosition:
                            {
                                uint id = bpr.readUInt32();

                                Vector3 pos = fromSharpDX(bpr.readVector3());

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped      = client.npcPedController.GetInstance(id);
                                        ped.position = pos;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.gameReference.Position = pos;
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_setHeading:
                            {
                                uint id = bpr.readUInt32();

                                float heading = bpr.readSingle();

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped     = client.npcPedController.GetInstance(id);
                                        ped.heading = heading;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.gameReference.Heading = heading;
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_runTo:
                            {
                                uint id = bpr.readUInt32();

                                Vector3 pos = fromSharpDX(bpr.readVector3());

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped      = client.npcPedController.GetInstance(id);
                                        ped.position = pos;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.animator.refreshAnimationForce();
                                            ped.animator.playAnimation(PedAnimations.RunTo, pos);
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_walkTo:
                            {
                                uint id = bpr.readUInt32();

                                Vector3 pos = fromSharpDX(bpr.readVector3());

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped      = client.npcPedController.GetInstance(id);
                                        ped.position = pos;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.animator.refreshAnimationForce();
                                            ped.animator.playAnimation(PedAnimations.WalkTo, pos);
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_enterVehicle:
                            {
                                uint id = bpr.readUInt32();

                                uint vid = bpr.readUInt32();

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped        = client.npcPedController.GetInstance(id);
                                        ped.vehicle_id = vid;
                                        var veh        = client.vehicleController.GetInstance(vid);
                                        if (ped.IsStreamedIn() && veh.IsStreamedIn())
                                        {
                                            ped.gameReference.WarpIntoVehicle(veh.gameReference, VehicleSeat.Driver);
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_driveTo:
                            {
                                uint id = bpr.readUInt32();

                                Vector3 pos = fromSharpDX(bpr.readVector3());

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped = client.npcPedController.GetInstance(id);
                                        if (ped.vehicle_id > 0)
                                        {
                                            var veh = client.vehicleController.GetInstance(ped.vehicle_id);
                                            if (ped.IsStreamedIn() && veh.IsStreamedIn())
                                            {
                                                ped.gameReference.Task.DriveTo(veh.gameReference, pos, 999.0f, false, true);
                                            }
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_leaveVehicle:
                            {
                                uint id = bpr.readUInt32();

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped        = client.npcPedController.GetInstance(id);
                                        ped.vehicle_id = 0;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.gameReference.LeaveVehicle();
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_setModel:
                            {
                                uint   id    = bpr.readUInt32();
                                string model = MIVSDK.ModelDictionary.getPedModelById(bpr.readUInt32());

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped   = client.npcPedController.GetInstance(id);
                                        ped.model = model;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.gameReference.Delete();
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPC_setImmortal:
                            {
                                uint id     = bpr.readUInt32();
                                byte option = bpr.readByte();

                                client.enqueueAction(new Action(delegate
                                    {
                                        var ped      = client.npcPedController.GetInstance(id);
                                        ped.immortal = option == 1;
                                        if (ped.IsStreamedIn())
                                        {
                                            ped.gameReference.Invincible = option == 1;
                                        }
                                    }));
                            }
                            break;

                            case Commands.NPCDialog_show:
                            {
                                uint     id          = bpr.readUInt32();
                                string   captiontext = bpr.readString();
                                string   texttext    = bpr.readString();
                                string   str         = bpr.readString();
                                string[] split       = str.Split('\x01');
                                client.enqueueAction(new Action(delegate
                                    {
                                        GTA.Forms.Form form = new GTA.Forms.Form();

                                        GTA.Forms.Label caption = new GTA.Forms.Label();
                                        caption.Location        = new System.Drawing.Point(10, 10);
                                        caption.Text            = captiontext;

                                        GTA.Forms.Label text = new GTA.Forms.Label();
                                        text.Location        = new System.Drawing.Point(10, 40);
                                        text.Text            = texttext;

                                        form.Controls.Add(caption);
                                        form.Controls.Add(text);

                                        for (int i = 0; i < split.Length; i++)
                                        {
                                            GTA.Forms.Button button = new GTA.Forms.Button();
                                            button.Location         = new System.Drawing.Point(10, 40 + i * 20);
                                            button.Text             = split[i];

                                            button.MouseDown += (s, o) =>
                                            {
                                                var bpf = new BinaryPacketFormatter(Commands.NPCDialog_sendResponse);
                                                bpf.Add(id);
                                                bpf.Add(new byte[1] {
                                                    (byte)(i - 2)
                                                });
                                                write(bpf.getBytes());

                                                form.Close();
                                            };

                                            form.Controls.Add(button);
                                        }
                                        form.Show();
                                    }));
                            }
                            break;
                            }
                        }
                        catch (Exception e)
                        {
                            Client.log("Failed dispatching command " + command.ToString() + " with " + e.Message + " " + e.StackTrace);
                            client.chatController.writeChat("Failed dispatching command " + command.ToString() + " with " + e.Message + " " + e.StackTrace);
                            client.chatController.writeChat("Disconnected abnormally from server");
                            client.currentState = ClientState.Disconnected;
                        }
                    }

                    client.client.Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, onReceive, null);
                    //}
                }
                catch (Exception e)
                {
                    Client.log("Failed receive with message " + e.Message + " " + e.StackTrace);
                    client.chatController.writeChat("Failed receive with message " + e.Message + " " + e.StackTrace);
                    client.chatController.writeChat("Disconnected abnormally from server");
                    client.currentState = ClientState.Disconnected;
                    //throw e;
                }
            }
        }