208 lines
8.5 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-08-08 17:03:41 +08:00
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
internal static partial class Networking
2022-05-22 15:55:26 +08:00
{
2022-08-13 08:22:14 +08:00
/// <summary>
/// Reduce GC pressure by reusing frequently used packets
/// </summary>
2022-09-06 21:46:35 +08:00
private static class SendPackets
2022-08-13 08:22:14 +08:00
{
public static Packets.PedSync PedPacket = new Packets.PedSync();
public static Packets.VehicleSync VehicelPacket = new Packets.VehicleSync();
public static Packets.ProjectileSync ProjectilePacket = new Packets.ProjectileSync();
}
2022-08-04 17:38:28 +08:00
public static int SyncInterval = 30;
2022-08-08 17:03:41 +08:00
public static List<NetConnection> Targets = new List<NetConnection>();
2022-08-11 16:25:38 +08:00
public static void SendSync(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
2022-05-23 15:48:02 +08:00
{
2022-08-11 16:25:38 +08:00
Peer.SendTo(p, Targets, channel, method);
2022-08-08 17:03:41 +08:00
}
2022-09-06 21:46:35 +08:00
public static void SendPed(SyncedPed sp, bool full)
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
if (sp.LastSentStopWatch.ElapsedMilliseconds < SyncInterval)
2022-08-04 17:38:28 +08:00
{
return;
}
Ped ped = sp.MainPed;
var p = SendPackets.PedPacket;
2022-09-06 21:46:35 +08:00
p.ID = sp.ID;
p.OwnerID = sp.OwnerID;
p.Health = ped.Health;
p.Rotation = ped.ReadRotation();
p.Velocity = ped.ReadVelocity();
p.Speed = ped.GetPedSpeed();
p.Flags = ped.GetPedFlags();
2022-09-06 21:46:35 +08:00
p.Heading = ped.Heading;
if (p.Flags.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
p.AimCoords = ped.GetAimCoord();
2022-05-22 15:55:26 +08:00
}
if (p.Flags.HasPedFlag(PedDataFlags.IsRagdoll))
2022-07-17 18:44:16 +08:00
{
2022-09-06 21:46:35 +08:00
p.HeadPosition = ped.Bones[Bone.SkelHead].Position;
p.RightFootPosition = ped.Bones[Bone.SkelRightFoot].Position;
p.LeftFootPosition = ped.Bones[Bone.SkelLeftFoot].Position;
2022-07-17 18:44:16 +08:00
}
else
{
// Seat sync
2022-09-06 21:46:35 +08:00
if (p.Speed >= 4)
{
var veh = ped.CurrentVehicle?.GetSyncEntity() ?? ped.VehicleTryingToEnter?.GetSyncEntity() ?? ped.LastVehicle?.GetSyncEntity();
p.VehicleID = veh?.ID ?? 0;
2022-09-06 21:46:35 +08:00
if (p.VehicleID == 0) { Main.Logger.Error("Invalid vehicle"); }
if (p.Speed == 5)
{
2022-09-06 21:46:35 +08:00
p.Seat = ped.GetSeatTryingToEnter();
}
else
{
2022-09-06 21:46:35 +08:00
p.Seat = ped.SeatIndex;
}
2022-09-06 21:46:35 +08:00
if (!veh.IsLocal && p.Speed == 4 && p.Seat == VehicleSeat.Driver)
{
2022-09-06 21:46:35 +08:00
veh.OwnerID = Main.LocalPlayerID;
SyncEvents.TriggerChangeOwner(veh.ID, Main.LocalPlayerID);
}
}
p.Position = ped.ReadPosition();
2022-07-17 18:44:16 +08:00
}
sp.LastSentStopWatch.Restart();
if (full)
{
var w = ped.VehicleWeapon;
2022-09-06 21:46:35 +08:00
p.CurrentWeaponHash = (w != VehicleWeaponHash.Invalid) ? (uint)w : (uint)ped.Weapons.Current.Hash;
p.Flags |= PedDataFlags.IsFullSync;
2022-09-06 21:46:35 +08:00
p.Clothes = ped.GetPedClothes();
p.ModelHash = ped.Model.Hash;
p.WeaponComponents = ped.Weapons.Current.GetWeaponComponents();
p.WeaponTint = (byte)Function.Call<int>(Hash.GET_PED_WEAPON_TINT_INDEX, ped, ped.Weapons.Current.Hash);
2022-07-04 22:50:47 +08:00
Blip b;
if (sp.IsPlayer)
{
2022-09-06 21:46:35 +08:00
p.BlipColor = Scripting.API.Config.BlipColor;
p.BlipSprite = Scripting.API.Config.BlipSprite;
p.BlipScale = Scripting.API.Config.BlipScale;
}
2022-09-06 21:46:35 +08:00
else if ((b = ped.AttachedBlip) != null)
2022-07-04 22:50:47 +08:00
{
2022-09-06 21:46:35 +08:00
p.BlipColor = b.Color;
p.BlipSprite = b.Sprite;
2022-09-06 21:46:35 +08:00
if (p.BlipSprite == BlipSprite.PoliceOfficer || p.BlipSprite == BlipSprite.PoliceOfficer2)
{
2022-09-06 21:46:35 +08:00
p.BlipScale = 0.5f;
}
2022-07-04 22:50:47 +08:00
}
2022-08-13 08:22:14 +08:00
else
{
2022-09-06 21:46:35 +08:00
p.BlipColor = (BlipColor)255;
2022-08-13 08:22:14 +08:00
}
}
SendSync(p, 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
{
2022-09-06 21:46:35 +08:00
if (v.LastSentStopWatch.ElapsedMilliseconds < SyncInterval)
2022-08-04 17:38:28 +08:00
{
return;
}
2022-05-22 15:55:26 +08:00
Vehicle veh = v.MainVehicle;
2022-08-13 08:22:14 +08:00
var packet = SendPackets.VehicelPacket;
2022-09-06 21:46:35 +08:00
packet.ID = v.ID;
packet.OwnerID = v.OwnerID;
2022-08-18 20:44:39 +08:00
packet.Flags = v.GetVehicleFlags();
2022-08-13 08:22:14 +08:00
packet.SteeringAngle = veh.SteeringAngle;
2022-08-16 19:51:56 +08:00
packet.Position = veh.ReadPosition();
2022-09-06 21:46:35 +08:00
packet.Velocity = veh.Velocity;
packet.Quaternion = veh.ReadQuaternion();
packet.RotationVelocity = veh.RotationVelocity;
2022-08-13 08:22:14 +08:00
packet.ThrottlePower = veh.ThrottlePower;
packet.BrakePower = veh.BrakePower;
v.LastSentStopWatch.Restart();
2022-09-06 21:46:35 +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 };
2022-09-06 21:46:35 +08:00
packet.DamageModel = veh.GetVehicleDamageModel();
packet.LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0;
2022-09-06 21:46:35 +08:00
packet.RoofState = (byte)veh.RoofState;
packet.Mods = veh.Mods.GetVehicleMods();
2022-09-06 21:46:35 +08:00
packet.ModelHash = veh.Model.Hash;
packet.EngineHealth = veh.EngineHealth;
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)
{
2022-09-06 21:46:35 +08:00
packet.RadioStation = Util.GetPlayerRadioIndex();
}
2022-09-06 21:46:35 +08:00
if (packet.EngineHealth > v.LastEngineHealth)
{
packet.Flags |= VehicleDataFlags.Repaired;
}
2022-09-06 21:46:35 +08:00
v.LastEngineHealth = packet.EngineHealth;
2022-06-03 13:11:17 +08:00
}
2022-08-11 16:25:38 +08:00
SendSync(packet, ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
public static void SendProjectile(SyncedProjectile sp)
{
2022-08-14 17:08:43 +08:00
sp.ExtractData(ref SendPackets.ProjectilePacket);
if (sp.MainProjectile.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
SendSync(SendPackets.ProjectilePacket, ConnectionChannel.ProjectileSync);
}
2022-05-23 15:48:02 +08:00
2022-05-22 15:55:26 +08:00
#region SYNC EVENTS
public static void SendBullet(Vector3 start, Vector3 end, uint weapon, int ownerID)
2022-05-22 15:55:26 +08:00
{
2022-08-11 16:25:38 +08:00
SendSync(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,
2022-09-06 21:46:35 +08:00
WeaponHash = weapon,
2022-05-23 15:48:02 +08:00
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
2022-09-06 21:46:35 +08:00
public static void SendVehicleBullet(uint hash, SyncedPed owner, EntityBone b)
{
SendSync(new Packets.VehicleBulletShot
{
StartPosition = b.Position,
2022-09-06 21:46:35 +08:00
EndPosition = b.Position + b.ForwardVector,
OwnerID = owner.ID,
Bone = (ushort)b.Index,
WeaponHash = hash
});
}
2022-05-22 15:55:26 +08:00
#endregion
public static void SendChatMessage(string message)
2022-05-22 15:55:26 +08:00
{
2022-08-11 14:59:09 +02:00
Peer.SendTo(new Packets.ChatMessage(new Func<string, byte[]>((s) => Security.Encrypt(s.GetBytes())))
2022-09-06 21:46:35 +08:00
{ Username = Main.Settings.Username, Message = message }, ServerConnection, ConnectionChannel.Chat, NetDeliveryMethod.ReliableOrdered);
2022-08-06 12:32:04 +08:00
Peer.FlushSendQueue();
2022-05-22 15:55:26 +08:00
}
2022-08-13 03:39:11 +02:00
public static void SendVoiceMessage(byte[] buffer, int recorded)
2022-08-13 02:19:40 +02:00
{
2022-08-14 23:35:36 +02:00
SendSync(new Packets.Voice() { ID = Main.LocalPlayerID, Buffer = buffer, Recorded = recorded }, ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
2022-08-13 02:19:40 +02:00
}
2022-05-22 15:55:26 +08:00
}
}