Skip to content

Commit

Permalink
more packet work :(
Browse files Browse the repository at this point in the history
  • Loading branch information
notchyves committed Aug 4, 2024
1 parent 7622ed0 commit 5d69f14
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 82 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ add_executable(bds_btb
src/lua/scripting.cpp
src/logger/logger.cpp
src/event/event.cpp
src/minecraft/network/protocol.cpp
${base64_SOURCE_DIR}/base64.cpp
)

Expand Down
4 changes: 0 additions & 4 deletions scripts/logger.lua

This file was deleted.

35 changes: 1 addition & 34 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "pch.h"
#include "event/types/ServerUpdate.h"

using namespace RakNet;

struct ResponsePacket1 {
int64_t pingId;
int64_t serverId;
Expand Down Expand Up @@ -40,38 +38,7 @@ int main() {
while (true) {
EventManager::registerListener(ServerUpdateType, onServerUpdate);

Packet* packet;
while ((packet = peer->Receive())) {

Logger::Info("Received packet with ID: " + std::to_string(packet->data[0]));

std::string hexData;
for (size_t i = 0; i < packet->length; ++i) {
hexData += " " + to_hex_string(packet->data[i]);
}
Logger::Info("Packet Data (Hex):" + hexData);

std::string u8Data(reinterpret_cast<const char*>(packet->data), packet->length);
Logger::Info("Packet Data (u8string): " + std::string(u8Data.begin(), u8Data.end()));

std::string charData(reinterpret_cast<const char*>(packet->data), packet->length);
Logger::Info("Packet Data (char): " + charData);

if (packet->data[0] == 1) {
Logger::Info("Ping received, sending empty packet back.");

ResponsePacket1 responsePacket;
responsePacket.pingId = 0x00000000003c6d0d;
responsePacket.serverId = 0x00000000372cdc9e;
responsePacket.identifier = "MCPE;Steve;2 7;1.1.3;0;20";

BitStream bs;
bs.Write(reinterpret_cast<const char*>(&responsePacket), sizeof(responsePacket));
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
}

peer->DeallocatePacket(packet);
}
ProtocolManager::Init(peer);


ServerUpdate event;
Expand Down
20 changes: 0 additions & 20 deletions src/minecraft/network/packets/LoginPacket.h

This file was deleted.

20 changes: 20 additions & 0 deletions src/minecraft/network/packets/PingPacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <RakPeerInterface.h>
#include <RakNetTypes.h>
#include <MessageIdentifiers.h>
#include <BitStream.h>

#include <string>
#include <vector>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <base64.h>

using json = nlohmann::json;
using namespace RakNet;

struct PingPacket : Packet {
uint8_t packetType;
int64_t clientTimestamp;
};
22 changes: 0 additions & 22 deletions src/minecraft/network/packets/PlayStatusPacket.h

This file was deleted.

30 changes: 30 additions & 0 deletions src/minecraft/network/protocol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "protocol.h"

void ProtocolManager::Init(RakPeerInterface* peer) {
Packet* packet;
while ((packet = peer->Receive())) {
int packetType = packet->data[0];

Logger::Info("Received packet with ID: " + std::to_string(packetType));

switch (packetType) {
case 0:
ProtocolManager::PingPacketCallback(peer, packet);
case 1:
ProtocolManager::PingPacketCallback(peer, packet);
}

peer->DeallocatePacket(packet);
}
};

void ProtocolManager::PingPacketCallback(RakPeerInterface* peer, Packet* packet) {
PingPacket* pingPacket = reinterpret_cast<PingPacket*>(packet);

PingPacket pongPacket;
pongPacket.packetType = 1;
pongPacket.clientTimestamp = pingPacket->clientTimestamp;

peer->Send(reinterpret_cast<const char*>(&pongPacket), sizeof(PingPacket), HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
Logger::Info("send a pong");
};
16 changes: 14 additions & 2 deletions src/minecraft/network/protocol.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#pragma once

#include "packets/LoginPacket.h"
#include "packets/PlayStatusPacket.h"
#include <RakPeerInterface.h>
#include <RakNetTypes.h>
#include <MessageIdentifiers.h>
#include <BitStream.h>

#include "packets/PingPacket.h"

using namespace RakNet;

class ProtocolManager {
public:
static void Init(RakPeerInterface* peer);
static void PingPacketCallback(RakPeerInterface* peer, Packet* packet);
};

0 comments on commit 5d69f14

Please sign in to comment.