|
|
@@ -1,8 +1,11 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
+using System.Reflection;
|
|
|
using System.Text;
|
|
|
-using MineCase.Protocol.Handshaking;
|
|
|
+using MineCase.Protocol.Protocol.Handshaking.Server;
|
|
|
+using MineCase.Protocol.Protocol.Login.Client;
|
|
|
+using MineCase.Protocol.Protocol.Login.Server;
|
|
|
using MineCase.Protocol.Protocol.Status.Client;
|
|
|
using MineCase.Protocol.Protocol.Status.Server;
|
|
|
|
|
|
@@ -33,46 +36,29 @@ namespace MineCase.Protocol.Protocol
|
|
|
|
|
|
public class PacketInfo
|
|
|
{
|
|
|
- private Dictionary<Type, int> _typeIdPairs = new Dictionary<Type, int>();
|
|
|
- private Dictionary<Type, ProtocolType> _idProtocolTypePairs = new Dictionary<Type, ProtocolType>();
|
|
|
- private Dictionary<Type, PacketDirection> _idPacketDirectionPairs = new Dictionary<Type, PacketDirection>();
|
|
|
-
|
|
|
public PacketInfo()
|
|
|
{
|
|
|
- _typeIdPairs.Add(typeof(Request), 0x00);
|
|
|
- _idProtocolTypePairs.Add(typeof(Request), ProtocolType.Status);
|
|
|
- _idPacketDirectionPairs.Add(typeof(Request), PacketDirection.ServerBound);
|
|
|
-
|
|
|
- _typeIdPairs.Add(typeof(Response), 0x00);
|
|
|
- _idProtocolTypePairs.Add(typeof(Response), ProtocolType.Status);
|
|
|
- _idPacketDirectionPairs.Add(typeof(Response), PacketDirection.ClientBound);
|
|
|
-
|
|
|
- _typeIdPairs.Add(typeof(Ping), 0x01);
|
|
|
- _idProtocolTypePairs.Add(typeof(Ping), ProtocolType.Status);
|
|
|
- _idPacketDirectionPairs.Add(typeof(Ping), PacketDirection.ServerBound);
|
|
|
-
|
|
|
- _typeIdPairs.Add(typeof(Pong), 0x01);
|
|
|
- _idProtocolTypePairs.Add(typeof(Pong), ProtocolType.Status);
|
|
|
- _idPacketDirectionPairs.Add(typeof(Pong), PacketDirection.ClientBound);
|
|
|
-
|
|
|
- _typeIdPairs.Add(typeof(Handshake), 0x00);
|
|
|
- _idProtocolTypePairs.Add(typeof(Handshake), ProtocolType.Handshake);
|
|
|
- _idPacketDirectionPairs.Add(typeof(Handshake), PacketDirection.ServerBound);
|
|
|
}
|
|
|
|
|
|
public int GetPacketId(ISerializablePacket packet)
|
|
|
{
|
|
|
- return _typeIdPairs.GetValueOrDefault(packet.GetType(), -1);
|
|
|
+ var typeInfo = packet.GetType().GetTypeInfo();
|
|
|
+ var attr = typeInfo.GetCustomAttribute<PacketAttribute>();
|
|
|
+ return (int)attr.PacketId;
|
|
|
}
|
|
|
|
|
|
public PacketDirection GetPacketDirection(ISerializablePacket packet)
|
|
|
{
|
|
|
- return _idPacketDirectionPairs[packet.GetType()];
|
|
|
+ var typeInfo = packet.GetType().GetTypeInfo();
|
|
|
+ var attr = typeInfo.GetCustomAttribute<PacketAttribute>();
|
|
|
+ return attr.Direction;
|
|
|
}
|
|
|
|
|
|
public ProtocolType GetProtocolType(ISerializablePacket packet)
|
|
|
{
|
|
|
- return _idProtocolTypePairs[packet.GetType()];
|
|
|
+ var typeInfo = packet.GetType().GetTypeInfo();
|
|
|
+ var attr = typeInfo.GetCustomAttribute<PacketAttribute>();
|
|
|
+ return attr.PacketType;
|
|
|
}
|
|
|
|
|
|
public ISerializablePacket GetPacket(PacketDirection direction, ProtocolType protocolType, int id)
|
|
|
@@ -84,7 +70,7 @@ namespace MineCase.Protocol.Protocol
|
|
|
case ProtocolType.Handshake:
|
|
|
return null;
|
|
|
case ProtocolType.Login:
|
|
|
- return null;
|
|
|
+ return GetPacketClientLogin(id);
|
|
|
case ProtocolType.Play:
|
|
|
return null;
|
|
|
case ProtocolType.Status:
|
|
|
@@ -100,7 +86,7 @@ namespace MineCase.Protocol.Protocol
|
|
|
case ProtocolType.Handshake:
|
|
|
return GetPacketServerHandshake(id);
|
|
|
case ProtocolType.Login:
|
|
|
- return null;
|
|
|
+ return GetPacketServerLogin(id);
|
|
|
case ProtocolType.Play:
|
|
|
return null;
|
|
|
case ProtocolType.Status:
|
|
|
@@ -154,5 +140,39 @@ namespace MineCase.Protocol.Protocol
|
|
|
throw new InvalidDataException($"Unrecognizable packet id: 0x{id:X2}.");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public ISerializablePacket GetPacketServerLogin(int id)
|
|
|
+ {
|
|
|
+ switch (id)
|
|
|
+ {
|
|
|
+ case 0x00:
|
|
|
+ return new LoginStart();
|
|
|
+ case 0x01:
|
|
|
+ return new EncryptionResponse();
|
|
|
+ case 0x02:
|
|
|
+ return new LoginPluginResponse();
|
|
|
+ default:
|
|
|
+ throw new InvalidDataException($"Unrecognizable packet id: 0x{id:X2}.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ISerializablePacket GetPacketClientLogin(int id)
|
|
|
+ {
|
|
|
+ switch (id)
|
|
|
+ {
|
|
|
+ case 0x00:
|
|
|
+ return new LoginDisconnect();
|
|
|
+ case 0x01:
|
|
|
+ return new EncryptionRequest();
|
|
|
+ case 0x02:
|
|
|
+ return new LoginSuccess();
|
|
|
+ case 0x03:
|
|
|
+ return new SetCompression();
|
|
|
+ case 0x04:
|
|
|
+ return new LoginPluginRequest();
|
|
|
+ default:
|
|
|
+ throw new InvalidDataException($"Unrecognizable packet id: 0x{id:X2}.");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|