403 lines
17 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 Lidgren.Network;
using RageCoop.Core;
using GTA;
using RageCoop.Client.Menus;
2022-05-22 15:55:26 +08:00
using GTA.Math;
using GTA.Native;
namespace RageCoop.Client
{
internal static partial class Networking
2022-05-22 15:55:26 +08:00
{
public static void ProcessMessage(NetIncomingMessage message)
2022-05-22 15:55:26 +08:00
{
if(message == null) { return; }
2022-05-22 15:55:26 +08:00
switch (message.MessageType)
2022-05-22 15:55:26 +08:00
{
case NetIncomingMessageType.StatusChanged:
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
2022-05-22 15:55:26 +08:00
string reason = message.ReadString();
2022-05-22 15:55:26 +08:00
switch (status)
{
case NetConnectionStatus.InitiatedConnect:
2022-05-22 15:55:26 +08:00
#if !NON_INTERACTIVE
CoopMenu.InitiateConnectionMenuSetting();
2022-05-22 15:55:26 +08:00
#endif
Main.QueueAction(() => { GTA.UI.Notification.Show("~y~Trying to connect..."); return true; });
break;
case NetConnectionStatus.Connected:
if (message.SenderConnection.RemoteHailMessage.ReadByte() != (byte)PacketTypes.Handshake)
{
Client.Disconnect("Wrong packet!");
}
else
{
int len = message.SenderConnection.RemoteHailMessage.ReadInt32();
byte[] data = message.SenderConnection.RemoteHailMessage.ReadBytes(len);
2022-05-22 15:55:26 +08:00
Packets.Handshake handshakePacket = new Packets.Handshake();
handshakePacket.Unpack(data);
2022-05-22 15:55:26 +08:00
#if !NON_INTERACTIVE
2022-05-22 15:55:26 +08:00
#endif
Main.QueueAction(() => {
CoopMenu.ConnectedMenuSetting();
Main.MainChat.Init();
PlayerList.Cleanup();
GTA.UI.Notification.Show("~g~Connected!");
});
2022-05-22 15:55:26 +08:00
Main.Logger.Info(">> Connected <<");
}
break;
case NetConnectionStatus.Disconnected:
DownloadManager.Cleanup();
2022-05-22 15:55:26 +08:00
// Reset all values
Latency = 0;
2022-05-22 15:55:26 +08:00
Main.QueueAction(() => Main.CleanUpWorld());
2022-05-22 15:55:26 +08:00
if (Main.MainChat.Focused)
{
Main.MainChat.Focused = false;
}
2022-05-22 15:55:26 +08:00
Main.QueueAction(() => Main.CleanUp());
2022-05-22 15:55:26 +08:00
#if !NON_INTERACTIVE
CoopMenu.DisconnectedMenuSetting();
2022-05-22 15:55:26 +08:00
#endif
Main.QueueAction(() =>
GTA.UI.Notification.Show("~r~Disconnected: " + reason));
2022-05-22 15:55:26 +08:00
MapLoader.DeleteAll();
Main.Resources.Unload();
2022-05-22 15:55:26 +08:00
Main.Logger.Info($">> Disconnected << reason: {reason}");
break;
}
break;
case NetIncomingMessageType.Data:
if (message.LengthBytes==0) { break; }
2022-05-22 15:55:26 +08:00
var packetType = (PacketTypes)message.ReadByte();
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
switch (packetType)
2022-05-22 15:55:26 +08:00
{
case PacketTypes.CleanUpWorld:
{
Main.QueueAction(() => { Main.CleanUpWorld(); return true; });
}
break;
case PacketTypes.PlayerConnect:
{
2022-05-22 15:55:26 +08:00
Packets.PlayerConnect packet = new Packets.PlayerConnect();
packet.Unpack(data);
Main.QueueAction(() => PlayerConnect(packet));
}
break;
case PacketTypes.PlayerDisconnect:
{
2022-05-22 15:55:26 +08:00
Packets.PlayerDisconnect packet = new Packets.PlayerDisconnect();
packet.Unpack(data);
Main.QueueAction(() => PlayerDisconnect(packet));
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.PlayerInfoUpdate:
{
var packet = new Packets.PlayerInfoUpdate();
packet.Unpack(data);
PlayerList.SetPlayer(packet.PedID, packet.Username, packet.Latency);
2022-05-22 15:55:26 +08:00
break;
}
#region ENTITY SYNC
case PacketTypes.VehicleSync:
{
2022-05-22 15:55:26 +08:00
Packets.VehicleSync packet = new Packets.VehicleSync();
packet.Unpack(data);
VehicleSync(packet);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.PedSync:
{
2022-05-22 15:55:26 +08:00
Packets.PedSync packet = new Packets.PedSync();
packet.Unpack(data);
PedSync(packet);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.VehicleStateSync:
{
2022-05-22 15:55:26 +08:00
Packets.VehicleStateSync packet = new Packets.VehicleStateSync();
packet.Unpack(data);
VehicleStateSync(packet);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.PedStateSync:
{
2022-05-22 15:55:26 +08:00
Packets.PedStateSync packet = new Packets.PedStateSync();
packet.Unpack(data);
PedStateSync(packet);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.ProjectileSync:
{
Packets.ProjectileSync packet = new Packets.ProjectileSync();
packet.Unpack(data);
ProjectileSync(packet);
2022-05-22 15:55:26 +08:00
break;
}
#endregion
case PacketTypes.ChatMessage:
{
2022-05-22 15:55:26 +08:00
Packets.ChatMessage packet = new Packets.ChatMessage();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
Main.QueueAction(() => { Main.MainChat.AddMessage(packet.Username, packet.Message); return true; });
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.NativeCall:
{
2022-05-22 15:55:26 +08:00
Packets.NativeCall packet = new Packets.NativeCall();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
DecodeNativeCall(packet.Hash, packet.Args, false);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.NativeResponse:
{
2022-05-22 15:55:26 +08:00
Packets.NativeResponse packet = new Packets.NativeResponse();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
DecodeNativeResponse(packet);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.FileTransferChunk:
{
Packets.FileTransferChunk packet = new Packets.FileTransferChunk();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
DownloadManager.Write(packet.ID, packet.FileChunk);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.FileTransferRequest:
{
Packets.FileTransferRequest packet = new Packets.FileTransferRequest();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
DownloadManager.AddFile(packet.ID, packet.Name, packet.FileLength);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.FileTransferComplete:
{
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
Main.Logger.Debug($"Finalizing download:{packet.ID}");
DownloadManager.Complete(packet.ID);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.CustomEvent:
{
Packets.CustomEvent packet = new Packets.CustomEvent();
packet.Unpack(data);
}
break;
default:
if (packetType.IsSyncEvent())
{
// Dispatch to main thread
Main.QueueAction(() => { SyncEvents.HandleEvent(packetType, data); return true; });
}
break;
2022-05-22 15:55:26 +08:00
}
}
catch (Exception ex)
{
Main.QueueAction(() => {
GTA.UI.Notification.Show("~r~~h~Packet Error");
return true;
});
Main.Logger.Error($"[{packetType}] {ex.Message}");
Main.Logger.Error(ex);
Client.Disconnect($"Packet Error [{packetType}]");
}
break;
case NetIncomingMessageType.ConnectionLatencyUpdated:
Latency = message.ReadFloat();
break;
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.ErrorMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.VerboseDebugMessage:
break;
default:
break;
2022-05-22 15:55:26 +08:00
}
Client.Recycle(message);
2022-05-22 15:55:26 +08:00
}
private static void PedSync(Packets.PedSync packet)
2022-05-22 15:55:26 +08:00
{
SyncedPed c = EntityPool.GetPedByID(packet.ID);
if (c==null)
2022-05-22 15:55:26 +08:00
{
Main.Logger.Debug($"Creating character for incoming sync:{packet.ID}");
EntityPool.ThreadSafe.Add(c=new SyncedPed(packet.ID));
}
PedDataFlags flags = packet.Flag;
c.ID=packet.ID;
//c.OwnerID=packet.OwnerID;
c.Health = packet.Health;
c.Position = packet.Position;
c.Rotation = packet.Rotation;
c.Velocity = packet.Velocity;
c.Speed = packet.Speed;
c.CurrentWeaponHash = packet.CurrentWeaponHash;
c.IsAiming = flags.HasFlag(PedDataFlags.IsAiming);
c.IsReloading = flags.HasFlag(PedDataFlags.IsReloading);
c.IsJumping = flags.HasFlag(PedDataFlags.IsJumping);
c.IsRagdoll = flags.HasFlag(PedDataFlags.IsRagdoll);
c.IsOnFire = flags.HasFlag(PedDataFlags.IsOnFire);
c.IsInParachuteFreeFall = flags.HasFlag(PedDataFlags.IsInParachuteFreeFall);
c.IsParachuteOpen = flags.HasFlag(PedDataFlags.IsParachuteOpen);
c.IsOnLadder = flags.HasFlag(PedDataFlags.IsOnLadder);
c.IsVaulting = flags.HasFlag(PedDataFlags.IsVaulting);
c.IsInCover = flags.HasFlag(PedDataFlags.IsInCover);
c.Heading=packet.Heading;
c.LastSynced = Main.Ticked;
if (c.IsAiming)
{
c.AimCoords = packet.AimCoords;
}
if (c.IsRagdoll)
{
c.RotationVelocity=packet.RotationVelocity;
2022-05-22 15:55:26 +08:00
}
}
private static void PedStateSync(Packets.PedStateSync packet)
2022-05-22 15:55:26 +08:00
{
SyncedPed c = EntityPool.GetPedByID(packet.ID);
if (c==null) { return; }
c.ID=packet.ID;
c.OwnerID=packet.OwnerID;
c.Clothes=packet.Clothes;
c.WeaponComponents=packet.WeaponComponents;
2022-06-19 11:47:39 +08:00
c.WeaponTint=packet.WeaponTint;
c.ModelHash=packet.ModelHash;
2022-06-03 13:11:17 +08:00
c.LastStateSynced = Main.Ticked;
2022-05-22 15:55:26 +08:00
}
private static void VehicleSync(Packets.VehicleSync packet)
2022-05-22 15:55:26 +08:00
{
SyncedVehicle v = EntityPool.GetVehicleByID(packet.ID);
if (v==null)
2022-05-22 15:55:26 +08:00
{
EntityPool.ThreadSafe.Add(v=new SyncedVehicle(packet.ID));
2022-05-22 15:55:26 +08:00
}
2022-05-29 18:27:07 +08:00
if (v.IsMine) { return; }
v.ID= packet.ID;
v.Position=packet.Position;
2022-06-03 14:40:41 +08:00
v.Quaternion=packet.Quaternion;
v.SteeringAngle=packet.SteeringAngle;
v.ThrottlePower=packet.ThrottlePower;
v.BrakePower=packet.BrakePower;
v.Velocity=packet.Velocity;
v.RotationVelocity=packet.RotationVelocity;
v.DeluxoWingRatio=packet.DeluxoWingRatio;
v.LastSynced=Main.Ticked;
2022-05-22 15:55:26 +08:00
}
private static void VehicleStateSync(Packets.VehicleStateSync packet)
2022-05-22 15:55:26 +08:00
{
SyncedVehicle v = EntityPool.GetVehicleByID(packet.ID);
if (v==null||v.IsMine) { return; }
v.ID= packet.ID;
v.OwnerID= packet.OwnerID;
v.DamageModel=packet.DamageModel;
v.EngineHealth=packet.EngineHealth;
v.OwnerID=packet.OwnerID;
v.Mods=packet.Mods;
v.ModelHash=packet.ModelHash;
v.Colors=packet.Colors;
v.LandingGear=packet.LandingGear;
2022-06-19 13:36:23 +08:00
v.RoofState=(VehicleRoofState)packet.RoofState;
v.EngineRunning = packet.Flag.HasFlag(VehicleDataFlags.IsEngineRunning);
v.LightsOn = packet.Flag.HasFlag(VehicleDataFlags.AreLightsOn);
v.BrakeLightsOn = packet.Flag.HasFlag(VehicleDataFlags.AreBrakeLightsOn);
v.HighBeamsOn = packet.Flag.HasFlag(VehicleDataFlags.AreHighBeamsOn);
v.SireneActive = packet.Flag.HasFlag(VehicleDataFlags.IsSirenActive);
v.IsDead = packet.Flag.HasFlag(VehicleDataFlags.IsDead);
v.HornActive = packet.Flag.HasFlag(VehicleDataFlags.IsHornActive);
v.Transformed = packet.Flag.HasFlag(VehicleDataFlags.IsTransformed);
v.Passengers=new Dictionary<VehicleSeat, SyncedPed>();
v.LockStatus=packet.LockStatus;
2022-06-03 13:11:17 +08:00
v.RadioStation=packet.RadioStation;
v.Flags=packet.Flag;
foreach (KeyValuePair<int, int> pair in packet.Passengers)
{
if (EntityPool.PedExists(pair.Value))
2022-05-22 15:55:26 +08:00
{
v.Passengers.Add((VehicleSeat)pair.Key, EntityPool.GetPedByID(pair.Value));
2022-05-22 15:55:26 +08:00
}
}
2022-06-03 13:11:17 +08:00
v.LastStateSynced= Main.Ticked;
}
private static void ProjectileSync(Packets.ProjectileSync packet)
{
var p = EntityPool.GetProjectileByID(packet.ID);
if (p==null)
{
if (packet.Exploded) { return; }
Main.Logger.Debug($"Creating new projectile: {(WeaponHash)packet.WeaponHash}");
EntityPool.ThreadSafe.Add(p=new SyncedProjectile(packet.ID));
}
p.Position=packet.Position;
p.Rotation=packet.Rotation;
p.Velocity=packet.Velocity;
p.Hash=(WeaponHash)packet.WeaponHash;
p.ShooterID=packet.ShooterID;
p.Exploded=packet.Exploded;
p.LastSynced=Main.Ticked;
2022-05-22 15:55:26 +08:00
}
}
}