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;
CalcLightOffset(block);
game.Inventory.AddDefault(block);
SetCustomDefined(block, true);
game.Events.RaiseBlockDefinitionChanged();
}
static void RecalcIsLiquid(BlockID block) {

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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;
}
/// <summary> Initalises variables for this protocol. </summary>
/// <remarks> Typically used to hook the packet handlers. </remarks>
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() { }
}
}
// 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();
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);