Files
test/Assets/PerfectWorld/Scripts/Chat/CHAT_S2C.cs
T
2026-02-27 18:14:01 +07:00

150 lines
5.1 KiB
C#

using System;
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using BrewMonster.Scripts.Managers;
using CSNetwork;
using CSNetwork.GPDataType;
namespace BrewMonster.Scripts.Chat
{
public struct chat_item_base
{
public short cmd_id;
};
public struct chat_equip_item
{
public short cmd_id;
public int type;
public int expire_date;
public int proc_type;
public ushort content_length;
public byte[] content;
public int LenghtHeader()
{
return Marshal.SizeOf<short>()+ (Marshal.SizeOf<int>() * 3) + Marshal.SizeOf<ushort>();
}
}
struct chat_generalcard_collection
{
public short cmd_id;
public int card_id;
};
public static class CHAT_S2C
{
public enum EChatS2CCommand : short
{
CHAT_EQUIP_ITEM,
CHAT_GENERALCARD_COLLECTION,
CHAT_POLICYCHAT_PARAMETER,
}
public static EC_IvtrItem CreateChatItem(Octets data)
{
EC_IvtrItem pIvtrItem = null;
if (data.Size > 0)
{
chat_item_base pInfo = GPDataTypeHelper.FromBytes<chat_item_base>(data.ByteArray);
if (pInfo.cmd_id == (short)EChatS2CCommand.CHAT_EQUIP_ITEM)
{
chat_equip_item pItemInfo = default;
var sz = pItemInfo.LenghtHeader();
var newByte = GPDataTypeHelper.ContentBytes<short>(data.Size, 0);
pItemInfo.cmd_id = GPDataTypeHelper.FromBytes<short>(newByte);
newByte = GPDataTypeHelper.ContentBytes<int>(data.Size, 2);
pItemInfo.type = GPDataTypeHelper.FromBytes<int>(newByte);
newByte = GPDataTypeHelper.ContentBytes<int>(data.Size, 6);
pItemInfo.expire_date = GPDataTypeHelper.FromBytes<int>(newByte);
newByte = GPDataTypeHelper.ContentBytes<int>(data.Size, 10);
pItemInfo.proc_type = GPDataTypeHelper.FromBytes<int>(newByte);
newByte = GPDataTypeHelper.ContentBytes<int>(data.Size, (10 + sizeof(ushort)));
pItemInfo.content_length = GPDataTypeHelper.FromBytes<ushort>(newByte);
if (data.Size >= sz && sz + pItemInfo.content_length == data.Size)
{
if (pItemInfo.cmd_id == (short)EChatS2CCommand.CHAT_EQUIP_ITEM)
{
pIvtrItem = EC_IvtrItem.CreateItem(pItemInfo.type, pItemInfo.expire_date, 1);
if (pIvtrItem != null)
{
pIvtrItem.SetProcType(pItemInfo.proc_type);
pIvtrItem.SetItemInfo(pItemInfo.content, pItemInfo.content_length);
}
}
}
}
else if(pInfo.cmd_id == (short)EChatS2CCommand.CHAT_GENERALCARD_COLLECTION)
{
chat_generalcard_collection pItemInfo = GPDataTypeHelper.FromBytes<chat_generalcard_collection>(data.ByteArray);
if (data.Size > Marshal.SizeOf<chat_generalcard_collection>())
{
if (pItemInfo.cmd_id == (short)EChatS2CCommand.CHAT_GENERALCARD_COLLECTION){
pIvtrItem = EC_IvtrItem.CreateItem(pItemInfo.card_id, 0, 1);
if (pIvtrItem != null){
pIvtrItem.GetDetailDataFromLocal();
}
}
}
}
}
return pIvtrItem;
}
}
public ref struct PacketReader
{
private ReadOnlySpan<byte> _buffer;
private int _offset;
public PacketReader(ReadOnlySpan<byte> buffer)
{
_buffer = buffer;
_offset = 0;
}
public int Remaining => _buffer.Length - _offset;
public short ReadInt16()
{
Ensure(2);
short value = BinaryPrimitives.ReadInt16LittleEndian(_buffer.Slice(_offset));
_offset += 2;
return value;
}
public ushort ReadUInt16()
{
Ensure(2);
ushort value = BinaryPrimitives.ReadUInt16LittleEndian(_buffer.Slice(_offset));
_offset += 2;
return value;
}
public int ReadInt32()
{
Ensure(4);
int value = BinaryPrimitives.ReadInt32LittleEndian(_buffer.Slice(_offset));
_offset += 4;
return value;
}
public ReadOnlySpan<byte> ReadBytes(int count)
{
Ensure(count);
var span = _buffer.Slice(_offset, count);
_offset += count;
return span;
}
private void Ensure(int size)
{
if (Remaining < size)
throw new InvalidOperationException("Packet out of range");
}
}
}