2022-06-19 11:12:20 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Lidgren.Network;
|
|
|
|
|
namespace RageCoop.Core
|
|
|
|
|
{
|
2022-07-01 14:39:43 +08:00
|
|
|
|
internal partial class Packets
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
2022-07-01 14:39:43 +08:00
|
|
|
|
internal class CustomEvent : Packet
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
2022-08-06 10:49:55 +08:00
|
|
|
|
public override PacketType Type => (_queued ? PacketType.CustomEventQueued : PacketType.CustomEvent);
|
2022-07-05 11:18:26 +08:00
|
|
|
|
public CustomEvent(Func<byte,BitReader,object> onResolve = null,bool queued=false)
|
2022-07-02 17:53:35 +08:00
|
|
|
|
{
|
|
|
|
|
_resolve= onResolve;
|
2022-07-05 11:18:26 +08:00
|
|
|
|
_queued= queued;
|
2022-07-02 17:53:35 +08:00
|
|
|
|
}
|
2022-07-05 11:18:26 +08:00
|
|
|
|
private bool _queued;
|
2022-07-02 17:53:35 +08:00
|
|
|
|
private Func<byte, BitReader, object> _resolve { get; set; }
|
2022-06-19 11:12:20 +08:00
|
|
|
|
public int Hash { get; set; }
|
2022-07-05 10:52:22 +08:00
|
|
|
|
public object[] Args { get; set; }
|
2022-06-19 11:12:20 +08:00
|
|
|
|
|
2022-08-06 10:43:24 +08:00
|
|
|
|
public override byte[] Serialize()
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args= Args ?? new object[] { };
|
2022-06-19 11:12:20 +08:00
|
|
|
|
|
|
|
|
|
List<byte> result = new List<byte>();
|
|
|
|
|
result.AddInt(Hash);
|
2022-07-05 10:52:22 +08:00
|
|
|
|
result.AddInt(Args.Length);
|
2022-06-19 11:12:20 +08:00
|
|
|
|
(byte, byte[]) tup;
|
|
|
|
|
foreach (var arg in Args)
|
|
|
|
|
{
|
|
|
|
|
tup=CoreUtils.GetBytesFromObject(arg);
|
2022-06-23 09:46:38 +08:00
|
|
|
|
if (tup.Item1==0||tup.Item2==null)
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Object of type {arg.GetType()} is not supported");
|
|
|
|
|
}
|
|
|
|
|
result.Add(tup.Item1);
|
|
|
|
|
result.AddRange(tup.Item2);
|
|
|
|
|
}
|
2022-08-06 10:43:24 +08:00
|
|
|
|
return result.ToArray();
|
2022-06-19 11:12:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 10:43:24 +08:00
|
|
|
|
public override void Deserialize(byte[] array)
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
|
|
|
|
BitReader reader = new BitReader(array);
|
|
|
|
|
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Hash = reader.ReadInt32();
|
|
|
|
|
var len=reader.ReadInt32();
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args=new object[len];
|
2022-06-19 11:12:20 +08:00
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
{
|
2022-06-23 09:46:38 +08:00
|
|
|
|
byte type = reader.ReadByte();
|
|
|
|
|
switch (type)
|
2022-06-19 11:12:20 +08:00
|
|
|
|
{
|
|
|
|
|
case 0x01:
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=reader.ReadByte(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x02:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadInt32(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x03:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadUInt16(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x04:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadInt32(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x05:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadUInt32(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x06:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadInt64(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x07:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadUInt64(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x08:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadSingle(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x09:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=reader.ReadBoolean(); break;
|
2022-06-19 11:12:20 +08:00
|
|
|
|
case 0x10:
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=reader.ReadString(); break;
|
2022-07-02 17:14:56 +08:00
|
|
|
|
case 0x11:
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=reader.ReadVector3(); break;
|
2022-07-02 17:14:56 +08:00
|
|
|
|
case 0x12:
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=reader.ReadQuaternion(); break;
|
2022-07-02 17:14:56 +08:00
|
|
|
|
case 0x13:
|
2022-08-06 11:40:38 +08:00
|
|
|
|
Args[i]=(GTA.Model)reader.ReadInt32(); break;
|
2022-07-03 15:28:28 +08:00
|
|
|
|
case 0x14:
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=reader.ReadVector2(); break;
|
2022-06-23 09:46:38 +08:00
|
|
|
|
default:
|
2022-07-02 17:53:35 +08:00
|
|
|
|
if (_resolve==null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Unexpected type:{type}\r\n{array.Dump()}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-07-05 10:52:22 +08:00
|
|
|
|
Args[i]=_resolve(type, reader); break;
|
2022-07-02 17:53:35 +08:00
|
|
|
|
}
|
2022-06-19 11:12:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|