From d255cb1caf50c644336a88242d4179bfb03cee11 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 24 Apr 2018 15:21:38 +1000 Subject: [PATCH] move some classic/CPE protocol specific bits out of NetworkProcessor --- ClassicalSharp/Blocks/BlockInfo.cs | 11 +++- ClassicalSharp/Map/Formats/MapCw.Importer.cs | 13 +---- ClassicalSharp/Network/NetworkProcessor.cs | 36 +++++-------- ClassicalSharp/Network/Protocols/BlockDefs.cs | 17 ++----- ClassicalSharp/Network/Protocols/CPE.cs | 25 ++++++--- ClassicalSharp/Network/Protocols/Classic.cs | 39 +++++++------- ClassicalSharp/Network/Protocols/IProtocol.cs | 51 ++++++++----------- ClassicalSharp/Network/Protocols/WoM.cs | 12 +++-- src/Client/Block.c | 23 ++++++--- src/Client/Block.h | 2 +- src/Client/Formats.c | 13 +---- 11 files changed, 117 insertions(+), 125 deletions(-) diff --git a/ClassicalSharp/Blocks/BlockInfo.cs b/ClassicalSharp/Blocks/BlockInfo.cs index 096f025ba..2c628c8df 100644 --- a/ClassicalSharp/Blocks/BlockInfo.cs +++ b/ClassicalSharp/Blocks/BlockInfo.cs @@ -150,8 +150,17 @@ namespace ClassicalSharp { } } - public static void CalcIsTinted(BlockID block) { + public static void DefineCustom(Game game, BlockID block) { + SetBlockDraw(block, Draw[block]); + CalcRenderBounds(block); + UpdateCulling(block); + Tinted[block] = FogColour[block] != FastColour.Black && Name[block].IndexOf('#') >= 0; + CalcLightOffset(block); + + game.Inventory.AddDefault(block); + SetCustomDefined(block, true); + game.Events.RaiseBlockDefinitionChanged(); } static void RecalcIsLiquid(BlockID block) { diff --git a/ClassicalSharp/Map/Formats/MapCw.Importer.cs b/ClassicalSharp/Map/Formats/MapCw.Importer.cs index 0f66a3323..b833fcb10 100644 --- a/ClassicalSharp/Map/Formats/MapCw.Importer.cs +++ b/ClassicalSharp/Map/Formats/MapCw.Importer.cs @@ -158,6 +158,7 @@ namespace ClassicalSharp.Map { BlockInfo.SpriteOffset[id] = blockDraw; blockDraw = DrawType.Sprite; } + BlockInfo.Draw[id] = blockDraw; data = (byte[])compound["Fog"].Value; BlockInfo.FogDensity[id] = (data[0] + 1) / 128f; @@ -169,17 +170,7 @@ namespace ClassicalSharp.Map { BlockInfo.MinBB[id] = new Vector3(data[0] / 16f, data[1] / 16f, data[2] / 16f); BlockInfo.MaxBB[id] = new Vector3(data[3] / 16f, data[4] / 16f, data[5] / 16f); - BlockInfo.SetBlockDraw(id, blockDraw); - BlockInfo.CalcRenderBounds(id); - BlockInfo.UpdateCulling(id); - - BlockInfo.CalcIsTinted(id); - BlockInfo.CalcLightOffset(id); - - game.Inventory.AddDefault(id); - BlockInfo.SetCustomDefined(id, true); - game.Events.RaiseBlockDefinitionChanged(); - + BlockInfo.DefineCustom(game, id); BlockInfo.CanPlace[id] = true; BlockInfo.CanDelete[id] = true; game.Events.RaiseBlockPermissionsChanged(); diff --git a/ClassicalSharp/Network/NetworkProcessor.cs b/ClassicalSharp/Network/NetworkProcessor.cs index 94a3853c5..423ecbaae 100644 --- a/ClassicalSharp/Network/NetworkProcessor.cs +++ b/ClassicalSharp/Network/NetworkProcessor.cs @@ -34,9 +34,7 @@ namespace ClassicalSharp.Network { internal WoMProtocol wom; internal CPESupport cpeData; - internal bool receivedFirstPosition; internal byte[] needRemoveNames = new byte[256 >> 3]; - int pingTicks; public override void Connect(IPAddress address, int port) { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); @@ -54,16 +52,12 @@ namespace ClassicalSharp.Network { writer = new NetWriter(socket); classic = new ClassicProtocol(game); - classic.Init(); cpe = new CPEProtocol(game); - cpe.Init(); cpeBlockDefs = new CPEProtocolBlockDefs(game); - cpeBlockDefs.Init(); wom = new WoMProtocol(game); - wom.Init(); + ResetProtocols(); Disconnected = false; - receivedFirstPosition = false; lastPacket = DateTime.UtcNow; game.WorldEvents.OnNewMap += OnNewMap; game.UserEvents.BlockChanged += BlockChanged; @@ -128,18 +122,8 @@ namespace ClassicalSharp.Network { void CoreTick() { CheckAsyncResources(); wom.Tick(); - - if (receivedFirstPosition) { - LocalPlayer player = game.LocalPlayer; - classic.WritePosition(player.Position, player.HeadY, player.HeadX); - } - - pingTicks++; - if (pingTicks >= 20 && cpeData.twoWayPing) { - cpe.WriteTwoWayPing(false, PingList.NextTwoWayPingData()); - pingTicks = 0; - } - + classic.Tick(); + cpe.Tick(); if (writer.index > 0) SendPacket(); } @@ -190,17 +174,21 @@ namespace ClassicalSharp.Network { handlers[i] = null; packetSizes[i] = 0; } - if (classic == null) return; // null if no successful connection ever made before - - classic.Reset(); - cpe.Reset(); - cpeBlockDefs.Reset(); + ResetProtocols(); reader.ExtendedPositions = false; reader.ExtendedBlocks = false; writer.ExtendedPositions = false; writer.ExtendedBlocks = false; BlockInfo.SetMaxUsed(255); } + void ResetProtocols() { + if (classic == null) return; // null if no successful connection ever made before + classic.Reset(); + cpe.Reset(); + cpeBlockDefs.Reset(); + wom.Reset(); + } + internal Action[] handlers = new Action[256]; internal ushort[] packetSizes = new ushort[256]; int maxHandledPacket = 0; diff --git a/ClassicalSharp/Network/Protocols/BlockDefs.cs b/ClassicalSharp/Network/Protocols/BlockDefs.cs index 71a499b0f..dfc724281 100644 --- a/ClassicalSharp/Network/Protocols/BlockDefs.cs +++ b/ClassicalSharp/Network/Protocols/BlockDefs.cs @@ -10,14 +10,13 @@ namespace ClassicalSharp.Network.Protocols { public CPEProtocolBlockDefs(Game game) : base(game) { } - public override void Init() { Reset(); } - public override void Reset() { if (!game.UseCPE || !game.AllowCustomBlocks) return; net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80); net.Set(Opcode.CpeUndefineBlock, HandleRemoveBlockDefinition, 2); net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85); - } + } + public override void Tick() { } internal void HandleDefineBlock() { BlockID block = HandleDefineBlockCommonStart(reader, false); @@ -118,21 +117,13 @@ namespace ClassicalSharp.Network.Protocols { BlockInfo.SpriteOffset[block] = blockDraw; blockDraw = DrawType.Sprite; } + BlockInfo.Draw[block] = blockDraw; byte fogDensity = reader.ReadUInt8(); BlockInfo.FogDensity[block] = fogDensity == 0 ? 0 : (fogDensity + 1) / 128f; BlockInfo.FogColour[block] = new FastColour(reader.ReadUInt8(), reader.ReadUInt8(), reader.ReadUInt8()); - BlockInfo.SetBlockDraw(block, blockDraw); - BlockInfo.CalcRenderBounds(block); - BlockInfo.UpdateCulling(block); - - BlockInfo.CalcIsTinted(block); - BlockInfo.CalcLightOffset(block); - - game.Inventory.AddDefault(block); - BlockInfo.SetCustomDefined(block, true); - game.Events.RaiseBlockDefinitionChanged(); + BlockInfo.DefineCustom(game, block); } #if FALSE diff --git a/ClassicalSharp/Network/Protocols/CPE.cs b/ClassicalSharp/Network/Protocols/CPE.cs index c05c6b5df..aa2489ca3 100644 --- a/ClassicalSharp/Network/Protocols/CPE.cs +++ b/ClassicalSharp/Network/Protocols/CPE.cs @@ -13,11 +13,12 @@ namespace ClassicalSharp.Network.Protocols { public sealed class CPEProtocol : IProtocol { public CPEProtocol(Game game) : base(game) { } - - public override void Init() { Reset(); } - + int pingTicks; + public override void Reset() { + pingTicks = 0; if (!game.UseCPE) return; + net.Set(Opcode.CpeExtInfo, HandleExtInfo, 67); net.Set(Opcode.CpeExtEntry, HandleExtEntry, 69); net.Set(Opcode.CpeSetClickDistance, HandleSetClickDistance, 3); @@ -48,6 +49,14 @@ namespace ClassicalSharp.Network.Protocols { net.Set(Opcode.CpeSetInventoryOrder, HandleSetInventoryOrder, 3); } + public override void Tick() { + pingTicks++; + if (pingTicks >= 20 && net.cpeData.twoWayPing) { + WriteTwoWayPing(false, PingList.NextTwoWayPingData()); + pingTicks = 0; + } + } + #region Read void HandleExtInfo() { string appName = reader.ReadString(); @@ -173,11 +182,11 @@ namespace ClassicalSharp.Network.Protocols { void HandleEnvColours() { byte variable = reader.ReadUInt8(); - short red = reader.ReadInt16(); - short green = reader.ReadInt16(); - short blue = reader.ReadInt16(); - bool invalid = red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255; - FastColour col = new FastColour(red, green, blue); + short r = reader.ReadInt16(); + short g = reader.ReadInt16(); + short b = reader.ReadInt16(); + bool invalid = r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255; + FastColour col = new FastColour(r, g, b); if (variable == 0) { game.World.Env.SetSkyColour(invalid ? WorldEnv.DefaultSkyCol : col); diff --git a/ClassicalSharp/Network/Protocols/Classic.cs b/ClassicalSharp/Network/Protocols/Classic.cs index 50c3fa900..715954595 100644 --- a/ClassicalSharp/Network/Protocols/Classic.cs +++ b/ClassicalSharp/Network/Protocols/Classic.cs @@ -17,13 +17,20 @@ namespace ClassicalSharp.Network.Protocols { public sealed class ClassicProtocol : IProtocol { public ClassicProtocol(Game game) : base(game) { } - - public override void Init() { - mapPartStream = new FixedBufferStream(net.reader.buffer); - Reset(); - } + bool receivedFirstPosition; + DateTime mapReceiveStart; + DeflateStream gzipStream; + GZipHeaderReader gzipHeader; + int mapSizeIndex, mapIndex, mapVolume; + byte[] mapSize = new byte[4], map; + FixedBufferStream mapPartStream; + Screen prevScreen; + bool prevCursorVisible; public override void Reset() { + if (mapPartStream == null) mapPartStream = new FixedBufferStream(net.reader.buffer); + receivedFirstPosition = false; + net.Set(Opcode.Handshake, HandleHandshake, 131); net.Set(Opcode.Ping, HandlePing, 1); net.Set(Opcode.LevelInit, HandleLevelInit, 1); @@ -43,14 +50,12 @@ namespace ClassicalSharp.Network.Protocols { net.Set(Opcode.SetPermission, HandleSetPermission, 2); } - DateTime mapReceiveStart; - DeflateStream gzipStream; - GZipHeaderReader gzipHeader; - int mapSizeIndex, mapIndex, mapVolume; - byte[] mapSize = new byte[4], map; - FixedBufferStream mapPartStream; - Screen prevScreen; - bool prevCursorVisible; + public override void Tick() { + if (receivedFirstPosition) { + LocalPlayer player = game.LocalPlayer; + WritePosition(player.Position, player.HeadY, player.HeadX); + } + } #if !ONLY_8BIT DeflateStream gzipStream2; @@ -96,7 +101,7 @@ namespace ClassicalSharp.Network.Protocols { game.Gui.SetNewScreen(new LoadingMapScreen(game, net.ServerName, net.ServerMotd), false); net.wom.CheckMotd(); - net.receivedFirstPosition = false; + receivedFirstPosition = false; gzipHeader = new GZipHeaderReader(); // Workaround because built in mono stream assumes that the end of stream @@ -188,14 +193,14 @@ namespace ClassicalSharp.Network.Protocols { #if !ONLY_8BIT if (reader.ExtendedBlocks) { // defer allocation of scond map array if possible - game.World.blocks2 = map2 == null ? map : map2; + game.World.blocks2 = map2 == null ? map : map2; BlockInfo.SetMaxUsed(map2 == null ? 255 : 767); } #endif game.WorldEvents.RaiseOnNewMapLoaded(); net.wom.CheckSendWomID(); - map = null; + map = null; gzipStream.Dispose(); gzipStream = null; #if !ONLY_8BIT @@ -303,7 +308,7 @@ namespace ClassicalSharp.Network.Protocols { float rotY = (float)Utils.PackedToDegrees(reader.ReadUInt8()); float headX = (float)Utils.PackedToDegrees(reader.ReadUInt8()); - if (id == EntityList.SelfID) net.receivedFirstPosition = true; + if (id == EntityList.SelfID) receivedFirstPosition = true; LocationUpdate update = LocationUpdate.MakePosAndOri(P, rotY, headX, false); net.UpdateLocation(id, update, interpolate); } diff --git a/ClassicalSharp/Network/Protocols/IProtocol.cs b/ClassicalSharp/Network/Protocols/IProtocol.cs index 92cb73c3f..8a9c1c795 100644 --- a/ClassicalSharp/Network/Protocols/IProtocol.cs +++ b/ClassicalSharp/Network/Protocols/IProtocol.cs @@ -1,29 +1,22 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; - -namespace ClassicalSharp.Network.Protocols { - - public abstract class IProtocol { - protected Game game; - protected NetworkProcessor net; - protected NetReader reader; - protected NetWriter writer; - - public IProtocol(Game game) { - this.game = game; - net = (NetworkProcessor)game.Server; - reader = net.reader; - writer = net.writer; - } - - /// Initalises variables for this protocol. - /// Typically used to hook the packet handlers. - public virtual void Init() { } - - /// Resets the state of the variables for this protocol. - public virtual void Reset() { } - - /// Performs a tick (if required) for this protocol. - public virtual void Tick() { } - } -} +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; + +namespace ClassicalSharp.Network.Protocols { + + public abstract class IProtocol { + protected Game game; + protected NetworkProcessor net; + protected NetReader reader; + protected NetWriter writer; + + public IProtocol(Game game) { + this.game = game; + net = (NetworkProcessor)game.Server; + reader = net.reader; + writer = net.writer; + } + + public abstract void Reset(); + public abstract void Tick(); + } +} diff --git a/ClassicalSharp/Network/Protocols/WoM.cs b/ClassicalSharp/Network/Protocols/WoM.cs index 198914234..324013929 100644 --- a/ClassicalSharp/Network/Protocols/WoM.cs +++ b/ClassicalSharp/Network/Protocols/WoM.cs @@ -11,9 +11,15 @@ namespace ClassicalSharp.Network.Protocols { public WoMProtocol(Game game) : base(game) { } - string womEnvIdentifier = "womenv_0"; - int womCounter = 0; - internal bool sendWomId = false, sentWomId = false; + string womEnvIdentifier; + int womCounter; + bool sendWomId, sentWomId; + + public override void Reset() { + womEnvIdentifier = "womenv_0"; + womCounter = 0; + sendWomId = false; sentWomId = false; + } public override void Tick() { Request item; diff --git a/src/Client/Block.c b/src/Client/Block.c index 079292365..a919fcd1e 100644 --- a/src/Client/Block.c +++ b/src/Client/Block.c @@ -4,6 +4,8 @@ #include "TerrainAtlas.h" #include "Game.h" #include "Entity.h" +#include "Inventory.h" +#include "Event.h" UInt32 Block_DefinedCustomBlocks[BLOCK_COUNT >> 5]; UInt8 Block_NamesBuffer[String_BufferSize(STRING_SIZE) * BLOCK_COUNT]; @@ -70,6 +72,21 @@ void Block_SetCustomDefined(BlockID block, bool defined) { } } +void Block_DefineCustom(BlockID block) { + Block_SetBlockDraw(block, Block_Draw[block]); + Block_CalcRenderBounds(block); + Block_UpdateCulling(block); + + PackedCol black = PACKEDCOL_BLACK; + String name = Block_UNSAFE_GetName(block); + Block_Tinted[block] = !PackedCol_Equals(Block_FogCol[block], black) && String_IndexOf(&name, '#', 0) >= 0; + Block_CalcLightOffset(block); + + Inventory_AddDefault(block); + Block_SetCustomDefined(block, true); + Event_RaiseVoid(&BlockEvents_BlockDefChanged); +} + void Block_RecalcIsLiquid(BlockID b) { UInt8 collide = Block_ExtendedCollide[b]; Block_IsLiquid[b] = @@ -77,12 +94,6 @@ void Block_RecalcIsLiquid(BlockID b) { (collide == COLLIDE_LIQUID_LAVA && Block_Draw[b] == DRAW_TRANSPARENT); } -void Block_CalcIsTinted(BlockID block) { - PackedCol black = PACKEDCOL_BLACK; - String name = Block_UNSAFE_GetName(block); - Block_Tinted[block] = !PackedCol_Equals(Block_FogCol[block], black) && String_IndexOf(&name, '#', 0) >= 0; -} - void Block_SetCollide(BlockID block, UInt8 collide) { /* necessary for cases where servers redefined core blocks before extended types were introduced. */ collide = DefaultSet_MapOldCollide(block, collide); diff --git a/src/Client/Block.h b/src/Client/Block.h index 3e965736a..b3ac6eb57 100644 --- a/src/Client/Block.h +++ b/src/Client/Block.h @@ -84,8 +84,8 @@ void Block_Init(void); void Block_SetDefaultPerms(void); bool Block_IsCustomDefined(BlockID block); void Block_SetCustomDefined(BlockID block, bool defined); +void Block_DefineCustom(BlockID block); -void Block_CalcIsTinted(BlockID block); void Block_SetCollide(BlockID block, UInt8 collide); void Block_SetDrawType(BlockID block, UInt8 draw); void Block_ResetProps(BlockID block); diff --git a/src/Client/Formats.c b/src/Client/Formats.c index 8d5e4b3ba..1f52d4d13 100644 --- a/src/Client/Formats.c +++ b/src/Client/Formats.c @@ -8,7 +8,6 @@ #include "ErrorHandler.h" #include "Game.h" #include "ServerConnection.h" -#include "Inventory.h" #include "Event.h" void Map_ReadBlocks(Stream* stream) { @@ -488,17 +487,7 @@ bool Cw_Callback_4(NbtTag* tag) { Block_SpriteOffset[id] = 0; } - Block_SetDrawType(id, Block_Draw[id]); - Block_CalcRenderBounds(id); - Block_UpdateCulling(id); - - Block_CalcLightOffset(id); - Block_CalcIsTinted(id); - - Inventory_AddDefault(id); - Block_SetCustomDefined(id, true); - Event_RaiseVoid(&BlockEvents_BlockDefChanged); - + Block_DefineCustom(id); Block_CanPlace[id] = true; Block_CanDelete[id] = true; Event_RaiseVoid(&BlockEvents_PermissionsChanged);