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

272 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;
using Lidgren.Network;
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-05-22 15:55:26 +08:00
{
2022-07-01 14:39:43 +08:00
internal class Handshake : Packet
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-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.Handshake);
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-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-05-22 15:55:26 +08:00
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
// Read player netHandle
PedID = reader.ReadInt();
// Read Username
2022-06-24 10:33:36 +08:00
Username = reader.ReadString(reader.ReadInt());
2022-05-22 15:55:26 +08:00
// Read ModVersion
2022-06-24 10:33:36 +08:00
ModVersion = reader.ReadString(reader.ReadInt());
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
}
}
public class PlayerConnect : Packet
{
public int PedID { get; set; }
public string Username { get; set; }
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.PlayerConnect);
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);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
// Read player netHandle
PedID = reader.ReadInt();
// Read Username
int usernameLength = reader.ReadInt();
Username = reader.ReadString(usernameLength);
#endregion
}
}
public class PlayerDisconnect : Packet
{
public int PedID { get; set; }
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.PlayerDisconnect);
2022-05-22 15:55:26 +08:00
List<byte> byteArray = new List<byte>();
byteArray.AddRange(BitConverter.GetBytes(PedID));
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
PedID = reader.ReadInt();
#endregion
}
}
public class PlayerInfoUpdate : Packet
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-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.PlayerInfoUpdate);
2022-05-22 15:55:26 +08:00
List<byte> byteArray = new List<byte>();
// Write ID
byteArray.AddRange(BitConverter.GetBytes(PedID));
// Get Username bytes
byte[] usernameBytes = Encoding.UTF8.GetBytes(Username);
2022-05-22 15:55:26 +08:00
// Write UsernameLength
byteArray.AddRange(BitConverter.GetBytes(usernameBytes.Length));
// Write Username
byteArray.AddRange(usernameBytes);
// Write Latency
byteArray.AddFloat(Latency);
2022-05-22 15:55:26 +08:00
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
{
BitReader reader = new BitReader(array);
// Read player ID
PedID = reader.ReadInt();
// Read Username
int usernameLength = reader.ReadInt();
Username = reader.ReadString(usernameLength);
Latency=reader.ReadFloat();
2022-05-22 15:55:26 +08:00
}
}
2022-06-24 10:33:36 +08:00
public class PublicKeyResponse : Packet
{
public byte[] Modulus;
public byte[] Exponent;
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.PublicKeyResponse);
2022-06-24 10:33:36 +08:00
List<byte> byteArray = new List<byte>();
byteArray.AddArray(Modulus);
byteArray.AddArray(Exponent);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
{
#region NetIncomingMessageToPacket
var reader=new BitReader(array);
Modulus=reader.ReadByteArray();
Exponent=reader.ReadByteArray();
#endregion
}
}
public class PublicKeyRequest : Packet
{
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.PublicKeyRequest);
2022-06-24 10:33:36 +08:00
#endregion
}
public override void Unpack(byte[] array)
{
}
}
2022-05-22 15:55:26 +08:00
}
}