mirror of
https://github.com/MeowCraftMC/CrystalConnector
synced 2025-05-22 02:43:00 +00:00
* 🚧 WIP v2. Signed-off-by: 秋雨落 <i@rain.cx> * 🚧 WIP v2 * 2. Signed-off-by: 秋雨落 <i@rain.cx> * ✨ It works? Signed-off-by: 秋雨落 <i@rain.cx> * ✨ Publish to nexus. Signed-off-by: 秋雨落 <i@rain.cx> --------- Signed-off-by: 秋雨落 <i@rain.cx>
50 lines
1.1 KiB
C#
50 lines
1.1 KiB
C#
using CrystalConnector.Protocol.Packets.C2S;
|
|
using CrystalConnector.Protocol.Packets.S2C;
|
|
using Google.Protobuf;
|
|
|
|
namespace CrystalConnector.Protocol.Packets;
|
|
|
|
public interface IPacket
|
|
{
|
|
public void Read(byte[] bytes);
|
|
|
|
public byte[] Write();
|
|
|
|
public static IPacket? From(byte[] data)
|
|
{
|
|
var result = TryForm<AuthenticatePacket>(data);
|
|
if (result is not null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = TryForm<PublishPacket>(data);
|
|
if (result is not null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = TryForm<ForwardPacket>(data);
|
|
if (result is not null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = TryForm<ResultPacket>(data);
|
|
return result;
|
|
}
|
|
|
|
private static IPacket? TryForm<T>(byte[] data) where T : IPacket, new()
|
|
{
|
|
try
|
|
{
|
|
var message = new T();
|
|
message.Read(data);
|
|
return message;
|
|
}
|
|
catch (InvalidProtocolBufferException ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
} |