236 lines
5.8 KiB
C#
Raw Normal View History

2022-01-01 03:07:18 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using Lidgren.Network;
using Newtonsoft.Json;
using GTA.Math;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
2022-01-01 03:07:18 +01:00
{
2022-07-01 14:39:43 +08:00
internal enum PacketType:byte
2022-05-22 15:55:26 +08:00
{
Handshake=0,
PlayerConnect=1,
PlayerDisconnect=2,
PlayerInfoUpdate=3,
2022-06-24 10:33:36 +08:00
PublicKeyRequest=4,
PublicKeyResponse=5,
2022-07-01 12:22:31 +08:00
Request=6,
Response=7,
2022-05-22 15:55:26 +08:00
ChatMessage=10,
2022-06-23 09:46:38 +08:00
// NativeCall=11,
// NativeResponse=12,
2022-06-24 10:33:36 +08:00
// Mod=13,
// CleanUpWorld=14,
2022-07-01 12:22:31 +08:00
FileTransferChunk=11,
FileTransferRequest=12,
FileTransferResponse = 13,
FileTransferComplete =14,
AllResourcesSent=15,
2022-07-01 12:22:31 +08:00
CustomEvent = 16,
2022-07-05 11:18:26 +08:00
CustomEventQueued = 17,
2022-05-22 15:55:26 +08:00
#region Sync
#region INTERVAL
VehicleSync = 20,
VehicleStateSync = 21,
PedSync = 22,
PedStateSync = 23,
ProjectileSync=24,
2022-05-22 15:55:26 +08:00
#endregion
#region EVENT
PedKilled=30,
BulletShot=31,
EnteringVehicle=32,
LeaveVehicle = 33,
EnteredVehicle=34,
OwnerChanged=35,
VehicleBulletShot = 36,
NozzleTransform=37,
2022-05-22 15:55:26 +08:00
#endregion
#endregion
2022-06-24 10:33:36 +08:00
Unknown=255
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
internal static class PacketExtensions
2022-01-01 03:07:18 +01:00
{
2022-07-01 14:39:43 +08:00
internal static bool IsSyncEvent(this PacketType p)
2022-05-22 15:55:26 +08:00
{
return (30<=(byte)p)&&((byte)p<=40);
}
2022-01-01 03:07:18 +01:00
}
2022-07-01 14:39:43 +08:00
internal enum ConnectionChannel
2022-01-01 03:07:18 +01:00
{
Default = 0,
Chat = 5,
Native = 6,
2022-04-02 18:11:30 +02:00
Mod = 7,
File = 8,
2022-05-22 15:55:26 +08:00
Event = 9,
2022-07-01 12:22:31 +08:00
RequestResponse=10,
2022-05-22 15:55:26 +08:00
VehicleSync=20,
PedSync=21,
ProjectileSync = 22,
SyncEvents =30,
2022-01-01 03:07:18 +01:00
}
2022-05-22 15:55:26 +08:00
[Flags]
2022-07-01 14:39:43 +08:00
internal enum PedDataFlags:ushort
2022-01-01 03:07:18 +01:00
{
2022-05-22 15:55:26 +08:00
None=0,
2022-01-01 03:07:18 +01:00
IsAiming = 1 << 0,
2022-06-22 14:18:20 +08:00
IsInStealthMode = 1 << 1,
2022-01-01 03:07:18 +01:00
IsReloading = 1 << 2,
IsJumping = 1 << 3,
IsRagdoll = 1 << 4,
IsOnFire = 1 << 5,
IsInParachuteFreeFall = 1 << 6,
2022-01-01 04:16:24 +01:00
IsParachuteOpen = 1 << 7,
IsOnLadder = 1 << 8,
2022-05-22 15:55:26 +08:00
IsVaulting = 1 << 9,
IsInCover = 1 << 10,
2022-01-01 03:07:18 +01:00
}
#region ===== VEHICLE DATA =====
2022-07-01 14:39:43 +08:00
internal enum VehicleDataFlags:ushort
2022-01-01 03:07:18 +01:00
{
2022-06-22 14:18:20 +08:00
None=0,
2022-01-01 03:07:18 +01:00
IsEngineRunning = 1 << 0,
AreLightsOn = 1 << 1,
2022-01-01 17:24:55 +01:00
AreBrakeLightsOn = 1 << 2,
AreHighBeamsOn = 1 << 3,
IsSirenActive = 1 << 4,
IsDead = 1 << 5,
IsHornActive = 1 << 6,
IsTransformed = 1 << 7,
RoofOpened = 1 << 8,
OnTurretSeat = 1 << 9,
2022-05-22 15:55:26 +08:00
IsAircraft = 1 << 10,
IsDeluxoHovering=1 << 11,
2022-06-19 13:36:23 +08:00
HasRoof=1 << 12,
2022-01-01 03:07:18 +01:00
}
2022-06-22 14:18:20 +08:00
2022-07-01 14:39:43 +08:00
internal enum PlayerConfigFlags : byte
2022-06-22 14:18:20 +08:00
{
None = 0,
ShowBlip= 1 << 0,
ShowNameTag= 1 << 1
}
2022-07-01 14:39:43 +08:00
internal struct VehicleDamageModel
2022-01-01 03:07:18 +01:00
{
public byte BrokenDoors { get; set; }
2022-05-22 15:55:26 +08:00
public byte OpenedDoors { get; set; }
public byte BrokenWindows { get; set; }
2022-04-14 02:08:13 +02:00
public short BurstedTires { get; set; }
public byte LeftHeadLightBroken { get; set; }
public byte RightHeadLightBroken { get; set; }
2022-01-01 03:07:18 +01:00
}
#endregion
interface IPacket
{
2022-05-22 15:55:26 +08:00
void Pack(NetOutgoingMessage message);
void Unpack(byte[] array);
2022-01-01 03:07:18 +01:00
}
2022-07-01 14:39:43 +08:00
internal abstract class Packet : IPacket
2022-01-01 03:07:18 +01:00
{
2022-05-22 15:55:26 +08:00
public abstract void Pack(NetOutgoingMessage message);
public abstract void Unpack(byte[] array);
2022-01-01 03:07:18 +01:00
}
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-01-01 03:07:18 +01:00
{
2022-07-01 14:39:43 +08:00
internal class ChatMessage : Packet
2022-01-01 03:07:18 +01:00
{
public string Username { get; set; }
public string Message { get; set; }
2022-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
2022-01-01 03:07:18 +01:00
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.ChatMessage);
2022-01-01 03:07:18 +01:00
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
}
2022-05-22 15:55:26 +08:00
public override void Unpack(byte[] array)
2022-01-01 03:07:18 +01:00
{
#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
}
}
}
2022-07-01 14:39:43 +08:00
internal static class CoopSerializer
2022-01-01 03:07:18 +01:00
{
/// <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);
}
}
}