2022-06-19 11:12:20 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Lidgren.Network;
|
|
|
|
|
namespace RageCoop.Core
|
|
|
|
|
{
|
|
|
|
|
public partial class Packets
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class CustomEvent : Packet
|
|
|
|
|
{
|
|
|
|
|
public int Hash { get; set; }
|
2022-06-23 09:46:38 +08:00
|
|
|
|
public List<object> Args { get; set; }
|
2022-06-19 11:12:20 +08:00
|
|
|
|
|
|
|
|
|
public override void Pack(NetOutgoingMessage message)
|
|
|
|
|
{
|
2022-06-23 14:10:16 +08:00
|
|
|
|
Args= Args ?? new List<object>(0);
|
2022-07-01 12:22:31 +08:00
|
|
|
|
message.Write((byte)PacketType.CustomEvent);
|
2022-06-19 11:12:20 +08:00
|
|
|
|
|
|
|
|
|
List<byte> result = new List<byte>();
|
|
|
|
|
result.AddInt(Hash);
|
|
|
|
|
result.AddInt(Args.Count);
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message.Write(result.Count);
|
|
|
|
|
message.Write(result.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Unpack(byte[] array)
|
|
|
|
|
{
|
|
|
|
|
BitReader reader = new BitReader(array);
|
|
|
|
|
|
|
|
|
|
Hash = reader.ReadInt();
|
|
|
|
|
var len=reader.ReadInt();
|
2022-06-23 09:46:38 +08:00
|
|
|
|
Args=new List<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:
|
|
|
|
|
Args.Add(reader.ReadByte());
|
|
|
|
|
break;
|
|
|
|
|
case 0x02:
|
|
|
|
|
Args.Add(reader.ReadShort());
|
|
|
|
|
break;
|
|
|
|
|
case 0x03:
|
|
|
|
|
Args.Add(reader.ReadUShort());
|
|
|
|
|
break;
|
|
|
|
|
case 0x04:
|
|
|
|
|
Args.Add(reader.ReadInt());
|
|
|
|
|
break;
|
|
|
|
|
case 0x05:
|
|
|
|
|
Args.Add(reader.ReadUInt());
|
|
|
|
|
break;
|
|
|
|
|
case 0x06:
|
|
|
|
|
Args.Add(reader.ReadLong());
|
|
|
|
|
break;
|
|
|
|
|
case 0x07:
|
|
|
|
|
Args.Add(reader.ReadULong());
|
|
|
|
|
break;
|
|
|
|
|
case 0x08:
|
|
|
|
|
Args.Add(reader.ReadFloat());
|
|
|
|
|
break;
|
|
|
|
|
case 0x09:
|
|
|
|
|
Args.Add(reader.ReadBool());
|
|
|
|
|
break;
|
|
|
|
|
case 0x10:
|
|
|
|
|
Args.Add(reader.ReadString());
|
|
|
|
|
break;
|
2022-06-23 09:46:38 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new InvalidOperationException($"Unexpected type:{type}\r\n{array.Dump()}");
|
|
|
|
|
|
2022-06-19 11:12:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|