From 47430c68d21375b14ac539a036785e725590cbe9 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sun, 2 Aug 2020 02:44:35 +0100 Subject: [PATCH] Hook up protocol generator for now --- meson.build | 2 ++ src/Protocol.cpp | 36 +++++++++++++---------------- src/Protocol.h | 9 +------- src/meson.build | 4 +++- src/protocol/generate_protocol.py | 38 ++++++++++++++++++++++--------- src/protocol/meson.build | 4 ++++ src/protocol/protocol.hjson | 21 +++++++++++++++++ 7 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 src/protocol/meson.build diff --git a/meson.build b/meson.build index 89ef925..8c2850c 100644 --- a/meson.build +++ b/meson.build @@ -45,4 +45,6 @@ else feather_deps += [ libevent_pthreads_dep ] endif +python = find_program('python') + subdir('src') diff --git a/src/Protocol.cpp b/src/Protocol.cpp index bb0a012..585a923 100644 --- a/src/Protocol.cpp +++ b/src/Protocol.cpp @@ -30,21 +30,16 @@ namespace Feather break; } - int clientProtocolVersion = packet.ReadVarInt(); - string serverIp = packet.ReadString(); - uint16_t port = packet.Read(); - - // next desired state - ProtocolState intention = packet.ReadVarInt(); + HandshakeMessage handshake(packet); printf("[Protocol] Client Intention Packet: version=%d, serverIp=%s, port=%u, intention=%d\n", - clientProtocolVersion, - serverIp.c_str(), - port, - intention + handshake.protocolVersion, + handshake.serverIP.c_str(), + handshake.port, + handshake.intention ); - context.SetState(intention); + context.SetState(handshake.intention); break; } @@ -72,13 +67,13 @@ namespace Feather } case ServerboundStatusPacketId::Ping: { - int64_t timestamp = packet.Read(); - printf("[Protocol] Client sent STATUS_PING: %lld\n", timestamp); + PingMessage ping(packet); + printf("[Protocol] Client sent STATUS_PING: %lld\n", ping.timestamp); NetworkMessage msg(VARINT_MAX_SIZE + sizeof(int64_t)); msg.WriteVarInt(ClientBoundStatusPacketId::Pong); - msg.Write(timestamp); + msg.Write(ping.timestamp); msg.Finalize(); client.SendMessage(msg); @@ -97,13 +92,14 @@ namespace Feather { case ServerboundLoginPacketId::LoginStart: { - std::string username = packet.ReadString(); - std::string uuid = "ecb99913-96a8-40a7-8529-a2ca6ad95768"; + LoginStartMessage loginStart(packet); - uuid.resize(36); - username.resize(16); + // 1.15.2... + //std::string uuid = "ecb99913-96a8-40a7-8529-a2ca6ad95768"; + //uuid.resize(36); + //NetworkMessage msg(VARINT_MAX_SIZE * 3 + uuid.length() + loginStart.username.length()); - NetworkMessage msg(VARINT_MAX_SIZE * 3 + uuid.length() + username.length()); + NetworkMessage msg(VARINT_MAX_SIZE * 3 + 2 * sizeof(uint64_t) + loginStart.username.length()); msg.WriteVarInt(ClientboundLoginPacketId::LoginSuccess); // UUID 1.16.1 @@ -113,7 +109,7 @@ namespace Feather // UUID 1.15.2 //msg.WriteString(uuid.c_str(), uuid.length()); - msg.WriteString(username.c_str(), username.length()); + msg.WriteString(loginStart.username.c_str(), loginStart.username.length()); msg.Finalize(); client.SendMessage(msg); diff --git a/src/Protocol.h b/src/Protocol.h index 7a6dc9b..0ba9b4a 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -2,6 +2,7 @@ #include "Common.h" #include "NetworkMessage.h" +#include "protocol/ProtocolDefinitions.h" #include #include @@ -11,14 +12,6 @@ namespace Feather class PacketReader; class MinecraftClient; - enum class ProtocolState : int32_t - { - Handholding = -1, - Play = 0, - Status = 1, - Login = 2, - }; - class ProtocolContext { public: diff --git a/src/meson.build b/src/meson.build index 4b97bf1..d18f819 100644 --- a/src/meson.build +++ b/src/meson.build @@ -18,7 +18,9 @@ feather_src = [ 'config/ServerProperties.cpp', ] -executable('FeatherMC', feather_src, +subdir('protocol') + +executable('FeatherMC', feather_src, protocol_headers, dependencies : feather_deps, include_directories : include_directories('.', '../subprojects/rapidjson/include'), install : true, diff --git a/src/protocol/generate_protocol.py b/src/protocol/generate_protocol.py index b782bf2..cf35f54 100644 --- a/src/protocol/generate_protocol.py +++ b/src/protocol/generate_protocol.py @@ -1,4 +1,5 @@ import hjson +import sys text = '' @@ -21,7 +22,10 @@ def print_states(states): def get_rw_func(primitiveType, aliasedType, read): prefix = 'Read' if read else 'Write' if aliasedType == 'varint': - return '{}VarInt<{}>'.format(prefix, primitiveType) + if primitiveType == 'int32_t': + return '{}VarInt'.format(prefix) + else: + return '{}VarInt<{}>'.format(prefix, primitiveType) elif aliasedType == 'string': return '{}String'.format(prefix) else: @@ -48,9 +52,10 @@ def print_messages(list, aliases, primitives, serverbound): add_text(' {} {};', resolve_type(primitives, type), name) add_text(' }};') + add_text('') def print_protocol(): - with open('protocol.hjson') as message_file: + with open(sys.argv[1]) as message_file: message_scheme = hjson.load(message_file) print_states(message_scheme['states']) @@ -63,13 +68,24 @@ def print_protocol(): message_scheme['types']['primitives'], True) -add_text('#pragma once') -add_text('') -add_text('#include ') -add_text('') -add_text('namespace Feather') -add_text('{{') -print_protocol() -add_text('}}') +def main(): + if len(sys.argv) != 2: + print('Please specify the input file.') + return -print(text) \ No newline at end of file + add_text('#pragma once') + add_text('') + add_text('#include ') + add_text('#include "PacketReader.h"') + add_text('') + add_text('namespace Feather') + add_text('{{') + print_protocol() + add_text('}}') + add_text('') + + with open('ProtocolDefinitions.h', 'w') as out_file: + out_file.write(text) + out_file.close() + +main() \ No newline at end of file diff --git a/src/protocol/meson.build b/src/protocol/meson.build new file mode 100644 index 0000000..a3f48d7 --- /dev/null +++ b/src/protocol/meson.build @@ -0,0 +1,4 @@ +protocol_headers = custom_target('ProtocolHeader', + input : [ 'generate_protocol.py', 'protocol.hjson' ], + output : [ 'ProtocolDefinitions.h' ], + command : [ python, '@INPUT@' ]) \ No newline at end of file diff --git a/src/protocol/protocol.hjson b/src/protocol/protocol.hjson index 35b59a2..6259574 100644 --- a/src/protocol/protocol.hjson +++ b/src/protocol/protocol.hjson @@ -9,6 +9,7 @@ primitives : { varint : int32_t + string : std::string } } @@ -36,6 +37,26 @@ intention : ProtocolState } } + + Ping : + { + id : 1 + state : Login + vars : + { + timestamp : uint64_t + } + } + + LoginStart : + { + id : 0 + state : Login + vars : + { + username : string + } + } } clientbound :