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") {
envMapVer = version;
if (version == 1) return;
net.packetSizes[(byte)Opcode.CpeEnvSetMapApperance] = 73;
net.packetSizes[Opcode.CpeEnvSetMapApperance] += 4;
} else if (ext == "LongerMessages") {
net.SupportsPartialMessages = true;
} else if (ext == "FullCP437") {
@ -51,10 +51,15 @@ namespace ClassicalSharp.Network {
} else if (ext == "BlockDefinitionsExt") {
blockDefsExtVer = version;
if (version == 1) return;
net.packetSizes[(byte)Opcode.CpeDefineBlockExt] = 88;
net.packetSizes[Opcode.CpeDefineBlockExt] += 3;
} else if (ext == "ExtEntityPositions") {
//extEntityPos = true;
// TODO: need to increase packet sizes accordingly
extEntityPos = true;
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 {
public enum Opcode {
Handshake = 0,
Ping = 1,
LevelInit = 2,
LevelDataChunk = 3,
LevelFinalise = 4,
SetBlockClient = 5,
SetBlock = 6,
AddEntity = 7,
EntityTeleport = 8,
RelPosAndOrientationUpdate = 9,
RelPosUpdate = 10,
OrientationUpdate = 11,
RemoveEntity = 12,
Message = 13,
Kick = 14,
SetPermission = 15,
CpeExtInfo = 16,
CpeExtEntry = 17,
CpeSetClickDistance = 18,
CpeCustomBlockSupportLevel = 19,
CpeHoldThis = 20,
CpeSetTextHotkey = 21,
CpeExtAddPlayerName = 22,
CpeExtAddEntity = 23,
CpeExtRemovePlayerName = 24,
CpeEnvColours = 25,
CpeMakeSelection = 26,
CpeRemoveSelection = 27,
CpeSetBlockPermission = 28,
CpeChangeModel = 29,
CpeEnvSetMapApperance = 30,
CpeEnvWeatherType = 31,
CpeHackControl = 32,
CpeExtAddEntity2 = 33,
CpePlayerClick = 34,
CpeDefineBlock = 35,
CpeRemoveBlockDefinition = 36,
CpeDefineBlockExt = 37,
CpeBulkBlockUpdate = 38,
CpeSetTextColor = 39,
CpeSetMapEnvUrl = 40,
CpeSetMapEnvProperty = 41,
CpeSetEntityProperty = 42,
public static class Opcode {
public const byte Handshake = 0;
public const byte Ping = 1;
public const byte LevelInit = 2;
public const byte LevelDataChunk = 3;
public const byte LevelFinalise = 4;
public const byte SetBlockClient = 5;
public const byte SetBlock = 6;
public const byte AddEntity = 7;
public const byte EntityTeleport = 8;
public const byte RelPosAndOrientationUpdate = 9;
public const byte RelPosUpdate = 10;
public const byte OrientationUpdate = 11;
public const byte RemoveEntity = 12;
public const byte Message = 13;
public const byte Kick = 14;
public const byte SetPermission = 15;
public const byte CpeExtInfo = 16;
public const byte CpeExtEntry = 17;
public const byte CpeSetClickDistance = 18;
public const byte CpeCustomBlockSupportLevel = 19;
public const byte CpeHoldThis = 20;
public const byte CpeSetTextHotkey = 21;
public const byte CpeExtAddPlayerName = 22;
public const byte CpeExtAddEntity = 23;
public const byte CpeExtRemovePlayerName = 24;
public const byte CpeEnvColours = 25;
public const byte CpeMakeSelection = 26;
public const byte CpeRemoveSelection = 27;
public const byte CpeSetBlockPermission = 28;
public const byte CpeChangeModel = 29;
public const byte CpeEnvSetMapApperance = 30;
public const byte CpeEnvWeatherType = 31;
public const byte CpeHackControl = 32;
public const byte CpeExtAddEntity2 = 33;
public const byte CpePlayerClick = 34;
public const byte CpeDefineBlock = 35;
public const byte CpeRemoveBlockDefinition = 36;
public const byte CpeDefineBlockExt = 37;
public const byte CpeBulkBlockUpdate = 38;
public const byte CpeSetTextColor = 39;
public const byte CpeSetMapEnvUrl = 40;
public const byte CpeSetMapEnvProperty = 41;
public const byte CpeSetEntityProperty = 42;
}
}

View File

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

View File

@ -9,7 +9,9 @@ namespace ClassicalSharp.Network.Protocols {
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;
net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80);
net.Set(Opcode.CpeRemoveBlockDefinition, HandleRemoveBlockDefinition, 2);

View File

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

View File

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

View File

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

View File

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