RAGECOOP-V/Client/Networking.cs

915 lines
41 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
2021-08-21 16:52:17 +02:00
using System.Collections.Generic;
2021-07-07 13:36:25 +02:00
using CoopClient.Entities;
using Lidgren.Network;
using GTA;
using GTA.Native;
namespace CoopClient
{
2021-12-03 20:30:00 +01:00
/// <summary>
/// Don't use it!
/// </summary>
public class Networking
2021-07-07 13:36:25 +02:00
{
2021-12-03 20:30:00 +01:00
internal NetClient Client;
internal float Latency;
2021-07-07 13:36:25 +02:00
2021-12-03 20:30:00 +01:00
internal bool ShowNetworkInfo = false;
2021-08-18 11:47:59 +02:00
2021-12-03 20:30:00 +01:00
internal int BytesReceived = 0;
internal int BytesSend = 0;
2021-08-18 11:47:59 +02:00
2021-12-03 20:30:00 +01:00
internal void DisConnectFromServer(string address)
2021-07-07 13:36:25 +02:00
{
if (IsOnServer())
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
2021-08-26 17:01:32 +02:00
new PlayerDisconnectPacket() { ID = Main.LocalClientID }.PacketToNetOutGoingMessage(outgoingMessage);
2021-07-07 13:36:25 +02:00
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();
2021-07-07 18:05:08 +02:00
Client.Disconnect("Bye!");
2021-07-07 13:36:25 +02:00
}
else
{
// 6d4ec318f1c43bd62fe13d5a7ab28650 = GTACOOP:R
NetPeerConfiguration config = new NetPeerConfiguration("6d4ec318f1c43bd62fe13d5a7ab28650")
{
AutoFlushSendQueue = false
};
config.EnableMessageType(NetIncomingMessageType.ConnectionLatencyUpdated);
2021-07-07 13:36:25 +02:00
Client = new NetClient(config);
Client.Start();
2021-12-07 08:18:29 +01:00
string[] ip = new string[2];
int idx = address.LastIndexOf(':');
if (idx != -1)
{
ip[0] = address.Substring(0, idx);
ip[1] = address.Substring(idx + 1);
}
if (ip.Length != 2)
{
throw new Exception("Malformed URL");
}
2021-07-07 13:36:25 +02:00
// Send HandshakePacket
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new HandshakePacket()
{
2021-08-16 14:03:05 +02:00
ID = 0,
2021-07-07 13:36:25 +02:00
SocialClubName = Game.Player.Name,
Username = Main.MainSettings.Username,
2021-09-28 16:51:16 +02:00
ModVersion = Main.CurrentVersion,
2021-07-07 13:36:25 +02:00
NpcsAllowed = false
}.PacketToNetOutGoingMessage(outgoingMessage);
Client.Connect(ip[0], short.Parse(ip[1]), outgoingMessage);
}
}
2021-12-03 20:30:00 +01:00
internal bool IsOnServer()
2021-07-07 13:36:25 +02:00
{
return Client?.ConnectionStatus == NetConnectionStatus.Connected;
}
2021-12-03 20:30:00 +01:00
internal void ReceiveMessages()
2021-07-07 13:36:25 +02:00
{
if (Client == null)
{
return;
}
NetIncomingMessage message;
while ((message = Client.ReadMessage()) != null)
{
2021-08-18 11:47:59 +02:00
BytesReceived += message.LengthBytes;
2021-07-07 13:36:25 +02:00
switch (message.MessageType)
{
case NetIncomingMessageType.StatusChanged:
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
string reason = message.ReadString();
switch (status)
{
case NetConnectionStatus.InitiatedConnect:
#if !NON_INTERACTIVE
Main.MainMenu.InitiateConnectionMenuSetting();
#endif
2021-07-07 13:36:25 +02:00
GTA.UI.Notification.Show("~y~Trying to connect...");
break;
case NetConnectionStatus.Connected:
if (message.SenderConnection.RemoteHailMessage.ReadByte() != (byte)PacketTypes.HandshakePacket)
{
Client.Disconnect("Wrong packet!");
}
else
{
Packet remoteHailMessagePacket;
remoteHailMessagePacket = new HandshakePacket();
remoteHailMessagePacket.NetIncomingMessageToPacket(message.SenderConnection.RemoteHailMessage);
HandshakePacket handshakePacket = (HandshakePacket)remoteHailMessagePacket;
2021-08-26 17:01:32 +02:00
Main.LocalClientID = handshakePacket.ID;
2021-07-07 13:36:25 +02:00
Main.NpcsAllowed = handshakePacket.NpcsAllowed;
Main.MainChat.Init();
// Send player connect packet
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new PlayerConnectPacket()
{
2021-08-26 17:01:32 +02:00
ID = Main.LocalClientID,
2021-07-07 13:36:25 +02:00
SocialClubName = string.Empty,
Username = string.Empty
}.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();
#if !NON_INTERACTIVE
Main.MainMenu.ConnectedMenuSetting();
#endif
COOPAPI.Connected();
GTA.UI.Notification.Show("~g~Connected!");
2021-07-07 13:36:25 +02:00
}
break;
case NetConnectionStatus.Disconnected:
// Reset all values
LastPlayerFullSync = 0;
2021-07-07 13:36:25 +02:00
Main.NpcsAllowed = false;
if (Main.MainChat.Focused)
{
Main.MainChat.Focused = false;
}
Main.CleanUp();
2021-11-28 22:57:39 +01:00
#if !NON_INTERACTIVE
Main.MainMenu.DisconnectedMenuSetting();
#endif
COOPAPI.Disconnected(reason);
GTA.UI.Notification.Show("~r~Disconnected: " + reason);
2021-07-07 13:36:25 +02:00
break;
}
break;
case NetIncomingMessageType.Data:
byte packetType = message.ReadByte();
Packet packet;
switch (packetType)
{
case (byte)PacketTypes.PlayerConnectPacket:
packet = new PlayerConnectPacket();
packet.NetIncomingMessageToPacket(message);
PlayerConnect((PlayerConnectPacket)packet);
break;
case (byte)PacketTypes.PlayerDisconnectPacket:
packet = new PlayerDisconnectPacket();
packet.NetIncomingMessageToPacket(message);
PlayerDisconnect((PlayerDisconnectPacket)packet);
break;
case (byte)PacketTypes.FullSyncPlayerPacket:
packet = new FullSyncPlayerPacket();
packet.NetIncomingMessageToPacket(message);
FullSyncPlayer((FullSyncPlayerPacket)packet);
break;
case (byte)PacketTypes.FullSyncPlayerVehPacket:
packet = new FullSyncPlayerVehPacket();
2021-07-07 13:36:25 +02:00
packet.NetIncomingMessageToPacket(message);
FullSyncPlayerVeh((FullSyncPlayerVehPacket)packet);
2021-07-07 13:36:25 +02:00
break;
case (byte)PacketTypes.LightSyncPlayerPacket:
packet = new LightSyncPlayerPacket();
packet.NetIncomingMessageToPacket(message);
LightSyncPlayer((LightSyncPlayerPacket)packet);
break;
case (byte)PacketTypes.LightSyncPlayerVehPacket:
packet = new LightSyncPlayerVehPacket();
packet.NetIncomingMessageToPacket(message);
LightSyncPlayerVeh((LightSyncPlayerVehPacket)packet);
break;
case (byte)PacketTypes.FullSyncNpcPacket:
packet = new FullSyncNpcPacket();
packet.NetIncomingMessageToPacket(message);
FullSyncNpc((FullSyncNpcPacket)packet);
break;
case (byte)PacketTypes.FullSyncNpcVehPacket:
packet = new FullSyncNpcVehPacket();
packet.NetIncomingMessageToPacket(message);
FullSyncNpcVeh((FullSyncNpcVehPacket)packet);
break;
2021-08-26 17:01:32 +02:00
case (byte)PacketTypes.SuperLightSyncPlayerPacket:
packet = new SuperLightSyncPlayerPacket();
packet.NetIncomingMessageToPacket(message);
SuperLightSyncPlayer((SuperLightSyncPlayerPacket)packet);
break;
2021-07-07 13:36:25 +02:00
case (byte)PacketTypes.ChatMessagePacket:
packet = new ChatMessagePacket();
packet.NetIncomingMessageToPacket(message);
ChatMessagePacket chatMessagePacket = (ChatMessagePacket)packet;
if (!COOPAPI.ChatMessageReceived(chatMessagePacket.Username, chatMessagePacket.Message))
{
Main.MainChat.AddMessage(chatMessagePacket.Username, chatMessagePacket.Message);
}
2021-07-07 13:36:25 +02:00
break;
2021-08-20 17:28:13 +02:00
case (byte)PacketTypes.NativeCallPacket:
packet = new NativeCallPacket();
packet.NetIncomingMessageToPacket(message);
DecodeNativeCall((NativeCallPacket)packet);
break;
2021-09-29 14:34:22 +02:00
case (byte)PacketTypes.ModPacket:
packet = new ModPacket();
packet.NetIncomingMessageToPacket(message);
ModPacket modPacket = (ModPacket)packet;
COOPAPI.ModPacketReceived(modPacket.ID, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes);
2021-09-29 14:34:22 +02:00
break;
2021-07-07 13:36:25 +02:00
}
break;
case NetIncomingMessageType.ConnectionLatencyUpdated:
Latency = message.ReadFloat();
break;
2021-07-07 13:36:25 +02:00
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.ErrorMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.VerboseDebugMessage:
2021-11-28 22:57:39 +01:00
#if DEBUG
// TODO?
#endif
2021-07-07 13:36:25 +02:00
break;
default:
break;
}
Client.Recycle(message);
}
}
2021-09-29 14:34:22 +02:00
#region -- GET --
#region -- PLAYER --
2021-07-07 13:36:25 +02:00
private void PlayerConnect(PlayerConnectPacket packet)
{
EntitiesPlayer player = new EntitiesPlayer()
{
SocialClubName = packet.SocialClubName,
2021-08-13 15:20:26 +02:00
Username = packet.Username,
2021-11-19 22:08:15 +01:00
LastUpdateReceived = Util.GetTickCount64()
2021-07-07 13:36:25 +02:00
};
2021-08-26 17:01:32 +02:00
Main.Players.Add(packet.ID, player);
COOPAPI.Connected(packet.ID);
2021-07-07 13:36:25 +02:00
}
private void PlayerDisconnect(PlayerDisconnectPacket packet)
{
2021-08-26 17:01:32 +02:00
if (Main.Players.ContainsKey(packet.ID))
2021-07-07 13:36:25 +02:00
{
2021-08-26 17:01:32 +02:00
EntitiesPlayer player = Main.Players[packet.ID];
2021-07-08 21:57:21 +02:00
if (player.Character != null && player.Character.Exists())
{
player.Character.Kill();
player.Character.Delete();
}
2021-07-09 00:20:09 +02:00
player.PedBlip?.Delete();
2021-07-07 13:36:25 +02:00
COOPAPI.Disconnected(packet.ID);
2021-08-26 17:01:32 +02:00
Main.Players.Remove(packet.ID);
2021-07-07 13:36:25 +02:00
}
}
private void FullSyncPlayer(FullSyncPlayerPacket packet)
{
2021-08-26 17:01:32 +02:00
if (Main.Players.ContainsKey(packet.Extra.ID))
2021-07-07 13:36:25 +02:00
{
2021-08-26 17:01:32 +02:00
EntitiesPlayer player = Main.Players[packet.Extra.ID];
2021-08-13 15:20:26 +02:00
2021-07-07 13:36:25 +02:00
player.ModelHash = packet.ModelHash;
player.Props = packet.Props;
player.Health = packet.Extra.Health;
player.Position = packet.Extra.Position.ToVector();
2021-07-07 13:36:25 +02:00
player.Rotation = packet.Rotation.ToVector();
player.Velocity = packet.Velocity.ToVector();
player.Speed = packet.Speed;
2021-07-08 21:57:21 +02:00
player.CurrentWeaponHash = packet.CurrentWeaponHash;
2021-07-07 13:36:25 +02:00
player.AimCoords = packet.AimCoords.ToVector();
player.LastSyncWasFull = (packet.Flag.Value & (byte)PedDataFlags.LastSyncWasFull) > 0;
player.IsAiming = (packet.Flag.Value & (byte)PedDataFlags.IsAiming) > 0;
player.IsShooting = (packet.Flag.Value & (byte)PedDataFlags.IsShooting) > 0;
player.IsReloading = (packet.Flag.Value & (byte)PedDataFlags.IsReloading) > 0;
player.IsJumping = (packet.Flag.Value & (byte)PedDataFlags.IsJumping) > 0;
player.IsRagdoll = (packet.Flag.Value & (byte)PedDataFlags.IsRagdoll) > 0;
player.IsOnFire = (packet.Flag.Value & (byte)PedDataFlags.IsOnFire) > 0;
2021-11-19 09:24:06 +01:00
player.IsInVehicle = false;
2021-08-26 17:01:32 +02:00
player.Latency = packet.Extra.Latency;
2021-11-19 22:08:15 +01:00
player.LastUpdateReceived = Util.GetTickCount64();
2021-07-07 13:36:25 +02:00
}
}
private void FullSyncPlayerVeh(FullSyncPlayerVehPacket packet)
{
2021-08-26 17:01:32 +02:00
if (Main.Players.ContainsKey(packet.Extra.ID))
{
2021-08-26 17:01:32 +02:00
EntitiesPlayer player = Main.Players[packet.Extra.ID];
2021-08-13 15:20:26 +02:00
player.ModelHash = packet.ModelHash;
player.Props = packet.Props;
player.Health = packet.Extra.Health;
player.Position = packet.Extra.Position.ToVector();
player.VehicleModelHash = packet.VehModelHash;
player.VehicleSeatIndex = packet.VehSeatIndex;
player.VehiclePosition = packet.VehPosition.ToVector();
player.VehicleRotation = packet.VehRotation.ToQuaternion();
2021-08-16 16:51:33 +02:00
player.VehicleEngineHealth = packet.VehEngineHealth;
2021-11-19 08:45:20 +01:00
player.VehRPM = packet.VehRPM;
player.VehicleVelocity = packet.VehVelocity.ToVector();
player.VehicleSpeed = packet.VehSpeed;
2021-07-13 16:30:33 +02:00
player.VehicleSteeringAngle = packet.VehSteeringAngle;
player.AimCoords = packet.VehAimCoords.ToVector();
2021-08-13 15:52:30 +02:00
player.VehicleColors = packet.VehColors;
2021-08-22 13:59:15 +02:00
player.VehicleMods = packet.VehMods;
player.VehDoors = packet.VehDoors;
2021-11-19 08:45:20 +01:00
player.VehTires = packet.VehTires;
player.LastSyncWasFull = (packet.Flag.Value & (byte)VehicleDataFlags.LastSyncWasFull) > 0;
2021-11-19 09:24:06 +01:00
player.IsInVehicle = true;
player.VehIsEngineRunning = (packet.Flag.Value & (byte)VehicleDataFlags.IsEngineRunning) > 0;
player.VehAreLightsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreLightsOn) > 0;
player.VehAreHighBeamsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreHighBeamsOn) > 0;
2021-07-13 12:00:37 +02:00
player.VehIsSireneActive = (packet.Flag.Value & (byte)VehicleDataFlags.IsSirenActive) > 0;
player.VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0;
2021-08-26 17:01:32 +02:00
player.Latency = packet.Extra.Latency;
2021-11-19 22:08:15 +01:00
player.LastUpdateReceived = Util.GetTickCount64();
}
}
private void LightSyncPlayer(LightSyncPlayerPacket packet)
{
2021-08-26 17:01:32 +02:00
if (Main.Players.ContainsKey(packet.Extra.ID))
{
2021-08-26 17:01:32 +02:00
EntitiesPlayer player = Main.Players[packet.Extra.ID];
2021-08-13 15:20:26 +02:00
player.Health = packet.Extra.Health;
player.Position = packet.Extra.Position.ToVector();
player.Rotation = packet.Rotation.ToVector();
player.Velocity = packet.Velocity.ToVector();
player.Speed = packet.Speed;
player.CurrentWeaponHash = packet.CurrentWeaponHash;
player.AimCoords = packet.AimCoords.ToVector();
player.LastSyncWasFull = (packet.Flag.Value & (byte)PedDataFlags.LastSyncWasFull) > 0;
player.IsAiming = (packet.Flag.Value & (byte)PedDataFlags.IsAiming) > 0;
player.IsShooting = (packet.Flag.Value & (byte)PedDataFlags.IsShooting) > 0;
player.IsReloading = (packet.Flag.Value & (byte)PedDataFlags.IsReloading) > 0;
player.IsJumping = (packet.Flag.Value & (byte)PedDataFlags.IsJumping) > 0;
player.IsRagdoll = (packet.Flag.Value & (byte)PedDataFlags.IsRagdoll) > 0;
player.IsOnFire = (packet.Flag.Value & (byte)PedDataFlags.IsOnFire) > 0;
2021-11-19 09:24:06 +01:00
player.IsInVehicle = false;
2021-08-26 17:01:32 +02:00
player.Latency = packet.Extra.Latency;
2021-11-19 22:08:15 +01:00
player.LastUpdateReceived = Util.GetTickCount64();
}
}
private void LightSyncPlayerVeh(LightSyncPlayerVehPacket packet)
{
2021-08-26 17:01:32 +02:00
if (Main.Players.ContainsKey(packet.Extra.ID))
{
2021-08-26 17:01:32 +02:00
EntitiesPlayer player = Main.Players[packet.Extra.ID];
2021-08-13 15:20:26 +02:00
player.Health = packet.Extra.Health;
player.Position = packet.Extra.Position.ToVector();
player.VehicleModelHash = packet.VehModelHash;
player.VehicleSeatIndex = packet.VehSeatIndex;
player.VehiclePosition = packet.VehPosition.ToVector();
player.VehicleRotation = packet.VehRotation.ToQuaternion();
player.VehicleVelocity = packet.VehVelocity.ToVector();
player.VehicleSpeed = packet.VehSpeed;
2021-07-13 16:30:33 +02:00
player.VehicleSteeringAngle = packet.VehSteeringAngle;
player.LastSyncWasFull = (packet.Flag.Value & (byte)VehicleDataFlags.LastSyncWasFull) > 0;
2021-11-19 09:24:06 +01:00
player.IsInVehicle = true;
player.VehIsEngineRunning = (packet.Flag.Value & (byte)VehicleDataFlags.IsEngineRunning) > 0;
player.VehAreLightsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreLightsOn) > 0;
player.VehAreHighBeamsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreHighBeamsOn) > 0;
2021-07-13 12:00:37 +02:00
player.VehIsSireneActive = (packet.Flag.Value & (byte)VehicleDataFlags.IsSirenActive) > 0;
player.VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0;
2021-08-26 17:01:32 +02:00
player.Latency = packet.Extra.Latency;
2021-11-19 22:08:15 +01:00
player.LastUpdateReceived = Util.GetTickCount64();
2021-08-26 17:01:32 +02:00
}
}
private void SuperLightSyncPlayer(SuperLightSyncPlayerPacket packet)
{
if (Main.Players.ContainsKey(packet.Extra.ID))
{
EntitiesPlayer player = Main.Players[packet.Extra.ID];
player.Health = packet.Extra.Health;
player.Position = packet.Extra.Position.ToVector();
player.Latency = packet.Extra.Latency;
2021-11-19 22:08:15 +01:00
player.LastUpdateReceived = Util.GetTickCount64();
}
}
2021-08-20 17:28:13 +02:00
private void DecodeNativeCall(NativeCallPacket packet)
{
List<InputArgument> arguments = new List<InputArgument>();
packet.Args.ForEach(arg =>
{
Type typeOf = arg.GetType();
if (typeOf == typeof(IntArgument))
{
arguments.Add(((IntArgument)arg).Data);
}
else if (typeOf == typeof(BoolArgument))
{
arguments.Add(((BoolArgument)arg).Data);
}
else if (typeOf == typeof(FloatArgument))
{
arguments.Add(((FloatArgument)arg).Data);
}
2021-08-21 16:52:17 +02:00
else if (typeOf == typeof(StringArgument))
{
arguments.Add(((StringArgument)arg).Data);
}
2021-08-20 17:28:13 +02:00
else if (typeOf == typeof(LVector3Argument))
{
arguments.Add(((LVector3Argument)arg).Data.X);
arguments.Add(((LVector3Argument)arg).Data.Y);
arguments.Add(((LVector3Argument)arg).Data.Z);
}
else
{
GTA.UI.Notification.Show("[DecodeNativeCall][" + packet.Hash + "]: Type of argument not found!");
return;
}
});
Function.Call((Hash)packet.Hash, arguments.ToArray());
}
2021-09-29 14:34:22 +02:00
#endregion // -- PLAYER --
2021-09-29 14:34:22 +02:00
#region -- NPC --
2021-07-07 13:36:25 +02:00
private void FullSyncNpc(FullSyncNpcPacket packet)
{
lock (Main.Npcs)
2021-07-07 13:36:25 +02:00
{
if (Main.Npcs.ContainsKey(packet.ID))
2021-07-07 13:36:25 +02:00
{
EntitiesNpc npc = Main.Npcs[packet.ID];
2021-08-13 15:20:26 +02:00
2021-11-19 22:08:15 +01:00
npc.LastUpdateReceived = Util.GetTickCount64();
2021-08-13 15:20:26 +02:00
npc.ModelHash = packet.ModelHash;
npc.Props = packet.Props;
npc.Health = packet.Health;
npc.Position = packet.Position.ToVector();
npc.Rotation = packet.Rotation.ToVector();
npc.Velocity = packet.Velocity.ToVector();
npc.Speed = packet.Speed;
npc.CurrentWeaponHash = packet.CurrentWeaponHash;
npc.AimCoords = packet.AimCoords.ToVector();
npc.LastSyncWasFull = (packet.Flag.Value & (byte)PedDataFlags.LastSyncWasFull) > 0;
npc.IsAiming = (packet.Flag.Value & (byte)PedDataFlags.IsAiming) > 0;
npc.IsShooting = (packet.Flag.Value & (byte)PedDataFlags.IsShooting) > 0;
npc.IsReloading = (packet.Flag.Value & (byte)PedDataFlags.IsReloading) > 0;
npc.IsJumping = (packet.Flag.Value & (byte)PedDataFlags.IsJumping) > 0;
npc.IsRagdoll = (packet.Flag.Value & (byte)PedDataFlags.IsRagdoll) > 0;
npc.IsOnFire = (packet.Flag.Value & (byte)PedDataFlags.IsOnFire) > 0;
2021-11-19 09:24:06 +01:00
npc.IsInVehicle = false;
}
else
{
Main.Npcs.Add(packet.ID, new EntitiesNpc()
{
2021-11-19 22:08:15 +01:00
LastUpdateReceived = Util.GetTickCount64(),
2021-08-13 15:20:26 +02:00
ModelHash = packet.ModelHash,
Props = packet.Props,
Health = packet.Health,
Position = packet.Position.ToVector(),
Rotation = packet.Rotation.ToVector(),
Velocity = packet.Velocity.ToVector(),
Speed = packet.Speed,
CurrentWeaponHash = packet.CurrentWeaponHash,
AimCoords = packet.AimCoords.ToVector(),
LastSyncWasFull = (packet.Flag.Value & (byte)PedDataFlags.LastSyncWasFull) > 0,
IsAiming = (packet.Flag.Value & (byte)PedDataFlags.IsAiming) > 0,
IsShooting = (packet.Flag.Value & (byte)PedDataFlags.IsShooting) > 0,
IsReloading = (packet.Flag.Value & (byte)PedDataFlags.IsReloading) > 0,
IsJumping = (packet.Flag.Value & (byte)PedDataFlags.IsJumping) > 0,
IsRagdoll = (packet.Flag.Value & (byte)PedDataFlags.IsRagdoll) > 0,
IsOnFire = (packet.Flag.Value & (byte)PedDataFlags.IsOnFire) > 0,
2021-11-19 09:24:06 +01:00
IsInVehicle = false
});
}
2021-07-07 13:36:25 +02:00
}
}
private void FullSyncNpcVeh(FullSyncNpcVehPacket packet)
{
lock (Main.Npcs)
{
if (Main.Npcs.ContainsKey(packet.ID))
{
EntitiesNpc npc = Main.Npcs[packet.ID];
2021-08-13 15:20:26 +02:00
2021-11-19 22:08:15 +01:00
npc.LastUpdateReceived = Util.GetTickCount64();
2021-08-13 15:20:26 +02:00
npc.ModelHash = packet.ModelHash;
npc.Props = packet.Props;
npc.Health = packet.Health;
npc.Position = packet.Position.ToVector();
npc.VehicleModelHash = packet.VehModelHash;
npc.VehicleSeatIndex = packet.VehSeatIndex;
npc.VehiclePosition = packet.VehPosition.ToVector();
npc.VehicleRotation = packet.VehRotation.ToQuaternion();
2021-08-16 16:51:33 +02:00
npc.VehicleEngineHealth = packet.VehEngineHealth;
2021-11-19 08:45:20 +01:00
npc.VehRPM = packet.VehRPM;
npc.VehicleVelocity = packet.VehVelocity.ToVector();
npc.VehicleSpeed = packet.VehSpeed;
2021-07-13 16:30:33 +02:00
npc.VehicleSteeringAngle = packet.VehSteeringAngle;
2021-08-13 15:52:30 +02:00
npc.VehicleColors = packet.VehColors;
npc.VehDoors = packet.VehDoors;
2021-11-19 08:45:20 +01:00
npc.VehTires = packet.VehTires;
npc.LastSyncWasFull = (packet.Flag.Value & (byte)VehicleDataFlags.LastSyncWasFull) > 0;
2021-11-19 09:24:06 +01:00
npc.IsInVehicle = true;
npc.VehIsEngineRunning = (packet.Flag.Value & (byte)VehicleDataFlags.IsEngineRunning) > 0;
npc.VehAreLightsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreLightsOn) > 0;
npc.VehAreHighBeamsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreHighBeamsOn) > 0;
2021-07-13 12:00:37 +02:00
npc.VehIsSireneActive = (packet.Flag.Value & (byte)VehicleDataFlags.IsSirenActive) > 0;
npc.VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0;
}
else
{
Main.Npcs.Add(packet.ID, new EntitiesNpc()
{
2021-11-19 22:08:15 +01:00
LastUpdateReceived = Util.GetTickCount64(),
2021-08-13 15:20:26 +02:00
ModelHash = packet.ModelHash,
Props = packet.Props,
Health = packet.Health,
Position = packet.Position.ToVector(),
VehicleModelHash = packet.VehModelHash,
VehicleSeatIndex = packet.VehSeatIndex,
VehiclePosition = packet.VehPosition.ToVector(),
VehicleRotation = packet.VehRotation.ToQuaternion(),
2021-08-16 16:51:33 +02:00
VehicleEngineHealth = packet.VehEngineHealth,
2021-11-19 08:45:20 +01:00
VehRPM = packet.VehRPM,
VehicleVelocity = packet.VehVelocity.ToVector(),
VehicleSpeed = packet.VehSpeed,
2021-07-13 16:30:33 +02:00
VehicleSteeringAngle = packet.VehSteeringAngle,
2021-08-13 15:52:30 +02:00
VehicleColors = packet.VehColors,
VehDoors = packet.VehDoors,
2021-11-19 08:45:20 +01:00
VehTires = packet.VehTires,
LastSyncWasFull = (packet.Flag.Value & (byte)VehicleDataFlags.LastSyncWasFull) > 0,
2021-11-19 09:24:06 +01:00
IsInVehicle = true,
VehIsEngineRunning = (packet.Flag.Value & (byte)VehicleDataFlags.IsEngineRunning) > 0,
VehAreLightsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreLightsOn) > 0,
2021-07-13 12:00:37 +02:00
VehAreHighBeamsOn = (packet.Flag.Value & (byte)VehicleDataFlags.AreHighBeamsOn) > 0,
VehIsSireneActive = (packet.Flag.Value & (byte)VehicleDataFlags.IsSirenActive) > 0,
VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0
});
}
}
}
2021-09-29 14:34:22 +02:00
#endregion // -- NPC --
#endregion
2021-07-07 13:36:25 +02:00
2021-09-29 14:34:22 +02:00
#region -- SEND --
2021-11-19 22:08:15 +01:00
private ulong LastPlayerFullSync = 0;
2021-12-03 20:30:00 +01:00
internal void SendPlayerData()
2021-07-07 13:36:25 +02:00
{
Ped player = Game.Player.Character;
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
2021-08-15 13:58:56 +02:00
NetDeliveryMethod messageType;
2021-07-07 13:36:25 +02:00
2021-11-19 22:08:15 +01:00
if ((Util.GetTickCount64() - LastPlayerFullSync) > 500)
2021-07-07 13:36:25 +02:00
{
2021-08-15 13:58:56 +02:00
messageType = NetDeliveryMethod.UnreliableSequenced;
Vehicle vehicleIsTryingToEnter = null;
if (player.IsInVehicle() || (vehicleIsTryingToEnter = player.VehicleTryingToEnter) != null)
{
Vehicle veh = player.CurrentVehicle ?? vehicleIsTryingToEnter;
2021-11-19 09:10:23 +01:00
LVector3 vehPosition = new LVector3();
LQuaternion vehRotation = new LQuaternion();
float vehEngineHealth = 0f;
float vehRPM = 0f;
LVector3 vehVelocity = new LVector3();
float vehSpeed = 0f;
float vehSteeringAngle = 0f;
Dictionary<int, int> vehMods = null;
VehicleDoors[] vehDoors = null;
int vehTires = 0;
2021-08-13 15:52:30 +02:00
2021-08-22 13:59:15 +02:00
int primaryColor = 0;
2021-11-19 09:10:23 +01:00
int secondaryColor = 0;
2021-08-22 13:59:15 +02:00
if (veh.GetResponsiblePedHandle() == player.Handle)
2021-08-13 15:52:30 +02:00
{
vehPosition = veh.Position.ToLVector();
vehRotation = veh.Quaternion.ToLQuaternion();
vehEngineHealth = veh.EngineHealth;
vehRPM = veh.CurrentRPM;
vehVelocity = veh.Velocity.ToLVector();
vehSpeed = veh.Speed;
vehSteeringAngle = veh.SteeringAngle;
vehMods = veh.Mods.GetVehicleMods();
vehDoors = veh.Doors.GetVehicleDoors();
vehTires = veh.Wheels.GetBrokenTires();
2021-11-19 09:10:23 +01:00
2021-08-22 13:59:15 +02:00
unsafe
{
Function.Call<int>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
2021-08-22 13:59:15 +02:00
}
2021-08-13 15:52:30 +02:00
}
new FullSyncPlayerVehPacket()
{
Extra = new PlayerPacket()
{
2021-08-26 17:01:32 +02:00
ID = Main.LocalClientID,
Health = player.Health,
Position = player.Position.ToLVector()
},
ModelHash = player.Model.Hash,
Props = player.GetPedProps(),
VehModelHash = veh.Model.Hash,
VehSeatIndex = (int)player.SeatIndex,
2021-11-19 09:10:23 +01:00
VehPosition = vehPosition,
VehRotation = vehRotation,
VehEngineHealth = vehEngineHealth,
VehRPM = vehRPM,
VehVelocity = vehVelocity,
VehSpeed = vehSpeed,
VehSteeringAngle = vehSteeringAngle,
VehAimCoords = veh.IsTurretSeat((int)player.SeatIndex) ? Util.GetVehicleAimCoords().ToLVector() : new LVector3(),
2021-11-19 09:10:23 +01:00
VehColors = new int[] { primaryColor, secondaryColor },
VehMods = vehMods,
VehDoors = vehDoors,
VehTires = vehTires,
Flag = veh.GetVehicleFlags(true)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
else
2021-07-07 13:36:25 +02:00
{
new FullSyncPlayerPacket()
{
Extra = new PlayerPacket()
{
2021-08-26 17:01:32 +02:00
ID = Main.LocalClientID,
Health = player.Health,
Position = player.Position.ToLVector()
},
ModelHash = player.Model.Hash,
Props = player.GetPedProps(),
Rotation = player.Rotation.ToLVector(),
Velocity = player.Velocity.ToLVector(),
Speed = player.GetPedSpeed(),
AimCoords = player.GetPedAimCoords(false).ToLVector(),
CurrentWeaponHash = (int)player.Weapons.Current.Hash,
Flag = player.GetPedFlags(true, true)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
2021-11-19 22:08:15 +01:00
LastPlayerFullSync = Util.GetTickCount64();
}
else
{
messageType = NetDeliveryMethod.ReliableSequenced;
if (player.IsInVehicle())
{
Vehicle veh = player.CurrentVehicle;
2021-11-19 09:10:23 +01:00
LVector3 vehPosition = new LVector3();
LQuaternion vehRotation = new LQuaternion();
LVector3 vehVelocity = new LVector3();
float vehSpeed = 0f;
float vehSteeringAngle = 0f;
if (veh.GetResponsiblePedHandle() == player.Handle)
2021-11-19 09:10:23 +01:00
{
vehPosition = veh.Position.ToLVector();
vehRotation = veh.Quaternion.ToLQuaternion();
vehVelocity = veh.Velocity.ToLVector();
vehSpeed = veh.Speed;
vehSteeringAngle = veh.SteeringAngle;
2021-11-19 09:10:23 +01:00
}
2021-08-22 13:59:15 +02:00
new LightSyncPlayerVehPacket()
{
Extra = new PlayerPacket()
{
2021-08-26 17:01:32 +02:00
ID = Main.LocalClientID,
Health = player.Health,
Position = player.Position.ToLVector()
},
VehModelHash = veh.Model.Hash,
VehSeatIndex = (int)player.SeatIndex,
2021-11-19 09:10:23 +01:00
VehPosition = vehPosition,
VehRotation = vehRotation,
VehVelocity = vehVelocity,
VehSpeed = vehSpeed,
VehSteeringAngle = vehSteeringAngle,
Flag = veh.GetVehicleFlags(false)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
else
{
new LightSyncPlayerPacket()
{
Extra = new PlayerPacket()
{
ID = Main.LocalClientID,
Health = player.Health,
Position = player.Position.ToLVector()
},
Rotation = player.Rotation.ToLVector(),
Velocity = player.Velocity.ToLVector(),
Speed = player.GetPedSpeed(),
AimCoords = player.GetPedAimCoords(false).ToLVector(),
CurrentWeaponHash = (int)player.Weapons.Current.Hash,
Flag = player.GetPedFlags(false, true)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
2021-07-07 13:36:25 +02:00
}
2021-08-15 13:58:56 +02:00
Client.SendMessage(outgoingMessage, messageType);
2021-07-07 13:36:25 +02:00
Client.FlushSendQueue();
2021-08-18 11:47:59 +02:00
2021-09-29 14:34:22 +02:00
#if DEBUG
2021-08-18 11:47:59 +02:00
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
2021-09-29 14:34:22 +02:00
#endif
2021-07-07 13:36:25 +02:00
}
2021-12-03 20:30:00 +01:00
internal void SendNpcData(Ped npc)
2021-07-07 13:36:25 +02:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
Vehicle vehicleTryingToEnter = null;
if (npc.IsInVehicle() || (vehicleTryingToEnter = npc.VehicleTryingToEnter) != null)
{
Vehicle veh = npc.CurrentVehicle ?? vehicleTryingToEnter;
2021-11-19 09:10:23 +01:00
LVector3 vehPosition = new LVector3();
LQuaternion vehRotation = new LQuaternion();
float vehEngineHealth = 0f;
float vehRPM = 0f;
LVector3 vehVelocity = new LVector3();
float vehSpeed = 0f;
float vehSteeringAngle = 0f;
Dictionary<int, int> vehMods = null;
VehicleDoors[] vehDoors = null;
int vehTires = 0;
2021-08-13 15:52:30 +02:00
2021-08-22 13:59:15 +02:00
int primaryColor = 0;
2021-11-19 09:10:23 +01:00
int secondaryColor = 0;
2021-08-22 13:59:15 +02:00
if (veh.GetResponsiblePedHandle() == npc.Handle)
2021-08-13 15:52:30 +02:00
{
vehPosition = veh.Position.ToLVector();
vehRotation = veh.Quaternion.ToLQuaternion();
vehEngineHealth = veh.EngineHealth;
vehRPM = veh.CurrentRPM;
vehVelocity = veh.Velocity.ToLVector();
vehSpeed = veh.Speed;
vehSteeringAngle = veh.SteeringAngle;
vehMods = veh.Mods.GetVehicleMods();
vehDoors = veh.Doors.GetVehicleDoors();
vehTires = veh.Wheels.GetBrokenTires();
2021-11-19 09:10:23 +01:00
2021-08-22 13:59:15 +02:00
unsafe
{
Function.Call<int>(Hash.GET_VEHICLE_COLOURS, npc.CurrentVehicle, &primaryColor, &secondaryColor);
}
2021-08-13 15:52:30 +02:00
}
new FullSyncNpcVehPacket()
{
2021-08-26 17:01:32 +02:00
ID = Main.LocalClientID + npc.Handle,
ModelHash = npc.Model.Hash,
Props = npc.GetPedProps(),
Health = npc.Health,
Position = npc.Position.ToLVector(),
VehModelHash = veh.Model.Hash,
VehSeatIndex = (int)npc.SeatIndex,
2021-11-19 09:10:23 +01:00
VehPosition = vehPosition,
VehRotation = vehRotation,
VehEngineHealth = vehEngineHealth,
VehRPM = vehRPM,
VehVelocity = vehVelocity,
VehSpeed = vehSpeed,
VehSteeringAngle = vehSteeringAngle,
VehColors = new int[] { primaryColor, secondaryColor },
VehMods = vehMods,
VehDoors = vehDoors,
VehTires = vehTires,
Flag = veh.GetVehicleFlags(true)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
else
{
new FullSyncNpcPacket()
{
ID = Main.LocalClientID + npc.Handle,
ModelHash = npc.Model.Hash,
Props = npc.GetPedProps(),
Health = npc.Health,
Position = npc.Position.ToLVector(),
Rotation = npc.Rotation.ToLVector(),
Velocity = npc.Velocity.ToLVector(),
Speed = npc.GetPedSpeed(),
AimCoords = npc.GetPedAimCoords(true).ToLVector(),
CurrentWeaponHash = (int)npc.Weapons.Current.Hash,
Flag = npc.GetPedFlags(true)
}.PacketToNetOutGoingMessage(outgoingMessage);
}
2021-07-07 13:36:25 +02:00
2021-08-15 13:58:56 +02:00
Client.SendMessage(outgoingMessage, NetDeliveryMethod.Unreliable);
2021-07-07 13:36:25 +02:00
Client.FlushSendQueue();
2021-08-18 11:47:59 +02:00
2021-09-29 14:34:22 +02:00
#if DEBUG
2021-08-18 11:47:59 +02:00
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
2021-09-29 14:34:22 +02:00
#endif
2021-07-07 13:36:25 +02:00
}
2021-12-03 20:30:00 +01:00
internal void SendChatMessage(string message)
2021-07-07 13:36:25 +02:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new ChatMessagePacket()
{
Username = Main.MainSettings.Username,
Message = message
}.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();
2021-08-18 11:47:59 +02:00
2021-09-29 14:34:22 +02:00
#if DEBUG
2021-08-18 11:47:59 +02:00
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
2021-09-29 14:34:22 +02:00
#endif
}
2021-12-03 20:30:00 +01:00
internal void SendModData(long target, string mod, byte customID, byte[] bytes)
2021-09-29 14:34:22 +02:00
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new ModPacket()
{
ID = Main.LocalClientID,
2021-09-30 23:35:42 +02:00
Target = target,
2021-09-29 14:34:22 +02:00
Mod = mod,
CustomPacketID = customID,
Bytes = bytes
}.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();
#if DEBUG
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
#endif
2021-07-07 13:36:25 +02:00
}
2021-09-29 14:34:22 +02:00
#endregion
2021-07-07 13:36:25 +02:00
}
}