RAGECOOP-V/RageCoop.Core/Packets/PlayerPackets.cs

276 lines
8.0 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
using System;
using System.Collections.Generic;
using System.Text;
2022-08-08 17:03:41 +08:00
using GTA.Math;
2022-08-06 12:32:04 +08:00
using System.Net;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-05-22 15:55:26 +08:00
{
2022-08-08 17:03:41 +08:00
internal struct PlayerData
{
public int ID;
public string Username;
}
public class Handshake : Packet
2022-05-22 15:55:26 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.Handshake;
2022-05-22 15:55:26 +08:00
public int PedID { get; set; }
public string Username { get; set; }
public string ModVersion { get; set; }
2022-06-24 10:33:36 +08:00
/// <summary>
/// The asymetrically crypted Aes key
/// </summary>
public byte[] AesKeyCrypted;
/// <summary>
/// The asymetrically crypted Aes IV
/// </summary>
public byte[] AesIVCrypted;
/// <summary>
/// The password hash with client Aes
/// </summary>
2022-07-29 14:51:17 +08:00
public byte[] PasswordEncrypted { get; set; }
2022-06-24 10:33:36 +08:00
2022-08-06 12:32:04 +08:00
public IPEndPoint InternalEndPoint { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-05-22 15:55:26 +08:00
{
List<byte> byteArray = new List<byte>();
// Write Player Ped ID
byteArray.AddRange(BitConverter.GetBytes(PedID));
// Write Username
byte[] usernameBytes = Encoding.UTF8.GetBytes(Username);
byteArray.AddRange(BitConverter.GetBytes(usernameBytes.Length));
byteArray.AddRange(usernameBytes);
// Write ModVersion
byte[] modVersionBytes = Encoding.UTF8.GetBytes(ModVersion);
byteArray.AddRange(BitConverter.GetBytes(modVersionBytes.Length));
byteArray.AddRange(modVersionBytes);
2022-08-06 12:32:04 +08:00
byteArray.AddString(InternalEndPoint.ToString());
2022-06-24 10:33:36 +08:00
// Write AesKeyCrypted
byteArray.AddArray(AesKeyCrypted);
// Write AesIVCrypted
byteArray.AddArray(AesIVCrypted);
// Write PassHash
2022-07-29 14:51:17 +08:00
byteArray.AddArray(PasswordEncrypted);
2022-06-24 10:33:36 +08:00
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-05-22 15:55:26 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-05-22 15:55:26 +08:00
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
// Read player netHandle
2022-08-06 11:40:38 +08:00
PedID = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
// Read Username
2022-08-06 11:40:38 +08:00
Username = reader.ReadString();
2022-05-22 15:55:26 +08:00
// Read ModVersion
2022-08-06 11:40:38 +08:00
ModVersion = reader.ReadString();
2022-06-24 10:33:36 +08:00
2022-08-06 12:32:04 +08:00
InternalEndPoint=CoreUtils.StringToEndPoint(reader.ReadString());
2022-06-24 10:33:36 +08:00
AesKeyCrypted=reader.ReadByteArray();
AesIVCrypted=reader.ReadByteArray();
2022-07-29 14:51:17 +08:00
PasswordEncrypted=reader.ReadByteArray();
2022-05-22 15:55:26 +08:00
#endregion
}
}
2022-08-08 17:03:41 +08:00
public class HandshakeSuccess : Packet
{
public PlayerData[] Players { get; set; }
public override PacketType Type => PacketType.HandshakeSuccess;
public override byte[] Serialize()
{
var data = new List<byte>();
data.AddInt(Players.Length);
foreach(var p in Players)
{
data.AddInt(p.ID);
data.AddString(p.Username);
}
return data.ToArray();
}
public override void Deserialize(byte[] array)
{
var reader = new BitReader(array);
Players=new PlayerData[reader.ReadInt32()];
for(int i = 0; i<Players.Length; i++)
{
Players[i]=new PlayerData()
{
ID=reader.ReadInt32(),
Username=reader.ReadString(),
};
}
}
}
2022-05-22 15:55:26 +08:00
public class PlayerConnect : Packet
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PlayerConnect;
2022-05-22 15:55:26 +08:00
public int PedID { get; set; }
public string Username { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-05-22 15:55:26 +08:00
{
List<byte> byteArray = new List<byte>();
// Write NetHandle
byteArray.AddRange(BitConverter.GetBytes(PedID));
// Get Username bytes
byte[] usernameBytes = Encoding.UTF8.GetBytes(Username);
// Write UsernameLength
byteArray.AddRange(BitConverter.GetBytes(usernameBytes.Length));
// Write Username
byteArray.AddRange(usernameBytes);
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-05-22 15:55:26 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-05-22 15:55:26 +08:00
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
// Read player netHandle
2022-08-06 11:40:38 +08:00
PedID = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
// Read Username
2022-08-06 11:40:38 +08:00
Username = reader.ReadString();
2022-05-22 15:55:26 +08:00
#endregion
}
}
public class PlayerDisconnect : Packet
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PlayerDisconnect;
2022-05-22 15:55:26 +08:00
public int PedID { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-05-22 15:55:26 +08:00
{
List<byte> byteArray = new List<byte>();
byteArray.AddRange(BitConverter.GetBytes(PedID));
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-05-22 15:55:26 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-05-22 15:55:26 +08:00
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
2022-08-06 11:40:38 +08:00
PedID = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
#endregion
}
}
public class PlayerInfoUpdate : Packet
2022-05-22 15:55:26 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PlayerInfoUpdate;
2022-08-06 10:43:24 +08:00
2022-05-22 15:55:26 +08:00
/// <summary>
/// Ped ID for this Player
/// </summary>
public int PedID { get; set; }
public string Username { get; set; }
public float Latency { get; set; }
2022-08-08 17:03:41 +08:00
public Vector3 Position { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-05-22 15:55:26 +08:00
{
List<byte> byteArray = new List<byte>();
// Write ID
byteArray.AddRange(BitConverter.GetBytes(PedID));
// Write Username
2022-08-08 17:03:41 +08:00
byteArray.AddString(Username);
2022-05-22 15:55:26 +08:00
// Write Latency
byteArray.AddFloat(Latency);
2022-08-08 17:03:41 +08:00
byteArray.AddVector3(Position);
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-05-22 15:55:26 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-05-22 15:55:26 +08:00
{
BitReader reader = new BitReader(array);
// Read player ID
2022-08-06 11:40:38 +08:00
PedID = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
// Read Username
2022-08-06 11:40:38 +08:00
Username = reader.ReadString();
2022-08-06 11:40:38 +08:00
Latency=reader.ReadSingle();
2022-08-08 17:03:41 +08:00
Position=reader.ReadVector3();
2022-05-22 15:55:26 +08:00
}
}
2022-06-24 10:33:36 +08:00
public class PublicKeyResponse : Packet
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PublicKeyResponse;
2022-08-06 10:43:24 +08:00
2022-06-24 10:33:36 +08:00
public byte[] Modulus;
public byte[] Exponent;
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-06-24 10:33:36 +08:00
{
List<byte> byteArray = new List<byte>();
byteArray.AddArray(Modulus);
byteArray.AddArray(Exponent);
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-06-24 10:33:36 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-06-24 10:33:36 +08:00
{
#region NetIncomingMessageToPacket
var reader=new BitReader(array);
Modulus=reader.ReadByteArray();
Exponent=reader.ReadByteArray();
#endregion
}
}
public class PublicKeyRequest : Packet
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PublicKeyRequest;
2022-06-24 10:33:36 +08:00
}
2022-05-22 15:55:26 +08:00
}
}