Implement extended player positions

This commit is contained in:
UnknownShadow200 2017-04-14 17:17:26 +10:00
parent 913e98926e
commit b90090d6b5
8 changed files with 103 additions and 71 deletions

View File

@ -43,7 +43,7 @@ namespace ClassicalSharp.Network {
} else if (ext == "EnvMapAppearance") { } else if (ext == "EnvMapAppearance") {
envMapVer = version; envMapVer = version;
if (version == 1) return; if (version == 1) return;
net.packetSizes[(byte)Opcode.CpeEnvSetMapApperance] = 73; net.packetSizes[Opcode.CpeEnvSetMapApperance] += 4;
} else if (ext == "LongerMessages") { } else if (ext == "LongerMessages") {
net.SupportsPartialMessages = true; net.SupportsPartialMessages = true;
} else if (ext == "FullCP437") { } else if (ext == "FullCP437") {
@ -51,10 +51,15 @@ namespace ClassicalSharp.Network {
} else if (ext == "BlockDefinitionsExt") { } else if (ext == "BlockDefinitionsExt") {
blockDefsExtVer = version; blockDefsExtVer = version;
if (version == 1) return; if (version == 1) return;
net.packetSizes[(byte)Opcode.CpeDefineBlockExt] = 88; net.packetSizes[Opcode.CpeDefineBlockExt] += 3;
} else if (ext == "ExtEntityPositions") { } else if (ext == "ExtEntityPositions") {
//extEntityPos = true; extEntityPos = true;
// TODO: need to increase packet sizes accordingly net.packetSizes[Opcode.EntityTeleport] += 6;
net.packetSizes[Opcode.AddEntity] += 6;
net.packetSizes[Opcode.CpeExtAddEntity2] += 6;
net.reader.ExtendedPositions = true;
net.writer.ExtendedPositions = true;
} }
} }

View File

@ -3,51 +3,51 @@ using System;
namespace ClassicalSharp.Network { namespace ClassicalSharp.Network {
public enum Opcode { public static class Opcode {
Handshake = 0, public const byte Handshake = 0;
Ping = 1, public const byte Ping = 1;
LevelInit = 2, public const byte LevelInit = 2;
LevelDataChunk = 3, public const byte LevelDataChunk = 3;
LevelFinalise = 4, public const byte LevelFinalise = 4;
SetBlockClient = 5, public const byte SetBlockClient = 5;
SetBlock = 6, public const byte SetBlock = 6;
AddEntity = 7, public const byte AddEntity = 7;
EntityTeleport = 8, public const byte EntityTeleport = 8;
RelPosAndOrientationUpdate = 9, public const byte RelPosAndOrientationUpdate = 9;
RelPosUpdate = 10, public const byte RelPosUpdate = 10;
OrientationUpdate = 11, public const byte OrientationUpdate = 11;
RemoveEntity = 12, public const byte RemoveEntity = 12;
Message = 13, public const byte Message = 13;
Kick = 14, public const byte Kick = 14;
SetPermission = 15, public const byte SetPermission = 15;
CpeExtInfo = 16, public const byte CpeExtInfo = 16;
CpeExtEntry = 17, public const byte CpeExtEntry = 17;
CpeSetClickDistance = 18, public const byte CpeSetClickDistance = 18;
CpeCustomBlockSupportLevel = 19, public const byte CpeCustomBlockSupportLevel = 19;
CpeHoldThis = 20, public const byte CpeHoldThis = 20;
CpeSetTextHotkey = 21, public const byte CpeSetTextHotkey = 21;
CpeExtAddPlayerName = 22, public const byte CpeExtAddPlayerName = 22;
CpeExtAddEntity = 23, public const byte CpeExtAddEntity = 23;
CpeExtRemovePlayerName = 24, public const byte CpeExtRemovePlayerName = 24;
CpeEnvColours = 25, public const byte CpeEnvColours = 25;
CpeMakeSelection = 26, public const byte CpeMakeSelection = 26;
CpeRemoveSelection = 27, public const byte CpeRemoveSelection = 27;
CpeSetBlockPermission = 28, public const byte CpeSetBlockPermission = 28;
CpeChangeModel = 29, public const byte CpeChangeModel = 29;
CpeEnvSetMapApperance = 30, public const byte CpeEnvSetMapApperance = 30;
CpeEnvWeatherType = 31, public const byte CpeEnvWeatherType = 31;
CpeHackControl = 32, public const byte CpeHackControl = 32;
CpeExtAddEntity2 = 33, public const byte CpeExtAddEntity2 = 33;
CpePlayerClick = 34, public const byte CpePlayerClick = 34;
CpeDefineBlock = 35, public const byte CpeDefineBlock = 35;
CpeRemoveBlockDefinition = 36, public const byte CpeRemoveBlockDefinition = 36;
CpeDefineBlockExt = 37, public const byte CpeDefineBlockExt = 37;
CpeBulkBlockUpdate = 38, public const byte CpeBulkBlockUpdate = 38;
CpeSetTextColor = 39, public const byte CpeSetTextColor = 39;
CpeSetMapEnvUrl = 40, public const byte CpeSetMapEnvUrl = 40;
CpeSetMapEnvProperty = 41, public const byte CpeSetMapEnvProperty = 41;
CpeSetEntityProperty = 42, public const byte CpeSetEntityProperty = 42;
} }
} }

View File

@ -30,7 +30,7 @@ namespace ClassicalSharp.Network {
Socket socket; Socket socket;
DateTime lastPacket; DateTime lastPacket;
Opcode lastOpcode; byte lastOpcode;
internal NetReader reader; internal NetReader reader;
internal NetWriter writer; internal NetWriter writer;
@ -135,10 +135,10 @@ namespace ClassicalSharp.Network {
} }
/// <summary> Sets the incoming packet handler for the given packet id. </summary> /// <summary> Sets the incoming packet handler for the given packet id. </summary>
public void Set(Opcode opcode, Action handler, int packetSize) { public void Set(byte opcode, Action handler, int packetSize) {
handlers[(byte)opcode] = handler; handlers[opcode] = handler;
packetSizes[(byte)opcode] = (ushort)packetSize; packetSizes[opcode] = (ushort)packetSize;
maxHandledPacket = Math.Max((byte)opcode, maxHandledPacket); maxHandledPacket = Math.Max(opcode, maxHandledPacket);
} }
internal void SendPacket() { internal void SendPacket() {
@ -156,17 +156,17 @@ namespace ClassicalSharp.Network {
void ReadPacket(byte opcode) { void ReadPacket(byte opcode) {
reader.Skip(1); // remove opcode reader.Skip(1); // remove opcode
lastOpcode = (Opcode)opcode; lastOpcode = opcode;
Action handler = handlers[opcode]; Action handler = handlers[opcode];
lastPacket = DateTime.UtcNow; lastPacket = DateTime.UtcNow;
if (handler == null) if (handler == null)
throw new NotImplementedException("Unsupported packet:" + (Opcode)opcode); throw new NotImplementedException("Unsupported packet:" + opcode);
handler(); handler();
} }
internal void SkipPacketData(Opcode opcode) { internal void SkipPacketData(byte opcode) {
reader.Skip(packetSizes[(byte)opcode] - 1); reader.Skip(packetSizes[opcode] - 1);
} }
internal void Reset() { internal void Reset() {
@ -176,11 +176,17 @@ namespace ClassicalSharp.Network {
SupportsFullCP437 = false; SupportsFullCP437 = false;
addEntityHack = true; addEntityHack = true;
for (int i = 0; i < handlers.Length; i++) for (int i = 0; i < handlers.Length; i++) {
handlers[i] = null; handlers[i] = null;
packetSizes[i] = 0;
}
packetSizes[(byte)Opcode.CpeEnvSetMapApperance] = 69; classic.Reset();
packetSizes[(byte)Opcode.CpeDefineBlockExt] = 85; cpe.Reset();
cpeBlockDefs.Reset();
reader.ExtendedPositions = false;
writer.ExtendedPositions = false;
} }
internal Action[] handlers = new Action[256]; internal Action[] handlers = new Action[256];

View File

@ -9,7 +9,9 @@ namespace ClassicalSharp.Network.Protocols {
public CPEProtocolBlockDefs(Game game) : base(game) { } public CPEProtocolBlockDefs(Game game) : base(game) { }
public override void Init() { public override void Init() { Reset(); }
public override void Reset() {
if (!game.UseCPE || !game.AllowCustomBlocks) return; if (!game.UseCPE || !game.AllowCustomBlocks) return;
net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80); net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80);
net.Set(Opcode.CpeRemoveBlockDefinition, HandleRemoveBlockDefinition, 2); net.Set(Opcode.CpeRemoveBlockDefinition, HandleRemoveBlockDefinition, 2);

View File

@ -19,7 +19,9 @@ namespace ClassicalSharp.Network.Protocols {
public CPEProtocol(Game game) : base(game) { } public CPEProtocol(Game game) : base(game) { }
public override void Init() { public override void Init() { Reset(); }
public override void Reset() {
if (!game.UseCPE) return; if (!game.UseCPE) return;
net.Set(Opcode.CpeExtInfo, HandleExtInfo, 67); net.Set(Opcode.CpeExtInfo, HandleExtInfo, 67);
net.Set(Opcode.CpeExtEntry, HandleExtEntry, 69); net.Set(Opcode.CpeExtEntry, HandleExtEntry, 69);

View File

@ -24,6 +24,10 @@ namespace ClassicalSharp.Network.Protocols {
public override void Init() { public override void Init() {
gzippedMap = new FixedBufferStream(net.reader.buffer); gzippedMap = new FixedBufferStream(net.reader.buffer);
Reset();
}
public override void Reset() {
net.Set(Opcode.Handshake, HandleHandshake, 131); net.Set(Opcode.Handshake, HandleHandshake, 131);
net.Set(Opcode.Ping, HandlePing, 1); net.Set(Opcode.Ping, HandlePing, 1);
net.Set(Opcode.LevelInit, HandleLevelInit, 1); net.Set(Opcode.LevelInit, HandleLevelInit, 1);

View File

@ -10,6 +10,7 @@ namespace ClassicalSharp.Network {
public byte[] buffer = new byte[4096 * 5]; public byte[] buffer = new byte[4096 * 5];
public int index = 0, size = 0; public int index = 0, size = 0;
public bool ExtendedPositions;
Socket socket; Socket socket;
public NetReader(Socket socket) { public NetReader(Socket socket) {
@ -75,11 +76,16 @@ namespace ClassicalSharp.Network {
} }
public Vector3 ReadPosition(byte id) { public Vector3 ReadPosition(byte id) {
float x = ReadInt16() / 32f; int x = 0, y = 0, z = 0;
float y = (ReadInt16() - 51) / 32f; // We have to do this. if (ExtendedPositions) {
if (id == EntityList.SelfID) y += 22/32f; x = ReadInt32(); y = ReadInt32(); z = ReadInt32();
float z = ReadInt16() / 32f; } else {
return new Vector3(x, y, z); x = ReadInt16(); y = ReadInt16(); z = ReadInt16();
}
float yAdj = (y - 51) / 32f; // We have to do this.
if (id == EntityList.SelfID) yAdj += 22/32f;
return new Vector3(x / 32f, yAdj, z / 32f);
} }
public string ReadString() { public string ReadString() {

View File

@ -9,6 +9,7 @@ namespace ClassicalSharp.Network {
public byte[] buffer = new byte[131]; public byte[] buffer = new byte[131];
public int index = 0; public int index = 0;
public bool ExtendedPositions;
Socket socket; Socket socket;
public NetWriter(Socket socket) { public NetWriter(Socket socket) {
@ -39,10 +40,16 @@ namespace ClassicalSharp.Network {
} }
public void WritePosition(Vector3 pos) { public void WritePosition(Vector3 pos) {
if (ExtendedPositions) {
WriteInt32((int)(pos.X * 32));
WriteInt32((int)((int)(pos.Y * 32) + 51));
WriteInt32((int)(pos.Z * 32));
} else {
WriteInt16((short)(pos.X * 32)); WriteInt16((short)(pos.X * 32));
WriteInt16((short)((int)(pos.Y * 32) + 51)); WriteInt16((short)((int)(pos.Y * 32) + 51));
WriteInt16((short)(pos.Z * 32)); WriteInt16((short)(pos.Z * 32));
} }
}
public void WriteUInt8(byte value) { public void WriteUInt8(byte value) {
buffer[index++] = value; buffer[index++] = value;