mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-16 11:06:06 -04:00
Some simplifications in utility functions, remove some old unused debug code, do not use magic constants for writing packet ids.
This commit is contained in:
parent
a9756fcb64
commit
c08e98d517
41
Entities/LocationUpdate.cs
Normal file
41
Entities/LocationUpdate.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using OpenTK;
|
||||||
|
|
||||||
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
|
public struct LocationUpdate {
|
||||||
|
|
||||||
|
public Vector3 Pos;
|
||||||
|
public float Yaw, Pitch;
|
||||||
|
|
||||||
|
public bool IncludesPosition;
|
||||||
|
public bool RelativePosition;
|
||||||
|
public bool IncludesOrientation;
|
||||||
|
|
||||||
|
public LocationUpdate( float x, float y, float z, float yaw, float pitch,
|
||||||
|
bool incPos, bool relPos, bool incOri ) {
|
||||||
|
Pos = new Vector3( x, y, z );
|
||||||
|
Yaw = yaw;
|
||||||
|
Pitch = pitch;
|
||||||
|
IncludesPosition = incPos;
|
||||||
|
RelativePosition = relPos;
|
||||||
|
IncludesOrientation = incOri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocationUpdate MakeOri( float yaw, float pitch ) {
|
||||||
|
return new LocationUpdate( 0, 0, 0, yaw, pitch, false, false, true );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocationUpdate MakePos( float x, float y, float z, bool rel ) {
|
||||||
|
return new LocationUpdate( x, y, z, 0, 0, true, rel, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocationUpdate MakePos( Vector3 pos, bool rel ) {
|
||||||
|
return new LocationUpdate( pos.X, pos.Y, pos.Z, 0, 0, true, rel, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocationUpdate MakePosAndOri( float x, float y, float z, float yaw, float pitch, bool rel ) {
|
||||||
|
return new LocationUpdate( x, y, z, yaw, pitch, true, rel, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
namespace ClassicalSharp {
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
public enum PacketId {
|
public enum PacketId {
|
||||||
ServerIdentification = 0,
|
Handshake = 0,
|
||||||
Ping = 1,
|
Ping = 1,
|
||||||
LevelInitialise = 2,
|
LevelInitialise = 2,
|
||||||
LevelDataChunk = 3,
|
LevelDataChunk = 3,
|
||||||
@ -38,6 +38,7 @@ namespace ClassicalSharp {
|
|||||||
CpeEnvWeatherType = 31,
|
CpeEnvWeatherType = 31,
|
||||||
CpeHackControl = 32,
|
CpeHackControl = 32,
|
||||||
CpeExtAddEntity2 = 33,
|
CpeExtAddEntity2 = 33,
|
||||||
|
CpePlayerClick = 34,
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CpeMessageType {
|
public enum CpeMessageType {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
//#define NET_DEBUG
|
using System;
|
||||||
using System;
|
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
#if __MonoCS__
|
#if __MonoCS__
|
||||||
@ -57,7 +56,7 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendPosition( Vector3 pos, byte yaw, byte pitch ) {
|
public void SendPosition( Vector3 pos, float yaw, float pitch ) {
|
||||||
byte payload = sendHeldBlock ? (byte)Window.HeldBlock : (byte)0xFF;
|
byte payload = sendHeldBlock ? (byte)Window.HeldBlock : (byte)0xFF;
|
||||||
MakePositionPacket( pos, yaw, pitch, payload );
|
MakePositionPacket( pos, yaw, pitch, payload );
|
||||||
SendPacket();
|
SendPacket();
|
||||||
@ -93,25 +92,15 @@ namespace ClassicalSharp {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int received = reader.size;
|
while( reader.size > 0 ) {
|
||||||
while( received > 0 ) {
|
|
||||||
byte opcode = reader.buffer[0];
|
byte opcode = reader.buffer[0];
|
||||||
if( received < packetSizes[opcode] ) break;
|
if( reader.size < packetSizes[opcode] ) break;
|
||||||
#if NET_DEBUG
|
|
||||||
Utils.LogDebug( "Remaining {0} bytes ({1} size {2} bytes)", received, (PacketId)opcode, packetSizes[opcode] );
|
|
||||||
#endif
|
|
||||||
ReadPacket( opcode );
|
ReadPacket( opcode );
|
||||||
#if NET_DEBUG
|
|
||||||
Utils.LogDebug( "Left {0} bytes", reader.size );
|
|
||||||
#endif
|
|
||||||
received = reader.size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = Window.LocalPlayer;
|
Player player = Window.LocalPlayer;
|
||||||
if( receivedFirstPosition ) {
|
if( receivedFirstPosition ) {
|
||||||
byte yawPacked = Utils.DegreesToPacked( player.YawDegrees );
|
SendPosition( player.Position, player.YawDegrees, player.PitchDegrees );
|
||||||
byte pitchPacked = Utils.DegreesToPacked( player.PitchDegrees );
|
|
||||||
SendPosition( player.Position, yawPacked, pitchPacked );
|
|
||||||
}
|
}
|
||||||
CheckForNewTerrainAtlas();
|
CheckForNewTerrainAtlas();
|
||||||
CheckForWomEnvironment();
|
CheckForWomEnvironment();
|
||||||
@ -136,7 +125,7 @@ namespace ClassicalSharp {
|
|||||||
static byte[] outBuffer = new byte[131];
|
static byte[] outBuffer = new byte[131];
|
||||||
static int outIndex = 0;
|
static int outIndex = 0;
|
||||||
private static void MakeLoginPacket( string username, string verKey ) {
|
private static void MakeLoginPacket( string username, string verKey ) {
|
||||||
WriteUInt8( 0 ); // packet id
|
WriteUInt8( (byte)PacketId.Handshake );
|
||||||
WriteUInt8( 7 ); // protocol version
|
WriteUInt8( 7 ); // protocol version
|
||||||
WriteString( username );
|
WriteString( username );
|
||||||
WriteString( verKey );
|
WriteString( verKey );
|
||||||
@ -144,7 +133,7 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void MakeSetBlockPacket( short x, short y, short z, bool place, byte block ) {
|
private static void MakeSetBlockPacket( short x, short y, short z, bool place, byte block ) {
|
||||||
WriteUInt8( 0x05 ); // packet id
|
WriteUInt8( (byte)PacketId.SetBlockClient );
|
||||||
WriteInt16( x );
|
WriteInt16( x );
|
||||||
WriteInt16( y );
|
WriteInt16( y );
|
||||||
WriteInt16( z );
|
WriteInt16( z );
|
||||||
@ -152,42 +141,42 @@ namespace ClassicalSharp {
|
|||||||
WriteUInt8( block );
|
WriteUInt8( block );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakePositionPacket( Vector3 pos, byte yaw, byte pitch, byte playerId ) {
|
private static void MakePositionPacket( Vector3 pos, float yaw, float pitch, byte payload ) {
|
||||||
WriteUInt8( 0x08 ); // packet id
|
WriteUInt8( (byte)PacketId.EntityTeleport );
|
||||||
WriteUInt8( playerId ); // player id (-1 is self)
|
WriteUInt8( payload ); // held block when using HeldBlock, otherwise just 255
|
||||||
WriteInt16( (short)( pos.X * 32 ) );
|
WriteInt16( (short)( pos.X * 32 ) );
|
||||||
WriteInt16( (short)( (int)( pos.Y * 32 ) + 51 ) );
|
WriteInt16( (short)( (int)( pos.Y * 32 ) + 51 ) );
|
||||||
WriteInt16( (short)( pos.Z * 32 ) );
|
WriteInt16( (short)( pos.Z * 32 ) );
|
||||||
WriteUInt8( yaw );
|
WriteUInt8( (byte)Utils.DegreesToPacked( yaw, 256 ) );
|
||||||
WriteUInt8( pitch );
|
WriteUInt8( (byte)Utils.DegreesToPacked( pitch, 256 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakeMessagePacket( string text ) {
|
private static void MakeMessagePacket( string text ) {
|
||||||
WriteUInt8( 0x0D ); // packet id
|
WriteUInt8( (byte)PacketId.Message );
|
||||||
WriteUInt8( 0xFF ); // unused
|
WriteUInt8( 0xFF ); // unused
|
||||||
WriteString( text );
|
WriteString( text );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakeExtInfo( string appName, int extensionsCount ) {
|
private static void MakeExtInfo( string appName, int extensionsCount ) {
|
||||||
WriteUInt8( 0x10 ); // packet id
|
WriteUInt8( (byte)PacketId.CpeExtInfo );
|
||||||
WriteString( appName );
|
WriteString( appName );
|
||||||
WriteInt16( (short)extensionsCount );
|
WriteInt16( (short)extensionsCount );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakeExtEntry( string extensionName, int extensionVersion ) {
|
private static void MakeExtEntry( string extensionName, int extensionVersion ) {
|
||||||
WriteUInt8( 0x11 ); // packet id
|
WriteUInt8( (byte)PacketId.CpeExtEntry );
|
||||||
WriteString( extensionName );
|
WriteString( extensionName );
|
||||||
WriteInt32( extensionVersion );
|
WriteInt32( extensionVersion );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakeCustomBlockSupportLevel( byte version ) {
|
private static void MakeCustomBlockSupportLevel( byte version ) {
|
||||||
WriteUInt8( 0x13 ); // packet id
|
WriteUInt8( (byte)PacketId.CpeCustomBlockSupportLevel );
|
||||||
WriteUInt8( version );
|
WriteUInt8( version );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MakePlayerClick( byte button, byte action, short yaw, short pitch, byte targetEntity,
|
private static void MakePlayerClick( byte button, byte action, short yaw, short pitch, byte targetEntity,
|
||||||
Vector3I targetPos, byte targetFace ) {
|
Vector3I targetPos, byte targetFace ) {
|
||||||
WriteUInt8( 0x22 ); // packet id
|
WriteUInt8( (byte)PacketId.CpePlayerClick );
|
||||||
WriteUInt8( button );
|
WriteUInt8( button );
|
||||||
WriteUInt8( action );
|
WriteUInt8( action );
|
||||||
WriteInt16( yaw );
|
WriteInt16( yaw );
|
||||||
@ -232,9 +221,6 @@ namespace ClassicalSharp {
|
|||||||
outIndex = 0;
|
outIndex = 0;
|
||||||
if( Disconnected ) return;
|
if( Disconnected ) return;
|
||||||
|
|
||||||
#if NET_DEBUG
|
|
||||||
Utils.LogDebug( "writing {0} bytes ({1})", packetLength, (PacketId)outBuffer[0] );
|
|
||||||
#endif
|
|
||||||
try {
|
try {
|
||||||
stream.Write( outBuffer, 0, packetLength );
|
stream.Write( outBuffer, 0, packetLength );
|
||||||
} catch( IOException ex ) {
|
} catch( IOException ex ) {
|
||||||
@ -253,19 +239,16 @@ namespace ClassicalSharp {
|
|||||||
DateTime receiveStart;
|
DateTime receiveStart;
|
||||||
DeflateStream gzipStream;
|
DeflateStream gzipStream;
|
||||||
GZipHeaderReader gzipHeader;
|
GZipHeaderReader gzipHeader;
|
||||||
int mapSizeIndex;
|
int mapSizeIndex, mapIndex;
|
||||||
byte[] mapSize = new byte[4];
|
byte[] mapSize = new byte[4], map;
|
||||||
byte[] map;
|
|
||||||
int mapIndex;
|
|
||||||
FixedBufferStream gzippedMap;
|
FixedBufferStream gzippedMap;
|
||||||
int womCounter = 0;
|
|
||||||
bool sendWomId = false, sentWomId = false;
|
bool sendWomId = false, sentWomId = false;
|
||||||
|
|
||||||
void ReadPacket( byte opcode ) {
|
void ReadPacket( byte opcode ) {
|
||||||
reader.Remove( 1 ); // remove opcode
|
reader.Remove( 1 ); // remove opcode
|
||||||
|
|
||||||
switch( (PacketId)opcode ) {
|
switch( (PacketId)opcode ) {
|
||||||
case PacketId.ServerIdentification:
|
case PacketId.Handshake:
|
||||||
{
|
{
|
||||||
byte protocolVer = reader.ReadUInt8();
|
byte protocolVer = reader.ReadUInt8();
|
||||||
ServerName = reader.ReadString();
|
ServerName = reader.ReadString();
|
||||||
@ -319,7 +302,7 @@ namespace ClassicalSharp {
|
|||||||
|
|
||||||
if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
|
if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
|
||||||
if( mapSizeIndex < 4 ) {
|
if( mapSizeIndex < 4 ) {
|
||||||
mapSizeIndex += gzipStream.Read( mapSize, 0, 4 - mapSizeIndex );
|
mapSizeIndex += gzipStream.Read( mapSize, mapSizeIndex, 4 - mapSizeIndex );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( mapSizeIndex == 4 ) {
|
if( mapSizeIndex == 4 ) {
|
||||||
@ -483,10 +466,10 @@ namespace ClassicalSharp {
|
|||||||
case PacketId.CpeHoldThis:
|
case PacketId.CpeHoldThis:
|
||||||
{
|
{
|
||||||
byte blockType = reader.ReadUInt8();
|
byte blockType = reader.ReadUInt8();
|
||||||
bool noChanging = reader.ReadUInt8() != 0;
|
bool canChange = reader.ReadUInt8() == 0;
|
||||||
Window.CanChangeHeldBlock = true;
|
Window.CanChangeHeldBlock = true;
|
||||||
Window.HeldBlock = (Block)blockType;
|
Window.HeldBlock = (Block)blockType;
|
||||||
Window.CanChangeHeldBlock = !noChanging;
|
Window.CanChangeHeldBlock = canChange;
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case PacketId.CpeExtAddPlayerName:
|
case PacketId.CpeExtAddPlayerName:
|
||||||
|
@ -8,6 +8,8 @@ namespace ClassicalSharp {
|
|||||||
public partial class NetworkProcessor {
|
public partial class NetworkProcessor {
|
||||||
|
|
||||||
string womEnvIdentifier = "womenv_0", womTerrainIdentifier = "womterrain_0";
|
string womEnvIdentifier = "womenv_0", womTerrainIdentifier = "womterrain_0";
|
||||||
|
int womCounter = 0;
|
||||||
|
|
||||||
void CheckForWomEnvironment() {
|
void CheckForWomEnvironment() {
|
||||||
DownloadedItem item;
|
DownloadedItem item;
|
||||||
Window.AsyncDownloader.TryGetItem( womEnvIdentifier, out item );
|
Window.AsyncDownloader.TryGetItem( womEnvIdentifier, out item );
|
||||||
|
@ -103,26 +103,14 @@ namespace ClassicalSharp {
|
|||||||
return radians * 180.0 / Math.PI;
|
return radians * 180.0 / Math.PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basically this works as a special unit circle in the range of [0..255].
|
public static int DegreesToPacked( double degrees, int period ) {
|
||||||
public static byte RadiansToPacked( double radians ) {
|
return (int)( degrees * period / 360.0 ) % period;
|
||||||
return DegreesToPacked( radians * 180.0 / Math.PI );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte DegreesToPacked( double degrees ) {
|
|
||||||
int packed = (int)( degrees * 256.0 / 360.0 ) % 256; // 256 = period
|
|
||||||
if( packed < 0 )
|
|
||||||
packed += 256; // Normalise into [0..255];
|
|
||||||
return (byte)packed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double PackedToDegrees( byte packed ) {
|
public static double PackedToDegrees( byte packed ) {
|
||||||
return packed * 360.0 / 256.0;
|
return packed * 360.0 / 256.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double PackedToRadians( byte packed ) {
|
|
||||||
return PackedToDegrees( packed ) * Math.PI / 180.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float DistanceSquared( Vector3 p1, Vector3 p2 ) {
|
public static float DistanceSquared( Vector3 p1, Vector3 p2 ) {
|
||||||
float dx = p2.X - p1.X;
|
float dx = p2.X - p1.X;
|
||||||
float dy = p2.Y - p1.Y;
|
float dy = p2.Y - p1.Y;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user