Split packets
This commit is contained in:
@ -195,35 +195,35 @@ namespace CoopClient
|
||||
/// <summary>
|
||||
/// Send any data (bytes) to the server
|
||||
/// </summary>
|
||||
/// <param name="mod">The name of this modification (script)</param>
|
||||
/// <param name="modName">The name of this modification (script)</param>
|
||||
/// <param name="customID">The ID to know what the data is</param>
|
||||
/// <param name="bytes">Your class, structure or whatever in bytes</param>
|
||||
public static void SendDataToServer(string mod, byte customID, byte[] bytes)
|
||||
public static void SendDataToServer(string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
Main.MainNetworking.SendModData(-1, mod, customID, bytes);
|
||||
Main.MainNetworking.SendModData(-1, modName, customID, bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send any data (bytes) to the all player
|
||||
/// </summary>
|
||||
/// <param name="mod">The name of this modification (script)</param>
|
||||
/// <param name="modName">The name of this modification (script)</param>
|
||||
/// <param name="customID">The ID to know what the data is</param>
|
||||
/// <param name="bytes">Your class, structure or whatever in bytes</param>
|
||||
public static void SendDataToAll(string mod, byte customID, byte[] bytes)
|
||||
public static void SendDataToAll(string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
Main.MainNetworking.SendModData(0, mod, customID, bytes);
|
||||
Main.MainNetworking.SendModData(0, modName, customID, bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send any data (bytes) to a player
|
||||
/// </summary>
|
||||
/// <param name="netHandle">The Lidgren Network net handle that receives the data</param>
|
||||
/// <param name="mod">The name of this modification (script)</param>
|
||||
/// <param name="modName">The name of this modification (script)</param>
|
||||
/// <param name="customID">The ID to know what the data is</param>
|
||||
/// <param name="bytes">Your class, structure or whatever in bytes</param>
|
||||
public static void SendDataToPlayer(long netHandle, string mod, byte customID, byte[] bytes)
|
||||
public static void SendDataToPlayer(long netHandle, string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
Main.MainNetworking.SendModData(netHandle, mod, customID, bytes);
|
||||
Main.MainNetworking.SendModData(netHandle, modName, customID, bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -86,7 +86,9 @@
|
||||
<Compile Include="Menus\Sub\Servers.cs" />
|
||||
<Compile Include="Menus\Sub\Settings.cs" />
|
||||
<Compile Include="Networking.cs" />
|
||||
<Compile Include="Packets.cs" />
|
||||
<Compile Include="Packets\NPCPackets.cs" />
|
||||
<Compile Include="Packets\Packets.cs" />
|
||||
<Compile Include="Packets\PlayerPackets.cs" />
|
||||
<Compile Include="PlayerList.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="WorldThread.cs" />
|
||||
|
@ -38,7 +38,6 @@ namespace CoopClient.Entities.NPC
|
||||
}
|
||||
}
|
||||
internal float VehicleSteeringAngle { get; set; }
|
||||
private int LastVehicleAim;
|
||||
internal bool VehIsEngineRunning { get; set; }
|
||||
internal float VehRPM { get; set; }
|
||||
private bool LastTransformed = false;
|
||||
|
@ -60,7 +60,7 @@ namespace CoopClient
|
||||
|
||||
// Send HandshakePacket
|
||||
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
||||
new HandshakePacket()
|
||||
new Packets.Handshake()
|
||||
{
|
||||
NetHandle = 0,
|
||||
Username = Main.MainSettings.Username,
|
||||
@ -106,7 +106,7 @@ namespace CoopClient
|
||||
GTA.UI.Notification.Show("~y~Trying to connect...");
|
||||
break;
|
||||
case NetConnectionStatus.Connected:
|
||||
if (message.SenderConnection.RemoteHailMessage.ReadByte() != (byte)PacketTypes.HandshakePacket)
|
||||
if (message.SenderConnection.RemoteHailMessage.ReadByte() != (byte)PacketTypes.Handshake)
|
||||
{
|
||||
Client.Disconnect("Wrong packet!");
|
||||
}
|
||||
@ -115,7 +115,7 @@ namespace CoopClient
|
||||
int len = message.SenderConnection.RemoteHailMessage.ReadInt32();
|
||||
byte[] data = message.SenderConnection.RemoteHailMessage.ReadBytes(len);
|
||||
|
||||
HandshakePacket handshakePacket = new HandshakePacket();
|
||||
Packets.Handshake handshakePacket = new Packets.Handshake();
|
||||
handshakePacket.NetIncomingMessageToPacket(data);
|
||||
|
||||
Main.LocalNetHandle = handshakePacket.NetHandle;
|
||||
@ -161,7 +161,7 @@ namespace CoopClient
|
||||
|
||||
switch (packetType)
|
||||
{
|
||||
case (byte)PacketTypes.CleanUpWorldPacket:
|
||||
case (byte)PacketTypes.CleanUpWorld:
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -174,14 +174,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.PlayerConnectPacket:
|
||||
case (byte)PacketTypes.PlayerConnect:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
PlayerConnectPacket packet = new PlayerConnectPacket();
|
||||
Packets.PlayerConnect packet = new Packets.PlayerConnect();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
PlayerConnect(packet);
|
||||
@ -193,14 +193,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.PlayerDisconnectPacket:
|
||||
case (byte)PacketTypes.PlayerDisconnect:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
PlayerDisconnectPacket packet = new PlayerDisconnectPacket();
|
||||
Packets.PlayerDisconnect packet = new Packets.PlayerDisconnect();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
PlayerDisconnect(packet);
|
||||
@ -212,14 +212,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncPlayerPacket:
|
||||
case (byte)PacketTypes.FullSyncPlayer:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncPlayerPacket packet = new FullSyncPlayerPacket();
|
||||
Packets.FullSyncPlayer packet = new Packets.FullSyncPlayer();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncPlayer(packet);
|
||||
@ -231,14 +231,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncPlayerVehPacket:
|
||||
case (byte)PacketTypes.FullSyncPlayerVeh:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncPlayerVehPacket packet = new FullSyncPlayerVehPacket();
|
||||
Packets.FullSyncPlayerVeh packet = new Packets.FullSyncPlayerVeh();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncPlayerVeh(packet);
|
||||
@ -250,14 +250,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.LightSyncPlayerPacket:
|
||||
case (byte)PacketTypes.LightSyncPlayer:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
LightSyncPlayerPacket packet = new LightSyncPlayerPacket();
|
||||
Packets.LightSyncPlayer packet = new Packets.LightSyncPlayer();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
LightSyncPlayer(packet);
|
||||
@ -269,14 +269,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.LightSyncPlayerVehPacket:
|
||||
case (byte)PacketTypes.LightSyncPlayerVeh:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
LightSyncPlayerVehPacket packet = new LightSyncPlayerVehPacket();
|
||||
Packets.LightSyncPlayerVeh packet = new Packets.LightSyncPlayerVeh();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
LightSyncPlayerVeh(packet);
|
||||
@ -288,14 +288,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.SuperLightSyncPacket:
|
||||
case (byte)PacketTypes.SuperLightSync:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
SuperLightSyncPacket packet = new SuperLightSyncPacket();
|
||||
Packets.SuperLightSync packet = new Packets.SuperLightSync();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
@ -313,14 +313,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncNpcPacket:
|
||||
case (byte)PacketTypes.FullSyncNpc:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncNpcPacket packet = new FullSyncNpcPacket();
|
||||
Packets.FullSyncNpc packet = new Packets.FullSyncNpc();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncNpc(packet);
|
||||
@ -332,14 +332,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncNpcVehPacket:
|
||||
case (byte)PacketTypes.FullSyncNpcVeh:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncNpcVehPacket packet = new FullSyncNpcVehPacket();
|
||||
Packets.FullSyncNpcVeh packet = new Packets.FullSyncNpcVeh();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncNpcVeh(packet);
|
||||
@ -351,14 +351,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.ChatMessagePacket:
|
||||
case (byte)PacketTypes.ChatMessage:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
ChatMessagePacket packet = new ChatMessagePacket();
|
||||
Packets.ChatMessage packet = new Packets.ChatMessage();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
if (!COOPAPI.ChatMessageReceived(packet.Username, packet.Message))
|
||||
@ -373,14 +373,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.NativeCallPacket:
|
||||
case (byte)PacketTypes.NativeCall:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
NativeCallPacket packet = new NativeCallPacket();
|
||||
Packets.NativeCall packet = new Packets.NativeCall();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
DecodeNativeCall(packet);
|
||||
@ -392,14 +392,14 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.NativeResponsePacket:
|
||||
case (byte)PacketTypes.NativeResponse:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
NativeResponsePacket packet = new NativeResponsePacket();
|
||||
Packets.NativeResponse packet = new Packets.NativeResponse();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
DecodeNativeResponse(packet);
|
||||
@ -411,17 +411,17 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.ModPacket:
|
||||
case (byte)PacketTypes.Mod:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
ModPacket packet = new ModPacket();
|
||||
Packets.Mod packet = new Packets.Mod();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
COOPAPI.ModPacketReceived(packet.NetHandle, packet.Mod, packet.CustomPacketID, packet.Bytes);
|
||||
COOPAPI.ModPacketReceived(packet.NetHandle, packet.Name, packet.CustomPacketID, packet.Bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -453,7 +453,7 @@ namespace CoopClient
|
||||
|
||||
#region -- GET --
|
||||
#region -- PLAYER --
|
||||
private void PlayerConnect(PlayerConnectPacket packet)
|
||||
private void PlayerConnect(Packets.PlayerConnect packet)
|
||||
{
|
||||
EntitiesPlayer player = new EntitiesPlayer() { Username = packet.Username };
|
||||
|
||||
@ -461,7 +461,7 @@ namespace CoopClient
|
||||
COOPAPI.Connected(packet.NetHandle);
|
||||
}
|
||||
|
||||
private void PlayerDisconnect(PlayerDisconnectPacket packet)
|
||||
private void PlayerDisconnect(Packets.PlayerDisconnect packet)
|
||||
{
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
{
|
||||
@ -479,7 +479,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void FullSyncPlayer(FullSyncPlayerPacket packet)
|
||||
private void FullSyncPlayer(Packets.FullSyncPlayer packet)
|
||||
{
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
{
|
||||
@ -512,7 +512,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void FullSyncPlayerVeh(FullSyncPlayerVehPacket packet)
|
||||
private void FullSyncPlayerVeh(Packets.FullSyncPlayerVeh packet)
|
||||
{
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
{
|
||||
@ -551,7 +551,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void LightSyncPlayer(LightSyncPlayerPacket packet)
|
||||
private void LightSyncPlayer(Packets.LightSyncPlayer packet)
|
||||
{
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
{
|
||||
@ -581,7 +581,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void LightSyncPlayerVeh(LightSyncPlayerVehPacket packet)
|
||||
private void LightSyncPlayerVeh(Packets.LightSyncPlayerVeh packet)
|
||||
{
|
||||
if (Main.Players.ContainsKey(packet.NetHandle))
|
||||
{
|
||||
@ -612,7 +612,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void DecodeNativeCall(NativeCallPacket packet)
|
||||
private void DecodeNativeCall(Packets.NativeCall packet)
|
||||
{
|
||||
List<InputArgument> arguments = new List<InputArgument>();
|
||||
|
||||
@ -656,7 +656,7 @@ namespace CoopClient
|
||||
Function.Call((Hash)packet.Hash, arguments.ToArray());
|
||||
}
|
||||
|
||||
private void DecodeNativeResponse(NativeResponsePacket packet)
|
||||
private void DecodeNativeResponse(Packets.NativeResponse packet)
|
||||
{
|
||||
List<InputArgument> arguments = new List<InputArgument>();
|
||||
Type typeOf = null;
|
||||
@ -737,7 +737,7 @@ namespace CoopClient
|
||||
}
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
||||
new NativeResponsePacket()
|
||||
new Packets.NativeResponse()
|
||||
{
|
||||
Hash = 0,
|
||||
Args = new List<object>() { result },
|
||||
@ -749,7 +749,7 @@ namespace CoopClient
|
||||
#endregion // -- PLAYER --
|
||||
|
||||
#region -- NPC --
|
||||
private void FullSyncNpc(FullSyncNpcPacket packet)
|
||||
private void FullSyncNpc(Packets.FullSyncNpc packet)
|
||||
{
|
||||
lock (Main.NPCs)
|
||||
{
|
||||
@ -808,7 +808,7 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void FullSyncNpcVeh(FullSyncNpcVehPacket packet)
|
||||
private void FullSyncNpcVeh(Packets.FullSyncNpcVeh packet)
|
||||
{
|
||||
lock (Main.NPCs)
|
||||
{
|
||||
@ -912,7 +912,7 @@ namespace CoopClient
|
||||
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
|
||||
}
|
||||
|
||||
new FullSyncPlayerVehPacket()
|
||||
new Packets.FullSyncPlayerVeh()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle,
|
||||
PedHandle = player.Handle,
|
||||
@ -939,7 +939,7 @@ namespace CoopClient
|
||||
}
|
||||
else
|
||||
{
|
||||
new FullSyncPlayerPacket()
|
||||
new Packets.FullSyncPlayer()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle,
|
||||
PedHandle = player.Handle,
|
||||
@ -968,7 +968,7 @@ namespace CoopClient
|
||||
{
|
||||
Vehicle veh = player.CurrentVehicle;
|
||||
|
||||
new LightSyncPlayerVehPacket()
|
||||
new Packets.LightSyncPlayerVeh()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle,
|
||||
Health = player.Health,
|
||||
@ -984,7 +984,7 @@ namespace CoopClient
|
||||
}
|
||||
else
|
||||
{
|
||||
new LightSyncPlayerPacket()
|
||||
new Packets.LightSyncPlayer()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle,
|
||||
Health = player.Health,
|
||||
@ -1025,7 +1025,7 @@ namespace CoopClient
|
||||
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, npc.CurrentVehicle, &primaryColor, &secondaryColor);
|
||||
}
|
||||
|
||||
new FullSyncNpcVehPacket()
|
||||
new Packets.FullSyncNpcVeh()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle + npc.Handle,
|
||||
VehHandle = Main.LocalNetHandle + veh.Handle,
|
||||
@ -1050,7 +1050,7 @@ namespace CoopClient
|
||||
}
|
||||
else
|
||||
{
|
||||
new FullSyncNpcPacket()
|
||||
new Packets.FullSyncNpc()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle + npc.Handle,
|
||||
ModelHash = npc.Model.Hash,
|
||||
@ -1081,7 +1081,7 @@ namespace CoopClient
|
||||
{
|
||||
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
||||
|
||||
new ChatMessagePacket() { Username = Main.MainSettings.Username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
new Packets.ChatMessage() { Username = Main.MainSettings.Username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
||||
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
|
||||
Client.FlushSendQueue();
|
||||
@ -1094,14 +1094,14 @@ namespace CoopClient
|
||||
#endif
|
||||
}
|
||||
|
||||
internal void SendModData(long target, string mod, byte customID, byte[] bytes)
|
||||
internal void SendModData(long target, string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
||||
new ModPacket()
|
||||
new Packets.Mod()
|
||||
{
|
||||
NetHandle = Main.LocalNetHandle,
|
||||
Target = target,
|
||||
Mod = mod,
|
||||
Name = modName,
|
||||
CustomPacketID = customID,
|
||||
Bytes = bytes
|
||||
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
2144
Client/Packets.cs
2144
Client/Packets.cs
File diff suppressed because it is too large
Load Diff
449
Client/Packets/NPCPackets.cs
Normal file
449
Client/Packets/NPCPackets.cs
Normal file
@ -0,0 +1,449 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace CoopClient
|
||||
{
|
||||
internal partial class Packets
|
||||
{
|
||||
public class FullSyncNpc : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public int ModelHash { get; set; }
|
||||
|
||||
public Dictionary<byte, short> Clothes { get; set; }
|
||||
|
||||
public int Health { get; set; }
|
||||
|
||||
public LVector3 Position { get; set; }
|
||||
|
||||
public LVector3 Rotation { get; set; }
|
||||
|
||||
public LVector3 Velocity { get; set; }
|
||||
|
||||
public byte Speed { get; set; }
|
||||
|
||||
public LVector3 AimCoords { get; set; }
|
||||
|
||||
public uint CurrentWeaponHash { get; set; }
|
||||
|
||||
public ushort? Flag { get; set; } = 0;
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.FullSyncNpc);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write player + ped handle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write npc flags
|
||||
byteArray.AddRange(BitConverter.GetBytes(Flag.Value));
|
||||
|
||||
// Write npc model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(ModelHash));
|
||||
|
||||
// Write npc position
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Z));
|
||||
|
||||
// Write npc rotation
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Z));
|
||||
|
||||
// Write npc clothes
|
||||
// Write the count of clothes
|
||||
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<byte, short> cloth in Clothes)
|
||||
{
|
||||
byteArray.Add(cloth.Key);
|
||||
byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
|
||||
}
|
||||
|
||||
// Write npc health
|
||||
byteArray.AddRange(BitConverter.GetBytes(Health));
|
||||
|
||||
// Write npc velocity
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.Z));
|
||||
|
||||
// Write npc speed
|
||||
byteArray.Add(Speed);
|
||||
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (byte)PedDataFlags.IsAiming) != 0 || (Flag.Value & (byte)PedDataFlags.IsShooting) != 0)
|
||||
{
|
||||
// Write player aim coords
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Z));
|
||||
}
|
||||
}
|
||||
|
||||
// Write npc weapon hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(CurrentWeaponHash));
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read player + ped handle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Read npc flag
|
||||
Flag = reader.ReadUShort();
|
||||
|
||||
// Read npc model hash
|
||||
ModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc position
|
||||
Position = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc rotation
|
||||
Rotation = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc clothes
|
||||
// Create new Dictionary
|
||||
Clothes = new Dictionary<byte, short>();
|
||||
// Read the count of clothes
|
||||
ushort clothCount = reader.ReadUShort();
|
||||
// For clothCount
|
||||
for (ushort i = 0; i < clothCount; i++)
|
||||
{
|
||||
// Read cloth value
|
||||
Clothes.Add(reader.ReadByte(), reader.ReadShort());
|
||||
}
|
||||
|
||||
// Read npc health
|
||||
Health = reader.ReadByte();
|
||||
|
||||
// Read npc velocity
|
||||
Velocity = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc speed
|
||||
Speed = reader.ReadByte();
|
||||
|
||||
// Read npc flag values
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (byte)PedDataFlags.IsAiming) != 0 || (Flag.Value & (byte)PedDataFlags.IsShooting) != 0)
|
||||
{
|
||||
AimCoords = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Read npc weapon hash
|
||||
CurrentWeaponHash = reader.ReadUInt();
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class FullSyncNpcVeh : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public long VehHandle { get; set; }
|
||||
|
||||
public int ModelHash { get; set; }
|
||||
|
||||
public Dictionary<byte, short> Clothes { get; set; }
|
||||
|
||||
public int Health { get; set; }
|
||||
|
||||
public LVector3 Position { get; set; }
|
||||
|
||||
public int VehModelHash { get; set; }
|
||||
|
||||
public short VehSeatIndex { get; set; }
|
||||
|
||||
public LQuaternion VehRotation { get; set; }
|
||||
|
||||
public float VehEngineHealth { get; set; }
|
||||
|
||||
public float VehRPM { get; set; }
|
||||
|
||||
public LVector3 VehVelocity { get; set; }
|
||||
|
||||
public float VehSpeed { get; set; }
|
||||
|
||||
public float VehSteeringAngle { get; set; }
|
||||
|
||||
public byte[] VehColors { get; set; }
|
||||
|
||||
public Dictionary<int, int> VehMods { get; set; }
|
||||
|
||||
public VehicleDamageModel VehDamageModel { get; set; }
|
||||
|
||||
public byte VehLandingGear { get; set; }
|
||||
|
||||
public ushort? Flag { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.FullSyncNpcVeh);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write player + npc netHandle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write player + vehicle handle
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehHandle));
|
||||
|
||||
// Write vehicles flags
|
||||
byteArray.AddRange(BitConverter.GetBytes(Flag.Value));
|
||||
|
||||
// Write npc health
|
||||
byteArray.AddRange(BitConverter.GetBytes(Health));
|
||||
|
||||
// Write npc model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(ModelHash));
|
||||
|
||||
// Write npc clothes
|
||||
// Write the count of clothes
|
||||
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<byte, short> cloth in Clothes)
|
||||
{
|
||||
byteArray.Add(cloth.Key);
|
||||
byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
|
||||
}
|
||||
|
||||
// Write vehicle model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehModelHash));
|
||||
|
||||
// Write player seat index
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSeatIndex));
|
||||
|
||||
// Write vehicle position
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Z));
|
||||
|
||||
// Write vehicle rotation
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.Z));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.W));
|
||||
|
||||
// Write vehicle engine health
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehEngineHealth));
|
||||
|
||||
// Write vehicle rpm
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRPM));
|
||||
|
||||
// Write vehicle velocity
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.Z));
|
||||
|
||||
// Write vehicle speed
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSpeed));
|
||||
|
||||
// Write vehicle steering angle
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSteeringAngle));
|
||||
|
||||
// Check
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (ushort)VehicleDataFlags.IsPlane) != 0)
|
||||
{
|
||||
// Write the vehicle landing gear
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehLandingGear));
|
||||
}
|
||||
}
|
||||
|
||||
// Write vehicle colors
|
||||
byteArray.Add(VehColors[0]);
|
||||
byteArray.Add(VehColors[1]);
|
||||
|
||||
// Write vehicle mods
|
||||
// Write the count of mods
|
||||
byteArray.AddRange(BitConverter.GetBytes((short)VehMods.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<int, int> mod in VehMods)
|
||||
{
|
||||
// Write the mod value
|
||||
byteArray.AddRange(BitConverter.GetBytes(mod.Key));
|
||||
byteArray.AddRange(BitConverter.GetBytes(mod.Value));
|
||||
}
|
||||
|
||||
if (!VehDamageModel.Equals(default(VehicleDamageModel)))
|
||||
{
|
||||
// Write boolean = true
|
||||
byteArray.Add(0x01);
|
||||
// Write vehicle damage model
|
||||
byteArray.Add(VehDamageModel.BrokenDoors);
|
||||
byteArray.Add(VehDamageModel.BrokenWindows);
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehDamageModel.BurstedTires));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehDamageModel.PuncturedTires));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write boolean = false
|
||||
byteArray.Add(0x00);
|
||||
}
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read player + npc netHandle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Reader player + vehicle handle
|
||||
VehHandle = reader.ReadLong();
|
||||
|
||||
// Read vehicle flags
|
||||
Flag = reader.ReadUShort();
|
||||
|
||||
// Read npc health
|
||||
Health = reader.ReadInt();
|
||||
|
||||
// Read npc model hash
|
||||
ModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc clothes
|
||||
// Create new Dictionary
|
||||
Clothes = new Dictionary<byte, short>();
|
||||
// Read the count of clothes
|
||||
ushort clothCount = reader.ReadUShort();
|
||||
// For clothCount
|
||||
for (int i = 0; i < clothCount; i++)
|
||||
{
|
||||
// Read cloth value
|
||||
Clothes.Add(reader.ReadByte(), reader.ReadShort());
|
||||
}
|
||||
|
||||
// Read vehicle model hash
|
||||
VehModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc seat index
|
||||
VehSeatIndex = reader.ReadShort();
|
||||
|
||||
// Read vehicle position
|
||||
Position = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle rotation
|
||||
VehRotation = new LQuaternion()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat(),
|
||||
W = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle engine health
|
||||
VehEngineHealth = reader.ReadFloat();
|
||||
|
||||
// Read vehicle rpm
|
||||
VehRPM = reader.ReadFloat();
|
||||
|
||||
// Read vehicle velocity
|
||||
VehVelocity = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle speed
|
||||
VehSpeed = reader.ReadFloat();
|
||||
|
||||
// Read vehicle steering angle
|
||||
VehSteeringAngle = reader.ReadFloat();
|
||||
|
||||
// Check
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (int)VehicleDataFlags.IsPlane) != 0)
|
||||
{
|
||||
// Read vehicle landing gear
|
||||
VehLandingGear = (byte)reader.ReadShort();
|
||||
}
|
||||
}
|
||||
|
||||
// Read vehicle colors
|
||||
byte vehColor1 = reader.ReadByte();
|
||||
byte vehColor2 = reader.ReadByte();
|
||||
VehColors = new byte[] { vehColor1, vehColor2 };
|
||||
|
||||
// Read vehicle mods
|
||||
// Create new Dictionary
|
||||
VehMods = new Dictionary<int, int>();
|
||||
// Read count of mods
|
||||
short vehModCount = reader.ReadShort();
|
||||
// Loop
|
||||
for (int i = 0; i < vehModCount; i++)
|
||||
{
|
||||
// Read the mod value
|
||||
VehMods.Add(reader.ReadInt(), reader.ReadInt());
|
||||
}
|
||||
|
||||
if (reader.ReadBool())
|
||||
{
|
||||
// Read vehicle damage model
|
||||
VehDamageModel = new VehicleDamageModel()
|
||||
{
|
||||
BrokenDoors = reader.ReadByte(),
|
||||
BrokenWindows = reader.ReadByte(),
|
||||
BurstedTires = reader.ReadUShort(),
|
||||
PuncturedTires = reader.ReadUShort()
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
576
Client/Packets/Packets.cs
Normal file
576
Client/Packets/Packets.cs
Normal file
@ -0,0 +1,576 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using Lidgren.Network;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using GTA.Math;
|
||||
|
||||
namespace CoopClient
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct LVector3
|
||||
{
|
||||
#region CLIENT-ONLY
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Vector3 ToVector()
|
||||
{
|
||||
return new Vector3(X, Y, Z);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public LVector3(float X, float Y, float Z)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float X { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float Y { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float Z { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct LQuaternion
|
||||
{
|
||||
#region CLIENT-ONLY
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Quaternion ToQuaternion()
|
||||
{
|
||||
return new Quaternion(X, Y, Z, W);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public LQuaternion(float X, float Y, float Z, float W)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
this.W = W;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float X { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float Y { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float Z { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float W { get; set; }
|
||||
}
|
||||
|
||||
enum PacketTypes
|
||||
{
|
||||
Handshake,
|
||||
PlayerConnect,
|
||||
PlayerDisconnect,
|
||||
FullSyncPlayer,
|
||||
FullSyncPlayerVeh,
|
||||
LightSyncPlayer,
|
||||
LightSyncPlayerVeh,
|
||||
SuperLightSync,
|
||||
FullSyncNpc,
|
||||
FullSyncNpcVeh,
|
||||
ChatMessage,
|
||||
NativeCall,
|
||||
NativeResponse,
|
||||
Mod,
|
||||
CleanUpWorld
|
||||
}
|
||||
|
||||
enum ConnectionChannel
|
||||
{
|
||||
Default = 0,
|
||||
PlayerLight = 1,
|
||||
PlayerFull = 2,
|
||||
PlayerSuperLight = 3,
|
||||
NPCFull = 4,
|
||||
Chat = 5,
|
||||
Native = 6,
|
||||
Mod = 7
|
||||
}
|
||||
|
||||
[Flags]
|
||||
enum PedDataFlags
|
||||
{
|
||||
IsAiming = 1 << 0,
|
||||
IsShooting = 1 << 1,
|
||||
IsReloading = 1 << 2,
|
||||
IsJumping = 1 << 3,
|
||||
IsRagdoll = 1 << 4,
|
||||
IsOnFire = 1 << 5,
|
||||
IsInParachuteFreeFall = 1 << 6,
|
||||
IsOnLadder = 1 << 7,
|
||||
IsVaulting = 1 << 8
|
||||
}
|
||||
|
||||
#region ===== VEHICLE DATA =====
|
||||
[Flags]
|
||||
enum VehicleDataFlags
|
||||
{
|
||||
IsEngineRunning = 1 << 0,
|
||||
AreLightsOn = 1 << 1,
|
||||
AreHighBeamsOn = 1 << 2,
|
||||
IsSirenActive = 1 << 3,
|
||||
IsDead = 1 << 4,
|
||||
IsHornActive = 1 << 5,
|
||||
IsTransformed = 1 << 6,
|
||||
RoofOpened = 1 << 7,
|
||||
OnTurretSeat = 1 << 8,
|
||||
IsPlane = 1 << 9
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ?
|
||||
/// </summary>
|
||||
struct VehicleDamageModel
|
||||
{
|
||||
public byte BrokenWindows { get; set; }
|
||||
|
||||
public byte BrokenDoors { get; set; }
|
||||
|
||||
public ushort BurstedTires { get; set; }
|
||||
|
||||
public ushort PuncturedTires { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
interface IPacket
|
||||
{
|
||||
void PacketToNetOutGoingMessage(NetOutgoingMessage message);
|
||||
void NetIncomingMessageToPacket(byte[] array);
|
||||
}
|
||||
|
||||
abstract class Packet : IPacket
|
||||
{
|
||||
public abstract void PacketToNetOutGoingMessage(NetOutgoingMessage message);
|
||||
public abstract void NetIncomingMessageToPacket(byte[] array);
|
||||
}
|
||||
|
||||
internal partial class Packets
|
||||
{
|
||||
public class Mod : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public long Target { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public byte CustomPacketID { get; set; }
|
||||
|
||||
public byte[] Bytes { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.Mod);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write NetHandle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write Target
|
||||
byteArray.AddRange(BitConverter.GetBytes(Target));
|
||||
|
||||
// Write Name
|
||||
byte[] nameBytes = Encoding.UTF8.GetBytes(Name);
|
||||
byteArray.AddRange(BitConverter.GetBytes(nameBytes.Length));
|
||||
byteArray.AddRange(nameBytes);
|
||||
|
||||
// Write CustomPacketID
|
||||
byteArray.Add(CustomPacketID);
|
||||
|
||||
// Write Bytes
|
||||
byteArray.AddRange(BitConverter.GetBytes(Bytes.Length));
|
||||
byteArray.AddRange(Bytes);
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read NetHandle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Read Target
|
||||
Target = reader.ReadLong();
|
||||
|
||||
// Read Name
|
||||
int nameLength = reader.ReadInt();
|
||||
Name = reader.ReadString(nameLength);
|
||||
|
||||
// Read CustomPacketID
|
||||
CustomPacketID = reader.ReadByte();
|
||||
|
||||
// Read Bytes
|
||||
int bytesLength = reader.ReadInt();
|
||||
Bytes = reader.ReadByteArray(bytesLength);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class ChatMessage : Packet
|
||||
{
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.ChatMessage);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
byte[] usernameBytes = Encoding.UTF8.GetBytes(Username);
|
||||
byte[] messageBytes = Encoding.UTF8.GetBytes(Message);
|
||||
|
||||
// Write UsernameLength
|
||||
byteArray.AddRange(BitConverter.GetBytes(usernameBytes.Length));
|
||||
|
||||
// Write Username
|
||||
byteArray.AddRange(usernameBytes);
|
||||
|
||||
// Write MessageLength
|
||||
byteArray.AddRange(BitConverter.GetBytes(messageBytes.Length));
|
||||
|
||||
// Write Message
|
||||
byteArray.AddRange(messageBytes);
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read username
|
||||
int usernameLength = reader.ReadInt();
|
||||
Username = reader.ReadString(usernameLength);
|
||||
|
||||
// Read message
|
||||
int messageLength = reader.ReadInt();
|
||||
Message = reader.ReadString(messageLength);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#region ===== NATIVECALL =====
|
||||
public class NativeCall : Packet
|
||||
{
|
||||
public ulong Hash { get; set; }
|
||||
|
||||
public List<object> Args { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.NativeCall);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write Hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(Hash));
|
||||
|
||||
// Write Args
|
||||
byteArray.AddRange(BitConverter.GetBytes(Args.Count));
|
||||
Args.ForEach(x =>
|
||||
{
|
||||
Type type = x.GetType();
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
byteArray.Add(0x00);
|
||||
byteArray.AddRange(BitConverter.GetBytes((int)x));
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
byteArray.Add(0x01);
|
||||
byteArray.AddRange(BitConverter.GetBytes((bool)x));
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
byteArray.Add(0x02);
|
||||
byteArray.AddRange(BitConverter.GetBytes((float)x));
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
byteArray.Add(0x03);
|
||||
byte[] stringBytes = Encoding.UTF8.GetBytes((string)x);
|
||||
byteArray.AddRange(BitConverter.GetBytes(stringBytes.Length));
|
||||
byteArray.AddRange(stringBytes);
|
||||
}
|
||||
else if (type == typeof(LVector3))
|
||||
{
|
||||
byteArray.Add(0x04);
|
||||
LVector3 vector = (LVector3)x;
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Z));
|
||||
}
|
||||
});
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read Hash
|
||||
Hash = reader.ReadULong();
|
||||
|
||||
// Read Args
|
||||
Args = new List<object>();
|
||||
int argsLength = reader.ReadInt();
|
||||
for (int i = 0; i < argsLength; i++)
|
||||
{
|
||||
byte argType = reader.ReadByte();
|
||||
switch (argType)
|
||||
{
|
||||
case 0x00:
|
||||
Args.Add(reader.ReadInt());
|
||||
break;
|
||||
case 0x01:
|
||||
Args.Add(reader.ReadBool());
|
||||
break;
|
||||
case 0x02:
|
||||
Args.Add(reader.ReadFloat());
|
||||
break;
|
||||
case 0x03:
|
||||
int stringLength = reader.ReadInt();
|
||||
Args.Add(reader.ReadString(stringLength));
|
||||
break;
|
||||
case 0x04:
|
||||
Args.Add(new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class NativeResponse : Packet
|
||||
{
|
||||
public ulong Hash { get; set; }
|
||||
|
||||
public List<object> Args { get; set; }
|
||||
|
||||
public byte? ResultType { get; set; }
|
||||
|
||||
public long ID { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.NativeResponse);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write Hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(Hash));
|
||||
|
||||
Type type;
|
||||
|
||||
// Write Args
|
||||
byteArray.AddRange(BitConverter.GetBytes(Args.Count));
|
||||
Args.ForEach(x =>
|
||||
{
|
||||
type = x.GetType();
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
byteArray.Add(0x00);
|
||||
byteArray.AddRange(BitConverter.GetBytes((int)x));
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
byteArray.Add(0x01);
|
||||
byteArray.AddRange(BitConverter.GetBytes((bool)x));
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
byteArray.Add(0x02);
|
||||
byteArray.AddRange(BitConverter.GetBytes((float)x));
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
byteArray.Add(0x03);
|
||||
byte[] stringBytes = Encoding.UTF8.GetBytes((string)x);
|
||||
byteArray.AddRange(BitConverter.GetBytes(stringBytes.Length));
|
||||
byteArray.AddRange(stringBytes);
|
||||
}
|
||||
else if (type == typeof(LVector3))
|
||||
{
|
||||
byteArray.Add(0x04);
|
||||
LVector3 vector = (LVector3)x;
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Z));
|
||||
}
|
||||
});
|
||||
|
||||
byteArray.AddRange(BitConverter.GetBytes(ID));
|
||||
|
||||
// Write type of result
|
||||
if (ResultType.HasValue)
|
||||
{
|
||||
byteArray.Add(ResultType.Value);
|
||||
}
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read Hash
|
||||
Hash = reader.ReadULong();
|
||||
|
||||
// Read Args
|
||||
Args = new List<object>();
|
||||
int argsLength = reader.ReadInt();
|
||||
for (int i = 0; i < argsLength; i++)
|
||||
{
|
||||
byte argType = reader.ReadByte();
|
||||
switch (argType)
|
||||
{
|
||||
case 0x00:
|
||||
Args.Add(reader.ReadInt());
|
||||
break;
|
||||
case 0x01:
|
||||
Args.Add(reader.ReadBool());
|
||||
break;
|
||||
case 0x02:
|
||||
Args.Add(reader.ReadFloat());
|
||||
break;
|
||||
case 0x03:
|
||||
int stringLength = reader.ReadInt();
|
||||
Args.Add(reader.ReadString(stringLength));
|
||||
break;
|
||||
case 0x04:
|
||||
Args.Add(new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ID = reader.ReadLong();
|
||||
|
||||
// Read type of result
|
||||
if (reader.CanRead(1))
|
||||
{
|
||||
ResultType = reader.ReadByte();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endregion // ===== NATIVECALL =====
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ?
|
||||
/// </summary>
|
||||
public static class CoopSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// ?
|
||||
/// </summary>
|
||||
public static byte[] Serialize(this object obj)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
1067
Client/Packets/PlayerPackets.cs
Normal file
1067
Client/Packets/PlayerPackets.cs
Normal file
File diff suppressed because it is too large
Load Diff
@ -91,7 +91,7 @@ namespace CoopServer
|
||||
return;
|
||||
}
|
||||
|
||||
NativeCallPacket packet = new()
|
||||
Packets.NativeCall packet = new()
|
||||
{
|
||||
Hash = hash,
|
||||
Args = new List<object>(args) ?? new List<object>(),
|
||||
@ -155,7 +155,7 @@ namespace CoopServer
|
||||
}
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
||||
new NativeResponsePacket()
|
||||
new Packets.NativeResponse()
|
||||
{
|
||||
Hash = hash,
|
||||
Args = new List<object>(args) ?? new List<object>(),
|
||||
@ -180,11 +180,11 @@ namespace CoopServer
|
||||
}
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
||||
outgoingMessage.Write((byte)PacketTypes.CleanUpWorldPacket);
|
||||
outgoingMessage.Write((byte)PacketTypes.CleanUpWorld);
|
||||
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default);
|
||||
}
|
||||
|
||||
public void SendModPacket(string mod, byte customID, byte[] bytes)
|
||||
public void SendModPacket(string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -195,11 +195,11 @@ namespace CoopServer
|
||||
}
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
||||
new ModPacket()
|
||||
new Packets.Mod()
|
||||
{
|
||||
NetHandle = 0,
|
||||
Target = 0,
|
||||
Mod = mod,
|
||||
Name = modName,
|
||||
CustomPacketID = customID,
|
||||
Bytes = bytes
|
||||
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
2118
Server/Packets.cs
2118
Server/Packets.cs
File diff suppressed because it is too large
Load Diff
449
Server/Packets/NPCPackets.cs
Normal file
449
Server/Packets/NPCPackets.cs
Normal file
@ -0,0 +1,449 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
internal partial class Packets
|
||||
{
|
||||
public class FullSyncNpc : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public int ModelHash { get; set; }
|
||||
|
||||
public Dictionary<byte, short> Clothes { get; set; }
|
||||
|
||||
public int Health { get; set; }
|
||||
|
||||
public LVector3 Position { get; set; }
|
||||
|
||||
public LVector3 Rotation { get; set; }
|
||||
|
||||
public LVector3 Velocity { get; set; }
|
||||
|
||||
public byte Speed { get; set; }
|
||||
|
||||
public LVector3 AimCoords { get; set; }
|
||||
|
||||
public uint CurrentWeaponHash { get; set; }
|
||||
|
||||
public ushort? Flag { get; set; } = 0;
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.FullSyncNpc);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write player + ped handle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write npc flags
|
||||
byteArray.AddRange(BitConverter.GetBytes(Flag.Value));
|
||||
|
||||
// Write npc model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(ModelHash));
|
||||
|
||||
// Write npc position
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Z));
|
||||
|
||||
// Write npc rotation
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Z));
|
||||
|
||||
// Write npc clothes
|
||||
// Write the count of clothes
|
||||
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<byte, short> cloth in Clothes)
|
||||
{
|
||||
byteArray.Add(cloth.Key);
|
||||
byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
|
||||
}
|
||||
|
||||
// Write npc health
|
||||
byteArray.AddRange(BitConverter.GetBytes(Health));
|
||||
|
||||
// Write npc velocity
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Velocity.Z));
|
||||
|
||||
// Write npc speed
|
||||
byteArray.Add(Speed);
|
||||
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (byte)PedDataFlags.IsAiming) != 0 || (Flag.Value & (byte)PedDataFlags.IsShooting) != 0)
|
||||
{
|
||||
// Write player aim coords
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Rotation.Z));
|
||||
}
|
||||
}
|
||||
|
||||
// Write npc weapon hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(CurrentWeaponHash));
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read player + ped handle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Read npc flag
|
||||
Flag = reader.ReadUShort();
|
||||
|
||||
// Read npc model hash
|
||||
ModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc position
|
||||
Position = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc rotation
|
||||
Rotation = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc clothes
|
||||
// Create new Dictionary
|
||||
Clothes = new Dictionary<byte, short>();
|
||||
// Read the count of clothes
|
||||
ushort clothCount = reader.ReadUShort();
|
||||
// For clothCount
|
||||
for (ushort i = 0; i < clothCount; i++)
|
||||
{
|
||||
// Read cloth value
|
||||
Clothes.Add(reader.ReadByte(), reader.ReadShort());
|
||||
}
|
||||
|
||||
// Read npc health
|
||||
Health = reader.ReadByte();
|
||||
|
||||
// Read npc velocity
|
||||
Velocity = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read npc speed
|
||||
Speed = reader.ReadByte();
|
||||
|
||||
// Read npc flag values
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (byte)PedDataFlags.IsAiming) != 0 || (Flag.Value & (byte)PedDataFlags.IsShooting) != 0)
|
||||
{
|
||||
AimCoords = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Read npc weapon hash
|
||||
CurrentWeaponHash = reader.ReadUInt();
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class FullSyncNpcVeh : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public long VehHandle { get; set; }
|
||||
|
||||
public int ModelHash { get; set; }
|
||||
|
||||
public Dictionary<byte, short> Clothes { get; set; }
|
||||
|
||||
public int Health { get; set; }
|
||||
|
||||
public LVector3 Position { get; set; }
|
||||
|
||||
public int VehModelHash { get; set; }
|
||||
|
||||
public short VehSeatIndex { get; set; }
|
||||
|
||||
public LQuaternion VehRotation { get; set; }
|
||||
|
||||
public float VehEngineHealth { get; set; }
|
||||
|
||||
public float VehRPM { get; set; }
|
||||
|
||||
public LVector3 VehVelocity { get; set; }
|
||||
|
||||
public float VehSpeed { get; set; }
|
||||
|
||||
public float VehSteeringAngle { get; set; }
|
||||
|
||||
public byte[] VehColors { get; set; }
|
||||
|
||||
public Dictionary<int, int> VehMods { get; set; }
|
||||
|
||||
public VehicleDamageModel VehDamageModel { get; set; }
|
||||
|
||||
public byte VehLandingGear { get; set; }
|
||||
|
||||
public ushort? Flag { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.FullSyncNpcVeh);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write player + npc netHandle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write player + vehicle handle
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehHandle));
|
||||
|
||||
// Write vehicles flags
|
||||
byteArray.AddRange(BitConverter.GetBytes(Flag.Value));
|
||||
|
||||
// Write npc health
|
||||
byteArray.AddRange(BitConverter.GetBytes(Health));
|
||||
|
||||
// Write npc model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(ModelHash));
|
||||
|
||||
// Write npc clothes
|
||||
// Write the count of clothes
|
||||
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<byte, short> cloth in Clothes)
|
||||
{
|
||||
byteArray.Add(cloth.Key);
|
||||
byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
|
||||
}
|
||||
|
||||
// Write vehicle model hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehModelHash));
|
||||
|
||||
// Write player seat index
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSeatIndex));
|
||||
|
||||
// Write vehicle position
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(Position.Z));
|
||||
|
||||
// Write vehicle rotation
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.Z));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRotation.W));
|
||||
|
||||
// Write vehicle engine health
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehEngineHealth));
|
||||
|
||||
// Write vehicle rpm
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehRPM));
|
||||
|
||||
// Write vehicle velocity
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehVelocity.Z));
|
||||
|
||||
// Write vehicle speed
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSpeed));
|
||||
|
||||
// Write vehicle steering angle
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehSteeringAngle));
|
||||
|
||||
// Check
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (ushort)VehicleDataFlags.IsPlane) != 0)
|
||||
{
|
||||
// Write the vehicle landing gear
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehLandingGear));
|
||||
}
|
||||
}
|
||||
|
||||
// Write vehicle colors
|
||||
byteArray.Add(VehColors[0]);
|
||||
byteArray.Add(VehColors[1]);
|
||||
|
||||
// Write vehicle mods
|
||||
// Write the count of mods
|
||||
byteArray.AddRange(BitConverter.GetBytes((short)VehMods.Count));
|
||||
// Loop the dictionary and add the values
|
||||
foreach (KeyValuePair<int, int> mod in VehMods)
|
||||
{
|
||||
// Write the mod value
|
||||
byteArray.AddRange(BitConverter.GetBytes(mod.Key));
|
||||
byteArray.AddRange(BitConverter.GetBytes(mod.Value));
|
||||
}
|
||||
|
||||
if (!VehDamageModel.Equals(default(VehicleDamageModel)))
|
||||
{
|
||||
// Write boolean = true
|
||||
byteArray.Add(0x01);
|
||||
// Write vehicle damage model
|
||||
byteArray.Add(VehDamageModel.BrokenDoors);
|
||||
byteArray.Add(VehDamageModel.BrokenWindows);
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehDamageModel.BurstedTires));
|
||||
byteArray.AddRange(BitConverter.GetBytes(VehDamageModel.PuncturedTires));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write boolean = false
|
||||
byteArray.Add(0x00);
|
||||
}
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read player + npc netHandle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Reader player + vehicle handle
|
||||
VehHandle = reader.ReadLong();
|
||||
|
||||
// Read vehicle flags
|
||||
Flag = reader.ReadUShort();
|
||||
|
||||
// Read npc health
|
||||
Health = reader.ReadInt();
|
||||
|
||||
// Read npc model hash
|
||||
ModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc clothes
|
||||
// Create new Dictionary
|
||||
Clothes = new Dictionary<byte, short>();
|
||||
// Read the count of clothes
|
||||
ushort clothCount = reader.ReadUShort();
|
||||
// For clothCount
|
||||
for (int i = 0; i < clothCount; i++)
|
||||
{
|
||||
// Read cloth value
|
||||
Clothes.Add(reader.ReadByte(), reader.ReadShort());
|
||||
}
|
||||
|
||||
// Read vehicle model hash
|
||||
VehModelHash = reader.ReadInt();
|
||||
|
||||
// Read npc seat index
|
||||
VehSeatIndex = reader.ReadShort();
|
||||
|
||||
// Read vehicle position
|
||||
Position = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle rotation
|
||||
VehRotation = new LQuaternion()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat(),
|
||||
W = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle engine health
|
||||
VehEngineHealth = reader.ReadFloat();
|
||||
|
||||
// Read vehicle rpm
|
||||
VehRPM = reader.ReadFloat();
|
||||
|
||||
// Read vehicle velocity
|
||||
VehVelocity = new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
};
|
||||
|
||||
// Read vehicle speed
|
||||
VehSpeed = reader.ReadFloat();
|
||||
|
||||
// Read vehicle steering angle
|
||||
VehSteeringAngle = reader.ReadFloat();
|
||||
|
||||
// Check
|
||||
if (Flag.HasValue)
|
||||
{
|
||||
if ((Flag.Value & (int)VehicleDataFlags.IsPlane) != 0)
|
||||
{
|
||||
// Read vehicle landing gear
|
||||
VehLandingGear = (byte)reader.ReadShort();
|
||||
}
|
||||
}
|
||||
|
||||
// Read vehicle colors
|
||||
byte vehColor1 = reader.ReadByte();
|
||||
byte vehColor2 = reader.ReadByte();
|
||||
VehColors = new byte[] { vehColor1, vehColor2 };
|
||||
|
||||
// Read vehicle mods
|
||||
// Create new Dictionary
|
||||
VehMods = new Dictionary<int, int>();
|
||||
// Read count of mods
|
||||
short vehModCount = reader.ReadShort();
|
||||
// Loop
|
||||
for (int i = 0; i < vehModCount; i++)
|
||||
{
|
||||
// Read the mod value
|
||||
VehMods.Add(reader.ReadInt(), reader.ReadInt());
|
||||
}
|
||||
|
||||
if (reader.ReadBool())
|
||||
{
|
||||
// Read vehicle damage model
|
||||
VehDamageModel = new VehicleDamageModel()
|
||||
{
|
||||
BrokenDoors = reader.ReadByte(),
|
||||
BrokenWindows = reader.ReadByte(),
|
||||
BurstedTires = reader.ReadUShort(),
|
||||
PuncturedTires = reader.ReadUShort()
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
550
Server/Packets/Packets.cs
Normal file
550
Server/Packets/Packets.cs
Normal file
@ -0,0 +1,550 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using Lidgren.Network;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
public static class VectorExtensions
|
||||
{
|
||||
public static LVector3 Normalize(this LVector3 value)
|
||||
{
|
||||
float value2 = value.Length();
|
||||
return value / value2;
|
||||
}
|
||||
|
||||
public static float Distance(this LVector3 value1, LVector3 value2)
|
||||
{
|
||||
LVector3 vector = value1 - value2;
|
||||
float num = Dot(vector, vector);
|
||||
return (float)Math.Sqrt(num);
|
||||
}
|
||||
|
||||
public static float Dot(this LVector3 vector1, LVector3 vector2)
|
||||
{
|
||||
return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z;
|
||||
}
|
||||
}
|
||||
|
||||
public struct LVector3
|
||||
{
|
||||
public LVector3(float X, float Y, float Z)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
public float X { get; set; }
|
||||
|
||||
public float Y { get; set; }
|
||||
|
||||
public float Z { get; set; }
|
||||
|
||||
#region SERVER-ONLY
|
||||
public float Length() => (float)Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
|
||||
public static LVector3 Subtract(LVector3 pos1, LVector3 pos2) => new(pos1.X - pos2.X, pos1.Y - pos2.Y, pos1.Z - pos2.Z);
|
||||
public static bool Equals(LVector3 value1, LVector3 value2) => value1.X == value2.X && value1.Y == value2.Y && value1.Z == value2.Z;
|
||||
public static LVector3 operator /(LVector3 value1, float value2)
|
||||
{
|
||||
float num = 1f / value2;
|
||||
return new LVector3(value1.X * num, value1.Y * num, value1.Z * num);
|
||||
}
|
||||
public static LVector3 operator -(LVector3 left, LVector3 right)
|
||||
{
|
||||
return new LVector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
|
||||
}
|
||||
public static LVector3 operator -(LVector3 value)
|
||||
{
|
||||
return default(LVector3) - value;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public struct LQuaternion
|
||||
{
|
||||
public LQuaternion(float X, float Y, float Z, float W)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
this.W = W;
|
||||
}
|
||||
|
||||
public float X { get; set; }
|
||||
|
||||
public float Y { get; set; }
|
||||
|
||||
public float Z { get; set; }
|
||||
|
||||
public float W { get; set; }
|
||||
}
|
||||
|
||||
enum PacketTypes
|
||||
{
|
||||
Handshake,
|
||||
PlayerConnect,
|
||||
PlayerDisconnect,
|
||||
FullSyncPlayer,
|
||||
FullSyncPlayerVeh,
|
||||
LightSyncPlayer,
|
||||
LightSyncPlayerVeh,
|
||||
SuperLightSync,
|
||||
FullSyncNpc,
|
||||
FullSyncNpcVeh,
|
||||
ChatMessage,
|
||||
NativeCall,
|
||||
NativeResponse,
|
||||
Mod,
|
||||
CleanUpWorld
|
||||
}
|
||||
|
||||
enum ConnectionChannel
|
||||
{
|
||||
Default = 0,
|
||||
PlayerLight = 1,
|
||||
PlayerFull = 2,
|
||||
PlayerSuperLight = 3,
|
||||
NPCFull = 4,
|
||||
Chat = 5,
|
||||
Native = 6,
|
||||
Mod = 7
|
||||
}
|
||||
|
||||
[Flags]
|
||||
enum PedDataFlags
|
||||
{
|
||||
IsAiming = 1 << 0,
|
||||
IsShooting = 1 << 1,
|
||||
IsReloading = 1 << 2,
|
||||
IsJumping = 1 << 3,
|
||||
IsRagdoll = 1 << 4,
|
||||
IsOnFire = 1 << 5,
|
||||
IsInParachuteFreeFall = 1 << 6,
|
||||
IsOnLadder = 1 << 7,
|
||||
IsVaulting = 1 << 8
|
||||
}
|
||||
|
||||
#region ===== VEHICLE DATA =====
|
||||
[Flags]
|
||||
enum VehicleDataFlags
|
||||
{
|
||||
IsEngineRunning = 1 << 0,
|
||||
AreLightsOn = 1 << 1,
|
||||
AreHighBeamsOn = 1 << 2,
|
||||
IsSirenActive = 1 << 3,
|
||||
IsDead = 1 << 4,
|
||||
IsHornActive = 1 << 5,
|
||||
IsTransformed = 1 << 6,
|
||||
RoofOpened = 1 << 7,
|
||||
OnTurretSeat = 1 << 8,
|
||||
IsPlane = 1 << 9
|
||||
}
|
||||
|
||||
struct VehicleDamageModel
|
||||
{
|
||||
public byte BrokenWindows { get; set; }
|
||||
|
||||
public byte BrokenDoors { get; set; }
|
||||
|
||||
public ushort BurstedTires { get; set; }
|
||||
|
||||
public ushort PuncturedTires { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
interface IPacket
|
||||
{
|
||||
void PacketToNetOutGoingMessage(NetOutgoingMessage message);
|
||||
void NetIncomingMessageToPacket(byte[] array);
|
||||
}
|
||||
|
||||
abstract class Packet : IPacket
|
||||
{
|
||||
public abstract void PacketToNetOutGoingMessage(NetOutgoingMessage message);
|
||||
public abstract void NetIncomingMessageToPacket(byte[] array);
|
||||
}
|
||||
|
||||
internal partial class Packets
|
||||
{
|
||||
public class Mod : Packet
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
|
||||
public long Target { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public byte CustomPacketID { get; set; }
|
||||
|
||||
public byte[] Bytes { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.Mod);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write NetHandle
|
||||
byteArray.AddRange(BitConverter.GetBytes(NetHandle));
|
||||
|
||||
// Write Target
|
||||
byteArray.AddRange(BitConverter.GetBytes(Target));
|
||||
|
||||
// Write Name
|
||||
byte[] nameBytes = Encoding.UTF8.GetBytes(Name);
|
||||
byteArray.AddRange(BitConverter.GetBytes(nameBytes.Length));
|
||||
byteArray.AddRange(nameBytes);
|
||||
|
||||
// Write CustomPacketID
|
||||
byteArray.Add(CustomPacketID);
|
||||
|
||||
// Write Bytes
|
||||
byteArray.AddRange(BitConverter.GetBytes(Bytes.Length));
|
||||
byteArray.AddRange(Bytes);
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read NetHandle
|
||||
NetHandle = reader.ReadLong();
|
||||
|
||||
// Read Target
|
||||
Target = reader.ReadLong();
|
||||
|
||||
// Read Name
|
||||
int nameLength = reader.ReadInt();
|
||||
Name = reader.ReadString(nameLength);
|
||||
|
||||
// Read CustomPacketID
|
||||
CustomPacketID = reader.ReadByte();
|
||||
|
||||
// Read Bytes
|
||||
int bytesLength = reader.ReadInt();
|
||||
Bytes = reader.ReadByteArray(bytesLength);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class ChatMessage : Packet
|
||||
{
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.ChatMessage);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
byte[] usernameBytes = Encoding.UTF8.GetBytes(Username);
|
||||
byte[] messageBytes = Encoding.UTF8.GetBytes(Message);
|
||||
|
||||
// Write UsernameLength
|
||||
byteArray.AddRange(BitConverter.GetBytes(usernameBytes.Length));
|
||||
|
||||
// Write Username
|
||||
byteArray.AddRange(usernameBytes);
|
||||
|
||||
// Write MessageLength
|
||||
byteArray.AddRange(BitConverter.GetBytes(messageBytes.Length));
|
||||
|
||||
// Write Message
|
||||
byteArray.AddRange(messageBytes);
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read username
|
||||
int usernameLength = reader.ReadInt();
|
||||
Username = reader.ReadString(usernameLength);
|
||||
|
||||
// Read message
|
||||
int messageLength = reader.ReadInt();
|
||||
Message = reader.ReadString(messageLength);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#region ===== NATIVECALL =====
|
||||
public class NativeCall : Packet
|
||||
{
|
||||
public ulong Hash { get; set; }
|
||||
|
||||
public List<object> Args { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.NativeCall);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write Hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(Hash));
|
||||
|
||||
// Write Args
|
||||
byteArray.AddRange(BitConverter.GetBytes(Args.Count));
|
||||
Args.ForEach(x =>
|
||||
{
|
||||
Type type = x.GetType();
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
byteArray.Add(0x00);
|
||||
byteArray.AddRange(BitConverter.GetBytes((int)x));
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
byteArray.Add(0x01);
|
||||
byteArray.AddRange(BitConverter.GetBytes((bool)x));
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
byteArray.Add(0x02);
|
||||
byteArray.AddRange(BitConverter.GetBytes((float)x));
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
byteArray.Add(0x03);
|
||||
byte[] stringBytes = Encoding.UTF8.GetBytes((string)x);
|
||||
byteArray.AddRange(BitConverter.GetBytes(stringBytes.Length));
|
||||
byteArray.AddRange(stringBytes);
|
||||
}
|
||||
else if (type == typeof(LVector3))
|
||||
{
|
||||
byteArray.Add(0x04);
|
||||
LVector3 vector = (LVector3)x;
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Z));
|
||||
}
|
||||
});
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read Hash
|
||||
Hash = reader.ReadULong();
|
||||
|
||||
// Read Args
|
||||
Args = new List<object>();
|
||||
int argsLength = reader.ReadInt();
|
||||
for (int i = 0; i < argsLength; i++)
|
||||
{
|
||||
byte argType = reader.ReadByte();
|
||||
switch (argType)
|
||||
{
|
||||
case 0x00:
|
||||
Args.Add(reader.ReadInt());
|
||||
break;
|
||||
case 0x01:
|
||||
Args.Add(reader.ReadBool());
|
||||
break;
|
||||
case 0x02:
|
||||
Args.Add(reader.ReadFloat());
|
||||
break;
|
||||
case 0x03:
|
||||
int stringLength = reader.ReadInt();
|
||||
Args.Add(reader.ReadString(stringLength));
|
||||
break;
|
||||
case 0x04:
|
||||
Args.Add(new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public class NativeResponse : Packet
|
||||
{
|
||||
public ulong Hash { get; set; }
|
||||
|
||||
public List<object> Args { get; set; }
|
||||
|
||||
public byte? ResultType { get; set; }
|
||||
|
||||
public long ID { get; set; }
|
||||
|
||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||
{
|
||||
#region PacketToNetOutGoingMessage
|
||||
message.Write((byte)PacketTypes.NativeResponse);
|
||||
|
||||
List<byte> byteArray = new List<byte>();
|
||||
|
||||
// Write Hash
|
||||
byteArray.AddRange(BitConverter.GetBytes(Hash));
|
||||
|
||||
Type type;
|
||||
|
||||
// Write Args
|
||||
byteArray.AddRange(BitConverter.GetBytes(Args.Count));
|
||||
Args.ForEach(x =>
|
||||
{
|
||||
type = x.GetType();
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
byteArray.Add(0x00);
|
||||
byteArray.AddRange(BitConverter.GetBytes((int)x));
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
byteArray.Add(0x01);
|
||||
byteArray.AddRange(BitConverter.GetBytes((bool)x));
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
byteArray.Add(0x02);
|
||||
byteArray.AddRange(BitConverter.GetBytes((float)x));
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
byteArray.Add(0x03);
|
||||
byte[] stringBytes = Encoding.UTF8.GetBytes((string)x);
|
||||
byteArray.AddRange(BitConverter.GetBytes(stringBytes.Length));
|
||||
byteArray.AddRange(stringBytes);
|
||||
}
|
||||
else if (type == typeof(LVector3))
|
||||
{
|
||||
byteArray.Add(0x04);
|
||||
LVector3 vector = (LVector3)x;
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.X));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Y));
|
||||
byteArray.AddRange(BitConverter.GetBytes(vector.Z));
|
||||
}
|
||||
});
|
||||
|
||||
byteArray.AddRange(BitConverter.GetBytes(ID));
|
||||
|
||||
// Write type of result
|
||||
if (ResultType.HasValue)
|
||||
{
|
||||
byteArray.Add(ResultType.Value);
|
||||
}
|
||||
|
||||
byte[] result = byteArray.ToArray();
|
||||
|
||||
message.Write(result.Length);
|
||||
message.Write(result);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public override void NetIncomingMessageToPacket(byte[] array)
|
||||
{
|
||||
#region NetIncomingMessageToPacket
|
||||
BitReader reader = new BitReader(array);
|
||||
|
||||
// Read Hash
|
||||
Hash = reader.ReadULong();
|
||||
|
||||
// Read Args
|
||||
Args = new List<object>();
|
||||
int argsLength = reader.ReadInt();
|
||||
for (int i = 0; i < argsLength; i++)
|
||||
{
|
||||
byte argType = reader.ReadByte();
|
||||
switch (argType)
|
||||
{
|
||||
case 0x00:
|
||||
Args.Add(reader.ReadInt());
|
||||
break;
|
||||
case 0x01:
|
||||
Args.Add(reader.ReadBool());
|
||||
break;
|
||||
case 0x02:
|
||||
Args.Add(reader.ReadFloat());
|
||||
break;
|
||||
case 0x03:
|
||||
int stringLength = reader.ReadInt();
|
||||
Args.Add(reader.ReadString(stringLength));
|
||||
break;
|
||||
case 0x04:
|
||||
Args.Add(new LVector3()
|
||||
{
|
||||
X = reader.ReadFloat(),
|
||||
Y = reader.ReadFloat(),
|
||||
Z = reader.ReadFloat()
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ID = reader.ReadLong();
|
||||
|
||||
// Read type of result
|
||||
if (reader.CanRead(1))
|
||||
{
|
||||
ResultType = reader.ReadByte();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endregion // ===== NATIVECALL =====
|
||||
}
|
||||
|
||||
public static class CoopSerializer
|
||||
{
|
||||
public static byte[] Serialize(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string jsonString = JsonConvert.SerializeObject(obj);
|
||||
return System.Text.Encoding.UTF8.GetBytes(jsonString);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
1067
Server/Packets/PlayerPackets.cs
Normal file
1067
Server/Packets/PlayerPackets.cs
Normal file
File diff suppressed because it is too large
Load Diff
@ -231,7 +231,7 @@ namespace CoopServer
|
||||
{
|
||||
case NetIncomingMessageType.ConnectionApproval:
|
||||
Logging.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
|
||||
if (message.ReadByte() != (byte)PacketTypes.HandshakePacket)
|
||||
if (message.ReadByte() != (byte)PacketTypes.Handshake)
|
||||
{
|
||||
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
|
||||
message.SenderConnection.Deny("Wrong packet!");
|
||||
@ -243,7 +243,7 @@ namespace CoopServer
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
HandshakePacket packet = new HandshakePacket();
|
||||
Packets.Handshake packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
GetHandshake(message.SenderConnection, packet);
|
||||
@ -273,14 +273,14 @@ namespace CoopServer
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case (byte)PacketTypes.FullSyncPlayerPacket:
|
||||
case (byte)PacketTypes.FullSyncPlayer:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncPlayerPacket packet = new FullSyncPlayerPacket();
|
||||
Packets.FullSyncPlayer packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncPlayer(packet);
|
||||
@ -291,14 +291,14 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncPlayerVehPacket:
|
||||
case (byte)PacketTypes.FullSyncPlayerVeh:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncPlayerVehPacket packet = new FullSyncPlayerVehPacket();
|
||||
Packets.FullSyncPlayerVeh packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncPlayerVeh(packet);
|
||||
@ -309,14 +309,14 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.LightSyncPlayerPacket:
|
||||
case (byte)PacketTypes.LightSyncPlayer:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
LightSyncPlayerPacket packet = new LightSyncPlayerPacket();
|
||||
Packets.LightSyncPlayer packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
LightSyncPlayer(packet);
|
||||
@ -327,14 +327,14 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.LightSyncPlayerVehPacket:
|
||||
case (byte)PacketTypes.LightSyncPlayerVeh:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
LightSyncPlayerVehPacket packet = new LightSyncPlayerVehPacket();
|
||||
Packets.LightSyncPlayerVeh packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
LightSyncPlayerVeh(packet);
|
||||
@ -345,14 +345,14 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.ChatMessagePacket:
|
||||
case (byte)PacketTypes.ChatMessage:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
ChatMessagePacket packet = new ChatMessagePacket();
|
||||
Packets.ChatMessage packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
SendChatMessage(packet);
|
||||
@ -363,7 +363,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncNpcPacket:
|
||||
case (byte)PacketTypes.FullSyncNpc:
|
||||
{
|
||||
if (MainSettings.NpcsAllowed)
|
||||
{
|
||||
@ -372,7 +372,7 @@ namespace CoopServer
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncNpcPacket packet = new FullSyncNpcPacket();
|
||||
Packets.FullSyncNpc packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncNpc(message.SenderConnection, packet);
|
||||
@ -388,7 +388,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.FullSyncNpcVehPacket:
|
||||
case (byte)PacketTypes.FullSyncNpcVeh:
|
||||
{
|
||||
if (MainSettings.NpcsAllowed)
|
||||
{
|
||||
@ -397,7 +397,7 @@ namespace CoopServer
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
FullSyncNpcVehPacket packet = new FullSyncNpcVehPacket();
|
||||
Packets.FullSyncNpcVeh packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
FullSyncNpcVeh(message.SenderConnection, packet);
|
||||
@ -413,14 +413,14 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.NativeResponsePacket:
|
||||
case (byte)PacketTypes.NativeResponse:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
NativeResponsePacket packet = new NativeResponsePacket();
|
||||
Packets.NativeResponse packet = new();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
Client client = Clients.Find(x => x.NetHandle == message.SenderConnection.RemoteUniqueIdentifier);
|
||||
@ -439,7 +439,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.ModPacket:
|
||||
case (byte)PacketTypes.Mod:
|
||||
{
|
||||
if (MainSettings.ModsAllowed)
|
||||
{
|
||||
@ -448,13 +448,13 @@ namespace CoopServer
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
ModPacket packet = new ModPacket();
|
||||
Packets.Mod packet = new Packets.Mod();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
bool resourceResult = false;
|
||||
if (RunningResource != null)
|
||||
{
|
||||
if (RunningResource.InvokeModPacketReceived(packet.NetHandle, packet.Target, packet.Mod, packet.CustomPacketID, packet.Bytes))
|
||||
if (RunningResource.InvokeModPacketReceived(packet.NetHandle, packet.Target, packet.Name, packet.CustomPacketID, packet.Bytes))
|
||||
{
|
||||
resourceResult = true;
|
||||
}
|
||||
@ -554,7 +554,7 @@ namespace CoopServer
|
||||
|
||||
#region -- PLAYER --
|
||||
// Before we approve the connection, we must shake hands
|
||||
private void GetHandshake(NetConnection local, HandshakePacket packet)
|
||||
private void GetHandshake(NetConnection local, Packets.Handshake packet)
|
||||
{
|
||||
Logging.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + local.RemoteEndPoint.Address.ToString() + "]");
|
||||
|
||||
@ -616,7 +616,7 @@ namespace CoopServer
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
|
||||
// Create a new handshake packet
|
||||
new HandshakePacket()
|
||||
new Packets.Handshake()
|
||||
{
|
||||
NetHandle = localNetHandle,
|
||||
Username = string.Empty,
|
||||
@ -655,7 +655,7 @@ namespace CoopServer
|
||||
if (targetClient != null)
|
||||
{
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
new PlayerConnectPacket()
|
||||
new Packets.PlayerConnect()
|
||||
{
|
||||
NetHandle = targetNetHandle,
|
||||
Username = targetClient.Player.Username
|
||||
@ -666,7 +666,7 @@ namespace CoopServer
|
||||
|
||||
// Send local to all players
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
new PlayerConnectPacket()
|
||||
new Packets.PlayerConnect()
|
||||
{
|
||||
NetHandle = local.RemoteUniqueIdentifier,
|
||||
Username = localClient.Player.Username
|
||||
@ -685,7 +685,7 @@ namespace CoopServer
|
||||
|
||||
if (!string.IsNullOrEmpty(MainSettings.WelcomeMessage))
|
||||
{
|
||||
SendChatMessage(new ChatMessagePacket() { Username = "Server", Message = MainSettings.WelcomeMessage }, new List<NetConnection>() { local });
|
||||
SendChatMessage(new Packets.ChatMessage() { Username = "Server", Message = MainSettings.WelcomeMessage }, new List<NetConnection>() { local });
|
||||
}
|
||||
}
|
||||
|
||||
@ -696,7 +696,7 @@ namespace CoopServer
|
||||
if (clients.Count > 0)
|
||||
{
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
new PlayerDisconnectPacket()
|
||||
new Packets.PlayerDisconnect()
|
||||
{
|
||||
NetHandle = clientID
|
||||
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
@ -721,7 +721,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
private static void FullSyncPlayer(FullSyncPlayerPacket packet)
|
||||
private static void FullSyncPlayer(Packets.FullSyncPlayer packet)
|
||||
{
|
||||
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
||||
if (client == null)
|
||||
@ -747,7 +747,7 @@ namespace CoopServer
|
||||
}
|
||||
else
|
||||
{
|
||||
new SuperLightSyncPacket()
|
||||
new Packets.SuperLightSync()
|
||||
{
|
||||
NetHandle = packet.NetHandle,
|
||||
Position = packet.Position,
|
||||
@ -763,7 +763,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
private static void FullSyncPlayerVeh(FullSyncPlayerVehPacket packet)
|
||||
private static void FullSyncPlayerVeh(Packets.FullSyncPlayerVeh packet)
|
||||
{
|
||||
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
||||
if (client == null)
|
||||
@ -791,7 +791,7 @@ namespace CoopServer
|
||||
}
|
||||
else
|
||||
{
|
||||
new SuperLightSyncPacket()
|
||||
new Packets.SuperLightSync()
|
||||
{
|
||||
NetHandle = packet.NetHandle,
|
||||
Position = packet.Position,
|
||||
@ -807,7 +807,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
private static void LightSyncPlayer(LightSyncPlayerPacket packet)
|
||||
private static void LightSyncPlayer(Packets.LightSyncPlayer packet)
|
||||
{
|
||||
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
||||
if (client == null)
|
||||
@ -832,7 +832,7 @@ namespace CoopServer
|
||||
}
|
||||
else
|
||||
{
|
||||
new SuperLightSyncPacket()
|
||||
new Packets.SuperLightSync()
|
||||
{
|
||||
NetHandle = packet.NetHandle,
|
||||
Position = packet.Position,
|
||||
@ -848,7 +848,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
private static void LightSyncPlayerVeh(LightSyncPlayerVehPacket packet)
|
||||
private static void LightSyncPlayerVeh(Packets.LightSyncPlayerVeh packet)
|
||||
{
|
||||
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
||||
if (client == null)
|
||||
@ -873,7 +873,7 @@ namespace CoopServer
|
||||
}
|
||||
else
|
||||
{
|
||||
new SuperLightSyncPacket()
|
||||
new Packets.SuperLightSync()
|
||||
{
|
||||
NetHandle = packet.NetHandle,
|
||||
Position = packet.Position,
|
||||
@ -890,7 +890,7 @@ namespace CoopServer
|
||||
}
|
||||
|
||||
// Send a message to targets or all players
|
||||
private static void SendChatMessage(ChatMessagePacket packet, List<NetConnection> targets = null)
|
||||
private static void SendChatMessage(Packets.ChatMessage packet, List<NetConnection> targets = null)
|
||||
{
|
||||
if (RunningResource != null)
|
||||
{
|
||||
@ -955,7 +955,7 @@ namespace CoopServer
|
||||
{
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
|
||||
new ChatMessagePacket() { Username = username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
new Packets.ChatMessage() { Username = username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
||||
MainNetServer.SendMessage(outgoingMessage, targets ?? MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
|
||||
}
|
||||
@ -966,7 +966,7 @@ namespace CoopServer
|
||||
#endregion
|
||||
|
||||
#region -- NPC --
|
||||
private static void FullSyncNpc(NetConnection local, FullSyncNpcPacket packet)
|
||||
private static void FullSyncNpc(NetConnection local, Packets.FullSyncNpc packet)
|
||||
{
|
||||
List<NetConnection> clients;
|
||||
if ((clients = Util.GetAllInRange(packet.Position, 550f, local)).Count == 0)
|
||||
@ -979,7 +979,7 @@ namespace CoopServer
|
||||
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.NPCFull);
|
||||
}
|
||||
|
||||
private static void FullSyncNpcVeh(NetConnection local, FullSyncNpcVehPacket packet)
|
||||
private static void FullSyncNpcVeh(NetConnection local, Packets.FullSyncNpcVeh packet)
|
||||
{
|
||||
List<NetConnection> clients;
|
||||
if ((clients = Util.GetAllInRange(packet.Position, 550f, local)).Count == 0)
|
||||
|
@ -63,9 +63,9 @@ namespace CoopServer
|
||||
ReadyToStop = true;
|
||||
}
|
||||
|
||||
public bool InvokeModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes)
|
||||
public bool InvokeModPacketReceived(long from, long target, string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
Task<bool> task = new(() => _script.API.InvokeModPacketReceived(from, target, mod, customID, bytes));
|
||||
Task<bool> task = new(() => _script.API.InvokeModPacketReceived(from, target, modName, customID, bytes));
|
||||
task.Start();
|
||||
task.Wait(5000);
|
||||
|
||||
@ -161,7 +161,7 @@ namespace CoopServer
|
||||
public delegate void OnTickEvent(long tick);
|
||||
public delegate void ChatEvent(string username, string message, CancelEventArgs cancel);
|
||||
public delegate void PlayerEvent(Client client);
|
||||
public delegate void ModEvent(long from, long target, string mod, byte customID, byte[] bytes, CancelEventArgs args);
|
||||
public delegate void ModEvent(long from, long target, string modName, byte customID, byte[] bytes, CancelEventArgs args);
|
||||
#endregion
|
||||
|
||||
#region EVENTS
|
||||
@ -280,10 +280,10 @@ namespace CoopServer
|
||||
OnPlayerVehicleHandleUpdate?.Invoke(Server.Clients.FirstOrDefault(x => x.Player.Username == username));
|
||||
}
|
||||
|
||||
internal bool InvokeModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes)
|
||||
internal bool InvokeModPacketReceived(long from, long target, string modName, byte customID, byte[] bytes)
|
||||
{
|
||||
CancelEventArgs args = new(false);
|
||||
OnModPacketReceived?.Invoke(from, target, mod, customID, bytes, args);
|
||||
OnModPacketReceived?.Invoke(from, target, modName, customID, bytes, args);
|
||||
return args.Cancel;
|
||||
}
|
||||
#endregion
|
||||
@ -292,11 +292,11 @@ namespace CoopServer
|
||||
/// <summary>
|
||||
/// Send a mod packet to all players
|
||||
/// </summary>
|
||||
/// <param name="mod">The name of the modification that will receive the data</param>
|
||||
/// <param name="modName">The name of the modification that will receive the data</param>
|
||||
/// <param name="customID">The ID to check what this data is</param>
|
||||
/// <param name="bytes">The serialized data</param>
|
||||
/// <param name="netHandleList">The list of connections (players) that will receive the data</param>
|
||||
public static void SendModPacketToAll(string mod, byte customID, byte[] bytes, List<long> netHandleList = null)
|
||||
public static void SendModPacketToAll(string modName, byte customID, byte[] bytes, List<long> netHandleList = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -305,11 +305,11 @@ namespace CoopServer
|
||||
: Server.MainNetServer.Connections.FindAll(c => netHandleList.Contains(c.RemoteUniqueIdentifier));
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
||||
new ModPacket()
|
||||
new Packets.Mod()
|
||||
{
|
||||
NetHandle = 0,
|
||||
Target = 0,
|
||||
Mod = mod,
|
||||
Name = modName,
|
||||
CustomPacketID = customID,
|
||||
Bytes = bytes
|
||||
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
@ -343,7 +343,7 @@ namespace CoopServer
|
||||
return;
|
||||
}
|
||||
|
||||
NativeCallPacket packet = new()
|
||||
Packets.NativeCall packet = new()
|
||||
{
|
||||
Hash = hash,
|
||||
Args = new List<object>(args) ?? new List<object>()
|
||||
@ -429,7 +429,7 @@ namespace CoopServer
|
||||
: Server.MainNetServer.Connections.FindAll(c => netHandleList.Contains(c.RemoteUniqueIdentifier));
|
||||
|
||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
||||
outgoingMessage.Write((byte)PacketTypes.CleanUpWorldPacket);
|
||||
outgoingMessage.Write((byte)PacketTypes.CleanUpWorld);
|
||||
Server.MainNetServer.SendMessage(outgoingMessage, connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user