179 lines
6.0 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Lidgren.Network;
using RageCoop.Core;
using GTA;
using GTA.Native;
using GTA.Math;
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
public static void SendPed(SyncedPed c)
2022-05-22 15:55:26 +08:00
{
Ped p = c.MainPed;
var packet=new Packets.PedSync()
{
ID =c.ID,
Health = p.Health,
Position = p.Position,
Rotation = p.Rotation,
Velocity = p.Velocity,
2022-05-22 15:55:26 +08:00
Speed = p.GetPedSpeed(),
CurrentWeaponHash = (uint)p.Weapons.Current.Hash,
Flag = p.GetPedFlags(),
Heading=p.Heading,
};
if (packet.Flag.HasFlag(PedDataFlags.IsAiming))
{
packet.AimCoords = p.GetAimCoord();
2022-05-22 15:55:26 +08:00
}
if (packet.Flag.HasFlag(PedDataFlags.IsRagdoll))
{
packet.RotationVelocity=p.RotationVelocity;
2022-05-22 15:55:26 +08:00
}
Send(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
public static void SendPedState(SyncedPed c)
2022-05-22 15:55:26 +08:00
{
Ped p = c.MainPed;
2022-05-23 15:48:02 +08:00
var packet=new Packets.PedStateSync()
2022-05-22 15:55:26 +08:00
{
ID = c.ID,
OwnerID=c.OwnerID,
Clothes=p.GetPedClothes(),
ModelHash=p.Model.Hash,
WeaponComponents=p.Weapons.Current.GetWeaponComponents(),
2022-05-23 15:48:02 +08:00
};
Send(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
public static void SendVehicle(SyncedVehicle v)
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,
SteeringAngle = veh.SteeringAngle,
Position = veh.PredictPosition(),
Rotation = veh.Rotation,
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
};
if (v.MainVehicle.Model.Hash==1483171323) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); }
2022-05-23 15:48:02 +08:00
Send(packet,ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
public static void SendVehicleState(SyncedVehicle v)
2022-05-22 15:55:26 +08:00
{
Vehicle veh = v.MainVehicle;
byte primaryColor = 0;
byte secondaryColor = 0;
unsafe
{
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
2022-05-23 15:48:02 +08:00
var packet=new Packets.VehicleStateSync()
2022-05-22 15:55:26 +08:00
{
ID =v.ID,
OwnerID = v.OwnerID,
Flag = veh.GetVehicleFlags(),
Colors=new byte[] { primaryColor, secondaryColor },
DamageModel=veh.GetVehicleDamageModel(),
LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0,
Mods = veh.Mods.GetVehicleMods(),
ModelHash=veh.Model.Hash,
EngineHealth=veh.EngineHealth,
Passengers=veh.GetPassengers(),
LockStatus=veh.LockStatus,
2022-05-23 15:48:02 +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,
Position=p.Position,
Rotation=p.Rotation,
Velocity=p.Velocity,
WeaponHash=(uint)p.WeaponHash,
Exploded=p.IsDead
};
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
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();
new Packets.ChatMessage() { Username = Main.Settings.Username, Message = message }.Pack(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
Client.FlushSendQueue();
#if DEBUG
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
#endif
}
public static void SendDownloadFinish(byte id)
2022-05-22 15:55:26 +08:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new Packets.FileTransferComplete() { ID = id }.Pack(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableUnordered, (byte)ConnectionChannel.File);
Client.FlushSendQueue();
#if DEBUG
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
#endif
}
#endregion
}
}