Files
mcpe/source/network/Packet.hpp
Brent f12a3c1c61 Visual Studio Project Overhaul + Cleanup (#80)
* Visual Studio Project Overhaul + Cleanup
* SDL2 project for Windows
* Re-added game client icon to SDL2 code
* Renamed "AppPlatform_windows" to "AppPlatform_win32" (this is the name of the Windows API and is not representative of the architecture type)
* Renamed "LoggerWindows" to "LoggerWin32"
* Renamed "SoundSystemWindows to "SoundSystemDS" (DirectSound). This may be used for the 360, so it wouldn't really be Windows-specific then.
* Moved "ClientSideNetworkHandler" from "network" to "client/network". We don't need it being compiled for the server if the client's the only thing that needs it.
* I wonder if this still works on macOS...

* Bugfixes & Fixed for macOS

* Options::savePropertiesToFile Logging Bugfix

* Silence Winsock Deprecation Warnings in RakNet

* VS Project Improvements
- Replaced 50 billion relative paths with $(MC_ROOT)
- Added $(RAKNET_PATH) variable to override RakNet location
- Re-added gitignore for .vcxproj.user files
- Added debugging config to Directory.Builds.props
- Slimmed down project configurations for SDL2

* VS Project Config Bugfixes
- Fixed RakNet header path for additional includes

* RakNet Target for XCode

* XCode Project Config Fixes

* Packet logging

* Network VS Project Filter Fix

* Fix RakNet Packet ID Length
We previously didn't have consistency between old and new C++ regarding PacketType enum length. Now we do. This is required or else it completely breaks networking between the versions.

* Additional RakNet Error Handling

* Disable packet logging

* * Fix CMakeLists.txt

This reflects the relocation of ClientSideNetworkHandler.cpp.

* * Also add renderer/GL/GL.cpp to the CMakeLists.txt

* * Replace libpng with stb_image

* * Fix buggy water behavior.

* * Put the CMakeLists of the SDL project in debug mode

* Visual Studio 2010 Support

* * Change the SdlIoCallbacks from an array to a single member.

This fixes compilation of the sdl2 target on VS.

* * Fix missing _error label.

* Revert "* Fix missing _error label."

This reverts commit 99a057fc84049a16c864bd840fb439a008af5c74.

* Revert "* Replace libpng with stb_image"

* info_updateGame Tiles

---------

Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
Co-authored-by: iProgramInCpp <iprogramincpp@gmail.com>
2023-10-22 10:08:59 +03:00

263 lines
6.0 KiB
C++

/********************************************************************
Minecraft: Pocket Edition - Decompilation Project
Copyright (C) 2023 iProgramInCpp
The following code is licensed under the BSD 1 clause license.
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#pragma once
#include <string>
#include "common/LongHack.hpp"
#include "RakNetTypes.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "NetEventCallback.hpp"
class NetEventCallback;
class Level;
class LevelChunk;
// RakNet requires this to be cast to an "unsigned char" before being written to the BitStream
enum ePacketType
#ifndef USE_OLD_CPP
: uint8_t // this is compiled as a 32-bit integer in C++03 and earlier, and we obviously CANNOT afford a 24-bit inconsitency.
// TODO: WritePacketType function that casts this down to a uint8_t / an unsigned 8-bit integer?
#endif
{
PACKET_LOGIN = ID_USER_PACKET_ENUM,
PACKET_MESSAGE,
PACKET_START_GAME,
PACKET_ADD_PLAYER,
PACKET_REMOVE_ENTITY,
PACKET_MOVE_PLAYER,
PACKET_PLACE_BLOCK,
PACKET_REMOVE_BLOCK,
PACKET_UPDATE_BLOCK,
PACKET_REQUEST_CHUNK,
PACKET_CHUNK_DATA,
PACKET_PLAYER_EQUIPMENT,
PACKET_LEVEL_DATA = 200,
};
class Packet
{
public:
virtual ~Packet() {}
virtual void write(RakNet::BitStream*) = 0;
virtual void read(RakNet::BitStream*) = 0;
virtual void handle(const RakNet::RakNetGUID&, NetEventCallback*) = 0;
};
class LoginPacket : public Packet
{
public:
LoginPacket() {}
LoginPacket(const std::string& uname) { m_str = RakNet::RakString(uname.c_str()); }
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
RakNet::RakString m_str;
};
class MessagePacket : public Packet
{
public:
MessagePacket() {}
MessagePacket(const std::string& msg)
{
m_str = msg.c_str();
}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
RakNet::RakString m_str;
};
class StartGamePacket : public Packet
{
public:
StartGamePacket()
{
m_version = 0;
m_time = 0;
}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
TLong field_4;
int field_8;
int field_C;
float field_10;
float field_14;
float field_18;
int m_version;
int m_time;
};
class AddPlayerPacket : public Packet
{
public:
AddPlayerPacket() {}
AddPlayerPacket(const RakNet::RakNetGUID& guid, RakNet::RakString name, int id, float x, float y, float z);
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int field_4;
RakNet::RakNetGUID m_guid;
int field_14;
RakNet::RakString m_name;
int m_id;
float m_x;
float m_y;
float m_z;
};
class RemoveEntityPacket : public Packet
{
public:
RemoveEntityPacket() {}
RemoveEntityPacket(int id) { m_id = id; }
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_id;
};
class MovePlayerPacket : public Packet
{
public:
MovePlayerPacket() {}
MovePlayerPacket(int id, float x, float y, float z, float pitch, float yaw): m_id(id), m_x(x), m_y(y), m_z(z), m_pitch(pitch), m_yaw(yaw) {}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_id;
float m_x;
float m_y;
float m_z;
float m_pitch;
float m_yaw;
};
class PlaceBlockPacket : public Packet
{
public:
PlaceBlockPacket() {}
PlaceBlockPacket(int playerID, int x, uint8_t y, int z, uint8_t tile, uint8_t face)
{
m_playerID = playerID;
m_x = x;
m_y = y;
m_z = z;
m_tile = tile;
m_face = face;
}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_playerID;
int m_x;
int m_z;
uint8_t m_y;
uint8_t m_tile;
uint8_t m_face;
};
class RemoveBlockPacket : public Packet
{
public:
RemoveBlockPacket() {}
RemoveBlockPacket(int id, int x, int y, int z) :m_playerID(id), m_x(x), m_z(z), m_y(uint8_t(y)) {}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_playerID;
int m_x;
int m_z;
uint8_t m_y;
};
class UpdateBlockPacket : public Packet
{
public:
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_x;
int m_z;
uint8_t m_y;
uint8_t m_tile;
uint8_t m_data;
};
class RequestChunkPacket : public Packet
{
public:
RequestChunkPacket() {}
RequestChunkPacket(int x, int z) { m_x = x; m_z = z; }
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_x;
int m_z;
};
class ChunkDataPacket : public Packet
{
public:
ChunkDataPacket() {}
ChunkDataPacket(int x, int z, LevelChunk* c) :m_x(x), m_z(z), m_pChunk(c) {}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_x;
int m_z;
RakNet::BitStream m_data;
LevelChunk* m_pChunk;
};
class LevelDataPacket : public Packet
{
public:
LevelDataPacket() {}
LevelDataPacket(Level* level) : m_pLevel(level) {}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
RakNet::BitStream m_data;
Level* m_pLevel;
};
class PlayerEquipmentPacket : public Packet
{
public:
PlayerEquipmentPacket() {}
PlayerEquipmentPacket(int playerID, int itemID): m_playerID(playerID), m_itemID(itemID) {}
void handle(const RakNet::RakNetGUID&, NetEventCallback* pCallback) override;
void write(RakNet::BitStream*);
void read(RakNet::BitStream*);
public:
int m_playerID;
uint8_t m_itemID;
};