182 lines
6.8 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
2022-05-22 15:55:26 +08:00
using GTA.Math;
2022-07-20 17:50:01 +08:00
using GTA.Native;
using Lidgren.Network;
using RageCoop.Core;
2022-07-29 18:17:45 +08:00
using System;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
internal static partial class Networking
2022-05-22 15:55:26 +08:00
{
#region -- SEND --
2022-05-23 15:48:02 +08:00
/// <summary>
/// Pack the packet then send to server.
/// </summary>
/// <param name="p"></param>
/// <param name="channel"></param>
/// <param name="method"></param>
public static void Send(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
2022-05-23 15:48:02 +08:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
p.Pack(outgoingMessage);
Client.SendMessage(outgoingMessage, method, (int)channel);
2022-05-22 15:55:26 +08:00
}
2022-05-23 15:48:02 +08:00
2022-07-20 17:50:01 +08:00
public static void SendPed(SyncedPed c, bool full)
2022-05-22 15:55:26 +08:00
{
Ped p = c.MainPed;
2022-07-20 17:50:01 +08:00
var packet = new Packets.PedSync()
2022-05-22 15:55:26 +08:00
{
ID =c.ID,
OwnerID=c.OwnerID,
2022-05-22 15:55:26 +08:00
Health = p.Health,
Rotation = p.ReadRotation(),
Velocity = p.Velocity,
2022-05-22 15:55:26 +08:00
Speed = p.GetPedSpeed(),
CurrentWeaponHash = (uint)p.Weapons.Current.Hash,
2022-07-17 12:22:11 +08:00
Flags = p.GetPedFlags(),
2022-05-22 15:55:26 +08:00
Heading=p.Heading,
};
2022-07-17 12:22:11 +08:00
if (packet.Flags.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
packet.AimCoords = p.GetAimCoord();
2022-05-22 15:55:26 +08:00
}
2022-07-17 18:44:16 +08:00
if (packet.Flags.HasPedFlag(PedDataFlags.IsRagdoll))
{
packet.HeadPosition=p.Bones[Bone.SkelHead].Position;
packet.RightFootPosition=p.Bones[Bone.SkelRightFoot].Position;
packet.LeftFootPosition=p.Bones[Bone.SkelLeftFoot].Position;
}
else
{
packet.Position = p.ReadPosition();
2022-07-17 18:44:16 +08:00
}
if (full)
{
2022-07-17 12:22:11 +08:00
packet.Flags |= PedDataFlags.IsFullSync;
packet.Clothes=p.GetPedClothes();
packet.ModelHash=p.Model.Hash;
packet.WeaponComponents=p.Weapons.Current.GetWeaponComponents();
packet.WeaponTint=(byte)Function.Call<int>(Hash.GET_PED_WEAPON_TINT_INDEX, p, p.Weapons.Current.Hash);
2022-07-04 22:50:47 +08:00
Blip b;
if (c.IsPlayer)
{
packet.BlipColor=Scripting.API.Config.BlipColor;
packet.BlipSprite=Scripting.API.Config.BlipSprite;
packet.BlipScale=Scripting.API.Config.BlipScale;
}
else if ((b = p.AttachedBlip) !=null)
2022-07-04 22:50:47 +08:00
{
packet.BlipColor=b.Color;
packet.BlipSprite=b.Sprite;
if (packet.BlipSprite==BlipSprite.PoliceOfficer || packet.BlipSprite==BlipSprite.PoliceOfficer2)
{
packet.BlipScale=0.5f;
}
2022-07-04 22:50:47 +08:00
}
}
Send(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void SendVehicle(SyncedVehicle v, bool full)
2022-05-22 15:55:26 +08:00
{
Vehicle veh = v.MainVehicle;
2022-05-23 15:48:02 +08:00
var packet = new Packets.VehicleSync()
2022-05-22 15:55:26 +08:00
{
ID =v.ID,
OwnerID=v.OwnerID,
2022-07-17 12:22:11 +08:00
Flags = veh.GetVehicleFlags(),
2022-05-22 15:55:26 +08:00
SteeringAngle = veh.SteeringAngle,
Position = veh.PredictPosition(),
Quaternion=veh.ReadQuaternion(),
Velocity = veh.Velocity,
RotationVelocity=veh.RotationVelocity,
2022-05-22 15:55:26 +08:00
ThrottlePower = veh.ThrottlePower,
BrakePower = veh.BrakePower,
2022-05-23 15:48:02 +08:00
};
2022-07-17 12:22:11 +08:00
if (packet.Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering)) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); }
if (full)
2022-06-03 13:11:17 +08:00
{
byte primaryColor = 0;
byte secondaryColor = 0;
unsafe
{
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
2022-07-17 12:22:11 +08:00
packet.Flags |= VehicleDataFlags.IsFullSync;
packet.Colors = new byte[] { primaryColor, secondaryColor };
packet.DamageModel=veh.GetVehicleDamageModel();
packet.LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0;
packet.RoofState=(byte)veh.RoofState;
packet.Mods = veh.Mods.GetVehicleMods();
packet.ModelHash=veh.Model.Hash;
packet.EngineHealth=veh.EngineHealth;
packet.Passengers=veh.GetPassengers();
packet.LockStatus=veh.LockStatus;
packet.LicensePlate=Function.Call<string>(Hash.GET_VEHICLE_NUMBER_PLATE_TEXT, veh);
packet.Livery=Function.Call<int>(Hash.GET_VEHICLE_LIVERY, veh);
if (v.MainVehicle==Game.Player.LastVehicle)
{
packet.RadioStation=Util.GetPlayerRadioIndex();
}
if (packet.EngineHealth>v.LastEngineHealth)
{
packet.Flags |= VehicleDataFlags.Repaired;
}
v.LastEngineHealth=packet.EngineHealth;
2022-06-03 13:11:17 +08:00
}
2022-07-20 17:50:01 +08:00
Send(packet, ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
public static void SendProjectile(SyncedProjectile sp)
{
var p = sp.MainProjectile;
var packet = new Packets.ProjectileSync()
{
ID =sp.ID,
ShooterID=sp.ShooterID,
Rotation=p.Rotation,
Velocity=p.Velocity,
WeaponHash=(uint)p.WeaponHash,
Exploded=p.IsDead
};
2022-07-30 14:09:07 +08:00
packet.Position=p.Position+packet.Velocity*Latency;
2022-07-20 17:50:01 +08:00
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
Send(packet, ConnectionChannel.ProjectileSync);
}
2022-05-23 15:48:02 +08:00
2022-05-22 15:55:26 +08:00
#region SYNC EVENTS
2022-07-20 17:50:01 +08:00
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID)
2022-05-22 15:55:26 +08:00
{
2022-05-23 15:48:02 +08:00
Send(new Packets.BulletShot()
2022-05-22 15:55:26 +08:00
{
StartPosition = start,
EndPosition = end,
2022-05-22 15:55:26 +08:00
OwnerID = ownerID,
WeaponHash=weapon,
2022-05-23 15:48:02 +08:00
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
#endregion
public static void SendChatMessage(string message)
2022-05-22 15:55:26 +08:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
2022-07-29 18:17:45 +08:00
new Packets.ChatMessage(new Func<string, byte[]>((s) =>
{
return Security.Encrypt(s.GetBytes());
})) { Username = Main.Settings.Username, Message = message }.Pack(outgoingMessage);
2022-05-22 15:55:26 +08:00
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
Client.FlushSendQueue();
#if DEBUG
#endif
}
#endregion
}
}