using System;
using System.Collections.Generic;
using System.Text;
using Lidgren.Network;
using Newtonsoft.Json;
using GTA.Math;
namespace RageCoop.Core
{
///
///
///
public struct LVector3
{
#region CLIENT-ONLY
///
///
///
public Vector3 ToVector()
{
return new Vector3(X, Y, Z);
}
#endregion
#region SERVER-ONLY
public float Length() => (float)Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
public static LVector3 Subtract(LVector3 pos1, LVector3 pos2) { return new LVector3(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 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; }
}
///
///
///
public struct LQuaternion
{
#region CLIENT-ONLY
///
///
///
public Quaternion ToQuaternion()
{
return new Quaternion(X, Y, Z, W);
}
#endregion
///
///
///
public LQuaternion(float X, float Y, float Z, float W)
{
this.X = X;
this.Y = Y;
this.Z = Z;
this.W = W;
}
///
///
///
public float X { get; set; }
///
///
///
public float Y { get; set; }
///
///
///
public float Z { get; set; }
///
///
///
public float W { get; set; }
}
public enum PacketTypes:byte
{
Handshake=0,
PlayerConnect=1,
PlayerDisconnect=2,
ChatMessage=10,
NativeCall=11,
NativeResponse=12,
Mod=13,
CleanUpWorld=14,
FileTransferTick=15,
FileTransferRequest=16,
FileTransferComplete=17,
ServerClientEvent=18,
#region Sync
#region INTERVAL
VehicleSync = 20,
VehicleStateSync = 21,
CharacterSync = 22,
CharacterStateSync = 23,
#endregion
#region EVENT
PedKilled=30,
BulletShot=31,
ProjectileShot=32,
ProjectileExploded=33,
EnteringVehicle=34,
LeaveVehicle = 35,
EnteredVehicle=36,
OwnerChanged=37,
VehicleBulletShot = 38,
#endregion
/// obsolete sync method
/*
FullSyncPlayer = 3,
FullSyncPlayerVeh = 4,
LightSyncPlayer = 5,
LightSyncPlayerVeh = 6,
SuperLightSync = 7,
FullSyncNpc = 8,
FullSyncNpcVeh = 9,
*/
#endregion
}
public static class PacketExtensions
{
public static bool IsSyncEvent(this PacketTypes p)
{
return (30<=(byte)p)&&((byte)p<=40);
}
}
public enum ConnectionChannel
{
Default = 0,
Chat = 5,
Native = 6,
Mod = 7,
File = 8,
Event = 9,
VehicleSync=20,
CharacterSync=21,
SyncEvents=30,
///obsolete
/*
PlayerLight = 1,
PlayerFull = 2,
PlayerSuperLight = 3,
NPCFull = 4,
*/
}
[Flags]
public enum PedDataFlags:ushort
{
None=0,
IsAiming = 1 << 0,
IsReloading = 1 << 2,
IsJumping = 1 << 3,
IsRagdoll = 1 << 4,
IsOnFire = 1 << 5,
IsInParachuteFreeFall = 1 << 6,
IsParachuteOpen = 1 << 7,
IsOnLadder = 1 << 8,
IsVaulting = 1 << 9,
IsInCover=1<< 10,
}
#region ===== VEHICLE DATA =====
public enum VehicleDataFlags:ushort
{
IsEngineRunning = 1 << 0,
AreLightsOn = 1 << 1,
AreBrakeLightsOn = 1 << 2,
AreHighBeamsOn = 1 << 3,
IsSirenActive = 1 << 4,
IsDead = 1 << 5,
IsHornActive = 1 << 6,
IsTransformed = 1 << 7,
RoofOpened = 1 << 8,
OnTurretSeat = 1 << 9,
IsAircraft = 1 << 10,
IsHandBrakeOn=1<<11,
}
public struct VehicleDamageModel
{
public byte BrokenDoors { get; set; }
public byte OpenedDoors { get; set; }
public byte BrokenWindows { get; set; }
public short BurstedTires { get; set; }
public byte LeftHeadLightBroken { get; set; }
public byte RightHeadLightBroken { get; set; }
}
#endregion
interface IPacket
{
void Pack(NetOutgoingMessage message);
void Unpack(byte[] array);
}
public abstract class Packet : IPacket
{
public abstract void Pack(NetOutgoingMessage message);
public abstract void Unpack(byte[] array);
}
public 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 Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketTypes.Mod);
List byteArray = new List();
// 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 Unpack(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 Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketTypes.ChatMessage);
List byteArray = new List();
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 Unpack(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
}
}
public class ServerClientEvent : Packet
{
public string EventName { get; set; }
public List