move some classic/CPE protocol specific bits out of NetworkProcessor

This commit is contained in:
UnknownShadow200 2018-04-24 15:21:38 +10:00
parent 19295f451a
commit d255cb1caf
11 changed files with 117 additions and 125 deletions

View File

@ -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; 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) { static void RecalcIsLiquid(BlockID block) {

View File

@ -158,6 +158,7 @@ namespace ClassicalSharp.Map {
BlockInfo.SpriteOffset[id] = blockDraw; BlockInfo.SpriteOffset[id] = blockDraw;
blockDraw = DrawType.Sprite; blockDraw = DrawType.Sprite;
} }
BlockInfo.Draw[id] = blockDraw;
data = (byte[])compound["Fog"].Value; data = (byte[])compound["Fog"].Value;
BlockInfo.FogDensity[id] = (data[0] + 1) / 128f; 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.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.MaxBB[id] = new Vector3(data[3] / 16f, data[4] / 16f, data[5] / 16f);
BlockInfo.SetBlockDraw(id, blockDraw); BlockInfo.DefineCustom(game, id);
BlockInfo.CalcRenderBounds(id);
BlockInfo.UpdateCulling(id);
BlockInfo.CalcIsTinted(id);
BlockInfo.CalcLightOffset(id);
game.Inventory.AddDefault(id);
BlockInfo.SetCustomDefined(id, true);
game.Events.RaiseBlockDefinitionChanged();
BlockInfo.CanPlace[id] = true; BlockInfo.CanPlace[id] = true;
BlockInfo.CanDelete[id] = true; BlockInfo.CanDelete[id] = true;
game.Events.RaiseBlockPermissionsChanged(); game.Events.RaiseBlockPermissionsChanged();

View File

@ -34,9 +34,7 @@ namespace ClassicalSharp.Network {
internal WoMProtocol wom; internal WoMProtocol wom;
internal CPESupport cpeData; internal CPESupport cpeData;
internal bool receivedFirstPosition;
internal byte[] needRemoveNames = new byte[256 >> 3]; internal byte[] needRemoveNames = new byte[256 >> 3];
int pingTicks;
public override void Connect(IPAddress address, int port) { public override void Connect(IPAddress address, int port) {
socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
@ -54,16 +52,12 @@ namespace ClassicalSharp.Network {
writer = new NetWriter(socket); writer = new NetWriter(socket);
classic = new ClassicProtocol(game); classic = new ClassicProtocol(game);
classic.Init();
cpe = new CPEProtocol(game); cpe = new CPEProtocol(game);
cpe.Init();
cpeBlockDefs = new CPEProtocolBlockDefs(game); cpeBlockDefs = new CPEProtocolBlockDefs(game);
cpeBlockDefs.Init();
wom = new WoMProtocol(game); wom = new WoMProtocol(game);
wom.Init(); ResetProtocols();
Disconnected = false; Disconnected = false;
receivedFirstPosition = false;
lastPacket = DateTime.UtcNow; lastPacket = DateTime.UtcNow;
game.WorldEvents.OnNewMap += OnNewMap; game.WorldEvents.OnNewMap += OnNewMap;
game.UserEvents.BlockChanged += BlockChanged; game.UserEvents.BlockChanged += BlockChanged;
@ -128,18 +122,8 @@ namespace ClassicalSharp.Network {
void CoreTick() { void CoreTick() {
CheckAsyncResources(); CheckAsyncResources();
wom.Tick(); wom.Tick();
classic.Tick();
if (receivedFirstPosition) { cpe.Tick();
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;
}
if (writer.index > 0) SendPacket(); if (writer.index > 0) SendPacket();
} }
@ -190,17 +174,21 @@ namespace ClassicalSharp.Network {
handlers[i] = null; handlers[i] = null;
packetSizes[i] = 0; packetSizes[i] = 0;
} }
if (classic == null) return; // null if no successful connection ever made before ResetProtocols();
classic.Reset();
cpe.Reset();
cpeBlockDefs.Reset();
reader.ExtendedPositions = false; reader.ExtendedBlocks = false; reader.ExtendedPositions = false; reader.ExtendedBlocks = false;
writer.ExtendedPositions = false; writer.ExtendedBlocks = false; writer.ExtendedPositions = false; writer.ExtendedBlocks = false;
BlockInfo.SetMaxUsed(255); 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 Action[] handlers = new Action[256];
internal ushort[] packetSizes = new ushort[256]; internal ushort[] packetSizes = new ushort[256];
int maxHandledPacket = 0; int maxHandledPacket = 0;

View File

@ -10,14 +10,13 @@ namespace ClassicalSharp.Network.Protocols {
public CPEProtocolBlockDefs(Game game) : base(game) { } public CPEProtocolBlockDefs(Game game) : base(game) { }
public override void Init() { Reset(); }
public override void 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.CpeUndefineBlock, HandleRemoveBlockDefinition, 2); net.Set(Opcode.CpeUndefineBlock, HandleRemoveBlockDefinition, 2);
net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85); net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85);
} }
public override void Tick() { }
internal void HandleDefineBlock() { internal void HandleDefineBlock() {
BlockID block = HandleDefineBlockCommonStart(reader, false); BlockID block = HandleDefineBlockCommonStart(reader, false);
@ -118,21 +117,13 @@ namespace ClassicalSharp.Network.Protocols {
BlockInfo.SpriteOffset[block] = blockDraw; BlockInfo.SpriteOffset[block] = blockDraw;
blockDraw = DrawType.Sprite; blockDraw = DrawType.Sprite;
} }
BlockInfo.Draw[block] = blockDraw;
byte fogDensity = reader.ReadUInt8(); byte fogDensity = reader.ReadUInt8();
BlockInfo.FogDensity[block] = fogDensity == 0 ? 0 : (fogDensity + 1) / 128f; BlockInfo.FogDensity[block] = fogDensity == 0 ? 0 : (fogDensity + 1) / 128f;
BlockInfo.FogColour[block] = new FastColour(reader.ReadUInt8(), reader.ReadUInt8(), reader.ReadUInt8()); BlockInfo.FogColour[block] = new FastColour(reader.ReadUInt8(), reader.ReadUInt8(), reader.ReadUInt8());
BlockInfo.SetBlockDraw(block, blockDraw); BlockInfo.DefineCustom(game, block);
BlockInfo.CalcRenderBounds(block);
BlockInfo.UpdateCulling(block);
BlockInfo.CalcIsTinted(block);
BlockInfo.CalcLightOffset(block);
game.Inventory.AddDefault(block);
BlockInfo.SetCustomDefined(block, true);
game.Events.RaiseBlockDefinitionChanged();
} }
#if FALSE #if FALSE

View File

@ -13,11 +13,12 @@ namespace ClassicalSharp.Network.Protocols {
public sealed class CPEProtocol : IProtocol { public sealed class CPEProtocol : IProtocol {
public CPEProtocol(Game game) : base(game) { } public CPEProtocol(Game game) : base(game) { }
int pingTicks;
public override void Init() { Reset(); }
public override void Reset() { public override void Reset() {
pingTicks = 0;
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);
net.Set(Opcode.CpeSetClickDistance, HandleSetClickDistance, 3); net.Set(Opcode.CpeSetClickDistance, HandleSetClickDistance, 3);
@ -48,6 +49,14 @@ namespace ClassicalSharp.Network.Protocols {
net.Set(Opcode.CpeSetInventoryOrder, HandleSetInventoryOrder, 3); 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 #region Read
void HandleExtInfo() { void HandleExtInfo() {
string appName = reader.ReadString(); string appName = reader.ReadString();
@ -173,11 +182,11 @@ namespace ClassicalSharp.Network.Protocols {
void HandleEnvColours() { void HandleEnvColours() {
byte variable = reader.ReadUInt8(); byte variable = reader.ReadUInt8();
short red = reader.ReadInt16(); short r = reader.ReadInt16();
short green = reader.ReadInt16(); short g = reader.ReadInt16();
short blue = reader.ReadInt16(); short b = reader.ReadInt16();
bool invalid = red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255; bool invalid = r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255;
FastColour col = new FastColour(red, green, blue); FastColour col = new FastColour(r, g, b);
if (variable == 0) { if (variable == 0) {
game.World.Env.SetSkyColour(invalid ? WorldEnv.DefaultSkyCol : col); game.World.Env.SetSkyColour(invalid ? WorldEnv.DefaultSkyCol : col);

View File

@ -17,13 +17,20 @@ namespace ClassicalSharp.Network.Protocols {
public sealed class ClassicProtocol : IProtocol { public sealed class ClassicProtocol : IProtocol {
public ClassicProtocol(Game game) : base(game) { } public ClassicProtocol(Game game) : base(game) { }
bool receivedFirstPosition;
public override void Init() { DateTime mapReceiveStart;
mapPartStream = new FixedBufferStream(net.reader.buffer); DeflateStream gzipStream;
Reset(); GZipHeaderReader gzipHeader;
} int mapSizeIndex, mapIndex, mapVolume;
byte[] mapSize = new byte[4], map;
FixedBufferStream mapPartStream;
Screen prevScreen;
bool prevCursorVisible;
public override void Reset() { public override void Reset() {
if (mapPartStream == null) mapPartStream = new FixedBufferStream(net.reader.buffer);
receivedFirstPosition = false;
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);
@ -43,14 +50,12 @@ namespace ClassicalSharp.Network.Protocols {
net.Set(Opcode.SetPermission, HandleSetPermission, 2); net.Set(Opcode.SetPermission, HandleSetPermission, 2);
} }
DateTime mapReceiveStart; public override void Tick() {
DeflateStream gzipStream; if (receivedFirstPosition) {
GZipHeaderReader gzipHeader; LocalPlayer player = game.LocalPlayer;
int mapSizeIndex, mapIndex, mapVolume; WritePosition(player.Position, player.HeadY, player.HeadX);
byte[] mapSize = new byte[4], map; }
FixedBufferStream mapPartStream; }
Screen prevScreen;
bool prevCursorVisible;
#if !ONLY_8BIT #if !ONLY_8BIT
DeflateStream gzipStream2; DeflateStream gzipStream2;
@ -96,7 +101,7 @@ namespace ClassicalSharp.Network.Protocols {
game.Gui.SetNewScreen(new LoadingMapScreen(game, net.ServerName, net.ServerMotd), false); game.Gui.SetNewScreen(new LoadingMapScreen(game, net.ServerName, net.ServerMotd), false);
net.wom.CheckMotd(); net.wom.CheckMotd();
net.receivedFirstPosition = false; receivedFirstPosition = false;
gzipHeader = new GZipHeaderReader(); gzipHeader = new GZipHeaderReader();
// Workaround because built in mono stream assumes that the end of stream // Workaround because built in mono stream assumes that the end of stream
@ -188,14 +193,14 @@ namespace ClassicalSharp.Network.Protocols {
#if !ONLY_8BIT #if !ONLY_8BIT
if (reader.ExtendedBlocks) { if (reader.ExtendedBlocks) {
// defer allocation of scond map array if possible // 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); BlockInfo.SetMaxUsed(map2 == null ? 255 : 767);
} }
#endif #endif
game.WorldEvents.RaiseOnNewMapLoaded(); game.WorldEvents.RaiseOnNewMapLoaded();
net.wom.CheckSendWomID(); net.wom.CheckSendWomID();
map = null; map = null;
gzipStream.Dispose(); gzipStream.Dispose();
gzipStream = null; gzipStream = null;
#if !ONLY_8BIT #if !ONLY_8BIT
@ -303,7 +308,7 @@ namespace ClassicalSharp.Network.Protocols {
float rotY = (float)Utils.PackedToDegrees(reader.ReadUInt8()); float rotY = (float)Utils.PackedToDegrees(reader.ReadUInt8());
float headX = (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); LocationUpdate update = LocationUpdate.MakePosAndOri(P, rotY, headX, false);
net.UpdateLocation(id, update, interpolate); net.UpdateLocation(id, update, interpolate);
} }

View File

@ -1,29 +1,22 @@
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System; using System;
namespace ClassicalSharp.Network.Protocols { namespace ClassicalSharp.Network.Protocols {
public abstract class IProtocol { public abstract class IProtocol {
protected Game game; protected Game game;
protected NetworkProcessor net; protected NetworkProcessor net;
protected NetReader reader; protected NetReader reader;
protected NetWriter writer; protected NetWriter writer;
public IProtocol(Game game) { public IProtocol(Game game) {
this.game = game; this.game = game;
net = (NetworkProcessor)game.Server; net = (NetworkProcessor)game.Server;
reader = net.reader; reader = net.reader;
writer = net.writer; writer = net.writer;
} }
/// <summary> Initalises variables for this protocol. </summary> public abstract void Reset();
/// <remarks> Typically used to hook the packet handlers. </remarks> public abstract void Tick();
public virtual void Init() { } }
}
/// <summary> Resets the state of the variables for this protocol. </summary>
public virtual void Reset() { }
/// <summary> Performs a tick (if required) for this protocol. </summary>
public virtual void Tick() { }
}
}

View File

@ -11,9 +11,15 @@ namespace ClassicalSharp.Network.Protocols {
public WoMProtocol(Game game) : base(game) { } public WoMProtocol(Game game) : base(game) { }
string womEnvIdentifier = "womenv_0"; string womEnvIdentifier;
int womCounter = 0; int womCounter;
internal bool sendWomId = false, sentWomId = false; bool sendWomId, sentWomId;
public override void Reset() {
womEnvIdentifier = "womenv_0";
womCounter = 0;
sendWomId = false; sentWomId = false;
}
public override void Tick() { public override void Tick() {
Request item; Request item;

View File

@ -4,6 +4,8 @@
#include "TerrainAtlas.h" #include "TerrainAtlas.h"
#include "Game.h" #include "Game.h"
#include "Entity.h" #include "Entity.h"
#include "Inventory.h"
#include "Event.h"
UInt32 Block_DefinedCustomBlocks[BLOCK_COUNT >> 5]; UInt32 Block_DefinedCustomBlocks[BLOCK_COUNT >> 5];
UInt8 Block_NamesBuffer[String_BufferSize(STRING_SIZE) * BLOCK_COUNT]; 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) { void Block_RecalcIsLiquid(BlockID b) {
UInt8 collide = Block_ExtendedCollide[b]; UInt8 collide = Block_ExtendedCollide[b];
Block_IsLiquid[b] = Block_IsLiquid[b] =
@ -77,12 +94,6 @@ void Block_RecalcIsLiquid(BlockID b) {
(collide == COLLIDE_LIQUID_LAVA && Block_Draw[b] == DRAW_TRANSPARENT); (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) { void Block_SetCollide(BlockID block, UInt8 collide) {
/* necessary for cases where servers redefined core blocks before extended types were introduced. */ /* necessary for cases where servers redefined core blocks before extended types were introduced. */
collide = DefaultSet_MapOldCollide(block, collide); collide = DefaultSet_MapOldCollide(block, collide);

View File

@ -84,8 +84,8 @@ void Block_Init(void);
void Block_SetDefaultPerms(void); void Block_SetDefaultPerms(void);
bool Block_IsCustomDefined(BlockID block); bool Block_IsCustomDefined(BlockID block);
void Block_SetCustomDefined(BlockID block, bool defined); 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_SetCollide(BlockID block, UInt8 collide);
void Block_SetDrawType(BlockID block, UInt8 draw); void Block_SetDrawType(BlockID block, UInt8 draw);
void Block_ResetProps(BlockID block); void Block_ResetProps(BlockID block);

View File

@ -8,7 +8,6 @@
#include "ErrorHandler.h" #include "ErrorHandler.h"
#include "Game.h" #include "Game.h"
#include "ServerConnection.h" #include "ServerConnection.h"
#include "Inventory.h"
#include "Event.h" #include "Event.h"
void Map_ReadBlocks(Stream* stream) { void Map_ReadBlocks(Stream* stream) {
@ -488,17 +487,7 @@ bool Cw_Callback_4(NbtTag* tag) {
Block_SpriteOffset[id] = 0; Block_SpriteOffset[id] = 0;
} }
Block_SetDrawType(id, Block_Draw[id]); Block_DefineCustom(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_CanPlace[id] = true; Block_CanPlace[id] = true;
Block_CanDelete[id] = true; Block_CanDelete[id] = true;
Event_RaiseVoid(&BlockEvents_PermissionsChanged); Event_RaiseVoid(&BlockEvents_PermissionsChanged);