From 4ec09f27e2363af009286f2c56ca1d73682b031c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 20 Aug 2016 19:03:21 +1000 Subject: [PATCH] Move more Packet code into separate class. --- Network/Packet.CPE.cs | 16 +++++++++++++ Network/Packet.cs | 7 ++++++ Network/Player.Networking.cs | 25 +------------------- Player/Player.Handlers.cs | 6 ++--- Player/Player.Login.cs | 44 ++++++++++++++++++------------------ Player/Player.cs | 3 ++- 6 files changed, 51 insertions(+), 50 deletions(-) diff --git a/Network/Packet.CPE.cs b/Network/Packet.CPE.cs index 5f50f88b6..428c7e61d 100644 --- a/Network/Packet.CPE.cs +++ b/Network/Packet.CPE.cs @@ -21,6 +21,22 @@ namespace MCGalaxy { public static partial class Packet { + public static byte[] MakeExtInfo(byte count) { + byte[] buffer = new byte[67]; + buffer[0] = Opcode.CpeExtInfo; + NetUtils.WriteAscii("MCGalaxy " + Server.Version, buffer, 1); + NetUtils.WriteI16((short)count, buffer, 65); + return buffer; + } + + public static byte[] MakeExtEntry(string name, int version) { + byte[] buffer = new byte[69]; + buffer[0] = Opcode.CpeExtEntry; + NetUtils.WriteAscii(name, buffer, 1); + NetUtils.WriteI32(version, buffer, 65); + return buffer; + } + public static byte[] MakeClickDistance(short distance) { byte[] buffer = new byte[3]; buffer[0] = Opcode.CpeSetClickDistance; diff --git a/Network/Packet.cs b/Network/Packet.cs index 6cbe22df2..43fe431da 100644 --- a/Network/Packet.cs +++ b/Network/Packet.cs @@ -50,5 +50,12 @@ namespace MCGalaxy { buffer[1] = Block.canPlace(p, Block.blackrock) ? (byte)100 : (byte)0; return buffer; } + + public static byte[] MakeKick(string message, bool cp437) { + byte[] buffer = new byte[65]; + buffer[0] = Opcode.Kick; + NetUtils.Write(message, buffer, 1, cp437); + return buffer; + } } } diff --git a/Network/Player.Networking.cs b/Network/Player.Networking.cs index f29a16b26..2259b5209 100644 --- a/Network/Player.Networking.cs +++ b/Network/Player.Networking.cs @@ -329,7 +329,7 @@ namespace MCGalaxy { SendRaw(Opcode.LevelInitialise); using (LevelChunkStream s = new LevelChunkStream(this)) - LevelChunkStream.CompressMap(this, s); + LevelChunkStream.CompressMap(this, s); byte[] buffer = new byte[7]; buffer[0] = Opcode.LevelFinalise; @@ -476,29 +476,6 @@ namespace MCGalaxy { } Send(buffer); } - - void SendKick(string message, bool sync) { - byte[] buffer = new byte[65]; - buffer[0] = Opcode.Kick; - NetUtils.Write(message, buffer, 1, HasCpeExt(CpeExt.FullCP437)); - Send(buffer, sync); - } - - void SendExtInfo( byte count ) { - byte[] buffer = new byte[67]; - buffer[0] = Opcode.CpeExtInfo; - NetUtils.WriteAscii("MCGalaxy " + Server.Version, buffer, 1); - NetUtils.WriteI16((short)count, buffer, 65); - Send(buffer, true); - } - - void SendExtEntry( string name, int version ) { - byte[] buffer = new byte[69]; - buffer[0] = Opcode.CpeExtEntry; - NetUtils.WriteAscii(name, buffer, 1); - NetUtils.WriteI32(version, buffer, 65); - Send(buffer, true); - } public void SendExtAddEntity(byte id, string name, string displayname = "") { byte[] buffer = new byte[130]; diff --git a/Player/Player.Handlers.cs b/Player/Player.Handlers.cs index 79bf3f9d8..3a44bbd2e 100644 --- a/Player/Player.Handlers.cs +++ b/Player/Player.Handlers.cs @@ -211,7 +211,7 @@ namespace MCGalaxy { case Opcode.CpeCustomBlockSupportLevel: return 2; default: if (!dontmindme) - Leave("Unhandled message id \"" + buffer[0] + "\"!", true); + Leave("Unhandled message id \"" + buffer[0] + "\"!", true); else Server.s.Log(Encoding.UTF8.GetString(buffer, 0, buffer.Length)); return -1; @@ -219,7 +219,7 @@ namespace MCGalaxy { } void HandlePacket(byte[] buffer) { - switch (buffer[0]) { + switch (buffer[0]) { case Opcode.Handshake: HandleLogin(buffer); break; case Opcode.SetBlockClient: @@ -242,7 +242,7 @@ namespace MCGalaxy { void HandleBlockchange(byte[] packet) { try { - if (!loggedIn || CheckBlockSpam()) return; + if (!loggedIn || CheckBlockSpam()) return; ushort x = NetUtils.ReadU16(packet, 1); ushort y = NetUtils.ReadU16(packet, 3); ushort z = NetUtils.ReadU16(packet, 5); diff --git a/Player/Player.Login.cs b/Player/Player.Login.cs index 4b8426910..ac015e347 100644 --- a/Player/Player.Login.cs +++ b/Player/Player.Login.cs @@ -157,34 +157,34 @@ namespace MCGalaxy { } void SendCpeExtensions() { - SendExtInfo(21); - SendExtEntry(CpeExt.ClickDistance, 1); - SendExtEntry(CpeExt.CustomBlocks, 1); - SendExtEntry(CpeExt.HeldBlock, 1); + Send(Packet.MakeExtInfo(21), true); + Send(Packet.MakeExtEntry(CpeExt.ClickDistance, 1), true); + Send(Packet.MakeExtEntry(CpeExt.CustomBlocks, 1), true); + Send(Packet.MakeExtEntry(CpeExt.HeldBlock, 1), true); - SendExtEntry(CpeExt.TextHotkey, 1); - SendExtEntry(CpeExt.EnvColors, 1); - SendExtEntry(CpeExt.SelectionCuboid, 1); + Send(Packet.MakeExtEntry(CpeExt.TextHotkey, 1), true); + Send(Packet.MakeExtEntry(CpeExt.EnvColors, 1), true); + Send(Packet.MakeExtEntry(CpeExt.SelectionCuboid, 1), true); - SendExtEntry(CpeExt.BlockPermissions, 1); - SendExtEntry(CpeExt.ChangeModel, 1); - SendExtEntry(CpeExt.EnvMapAppearance, 2); + Send(Packet.MakeExtEntry(CpeExt.BlockPermissions, 1), true); + Send(Packet.MakeExtEntry(CpeExt.ChangeModel, 1), true); + Send(Packet.MakeExtEntry(CpeExt.EnvMapAppearance, 2), true); - SendExtEntry(CpeExt.EnvWeatherType, 1); - SendExtEntry(CpeExt.HackControl, 1); - SendExtEntry(CpeExt.EmoteFix, 1); + Send(Packet.MakeExtEntry(CpeExt.EnvWeatherType, 1), true); + Send(Packet.MakeExtEntry(CpeExt.HackControl, 1), true); + Send(Packet.MakeExtEntry(CpeExt.EmoteFix, 1), true); - SendExtEntry(CpeExt.FullCP437, 1); - SendExtEntry(CpeExt.LongerMessages, 1); - SendExtEntry(CpeExt.BlockDefinitions, 1); + Send(Packet.MakeExtEntry(CpeExt.FullCP437, 1), true); + Send(Packet.MakeExtEntry(CpeExt.LongerMessages, 1), true); + Send(Packet.MakeExtEntry(CpeExt.BlockDefinitions, 1), true); - SendExtEntry(CpeExt.BlockDefinitionsExt, 2); - SendExtEntry(CpeExt.TextColors, 1); - SendExtEntry(CpeExt.BulkBlockUpdate, 1); + Send(Packet.MakeExtEntry(CpeExt.BlockDefinitionsExt, 2), true); + Send(Packet.MakeExtEntry(CpeExt.TextColors, 1), true); + Send(Packet.MakeExtEntry(CpeExt.BulkBlockUpdate, 1), true); - SendExtEntry(CpeExt.MessageTypes, 1); - SendExtEntry(CpeExt.ExtPlayerList, 2); - SendExtEntry(CpeExt.EnvMapAspect, 1); + Send(Packet.MakeExtEntry(CpeExt.MessageTypes, 1), true); + Send(Packet.MakeExtEntry(CpeExt.ExtPlayerList, 2), true); + Send(Packet.MakeExtEntry(CpeExt.EnvMapAspect, 1), true); } bool CheckWhitelist() { diff --git a/Player/Player.cs b/Player/Player.cs index 5ea5c7d7e..4faded867 100644 --- a/Player/Player.cs +++ b/Player/Player.cs @@ -279,7 +279,8 @@ namespace MCGalaxy { isFlying = false; aiming = false; - SendKick(kickMsg, sync); + bool cp437 = HasCpeExt(CpeExt.FullCP437); + Send(Packet.MakeKick(kickMsg, cp437), sync); disconnected = true; if (!loggedIn) { connections.Remove(this);