add game info

This commit is contained in:
Lion Kortlepel
2024-03-02 17:58:58 +01:00
parent c5a6fc711f
commit b5f0d9dfdb
3 changed files with 55 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
#include "ClientNetwork.h"
#include "ClientPacket.h"
#include "ClientTransport.h"
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
@@ -124,6 +125,49 @@ void ClientNetwork::handle_packet(ip::tcp::socket& socket, bmp::ClientPacket& pa
}
void ClientNetwork::handle_client_identification(ip::tcp::socket& socket, bmp::ClientPacket& packet) {
switch (packet.purpose) {
case bmp::ClientPurpose::GameInfo: {
try {
auto game_info = vec_to_json(packet.get_readable_data());
std::string impl = game_info.at("implementation");
std::vector<int> mod_version = game_info.at("mod_version");
std::vector<int> game_version = game_info.at("game_version");
std::vector<int> protocol_version = game_info.at("protocol_version");
if (protocol_version.at(0) != 1) {
disconnect(socket, fmt::format("Incompatible protocol version, expected v{}", "1.x.x"));
return;
}
m_mod_version = Version { uint8_t(mod_version.at(0)), uint8_t(mod_version.at(1)), uint8_t(mod_version.at(2)) };
m_game_version = Version { uint8_t(game_version.at(0)), uint8_t(game_version.at(1)), uint8_t(game_version.at(2)) };
spdlog::info("Connected to {} (mod v{}, game v{}, protocol v{}.{}.{}",
impl,
m_mod_version.to_string(),
m_game_version.to_string(),
protocol_version.at(0),
protocol_version.at(1), protocol_version.at(2));
} catch (const std::exception& e) {
spdlog::error("Failed to read json for purpose 0x{:x}: {}", uint16_t(packet.purpose), e.what());
disconnect(socket, fmt::format("Invalid json in purpose 0x{:x}, see launcher logs for more info", uint16_t(packet.purpose)));
}
bmp::ClientPacket state_change {
.purpose = bmp::ClientPurpose::StateChangeLogin,
};
client_tcp_write(socket, state_change);
break;
}
default:
disconnect(socket, fmt::format("Invalid packet purpose in state 0x{:x}: 0x{:x}", uint16_t(m_client_state), uint16_t(packet.purpose)));
break;
}
}
void ClientNetwork::disconnect(ip::tcp::socket& socket, const std::string& reason) {
bmp::ClientPacket error {
.purpose = bmp::ClientPurpose::Error,
.raw_data = json_to_vec({ "message", reason })
};
client_tcp_write(socket, error);
socket.close();
}
void ClientNetwork::handle_login(ip::tcp::socket& socket, bmp::ClientPacket& packet) {
@@ -181,3 +225,6 @@ std::vector<uint8_t> ClientNetwork::json_to_vec(const nlohmann::json& value) {
auto str = value.dump();
return std::vector<uint8_t>(str.begin(), str.end());
}
nlohmann::json ClientNetwork::vec_to_json(const std::vector<uint8_t>& vec) {
return json::parse(std::string(vec.begin(), vec.end()));
}

View File

@@ -4,6 +4,7 @@
#include "ClientState.h"
#include "Launcher.h"
#include "Sync.h"
#include "Version.h"
#include <boost/asio.hpp>
#include <nlohmann/json.hpp>
@@ -35,7 +36,13 @@ private:
void handle_server_playing(ip::tcp::socket& socket, bmp::ClientPacket& packet);
void handle_server_leaving(ip::tcp::socket& socket, bmp::ClientPacket& packet);
void disconnect(ip::tcp::socket& socket, const std::string& reason);
static std::vector<uint8_t> json_to_vec(const nlohmann::json& json);
static nlohmann::json vec_to_json(const std::vector<uint8_t>& vec);
Version m_mod_version;
Version m_game_version;
uint16_t m_listen_port {};
io_context m_io {};

View File

@@ -8,6 +8,7 @@ struct Version {
uint8_t major {};
uint8_t minor {};
uint8_t patch {};
Version() {}
Version(uint8_t major, uint8_t minor, uint8_t patch);
Version(const std::array<uint8_t, 3>& v);
explicit Version(const std::string& str);