Browse Source

Hook up protocol generator for now

main
Joshua Ashton 1 year ago
parent
commit
47430c68d2
  1. 2
      meson.build
  2. 36
      src/Protocol.cpp
  3. 9
      src/Protocol.h
  4. 4
      src/meson.build
  5. 38
      src/protocol/generate_protocol.py
  6. 4
      src/protocol/meson.build
  7. 21
      src/protocol/protocol.hjson

2
meson.build

@ -45,4 +45,6 @@ else
feather_deps += [ libevent_pthreads_dep ]
endif
python = find_program('python')
subdir('src')

36
src/Protocol.cpp

@ -30,21 +30,16 @@ namespace Feather
break;
}
int clientProtocolVersion = packet.ReadVarInt();
string serverIp = packet.ReadString();
uint16_t port = packet.Read<uint16_t>();
// next desired state
ProtocolState intention = packet.ReadVarInt<ProtocolState>();
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<int64_t>();
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<uint64_t>(timestamp);
msg.Write<uint64_t>(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);

9
src/Protocol.h

@ -2,6 +2,7 @@
#include "Common.h"
#include "NetworkMessage.h"
#include "protocol/ProtocolDefinitions.h"
#include <cstdio>
#include <cstdint>
@ -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:

4
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,

38
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 <cstdint>')
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
add_text('#pragma once')
add_text('')
add_text('#include <cstdint>')
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()
print(text)
main()

4
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@' ])

21
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 :

Loading…
Cancel
Save