Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <span>
|
||||
|
||||
namespace metin2::network {
|
||||
|
||||
using TeaKey = std::array<uint32_t, 4>;
|
||||
|
||||
class TeaCipher {
|
||||
public:
|
||||
TeaCipher() = default;
|
||||
explicit TeaCipher(const TeaKey& key);
|
||||
|
||||
void SetKey(const TeaKey& key);
|
||||
const TeaKey& GetKey() const { return m_key; }
|
||||
|
||||
void EncryptBlock(uint32_t& y, uint32_t& z) const;
|
||||
void DecryptBlock(uint32_t& y, uint32_t& z) const;
|
||||
|
||||
std::vector<uint8_t> Encrypt(std::span<const uint8_t> input) const;
|
||||
std::vector<uint8_t> Decrypt(std::span<const uint8_t> input) const;
|
||||
|
||||
private:
|
||||
TeaKey m_key{0, 0, 0, 0};
|
||||
};
|
||||
|
||||
class RollingXorCipher {
|
||||
public:
|
||||
RollingXorCipher() = default;
|
||||
explicit RollingXorCipher(uint32_t seed_key);
|
||||
|
||||
void Reset(uint32_t seed_key);
|
||||
void Process(uint8_t* data, size_t size);
|
||||
|
||||
private:
|
||||
uint32_t m_key = 0;
|
||||
uint32_t m_state = 0;
|
||||
};
|
||||
|
||||
class CipherContext {
|
||||
public:
|
||||
CipherContext() = default;
|
||||
|
||||
void SetEnabled(bool enabled) { m_enabled = enabled; }
|
||||
bool IsEnabled() const { return m_enabled; }
|
||||
|
||||
void InitializeKeys(const TeaKey& tea_key, uint32_t xor_seed);
|
||||
|
||||
void EncryptSend(uint8_t* data, size_t size);
|
||||
void DecryptRecv(uint8_t* data, size_t size);
|
||||
|
||||
TeaCipher& GetSendTea() { return m_send_tea; }
|
||||
TeaCipher& GetRecvTea() { return m_recv_tea; }
|
||||
|
||||
private:
|
||||
bool m_enabled = false;
|
||||
TeaCipher m_send_tea;
|
||||
TeaCipher m_recv_tea;
|
||||
RollingXorCipher m_send_xor;
|
||||
RollingXorCipher m_recv_xor;
|
||||
};
|
||||
|
||||
} // namespace metin2::network
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <functional>
|
||||
|
||||
namespace metin2::network {
|
||||
|
||||
class PhaseStateMachine {
|
||||
public:
|
||||
using PhaseChangeCallback = std::function<void(ConnectionPhase old_phase, ConnectionPhase new_phase)>;
|
||||
|
||||
PhaseStateMachine();
|
||||
explicit PhaseStateMachine(ConnectionPhase initial_phase);
|
||||
|
||||
ConnectionPhase GetCurrentPhase() const { return m_current_phase; }
|
||||
const char* GetPhaseName() const { return ToString(m_current_phase); }
|
||||
|
||||
bool CanTransitionTo(ConnectionPhase target_phase) const;
|
||||
bool TransitionTo(ConnectionPhase target_phase);
|
||||
void ForcePhase(ConnectionPhase phase);
|
||||
|
||||
void SetPhaseChangeCallback(PhaseChangeCallback cb) { m_callback = std::move(cb); }
|
||||
|
||||
bool IsConnected() const { return m_current_phase != ConnectionPhase::Disconnected; }
|
||||
bool IsInGame() const { return m_current_phase == ConnectionPhase::InGame; }
|
||||
|
||||
private:
|
||||
ConnectionPhase m_current_phase = ConnectionPhase::Disconnected;
|
||||
PhaseChangeCallback m_callback;
|
||||
};
|
||||
|
||||
} // namespace metin2::network
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "connection_phase.h"
|
||||
#include "cipher.h"
|
||||
#include "packet.h"
|
||||
#include "network_client.h"
|
||||
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "connection_phase.h"
|
||||
#include "cipher.h"
|
||||
#include "packet.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace metin2::network {
|
||||
|
||||
class NetworkClient {
|
||||
public:
|
||||
using HandshakeCallback = std::function<void(uint32_t server_time)>;
|
||||
using AuthResultCallback = std::function<void(bool success, uint32_t error_code, const std::string& message)>;
|
||||
using CharacterListCallback = std::function<void(const std::vector<CharacterSummary>& characters)>;
|
||||
using GameStartCallback = std::function<void(const PlayerInfo& player)>;
|
||||
using ChatCallback = std::function<void(uint32_t sender_id, uint8_t chat_type, const std::string& text)>;
|
||||
using ActorMoveCallback = std::function<void(uint32_t actor_id, float x, float y, float z, float rot, uint32_t move_type)>;
|
||||
using GenericPacketCallback = std::function<void(const Packet& packet)>;
|
||||
|
||||
NetworkClient();
|
||||
|
||||
// Transport Interface (zero socket binding)
|
||||
void PushReceivedData(const uint8_t* data, size_t size);
|
||||
std::vector<uint8_t> PopSendData();
|
||||
bool HasPendingSendData() const { return !m_send_buffer.empty(); }
|
||||
|
||||
// Phase and State
|
||||
ConnectionPhase GetPhase() const { return m_phase_fsm.GetCurrentPhase(); }
|
||||
const char* GetPhaseName() const { return m_phase_fsm.GetPhaseName(); }
|
||||
bool SetPhase(ConnectionPhase phase) { return m_phase_fsm.TransitionTo(phase); }
|
||||
void ForcePhase(ConnectionPhase phase) { m_phase_fsm.ForcePhase(phase); }
|
||||
bool IsConnected() const { return m_phase_fsm.IsConnected(); }
|
||||
bool IsInGame() const { return m_phase_fsm.IsInGame(); }
|
||||
|
||||
// Cipher Control
|
||||
CipherContext& GetCipher() { return m_cipher; }
|
||||
void EnableCipher(bool enable) { m_cipher.SetEnabled(enable); }
|
||||
void SetEncryptionKeys(const TeaKey& tea_key, uint32_t xor_seed) {
|
||||
m_cipher.InitializeKeys(tea_key, xor_seed);
|
||||
}
|
||||
|
||||
// Packet Registry
|
||||
PacketRegistry& GetRegistry() { return m_registry; }
|
||||
|
||||
// Callback Setup
|
||||
void SetPhaseCallback(PhaseStateMachine::PhaseChangeCallback cb) { m_phase_fsm.SetPhaseChangeCallback(std::move(cb)); }
|
||||
void SetOnHandshake(HandshakeCallback cb) { m_on_handshake = std::move(cb); }
|
||||
void SetOnAuthResult(AuthResultCallback cb) { m_on_auth_result = std::move(cb); }
|
||||
void SetOnCharacterList(CharacterListCallback cb) { m_on_character_list = std::move(cb); }
|
||||
void SetOnGameStart(GameStartCallback cb) { m_on_game_start = std::move(cb); }
|
||||
void SetOnChat(ChatCallback cb) { m_on_chat = std::move(cb); }
|
||||
void SetOnActorMove(ActorMoveCallback cb) { m_on_actor_move = std::move(cb); }
|
||||
void SetOnGenericPacket(GenericPacketCallback cb) { m_on_generic_packet = std::move(cb); }
|
||||
|
||||
// High-Level Sending Methods
|
||||
void SendHandshakeAck(uint32_t client_time);
|
||||
void SendLogin(const std::string& username, const std::string& password);
|
||||
void SendSelectCharacter(uint8_t slot_index);
|
||||
void SendEnterGame();
|
||||
void SendChat(uint8_t chat_type, const std::string& text);
|
||||
void SendMove(float x, float y, float z, float rotation, uint32_t move_type);
|
||||
void SendRawPacket(uint8_t header, const void* payload, size_t payload_size);
|
||||
void SendDynamicPacket(uint8_t header, const void* payload, size_t payload_size);
|
||||
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
void ProcessPacket(const Packet& packet);
|
||||
|
||||
PhaseStateMachine m_phase_fsm;
|
||||
CipherContext m_cipher;
|
||||
PacketRegistry m_registry;
|
||||
PacketBuffer m_recv_buffer;
|
||||
std::vector<uint8_t> m_send_buffer;
|
||||
|
||||
HandshakeCallback m_on_handshake;
|
||||
AuthResultCallback m_on_auth_result;
|
||||
CharacterListCallback m_on_character_list;
|
||||
GameStartCallback m_on_game_start;
|
||||
ChatCallback m_on_chat;
|
||||
ActorMoveCallback m_on_actor_move;
|
||||
GenericPacketCallback m_on_generic_packet;
|
||||
};
|
||||
|
||||
} // namespace metin2::network
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
|
||||
namespace metin2::network {
|
||||
|
||||
struct Packet {
|
||||
uint8_t header = 0;
|
||||
std::vector<uint8_t> payload;
|
||||
|
||||
size_t TotalSize() const { return 1 + payload.size(); }
|
||||
};
|
||||
|
||||
enum class PacketSizeMode {
|
||||
Fixed, // Fixed payload size (header + payload)
|
||||
Dynamic // Header (1 byte) + uint16_t size (2 bytes) + payload
|
||||
};
|
||||
|
||||
struct PacketDescriptor {
|
||||
uint8_t header = 0;
|
||||
PacketSizeMode mode = PacketSizeMode::Fixed;
|
||||
size_t fixed_payload_size = 0;
|
||||
};
|
||||
|
||||
class PacketRegistry {
|
||||
public:
|
||||
PacketRegistry();
|
||||
|
||||
void Register(uint8_t header, size_t fixed_payload_size);
|
||||
void RegisterDynamic(uint8_t header);
|
||||
|
||||
const PacketDescriptor* Find(uint8_t header) const;
|
||||
bool HasHeader(uint8_t header) const;
|
||||
|
||||
private:
|
||||
void RegisterDefaultPackets();
|
||||
std::unordered_map<uint8_t, PacketDescriptor> m_descriptors;
|
||||
};
|
||||
|
||||
class PacketBuffer {
|
||||
public:
|
||||
PacketBuffer() = default;
|
||||
|
||||
void Append(const uint8_t* data, size_t size);
|
||||
void Append(const std::vector<uint8_t>& data);
|
||||
|
||||
// Extracts all full frames available in buffer according to registry
|
||||
std::vector<Packet> ExtractPackets(const PacketRegistry& registry);
|
||||
|
||||
size_t AvailableBytes() const { return m_buffer.size() - m_read_offset; }
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> m_buffer;
|
||||
size_t m_read_offset = 0;
|
||||
|
||||
void Compact();
|
||||
};
|
||||
|
||||
} // namespace metin2::network
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
|
||||
namespace metin2::network {
|
||||
|
||||
enum class ConnectionPhase {
|
||||
Disconnected = 0,
|
||||
Handshake,
|
||||
Auth,
|
||||
CharacterSelect,
|
||||
GameLoading,
|
||||
InGame
|
||||
};
|
||||
|
||||
const char* ToString(ConnectionPhase phase);
|
||||
|
||||
enum EPacketHeader : uint8_t {
|
||||
HEADER_NONE = 0,
|
||||
HEADER_CG_LOGIN = 1,
|
||||
HEADER_CG_ATTACK = 2,
|
||||
HEADER_CG_CHAT = 3,
|
||||
HEADER_CG_CHARACTER_CREATE = 4,
|
||||
HEADER_CG_CHARACTER_DELETE = 5,
|
||||
HEADER_CG_CHARACTER_SELECT = 6,
|
||||
HEADER_CG_MOVE = 7,
|
||||
HEADER_CG_SYNC_POSITION = 8,
|
||||
HEADER_CG_ENTERGAME = 10,
|
||||
HEADER_CG_ITEM_USE = 11,
|
||||
HEADER_CG_ITEM_MOVE = 12,
|
||||
HEADER_CG_PONG = 254,
|
||||
HEADER_CG_HANDSHAKE = 255,
|
||||
|
||||
HEADER_GC_AUTH_SUCCESS = 101,
|
||||
HEADER_GC_AUTH_FAILURE = 102,
|
||||
HEADER_GC_CHARACTER_INFO = 103,
|
||||
HEADER_GC_CHARACTER_LIST = 104,
|
||||
HEADER_GC_GAME_START = 105,
|
||||
HEADER_GC_CHAT = 106,
|
||||
HEADER_GC_ACTOR_ADD = 107,
|
||||
HEADER_GC_ACTOR_REMOVE = 108,
|
||||
HEADER_GC_ACTOR_MOVE = 109,
|
||||
HEADER_GC_ACTOR_ATTACK = 110,
|
||||
HEADER_GC_ITEM_SET = 111,
|
||||
HEADER_GC_PING = 253,
|
||||
HEADER_GC_HANDSHAKE = 255
|
||||
};
|
||||
|
||||
struct CharacterSummary {
|
||||
uint32_t id = 0;
|
||||
std::string name;
|
||||
uint8_t job = 0; // Warrior, Assassin, Sura, Shaman
|
||||
uint8_t level = 1;
|
||||
uint32_t play_time = 0;
|
||||
uint16_t body_armor = 0;
|
||||
uint16_t weapon = 0;
|
||||
uint16_t hair = 0;
|
||||
};
|
||||
|
||||
struct PlayerInfo {
|
||||
uint32_t player_id = 0;
|
||||
std::string name;
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
float rotation = 0.0f;
|
||||
uint32_t hp = 100;
|
||||
uint32_t max_hp = 100;
|
||||
uint32_t sp = 100;
|
||||
uint32_t max_sp = 100;
|
||||
};
|
||||
|
||||
} // namespace metin2::network
|
||||
Reference in New Issue
Block a user