Packet refactor

This commit is contained in:
Sardelka 2022-08-06 10:43:24 +08:00
parent 69bd4c837c
commit 742e7ea998
22 changed files with 142 additions and 261 deletions

View File

@ -14,7 +14,7 @@ namespace RageCoop.Client
Networking.RequestHandlers.Add(PacketType.FileTransferRequest, (data) =>
{
var fr = new Packets.FileTransferRequest();
fr.Unpack(data);
fr.Deserialize(data);
if (fr.Name.EndsWith(".res"))
{
_resources.Add(fr.Name);
@ -28,7 +28,7 @@ namespace RageCoop.Client
Networking.RequestHandlers.Add(PacketType.FileTransferComplete, (data) =>
{
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
packet.Unpack(data);
packet.Deserialize(data);
Main.Logger.Debug($"Finalizing download:{packet.ID}");
Complete(packet.ID);

View File

@ -172,7 +172,7 @@ namespace RageCoop.Client
PendingResponses.Add(id, (type, p) =>
{
var result = new T();
result.Unpack(p);
result.Deserialize(p);
callback(result);
});
var msg = Client.CreateMessage();

View File

@ -148,7 +148,7 @@ namespace RageCoop.Client
if (packetType==PacketType.PublicKeyResponse)
{
var packet = new Packets.PublicKeyResponse();
packet.Unpack(data);
packet.Deserialize(data);
Security.SetServerPublicKey(packet.Modulus, packet.Exponent);
_publicKeyReceived.Set();
}
@ -175,7 +175,7 @@ namespace RageCoop.Client
{
Packets.PlayerConnect packet = new Packets.PlayerConnect();
packet.Unpack(data);
packet.Deserialize(data);
Main.QueueAction(() => PlayerConnect(packet));
}
@ -184,7 +184,7 @@ namespace RageCoop.Client
{
Packets.PlayerDisconnect packet = new Packets.PlayerDisconnect();
packet.Unpack(data);
packet.Deserialize(data);
Main.QueueAction(() => PlayerDisconnect(packet));
}
@ -192,7 +192,7 @@ namespace RageCoop.Client
case PacketType.PlayerInfoUpdate:
{
var packet = new Packets.PlayerInfoUpdate();
packet.Unpack(data);
packet.Deserialize(data);
PlayerList.UpdatePlayer(packet);
break;
}
@ -201,7 +201,7 @@ namespace RageCoop.Client
{
Packets.VehicleSync packet = new Packets.VehicleSync();
packet.Unpack(data);
packet.Deserialize(data);
VehicleSync(packet);
}
@ -210,7 +210,7 @@ namespace RageCoop.Client
{
Packets.PedSync packet = new Packets.PedSync();
packet.Unpack(data);
packet.Deserialize(data);
PedSync(packet);
}
@ -218,7 +218,7 @@ namespace RageCoop.Client
case PacketType.ProjectileSync:
{
Packets.ProjectileSync packet = new Packets.ProjectileSync();
packet.Unpack(data);
packet.Deserialize(data);
ProjectileSync(packet);
break;
}
@ -230,7 +230,7 @@ namespace RageCoop.Client
{
return Security.Decrypt(b);
});
packet.Unpack(data);
packet.Deserialize(data);
Main.QueueAction(() => { Main.MainChat.AddMessage(packet.Username, packet.Message); return true; });
@ -240,7 +240,7 @@ namespace RageCoop.Client
case PacketType.CustomEvent:
{
Packets.CustomEvent packet = new Packets.CustomEvent(_resolveHandle);
packet.Unpack(data);
packet.Deserialize(data);
Scripting.API.Events.InvokeCustomEventReceived(packet);
}
break;
@ -249,7 +249,7 @@ namespace RageCoop.Client
Packets.CustomEvent packet = new Packets.CustomEvent(_resolveHandle);
Main.QueueAction(() =>
{
packet.Unpack(data);
packet.Deserialize(data);
Scripting.API.Events.InvokeCustomEventReceived(packet);
});
}
@ -257,7 +257,7 @@ namespace RageCoop.Client
case PacketType.FileTransferChunk:
{
Packets.FileTransferChunk packet = new Packets.FileTransferChunk();
packet.Unpack(data);
packet.Deserialize(data);
DownloadManager.Write(packet.ID, packet.FileChunk);
}
break;

View File

@ -232,14 +232,14 @@ namespace RageCoop.Client
case PacketType.BulletShot:
{
Packets.BulletShot p = new Packets.BulletShot();
p.Unpack(data);
p.Deserialize(data);
HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID);
break;
}
case PacketType.EnteringVehicle:
{
Packets.EnteringVehicle p = new Packets.EnteringVehicle();
p.Unpack(data);
p.Deserialize(data);
HandleEnteringVehicle(EntityPool.GetPedByID(p.PedID), EntityPool.GetVehicleByID(p.VehicleID), (VehicleSeat)p.VehicleSeat);
@ -248,35 +248,35 @@ namespace RageCoop.Client
case PacketType.LeaveVehicle:
{
Packets.LeaveVehicle packet = new Packets.LeaveVehicle();
packet.Unpack(data);
packet.Deserialize(data);
HandleLeaveVehicle(packet);
}
break;
case PacketType.OwnerChanged:
{
Packets.OwnerChanged packet = new Packets.OwnerChanged();
packet.Unpack(data);
packet.Deserialize(data);
HandleOwnerChanged(packet);
}
break;
case PacketType.PedKilled:
{
var packet = new Packets.PedKilled();
packet.Unpack(data);
packet.Deserialize(data);
HandlePedKilled(packet);
}
break;
case PacketType.EnteredVehicle:
{
var packet = new Packets.EnteredVehicle();
packet.Unpack(data);
packet.Deserialize(data);
HandleEnteredVehicle(packet.PedID, packet.VehicleID, (VehicleSeat)packet.VehicleSeat);
break;
}
case PacketType.NozzleTransform:
{
var packet = new Packets.NozzleTransform();
packet.Unpack(data);
packet.Deserialize(data);
HandleNozzleTransform(packet);
break;
}

View File

@ -10,6 +10,7 @@ namespace RageCoop.Core
internal class ChatMessage : Packet
{
public override PacketType Type { get { return PacketType.ChatMessage; } }
private Func<string, byte[]> crypt;
private Func<byte[], byte[]> decrypt;
public ChatMessage(Func<string, byte[]> crypter)
@ -24,10 +25,8 @@ namespace RageCoop.Core
public string Message { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.ChatMessage);
List<byte> byteArray = new List<byte>();
@ -40,14 +39,11 @@ namespace RageCoop.Core
// Write Message
byteArray.AddArray(crypt(Message));
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -9,6 +9,7 @@ namespace RageCoop.Core
internal class CustomEvent : Packet
{
public override PacketType Type { get { return (_queued ? PacketType.CustomEventQueued : PacketType.CustomEvent); } }
public CustomEvent(Func<byte,BitReader,object> onResolve = null,bool queued=false)
{
_resolve= onResolve;
@ -17,13 +18,11 @@ namespace RageCoop.Core
private bool _queued;
private Func<byte, BitReader, object> _resolve { get; set; }
public int Hash { get; set; }
public bool IsStaged { get; set; }=false;
public object[] Args { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
Args= Args ?? new object[] { };
message.Write(_queued ? (byte)PacketType.CustomEventQueued: (byte)PacketType.CustomEvent);
List<byte> result = new List<byte>();
result.AddInt(Hash);
@ -39,12 +38,10 @@ namespace RageCoop.Core
result.Add(tup.Item1);
result.AddRange(tup.Item2);
}
message.Write(result.Count);
message.Write(result.ToArray());
return result.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
BitReader reader = new BitReader(array);

View File

@ -18,16 +18,15 @@ namespace RageCoop.Core
{
internal class FileTransferRequest : Packet
{
public override PacketType Type { get { return PacketType.FileTransferRequest; } }
public int ID { get; set; }
public string Name { get; set; }
public long FileLength { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.FileTransferRequest);
List<byte> byteArray = new List<byte>();
@ -43,14 +42,11 @@ namespace RageCoop.Core
// The length of the file
byteArray.AddRange(BitConverter.GetBytes(FileLength));
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -65,11 +61,11 @@ namespace RageCoop.Core
internal class FileTransferResponse : Packet
{
public override PacketType Type { get { return PacketType.FileTransferResponse; } }
public int ID { get; set; }
public FileResponse Response { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
message.Write((byte)PacketType.FileTransferResponse);
List<byte> byteArray = new List<byte>();
@ -78,13 +74,10 @@ namespace RageCoop.Core
byteArray.Add((byte)Response);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
BitReader reader = new BitReader(array);
@ -95,32 +88,27 @@ namespace RageCoop.Core
internal class FileTransferChunk : Packet
{
public override PacketType Type { get { return PacketType.FileTransferChunk; } }
public int ID { get; set; }
public byte[] FileChunk { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.FileTransferChunk);
List<byte> byteArray = new List<byte>();
// The ID from the download
byteArray.AddInt(ID);
// The chunk of the file
byteArray.AddRange(BitConverter.GetBytes(FileChunk.Length));
byteArray.AddInt(FileChunk.Length);
byteArray.AddRange(FileChunk);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -134,26 +122,21 @@ namespace RageCoop.Core
internal class FileTransferComplete : Packet
{
public override PacketType Type { get { return PacketType.FileTransferComplete; } }
public int ID { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.FileTransferComplete);
List<byte> byteArray = new List<byte>();
// The ID for the download
byteArray.AddInt(ID);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -165,18 +148,14 @@ namespace RageCoop.Core
internal class AllResourcesSent : Packet
{
public override void Pack(NetOutgoingMessage message)
public override PacketType Type { get { return PacketType.AllResourcesSent; } }
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.AllResourcesSent);
message.Write(0);
#endregion
return new byte[0];
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
#endregion
}
}
}

View File

@ -8,17 +8,13 @@ namespace RageCoop.Core
{
internal class PingPong : Packet
{
public override void Pack(NetOutgoingMessage message)
public override PacketType Type { get { return PacketType.PingPong; } }
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PingPong);
message.Write(1);
message.Write((byte)0);
#endregion
return new byte[0];
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
}
}

View File

@ -135,47 +135,26 @@ namespace RageCoop.Core
}
#endregion
interface IPacket
internal interface IPacket
{
PacketType Type { get; }
byte[] Serialize();
void Deserialize(byte[] data);
void Pack(NetOutgoingMessage message);
void Unpack(byte[] array);
}
internal abstract class Packet : IPacket
{
public abstract void Pack(NetOutgoingMessage message);
public abstract void Unpack(byte[] array);
}
internal static class CoopSerializer
{
/// <summary>
/// ?
/// </summary>
public static byte[] Serialize(this object obj)
public abstract PacketType Type { get; }
public abstract byte[] Serialize();
public void Pack(NetOutgoingMessage message)
{
if (obj == null)
{
return null;
}
string jsonString = JsonConvert.SerializeObject(obj);
return System.Text.Encoding.UTF8.GetBytes(jsonString);
}
/// <summary>
/// ?
/// </summary>
public static T Deserialize<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);
var d=Serialize();
message.Write((byte)Type);
message.Write(d.Length);
message.Write(d);
}
public abstract void Deserialize(byte[] array);
}
}

View File

@ -13,6 +13,7 @@ namespace RageCoop.Core
internal class PedSync : Packet
{
public override PacketType Type { get { return PacketType.PedSync; } }
public int ID { get; set; }
public int OwnerID { get; set; }
@ -56,10 +57,8 @@ namespace RageCoop.Core
public float BlipScale { get; set; } = 1;
#endregion
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PedSync);
List<byte> byteArray = new List<byte>();
@ -145,14 +144,10 @@ namespace RageCoop.Core
}
}
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -10,6 +10,7 @@ namespace RageCoop.Core
{
internal class Handshake : Packet
{
public override PacketType Type { get { return PacketType.Handshake; } }
public int PedID { get; set; }
public string Username { get; set; }
@ -31,10 +32,8 @@ namespace RageCoop.Core
/// </summary>
public byte[] PasswordEncrypted { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.Handshake);
List<byte> byteArray = new List<byte>();
@ -61,14 +60,11 @@ namespace RageCoop.Core
// Write PassHash
byteArray.AddArray(PasswordEncrypted);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -94,14 +90,13 @@ namespace RageCoop.Core
public class PlayerConnect : Packet
{
public override PacketType Type { get { return PacketType.PlayerConnect; } }
public int PedID { get; set; }
public string Username { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PlayerConnect);
List<byte> byteArray = new List<byte>();
@ -117,14 +112,10 @@ namespace RageCoop.Core
// Write Username
byteArray.AddRange(usernameBytes);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -141,25 +132,20 @@ namespace RageCoop.Core
public class PlayerDisconnect : Packet
{
public override PacketType Type { get { return PacketType.PlayerDisconnect; } }
public int PedID { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PlayerDisconnect);
List<byte> byteArray = new List<byte>();
byteArray.AddRange(BitConverter.GetBytes(PedID));
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
@ -170,16 +156,16 @@ namespace RageCoop.Core
}
public class PlayerInfoUpdate : Packet
{
public override PacketType Type { get { return PacketType.PlayerInfoUpdate; } }
/// <summary>
/// Ped ID for this Player
/// </summary>
public int PedID { get; set; }
public string Username { get; set; }
public float Latency { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PlayerInfoUpdate);
List<byte> byteArray = new List<byte>();
@ -199,14 +185,10 @@ namespace RageCoop.Core
// Write Latency
byteArray.AddFloat(Latency);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
BitReader reader = new BitReader(array);
@ -223,13 +205,13 @@ namespace RageCoop.Core
public class PublicKeyResponse : Packet
{
public override PacketType Type { get { return PacketType.PublicKeyResponse; } }
public byte[] Modulus;
public byte[] Exponent;
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PublicKeyResponse);
List<byte> byteArray = new List<byte>();
@ -238,13 +220,9 @@ namespace RageCoop.Core
byteArray.AddArray(Exponent);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
var reader=new BitReader(array);
@ -257,13 +235,13 @@ namespace RageCoop.Core
public class PublicKeyRequest : Packet
{
public override void Pack(NetOutgoingMessage message)
public override PacketType Type { get { return PacketType.PublicKeyRequest; } }
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PublicKeyRequest);
#endregion
return new byte[0];
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
}
}

View File

@ -10,7 +10,7 @@ namespace RageCoop.Core
{
internal class ProjectileSync : Packet
{
public override PacketType Type { get { return PacketType.ProjectileSync; } }
public int ID { get; set; }
public int ShooterID { get; set; }
@ -26,10 +26,8 @@ namespace RageCoop.Core
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.ProjectileSync);
List<byte> byteArray = new List<byte>();
@ -53,14 +51,11 @@ namespace RageCoop.Core
if (Exploded) { byteArray.Add(1); }
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -11,6 +11,7 @@ namespace RageCoop.Core
internal class BulletShot : Packet
{
public override PacketType Type { get { return PacketType.BulletShot; } }
public int OwnerID { get; set; }
public uint WeaponHash { get; set; }
@ -18,10 +19,8 @@ namespace RageCoop.Core
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.BulletShot);
List<byte> byteArray = new List<byte>();
@ -38,14 +37,11 @@ namespace RageCoop.Core
byteArray.AddVector3(EndPosition);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -10,16 +10,15 @@ namespace RageCoop.Core
{
internal class EnteredVehicle : Packet
{
public override PacketType Type { get { return PacketType.EnteredVehicle; } }
public int PedID { get; set; }
public int VehicleID { get; set; }
public short VehicleSeat { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.EnteredVehicle);
List<byte> byteArray = new List<byte>();
@ -27,14 +26,11 @@ namespace RageCoop.Core
byteArray.AddInt(VehicleID);
byteArray.AddInt(VehicleSeat);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -10,16 +10,15 @@ namespace RageCoop.Core
{
internal class EnteringVehicle : Packet
{
public override PacketType Type { get { return PacketType.EnteringVehicle; } }
public int PedID { get; set; }
public int VehicleID { get; set; }
public short VehicleSeat { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.EnteringVehicle);
List<byte> byteArray = new List<byte>();
@ -27,14 +26,11 @@ namespace RageCoop.Core
byteArray.AddInt(VehicleID);
byteArray.AddInt(VehicleSeat);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -11,26 +11,22 @@ namespace RageCoop.Core
internal class LeaveVehicle : Packet
{
public override PacketType Type { get { return PacketType.LeaveVehicle; } }
public int ID { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.LeaveVehicle);
List<byte> byteArray = new List<byte>();
byteArray.AddInt(ID);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -10,28 +10,24 @@ namespace RageCoop.Core
{
internal class NozzleTransform : Packet
{
public override PacketType Type { get { return PacketType.NozzleTransform; } }
public int VehicleID { get; set; }
public bool Hover { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.NozzleTransform);
List<byte> byteArray = new List<byte>();
byteArray.AddInt(VehicleID);
if (Hover) { byteArray.Add(1); }
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -11,28 +11,23 @@ namespace RageCoop.Core
internal class OwnerChanged : Packet
{
public override PacketType Type { get { return PacketType.OwnerChanged; } }
public int ID { get; set; }
public int NewOwnerID { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.OwnerChanged);
List<byte> byteArray = new List<byte>();
byteArray.AddInt(ID);
byteArray.AddInt(NewOwnerID);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -11,24 +11,20 @@ namespace RageCoop.Core
internal class PedKilled : Packet
{
public override PacketType Type { get { return PacketType.PedKilled; } }
public int VictimID { get; set; }
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.PedKilled);
List<byte> byteArray = new List<byte>();
byteArray.AddInt(VictimID);
byte[] result = byteArray.ToArray();
return byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -13,6 +13,7 @@ namespace RageCoop.Core
public class VehicleSync : Packet
{
public override PacketType Type { get { return PacketType.VehicleSync; } }
public int ID { get; set; }
public int OwnerID { get; set; }
@ -63,10 +64,8 @@ namespace RageCoop.Core
public string LicensePlate { get; set; }
#endregion
public override void Pack(NetOutgoingMessage message)
public override byte[] Serialize()
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.VehicleSync);
List<byte> byteArray = new List<byte>(100);
@ -166,14 +165,10 @@ namespace RageCoop.Core
byteArray.Add((byte)(Livery+1));
}
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
return byteArray.ToArray();
}
public override void Unpack(byte[] array)
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);

View File

@ -144,7 +144,7 @@ namespace RageCoop.Server.Scripting
Server.RequestHandlers.Add(PacketType.FileTransferRequest, (data,client) =>
{
var p = new Packets.FileTransferRequest();
p.Unpack(data);
p.Deserialize(data);
var id=Server.NewFileID();
if(RegisteredFiles.TryGetValue(p.Name,out var s))
{

View File

@ -301,7 +301,7 @@ namespace RageCoop.Server
byte[] data = message.ReadBytes(len);
Packets.Handshake packet = new();
packet.Unpack(data);
packet.Deserialize(data);
GetHandshake(message.SenderConnection, packet);
}
catch (Exception e)
@ -448,7 +448,7 @@ namespace RageCoop.Server
{
Packets.PedSync packet = new();
packet.Unpack(data);
packet.Deserialize(data);
PedSync(packet, sender);
@ -457,7 +457,7 @@ namespace RageCoop.Server
case PacketType.VehicleSync:
{
Packets.VehicleSync packet = new();
packet.Unpack(data);
packet.Deserialize(data);
VehicleSync(packet, sender);
@ -467,7 +467,7 @@ namespace RageCoop.Server
{
Packets.ProjectileSync packet = new();
packet.Unpack(data);
packet.Deserialize(data);
ProjectileSync(packet, sender);
}
@ -480,7 +480,7 @@ namespace RageCoop.Server
{
return Security.Decrypt(b,sender.EndPoint);
});
packet.Unpack(data);
packet.Deserialize(data);
ChatMessageReceived(packet.Username,packet.Message, sender);
}
@ -488,7 +488,7 @@ namespace RageCoop.Server
case PacketType.CustomEvent:
{
Packets.CustomEvent packet = new Packets.CustomEvent();
packet.Unpack(data);
packet.Deserialize(data);
_worker.QueueJob(() => API.Events.InvokeCustomEventReceived(packet, sender));
}
break;
@ -851,7 +851,7 @@ namespace RageCoop.Server
if (received.WaitOne(timeout))
{
var p = new T();
p.Unpack(response);
p.Deserialize(response);
return p;
}
else