RAGECOOP-V/Client/Packets.cs

1054 lines
27 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.IO;
using System.Collections.Generic;
using Lidgren.Network;
using ProtoBuf;
using Newtonsoft.Json;
2021-07-07 13:36:25 +02:00
using GTA.Math;
namespace CoopClient
{
#region CLIENT-ONLY
2021-08-14 21:49:23 +02:00
static class VectorExtensions
2021-07-07 13:36:25 +02:00
{
2021-08-13 15:20:26 +02:00
public static Vector3 ToVector(this Quaternion vec)
{
return new Vector3()
{
X = vec.X,
Y = vec.Y,
Z = vec.Z
};
}
public static Quaternion ToQuaternion(this Vector3 vec, float vW = 0.0f)
{
return new Quaternion()
{
X = vec.X,
Y = vec.Y,
Z = vec.Z,
W = vW
};
}
2021-07-07 13:36:25 +02:00
public static LVector3 ToLVector(this Vector3 vec)
{
return new LVector3()
{
X = vec.X,
Y = vec.Y,
2021-08-13 15:20:26 +02:00
Z = vec.Z
2021-07-07 13:36:25 +02:00
};
}
public static LQuaternion ToLQuaternion(this Quaternion vec)
{
return new LQuaternion()
{
X = vec.X,
Y = vec.Y,
Z = vec.Z,
W = vec.W
};
}
2021-07-07 13:36:25 +02:00
}
#endregion
[ProtoContract]
2021-08-14 21:49:23 +02:00
struct LVector3
2021-07-07 13:36:25 +02:00
{
#region CLIENT-ONLY
public Vector3 ToVector()
{
return new Vector3(X, Y, Z);
}
#endregion
public LVector3(float X, float Y, float Z)
{
this.X = X;
this.Y = Y;
this.Z = Z;
}
[ProtoMember(1)]
public float X { get; set; }
[ProtoMember(2)]
public float Y { get; set; }
[ProtoMember(3)]
public float Z { get; set; }
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
struct LQuaternion
{
#region CLIENT-ONLY
public Quaternion ToQuaternion()
{
return new Quaternion(X, Y, Z, W);
}
#endregion
public LQuaternion(float X, float Y, float Z, float W)
{
this.X = X;
this.Y = Y;
this.Z = Z;
this.W = W;
}
[ProtoMember(1)]
public float X { get; set; }
[ProtoMember(2)]
public float Y { get; set; }
[ProtoMember(3)]
public float Z { get; set; }
[ProtoMember(4)]
public float W { get; set; }
}
2021-08-14 21:49:23 +02:00
enum PacketTypes
2021-07-07 13:36:25 +02:00
{
HandshakePacket,
PlayerConnectPacket,
PlayerDisconnectPacket,
FullSyncPlayerPacket,
FullSyncPlayerVehPacket,
2021-07-07 13:36:25 +02:00
LightSyncPlayerPacket,
LightSyncPlayerVehPacket,
2021-08-26 17:01:32 +02:00
SuperLightSyncPlayerPacket,
FullSyncNpcPacket,
FullSyncNpcVehPacket,
2021-08-20 17:28:13 +02:00
ChatMessagePacket,
2021-09-29 14:34:22 +02:00
NativeCallPacket,
2021-12-10 13:38:03 +01:00
NativeResponsePacket,
2021-09-29 14:34:22 +02:00
ModPacket
2021-07-07 13:36:25 +02:00
}
2021-12-13 22:41:07 +01:00
enum ConnectionChannel
{
Default = 0,
Player = 1,
NPC = 2,
Chat = 3,
Native = 4,
Mod = 5
}
2021-07-07 13:36:25 +02:00
[Flags]
2021-08-14 21:49:23 +02:00
enum PedDataFlags
2021-07-07 13:36:25 +02:00
{
IsAiming = 1 << 0,
IsShooting = 1 << 1,
IsReloading = 1 << 2,
IsJumping = 1 << 3,
IsRagdoll = 1 << 4,
IsOnFire = 1 << 5
2021-07-07 13:36:25 +02:00
}
#region ===== VEHICLE DATA =====
2021-07-10 23:41:28 +02:00
[Flags]
2021-08-14 21:49:23 +02:00
enum VehicleDataFlags
2021-07-10 23:41:28 +02:00
{
IsEngineRunning = 1 << 0,
AreLightsOn = 1 << 1,
AreHighBeamsOn = 1 << 2,
IsSirenActive = 1 << 3,
IsDead = 1 << 4,
IsHornActive = 1 << 5,
IsTransformed = 1 << 6
2021-07-10 23:41:28 +02:00
}
/// <summary>
/// ?
/// </summary>
[ProtoContract]
public struct VehicleDoors
{
#region CLIENT-ONLY
/// <summary>
/// ?
/// </summary>
public VehicleDoors(float angleRatio = 0f, bool broken = false, bool open = false, bool fullyOpen = false)
{
AngleRatio = angleRatio;
Broken = broken;
Open = open;
FullyOpen = fullyOpen;
}
#endregion
/// <summary>
/// ?
/// </summary>
[ProtoMember(1)]
public float AngleRatio { get; set; }
/// <summary>
/// ?
/// </summary>
[ProtoMember(2)]
public bool Broken { get; set; }
/// <summary>
/// ?
/// </summary>
[ProtoMember(3)]
public bool Open { get; set; }
/// <summary>
/// ?
/// </summary>
[ProtoMember(4)]
public bool FullyOpen { get; set; }
}
#endregion
interface IPacket
2021-07-07 13:36:25 +02:00
{
void PacketToNetOutGoingMessage(NetOutgoingMessage message);
void NetIncomingMessageToPacket(NetIncomingMessage message);
}
abstract class Packet : IPacket
2021-07-07 13:36:25 +02:00
{
public abstract void PacketToNetOutGoingMessage(NetOutgoingMessage message);
public abstract void NetIncomingMessageToPacket(NetIncomingMessage message);
}
2021-09-29 14:34:22 +02:00
[ProtoContract]
class ModPacket : Packet
{
[ProtoMember(1)]
public long NetHandle { get; set; }
2021-09-29 14:34:22 +02:00
[ProtoMember(2)]
2021-09-30 23:35:42 +02:00
public long Target { get; set; }
2021-09-29 14:34:22 +02:00
[ProtoMember(3)]
2021-09-30 23:35:42 +02:00
public string Mod { get; set; }
2021-09-29 14:34:22 +02:00
[ProtoMember(4)]
2021-09-30 23:35:42 +02:00
public byte CustomPacketID { get; set; }
[ProtoMember(5)]
2021-09-29 14:34:22 +02:00
public byte[] Bytes { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.ModPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-09-29 14:34:22 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
ModPacket data = message.ReadBytes(len).Deserialize<ModPacket>();
2021-09-29 14:34:22 +02:00
NetHandle = data.NetHandle;
2021-09-30 23:35:42 +02:00
Target = data.Target;
2021-09-29 14:34:22 +02:00
Mod = data.Mod;
CustomPacketID = data.CustomPacketID;
2021-09-29 14:34:22 +02:00
Bytes = data.Bytes;
}
}
#region -- PLAYER --
2021-07-07 13:36:25 +02:00
[ProtoContract]
2021-08-14 21:49:23 +02:00
class HandshakePacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public long NetHandle { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public string Username { get; set; }
[ProtoMember(3)]
2021-07-07 13:36:25 +02:00
public string ModVersion { get; set; }
[ProtoMember(4)]
2021-07-07 13:36:25 +02:00
public bool NpcsAllowed { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.HandshakePacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
HandshakePacket data = message.ReadBytes(len).Deserialize<HandshakePacket>();
2021-07-07 13:36:25 +02:00
NetHandle = data.NetHandle;
2021-07-07 13:36:25 +02:00
Username = data.Username;
ModVersion = data.ModVersion;
NpcsAllowed = data.NpcsAllowed;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class PlayerConnectPacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public long NetHandle { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public string SocialClubName { get; set; }
[ProtoMember(3)]
public string Username { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.PlayerConnectPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
PlayerConnectPacket data = message.ReadBytes(len).Deserialize<PlayerConnectPacket>();
2021-07-07 13:36:25 +02:00
NetHandle = data.NetHandle;
2021-07-07 13:36:25 +02:00
SocialClubName = data.SocialClubName;
Username = data.Username;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class PlayerDisconnectPacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public long NetHandle { get; set; }
2021-07-07 13:36:25 +02:00
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.PlayerDisconnectPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
PlayerDisconnectPacket data = message.ReadBytes(len).Deserialize<PlayerDisconnectPacket>();
2021-07-07 13:36:25 +02:00
NetHandle = data.NetHandle;
2021-07-07 13:36:25 +02:00
}
}
[ProtoContract]
struct PlayerPacket
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public long NetHandle { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public int Health { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(3)]
public LVector3 Position { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(4)]
public float Latency { get; set; }
}
2021-07-07 13:36:25 +02:00
[ProtoContract]
class FullSyncPlayerPacket : Packet
{
[ProtoMember(1)]
public PlayerPacket Extra { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public int ModelHash { get; set; }
[ProtoMember(3)]
public Dictionary<byte, short> Clothes { get; set; }
[ProtoMember(4)]
2021-07-07 13:36:25 +02:00
public LVector3 Rotation { get; set; }
[ProtoMember(5)]
2021-07-07 13:36:25 +02:00
public LVector3 Velocity { get; set; }
[ProtoMember(6)]
2021-07-07 13:36:25 +02:00
public byte Speed { get; set; }
[ProtoMember(7)]
2021-07-07 13:36:25 +02:00
public LVector3 AimCoords { get; set; }
[ProtoMember(8)]
public uint CurrentWeaponHash { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(9)]
public Dictionary<uint, bool> WeaponComponents { get; set; }
[ProtoMember(10)]
2021-07-07 13:36:25 +02:00
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.FullSyncPlayerPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
FullSyncPlayerPacket data = message.ReadBytes(len).Deserialize<FullSyncPlayerPacket>();
2021-07-07 13:36:25 +02:00
Extra = data.Extra;
2021-07-07 13:36:25 +02:00
ModelHash = data.ModelHash;
Clothes = data.Clothes;
2021-07-07 13:36:25 +02:00
Rotation = data.Rotation;
Velocity = data.Velocity;
Speed = data.Speed;
AimCoords = data.AimCoords;
CurrentWeaponHash = data.CurrentWeaponHash;
WeaponComponents = data.WeaponComponents;
2021-07-07 13:36:25 +02:00
Flag = data.Flag;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class FullSyncPlayerVehPacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public PlayerPacket Extra { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public int ModelHash { get; set; }
[ProtoMember(3)]
public Dictionary<byte, short> Clothes { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(4)]
public int VehModelHash { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(5)]
public short VehSeatIndex { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(6)]
public LVector3 VehPosition { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(7)]
public LQuaternion VehRotation { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(8)]
2021-08-16 16:51:33 +02:00
public float VehEngineHealth { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(9)]
public float VehRPM { get; set; }
[ProtoMember(10)]
public LVector3 VehVelocity { get; set; }
[ProtoMember(11)]
public float VehSpeed { get; set; }
2021-08-13 15:52:30 +02:00
[ProtoMember(12)]
public float VehSteeringAngle { get; set; }
[ProtoMember(13)]
public LVector3 VehAimCoords { get; set; }
2021-08-16 16:51:33 +02:00
[ProtoMember(14)]
public byte[] VehColors { get; set; }
2021-08-22 13:59:15 +02:00
[ProtoMember(15)]
public Dictionary<int, int> VehMods { get; set; }
[ProtoMember(16)]
public VehicleDoors[] VehDoors { get; set; }
[ProtoMember(17)]
public int VehTires { get; set; }
[ProtoMember(18)]
2021-12-14 14:46:10 +01:00
public byte VehLandingGear { get; set; }
[ProtoMember(19)]
2021-07-07 13:36:25 +02:00
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.FullSyncPlayerVehPacket);
2021-07-07 13:36:25 +02:00
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
FullSyncPlayerVehPacket data = message.ReadBytes(len).Deserialize<FullSyncPlayerVehPacket>();
2021-07-07 13:36:25 +02:00
Extra = data.Extra;
2021-07-07 13:36:25 +02:00
ModelHash = data.ModelHash;
Clothes = data.Clothes;
VehModelHash = data.VehModelHash;
VehSeatIndex = data.VehSeatIndex;
VehPosition = data.VehPosition;
VehRotation = data.VehRotation;
2021-08-16 16:51:33 +02:00
VehEngineHealth = data.VehEngineHealth;
VehRPM = data.VehRPM;
VehVelocity = data.VehVelocity;
VehSpeed = data.VehSpeed;
2021-07-13 16:30:33 +02:00
VehSteeringAngle = data.VehSteeringAngle;
VehAimCoords = data.VehAimCoords;
VehColors = data.VehColors;
2021-08-22 13:59:15 +02:00
VehMods = data.VehMods;
VehDoors = data.VehDoors;
VehTires = data.VehTires;
2021-12-14 14:46:10 +01:00
VehLandingGear = data.VehLandingGear;
2021-07-07 13:36:25 +02:00
Flag = data.Flag;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class LightSyncPlayerPacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public PlayerPacket Extra { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(2)]
public LVector3 Rotation { get; set; }
[ProtoMember(3)]
2021-07-07 13:36:25 +02:00
public LVector3 Velocity { get; set; }
[ProtoMember(4)]
2021-07-07 13:36:25 +02:00
public byte Speed { get; set; }
[ProtoMember(5)]
2021-07-07 13:36:25 +02:00
public LVector3 AimCoords { get; set; }
[ProtoMember(6)]
public uint CurrentWeaponHash { get; set; }
2021-07-07 13:36:25 +02:00
[ProtoMember(7)]
2021-07-07 13:36:25 +02:00
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.LightSyncPlayerPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
LightSyncPlayerPacket data = message.ReadBytes(len).Deserialize<LightSyncPlayerPacket>();
2021-07-07 13:36:25 +02:00
Extra = data.Extra;
2021-07-07 13:36:25 +02:00
Rotation = data.Rotation;
Velocity = data.Velocity;
Speed = data.Speed;
AimCoords = data.AimCoords;
CurrentWeaponHash = data.CurrentWeaponHash;
Flag = data.Flag;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class LightSyncPlayerVehPacket : Packet
{
[ProtoMember(1)]
public PlayerPacket Extra { get; set; }
[ProtoMember(4)]
public int VehModelHash { get; set; }
[ProtoMember(5)]
public short VehSeatIndex { get; set; }
[ProtoMember(6)]
public LVector3 VehPosition { get; set; }
[ProtoMember(7)]
public LQuaternion VehRotation { get; set; }
[ProtoMember(8)]
2021-07-11 00:51:46 +02:00
public LVector3 VehVelocity { get; set; }
2021-07-10 10:52:43 +02:00
[ProtoMember(9)]
2021-07-11 00:51:46 +02:00
public float VehSpeed { get; set; }
2021-07-10 22:01:36 +02:00
[ProtoMember(10)]
2021-07-13 16:30:33 +02:00
public float VehSteeringAngle { get; set; }
2021-07-11 00:51:46 +02:00
[ProtoMember(11)]
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.LightSyncPlayerVehPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
LightSyncPlayerVehPacket data = message.ReadBytes(len).Deserialize<LightSyncPlayerVehPacket>();
Extra = data.Extra;
VehModelHash = data.VehModelHash;
VehSeatIndex = data.VehSeatIndex;
VehPosition = data.VehPosition;
VehRotation = data.VehRotation;
2021-07-11 00:51:46 +02:00
VehVelocity = data.VehVelocity;
2021-07-10 10:52:43 +02:00
VehSpeed = data.VehSpeed;
2021-07-13 16:30:33 +02:00
VehSteeringAngle = data.VehSteeringAngle;
Flag = data.Flag;
}
}
2021-08-26 17:01:32 +02:00
[ProtoContract]
class SuperLightSyncPlayerPacket : Packet
{
[ProtoMember(1)]
public PlayerPacket Extra { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.SuperLightSyncPlayerPacket);
2021-09-29 15:51:28 +02:00
byte[] result = this.Serialize();
2021-08-26 17:01:32 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
SuperLightSyncPlayerPacket data = message.ReadBytes(len).Deserialize<SuperLightSyncPlayerPacket>();
2021-08-26 17:01:32 +02:00
Extra = data.Extra;
}
}
2021-07-07 13:36:25 +02:00
[ProtoContract]
2021-08-14 21:49:23 +02:00
class ChatMessagePacket : Packet
2021-07-07 13:36:25 +02:00
{
[ProtoMember(1)]
public string Username { get; set; }
[ProtoMember(2)]
public string Message { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.ChatMessagePacket);
2021-07-07 18:05:08 +02:00
byte[] result = CoopSerializer.Serialize(this);
2021-07-07 13:36:25 +02:00
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
ChatMessagePacket data = message.ReadBytes(len).Deserialize<ChatMessagePacket>();
2021-07-07 13:36:25 +02:00
Username = data.Username;
Message = data.Message;
}
}
2021-08-20 17:28:13 +02:00
#region ===== NATIVECALL =====
[ProtoContract]
class NativeCallPacket : Packet
{
[ProtoMember(1)]
public ulong Hash { get; set; }
[ProtoMember(2)]
public List<NativeArgument> Args { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.NativeCallPacket);
byte[] result = CoopSerializer.Serialize(this);
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
NativeCallPacket data = message.ReadBytes(len).Deserialize<NativeCallPacket>();
2021-08-20 17:28:13 +02:00
Hash = data.Hash;
Args = data.Args;
}
}
2021-12-10 13:38:03 +01:00
[ProtoContract]
class NativeResponsePacket : Packet
{
[ProtoMember(1)]
public ulong Hash { get; set; }
[ProtoMember(2)]
public List<NativeArgument> Args { get; set; }
[ProtoMember(3)]
public NativeArgument Type { get; set; }
[ProtoMember(4)]
public long NetHandle { get; set; }
2021-12-10 13:38:03 +01:00
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.NativeResponsePacket);
byte[] result = CoopSerializer.Serialize(this);
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
NativeResponsePacket data = message.ReadBytes(len).Deserialize<NativeResponsePacket>();
Hash = data.Hash;
Args = data.Args;
Type = data.Type;
NetHandle = data.NetHandle;
2021-12-10 13:38:03 +01:00
}
}
2021-08-20 17:28:13 +02:00
[ProtoContract]
[ProtoInclude(1, typeof(IntArgument))]
[ProtoInclude(2, typeof(BoolArgument))]
[ProtoInclude(3, typeof(FloatArgument))]
2021-08-21 16:52:17 +02:00
[ProtoInclude(4, typeof(StringArgument))]
[ProtoInclude(5, typeof(LVector3Argument))]
2021-08-20 17:28:13 +02:00
class NativeArgument { }
[ProtoContract]
class IntArgument : NativeArgument
{
[ProtoMember(1)]
public int Data { get; set; }
}
[ProtoContract]
class BoolArgument : NativeArgument
{
[ProtoMember(1)]
public bool Data { get; set; }
}
[ProtoContract]
class FloatArgument : NativeArgument
{
[ProtoMember(1)]
public float Data { get; set; }
}
2021-08-21 16:52:17 +02:00
[ProtoContract]
class StringArgument : NativeArgument
{
[ProtoMember(1)]
public string Data { get; set; }
}
2021-08-20 17:28:13 +02:00
[ProtoContract]
class LVector3Argument : NativeArgument
{
[ProtoMember(1)]
public LVector3 Data { get; set; }
}
#endregion // ===== NATIVECALL =====
#endregion
#region -- NPC --
[ProtoContract]
2021-08-14 21:49:23 +02:00
class FullSyncNpcPacket : Packet
{
[ProtoMember(1)]
public long NetHandle { get; set; }
[ProtoMember(2)]
public int ModelHash { get; set; }
[ProtoMember(3)]
public Dictionary<byte, short> Clothes { get; set; }
[ProtoMember(4)]
public int Health { get; set; }
[ProtoMember(5)]
public LVector3 Position { get; set; }
[ProtoMember(6)]
public LVector3 Rotation { get; set; }
[ProtoMember(7)]
public LVector3 Velocity { get; set; }
[ProtoMember(8)]
public byte Speed { get; set; }
[ProtoMember(9)]
public LVector3 AimCoords { get; set; }
[ProtoMember(10)]
public uint CurrentWeaponHash { get; set; }
[ProtoMember(11)]
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.FullSyncNpcPacket);
byte[] result = CoopSerializer.Serialize(this);
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
FullSyncNpcPacket data = message.ReadBytes(len).Deserialize<FullSyncNpcPacket>();
NetHandle = data.NetHandle;
ModelHash = data.ModelHash;
Clothes = data.Clothes;
Health = data.Health;
Position = data.Position;
Rotation = data.Rotation;
Velocity = data.Velocity;
Speed = data.Speed;
AimCoords = data.AimCoords;
CurrentWeaponHash = data.CurrentWeaponHash;
Flag = data.Flag;
}
}
[ProtoContract]
2021-08-14 21:49:23 +02:00
class FullSyncNpcVehPacket : Packet
{
[ProtoMember(1)]
public long NetHandle { get; set; }
[ProtoMember(2)]
public long VehHandle { get; set; }
[ProtoMember(3)]
public int ModelHash { get; set; }
[ProtoMember(4)]
public Dictionary<byte, short> Clothes { get; set; }
[ProtoMember(5)]
public int Health { get; set; }
[ProtoMember(6)]
public LVector3 Position { get; set; }
[ProtoMember(7)]
public int VehModelHash { get; set; }
[ProtoMember(8)]
public short VehSeatIndex { get; set; }
[ProtoMember(9)]
public LVector3 VehPosition { get; set; }
[ProtoMember(10)]
public LQuaternion VehRotation { get; set; }
[ProtoMember(11)]
public float VehEngineHealth { get; set; }
[ProtoMember(12)]
public float VehRPM { get; set; }
[ProtoMember(13)]
public LVector3 VehVelocity { get; set; }
2021-08-13 15:52:30 +02:00
[ProtoMember(14)]
public float VehSpeed { get; set; }
[ProtoMember(15)]
public float VehSteeringAngle { get; set; }
2021-08-16 16:51:33 +02:00
[ProtoMember(16)]
public byte[] VehColors { get; set; }
2021-08-22 13:59:15 +02:00
[ProtoMember(17)]
public Dictionary<int, int> VehMods { get; set; }
[ProtoMember(18)]
public VehicleDoors[] VehDoors { get; set; }
[ProtoMember(19)]
public int VehTires { get; set; }
2021-12-14 14:46:10 +01:00
[ProtoMember(20)]
public byte VehLandingGear { get; set; }
[ProtoMember(21)]
public byte? Flag { get; set; } = 0;
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
message.Write((byte)PacketTypes.FullSyncNpcVehPacket);
byte[] result = CoopSerializer.Serialize(this);
message.Write(result.Length);
message.Write(result);
}
public override void NetIncomingMessageToPacket(NetIncomingMessage message)
{
int len = message.ReadInt32();
2021-09-29 15:51:28 +02:00
FullSyncNpcVehPacket data = message.ReadBytes(len).Deserialize<FullSyncNpcVehPacket>();
NetHandle = data.NetHandle;
VehHandle = data.VehHandle;
ModelHash = data.ModelHash;
Clothes = data.Clothes;
Health = data.Health;
Position = data.Position;
VehModelHash = data.VehModelHash;
VehSeatIndex = data.VehSeatIndex;
VehPosition = data.VehPosition;
VehRotation = data.VehRotation;
2021-08-16 16:51:33 +02:00
VehEngineHealth = data.VehEngineHealth;
VehRPM = data.VehRPM;
VehVelocity = data.VehVelocity;
VehSpeed = data.VehSpeed;
2021-07-13 16:30:33 +02:00
VehSteeringAngle = data.VehSteeringAngle;
VehColors = data.VehColors;
2021-08-22 13:59:15 +02:00
VehMods = data.VehMods;
VehDoors = data.VehDoors;
VehTires = data.VehTires;
2021-12-14 14:46:10 +01:00
VehLandingGear = data.VehLandingGear;
Flag = data.Flag;
}
}
#endregion
2021-07-07 18:05:08 +02:00
/// <summary>
/// ?
/// </summary>
2021-09-29 15:51:28 +02:00
public static class CoopSerializer
2021-07-07 18:05:08 +02:00
{
/// <summary>
/// ?
/// </summary>
2021-09-29 15:51:28 +02:00
public static byte[] CSerialize(this object obj)
{
if (obj == null)
{
return null;
}
string jsonString = JsonConvert.SerializeObject(obj);
return System.Text.Encoding.UTF8.GetBytes(jsonString);
2021-09-29 15:51:28 +02:00
}
/// <summary>
/// ?
/// </summary>
2021-09-29 15:51:28 +02:00
public static T CDeserialize<T>(this byte[] bytes) where T : class
{
if (bytes == null)
{
return null;
}
var jsonString = System.Text.Encoding.UTF8.GetString(bytes);
return JsonConvert.DeserializeObject<T>(jsonString);
2021-09-29 15:51:28 +02:00
}
internal static T Deserialize<T>(this byte[] data) where T : new()
2021-07-07 18:05:08 +02:00
{
try
{
using (MemoryStream stream = new MemoryStream(data))
{
return Serializer.Deserialize<T>(stream);
}
}
catch
{
throw new Exception(string.Format("The deserialization of the packet {0} failed!", typeof(T).Name));
}
}
2021-09-29 15:51:28 +02:00
internal static byte[] Serialize<T>(this T packet)
2021-07-07 18:05:08 +02:00
{
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize(stream, packet);
return stream.ToArray();
}
}
}
2021-07-07 13:36:25 +02:00
}