Files
mtgodot-poc/extension/src/net/classic/classic_parser.h
T
shenleiandClaude Sonnet 5 b296457e3c 40250 classic: increment 50 (Phase 1 W1) — §1.5 client version, §1.10 text codec, §2.2 two-packet merge
§1.5 CG_CLIENT_VERSION: send_client_version() now branches on locale. EUROPE
(this project's locale) sends CGClientVersion2 (header 0xf1, 67B, timestamp
"1215955205"); non-EUROPE sends CGClientVersion (0xfd) + CMake-injected
MT_BUILD_TIMESTAMP in C __TIMESTAMP__ form. New ClassicSession::set_executable_name()
(default "metin2.bin"; TODO(W0): M2Client passes OS::get_executable_path().get_file()).
m_version_sent set only after send_fixed() succeeds. wire_classic.h gains
CGClientVersion2 + size-table entry.

§1.10 fixed-length text: new extension/src/net/text_codec.h — to_wire()/
from_wire_str() apply a hard byte cap without splitting a UTF-8 multibyte
sequence; godot::String overloads are __has_include-guarded so mtnet stays
godot-free. classic_session.cpp routes 8 fixed-length name/sign/comment fills
through to_wire(); parser reads GC_CHAR_ADDITIONAL_INFO name via from_wire_str().
New net_text_codec_test (ctest net.text_codec). Locale codepage (CP949/CP1252)
transcoding remains a follow-up; wire bytes are currently UTF-8 (ASCII round-trips).

§2.2 two-packet PC/NPC merge: ClassicParser GC_CHARACTER_ADD drops invisible
races (20025/20038/20039); PC/NPC only stash a bare Entity into m_pending_actor;
everything else spawns immediately with a name from root/npclist.txt (new
npc_names.h + set_npclist_path(); $MT_ASSETS/$M2_ASSETS fallback; TODO(W0):
M2Client passes the asset root). GC_CHAR_ADDITIONAL_INFO with no pending entry
records m_last_error + m_unhandled_headers and drops (never half-creates);
on a hit it merges the pending Entity with the additional-info's 9 fields into
one mut_spawn_full() then erases the pending entry. New pending_actor_count()
test hook; net_classic_session_test updated (version assertions -> 0xf1;
stash -> merge -> spawn, immediate monster spawn, invisible-race drop,
no-pending additional-info is a no-op). Depends on W2 §2.2 steps 4-5
(entity_store mut_char_info touch / mut_shop_sign cleanup) — the merge works
with current W2 code as-is.

Build + ctest 17/17 (16 + net.text_codec); netbridge_test, p10_test green.
Docs: CLIENT-GAP.md W1 row + increment-50 change-log entry; CLIENT-GAP-FIX.md
§1.5/§1.10/§2.2 status + facts and C.5 batch record.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yvAPqPivoY7vmBbzgqK4W
2026-09-02 15:51:48 +09:00

144 lines
4.7 KiB
C++

#pragma once
// ClassicParser — 40250 GC packet bodies -> shared world model (EntityStore via
// its protocol-neutral mut_* API) + login/char-list state.
// Counterpart of EntityStore::apply() for MT_PROTOCOL=classic.
// See docs/CLIENT-40250-PORT.md §4 / §5.
//
// `body` is the bytes AFTER the 1-byte header (for dynamic packets: after the
// [header][uint16 size] prefix); `len` its length — i.e. exactly what
// ClassicStream::on_packet delivers.
#include "npc_names.h"
#include "wire_classic.h"
#include "../entity_store.h"
#include <cstdint>
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>
namespace mtnet::classic {
struct CharSlot {
uint32_t id = 0;
std::string name;
uint8_t job = 0;
uint8_t level = 0;
uint8_t st = 0, ht = 0, dx = 0, iq = 0;
uint32_t play_minutes = 0;
int32_t x = 0, y = 0;
int32_t addr = 0;
uint16_t port = 0;
uint16_t main_part = 0, hair_part = 0;
uint8_t skill_group = 0;
// Server requires this character to be renamed before entering the game.
bool change_name = false;
uint32_t guild_id = 0;
std::string guild_name;
bool empty() const { return id == 0 && name.empty(); }
};
class ClassicParser {
public:
struct CharEvent {
enum Kind { CreateOk, CreateFail, DeleteOk, DeleteFail } kind;
int slot = -1;
int fail_type = 0;
};
struct NameEvent {
uint32_t pid = 0;
std::string name;
};
explicit ClassicParser(EntityStore &world) : m_world(world) {}
// one GC packet. returns false to abort the connection.
bool on_gc(uint8_t header, const uint8_t *body, uint32_t len);
// --- login / select state ---
const std::vector<CharSlot> &char_slots() const { return m_slots; }
int slot_count() const { return m_slot_count; } // 3 or 4
uint32_t handle() const { return m_handle; }
uint32_t random_key() const { return m_random_key; }
bool char_list_ready() const { return m_char_list_ready; }
const std::string &login_failure() const { return m_login_failure; }
uint32_t login_key() const { return m_login_key; } // GC_LOGIN_KEY (auth path)
uint8_t empire() const { return m_empire; }
void set_phase_name(const std::string &name) { m_phase_name = name; }
const std::string &last_error() const { return m_last_error; }
// §2.2: path to root/npclist.txt (race -> model-code table). Used to name
// monster / stone / object spawns, mirroring CPythonNonPlayer::GetName().
// Empty path -> names resolve to "" (permitted). Falls back to
// $MT_ASSETS / $M2_ASSETS when never set.
// TODO(W0): M2Client should call
// parser().set_npclist_path(AssetRoot::path("root/npclist.txt"))
void set_npclist_path(std::string path);
// §2.2 test hook: number of GC_CHARACTER_ADD records awaiting their
// GC_CHAR_ADDITIONAL_INFO merge.
size_t pending_actor_count() const { return m_pending_actor.size(); }
std::vector<uint8_t> drain_unhandled_headers() {
auto v = std::move(m_unhandled_headers);
m_unhandled_headers.clear();
return v;
}
std::vector<CharEvent> drain_char_events() {
auto v = std::move(m_char_events);
m_char_events.clear();
return v;
}
std::vector<NameEvent> drain_name_events() {
auto v = std::move(m_name_events);
m_name_events.clear();
return v;
}
int drain_guild_make_requests() {
int count = m_guild_make_requests;
m_guild_make_requests = 0;
return count;
}
private:
template <class T>
static bool fill(T &t, uint8_t header, const uint8_t *body, uint32_t len) {
if (len + 1u < sizeof(T)) {
return false;
}
t.header = header;
std::memcpy(reinterpret_cast<uint8_t *>(&t) + 1, body, sizeof(T) - 1);
return true;
}
void parse_login_success(const SimplePlayer *players, const uint32_t *guild_id,
const char (*guild_name)[GUILD_NAME_MAX_LEN + 1], int n, uint32_t handle,
uint32_t rkey);
// Lazily loads npclist.txt on first use; returns "" for an unknown race or
// when no list is available.
const std::string &npc_name_for(uint16_t race);
EntityStore &m_world;
// §2.2 two-packet PC/NPC merge: GC_CHARACTER_ADD stages a bare Entity here
// keyed by vid; GC_CHAR_ADDITIONAL_INFO merges its fields and flushes the
// combined record via m_world.mut_spawn_full(). W1 owns the merge logic.
std::unordered_map<uint32_t, Entity> m_pending_actor;
std::string m_npclist_path;
std::unordered_map<uint32_t, std::string> m_npc_names;
bool m_npc_names_loaded = false;
std::vector<CharSlot> m_slots;
int m_slot_count = 0;
uint32_t m_handle = 0;
uint32_t m_random_key = 0;
uint32_t m_login_key = 0;
uint8_t m_empire = 0;
std::vector<CharEvent> m_char_events;
std::vector<NameEvent> m_name_events;
int m_guild_make_requests = 0;
bool m_char_list_ready = false;
std::string m_login_failure;
std::string m_phase_name = "Offline";
std::string m_last_error;
std::vector<uint8_t> m_unhandled_headers;
};
} // namespace mtnet::classic