Net: port the select-phase packets

Character create/delete/rename and empire selection now reach the
server, and LOGIN_SUCCESS3, PLAYER_CREATE_SUCCESS/FAILURE,
PLAYER_DELETE_SUCCESS/WRONG_SOCIAL_ID, POINT_CHANGE and CHANGE_NAME are
parsed and forwarded to the select/create windows like Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-13 13:27:52 +09:00
co-authored by Claude Opus 5.5
parent 40cac5cf52
commit 31c74cec28
10 changed files with 205 additions and 15 deletions
+5
View File
@@ -131,6 +131,7 @@ bool LoginClient::advance() {
}
if(on_game_ && session_->handshake_phase_entered() && !handshake_notified_) { handshake_notified_=true;if(!direct_enter_) notify(EventType::Handshake); }
if(on_game_ && session_->game_login_sent() && !login_notified_) { login_notified_=true;if(!direct_enter_) notify(EventType::LoginStart); }
for(auto& result:session_->take_select_results()) { notify(EventType::SelectResult);events_.back().result=std::move(result); }
// __RecvLoginSuccessPacket4 in SelectPhase: PHASE_WINDOW_SELECT.Refresh.
if(session_->state()==LoginSession::State::Selecting && selection_notified_ && !direct_enter_ && session_->roster_version()!=roster_version_)
notify(EventType::SelectRefresh);
@@ -186,6 +187,10 @@ void LoginClient::reconnect(std::uint32_t wire_address,std::uint16_t port,std::u
deadline_=std::chrono::steady_clock::now()+std::chrono::seconds(15);
start_endpoint({host,port});
}
void LoginClient::send_select_packet(wire::Bytes packet) {
if(!session_ || status_!=Status::Selecting) throw std::logic_error("not in the select phase");
session_->send_select_packet(std::move(packet));enqueue();flush();
}
void LoginClient::select(std::uint8_t slot) {
if(!session_) throw std::logic_error("not connected");session_->select(slot);selected_slot_=slot;status_=Status::Entering;deadline_=std::chrono::steady_clock::now()+std::chrono::seconds(15);enqueue();
}
+5 -2
View File
@@ -9,8 +9,8 @@ class LoginClient {
public:
struct Endpoint { std::string host; int port=0; };
enum class Status { Idle, Resolving, Connecting, Authenticating, Selecting, Entering, Loading, Failed };
enum class EventType { Handshake, RemoteDisconnect, LoginStart, SelectReady, SelectRefresh, DirectEnterReady, Loading, Failure };
struct Event { EventType type; std::string reason; bool connection_failure=false; std::uint64_t generation=0; };
enum class EventType { Handshake, RemoteDisconnect, LoginStart, SelectReady, SelectRefresh, SelectResult, DirectEnterReady, Loading, Failure };
struct Event { EventType type; std::string reason; bool connection_failure=false; std::uint64_t generation=0; ava2::SelectResult result{}; };
LoginClient()=default;
~LoginClient();
LoginClient(const LoginClient&)=delete;
@@ -20,6 +20,9 @@ public:
void pump(); // no blocking socket operation or DNS lookup on the caller thread
void update_player_guild(const std::string& player,uint32_t id,const std::string& name) {if(session_)session_->update_player_guild(player,id,name);}
void select(std::uint8_t slot);
// SendCreateCharacterPacket/SendDestroyCharacterPacket/SendSelectEmpirePacket/SendChangeNamePacket.
void send_select_packet(wire::Bytes packet);
void set_empire(std::uint8_t empire) { if(session_) session_->set_empire(empire); }
void direct_enter(std::uint8_t slot);
// CPythonNetworkStream::RecvWarpPacket: __DirectEnterMode_Set(m_dwSelectedCharacterIndex, true) + Connect(lAddr, wPort).
void warp(std::uint32_t address,std::uint16_t port);
+58 -9
View File
@@ -13,6 +13,18 @@ void text(Bytes& out,const std::string& value,std::size_t max) {
std::string string_at(std::span<const std::uint8_t> b,std::size_t offset,std::size_t width) {
auto end=std::find(b.begin()+offset,b.begin()+offset+width,0); return {b.begin()+offset,end};
}
// TSimplePlayerInformation (70 bytes, sash + conqueror fields enabled).
Character read_player(std::span<const std::uint8_t> packet,std::size_t o) {
Character c;
c.id=read32(packet,o);c.name=string_at(packet,o+4,25);c.job=packet[o+29];c.level=packet[o+30];
c.play_minutes=read32(packet,o+31);c.strength=packet[o+35];c.health=packet[o+36];c.dexterity=packet[o+37];c.intelligence=packet[o+38];
c.main_part=packet[o+39]|(std::uint16_t(packet[o+40])<<8);c.change_name=packet[o+41];
c.hair_part=packet[o+42]|(std::uint16_t(packet[o+43])<<8);c.sash_part=packet[o+44]|(std::uint16_t(packet[o+45])<<8);
for(std::size_t j=0;j<4;++j) c.sungma[j]=packet[o+66+j];
c.x=static_cast<std::int32_t>(read32(packet,o+50));c.y=static_cast<std::int32_t>(read32(packet,o+54));
c.address=read32(packet,o+58);c.port=packet[o+62]|(std::uint16_t(packet[o+63])<<8);c.skill_group=packet[o+64];c.conqueror_level=packet[o+65];
return c;
}
}
LoginSession::LoginSession(std::string account,std::string password,Key client)
:account_(std::move(account)),password_(std::move(password)),client_key_(client),receive_key_(derive_receive_key(client)),tx_(locale_key()),rx_(locale_key()) {
@@ -74,6 +86,13 @@ bool LoginSession::process_packet() {
break;
case 150:size=6;break; case 7:size=10;break; case 90:size=2;break;
case 32:size=444;break; // 5 * (70-byte player + 4-byte guild id + 13-byte guild name) + 9
// SelectPhase (PythonNetworkStreamPhaseSelect.cpp)
case 6:size=270;break; // LOGIN_SUCCESS3: 3 * (70 + 4 + 13) + 9
case 8:size=72;break; // PLAYER_CREATE_SUCCESS: slot + TSimplePlayerInformation
case 9: case 10:size=2;break; // PLAYER_CREATE_FAILURE(bType) / PLAYER_DELETE_SUCCESS(account_index)
case 11:size=1;break; // PLAYER_DELETE_WRONG_SOCIAL_ID (TPacketGCBlank)
case 17:size=25;break; // PLAYER_POINT_CHANGE (int header): received and dropped
case 107:size=30;break; // CHANGE_NAME: pid + name[25]
default:
if(state_==State::Entering) {
state_=State::Loading;
@@ -121,17 +140,43 @@ bool LoginSession::process_packet() {
++roster_version_;
characters_.clear();
for(std::size_t i=0;i<5;++i) {
std::size_t o=1+i*70; Character c;
c.id=read32(packet,o);c.name=string_at(packet,o+4,25);c.job=packet[o+29];c.level=packet[o+30];
c.play_minutes=read32(packet,o+31);c.strength=packet[o+35];c.health=packet[o+36];c.dexterity=packet[o+37];c.intelligence=packet[o+38];
c.main_part=packet[o+39]|(std::uint16_t(packet[o+40])<<8);c.change_name=packet[o+41];
c.hair_part=packet[o+42]|(std::uint16_t(packet[o+43])<<8);c.sash_part=packet[o+44]|(std::uint16_t(packet[o+45])<<8);
c.guild_id=read32(packet,351+i*4);c.guild_name=string_at(packet,371+i*13,13);
for(std::size_t j=0;j<4;++j) c.sungma[j]=packet[o+66+j];
c.x=static_cast<std::int32_t>(read32(packet,o+50));c.y=static_cast<std::int32_t>(read32(packet,o+54));
c.address=read32(packet,o+58);c.port=packet[o+62]|(std::uint16_t(packet[o+63])<<8);c.skill_group=packet[o+64];c.conqueror_level=packet[o+65];characters_.push_back(std::move(c));
auto c=read_player(packet,1+i*70);
c.guild_id=read32(packet,351+i*4);c.guild_name=string_at(packet,371+i*13,13);characters_.push_back(std::move(c));
}
roster_received_=true; if(select_phase_received_) state_=State::Selecting;
} else if(header==6) {
// __RecvLoginSuccessPacket3: only the first PLAYER_PER_ACCOUNT3 slots are overwritten.
if(!game_ || (state_!=State::LoggingIn && state_!=State::Selecting)) { fail("character roster in wrong state"); return false; }
++roster_version_;characters_.resize(5);
for(std::size_t i=0;i<3;++i) {
auto c=read_player(packet,1+i*70);
c.guild_id=read32(packet,211+i*4);c.guild_name=string_at(packet,223+i*13,13);characters_[i]=std::move(c);
}
roster_received_=true; if(select_phase_received_) state_=State::Selecting;
} else if(header==8 || header==9 || header==10 || header==11 || header==17 || header==107) {
if(!game_ || (state_!=State::LoggingIn && state_!=State::Selecting && state_!=State::Entering)) { fail("select packet in wrong state"); return false; }
if(header==8) {
// __RecvPlayerCreateSuccessPacket: m_akSimplePlayerInfo only; guild id/name stay.
auto slot=packet[1];
if(slot<characters_.size()) {
auto c=read_player(packet,2);c.guild_id=characters_[slot].guild_id;c.guild_name=characters_[slot].guild_name;
characters_[slot]=std::move(c);
select_results_.push_back({SelectResult::Kind::CreateSuccess});
}
} else if(header==9) select_results_.push_back({SelectResult::Kind::CreateFailure,packet[1]});
else if(header==10) {
auto slot=packet[1];
if(slot<characters_.size()) characters_[slot]=Character{};
select_results_.push_back({SelectResult::Kind::DeleteSuccess,slot});
} else if(header==11) select_results_.push_back({SelectResult::Kind::DeleteFailure});
else if(header==107) {
auto pid=read32(packet,1);auto name=string_at(packet,5,25);
auto it=std::find_if(characters_.begin(),characters_.end(),[&](const Character& c) { return c.id==pid; });
if(it!=characters_.end()) {
it->change_name=0;it->name=name;
select_results_.push_back({SelectResult::Kind::ChangeName,int(it-characters_.begin()),name});
} else select_results_.push_back({SelectResult::Kind::CreateFailure,100});
}
} else if(header==90) {
if(!game_ || (state_!=State::LoggingIn && state_!=State::Selecting)) fail("empire packet in wrong state");
else empire_=packet[1];
@@ -148,6 +193,10 @@ void LoginSession::send_game_packet(wire::Bytes packet,bool sequence) {
if(packet.empty() || packet.size()>65535) throw std::invalid_argument("invalid game packet size");
emit(std::move(packet),sequence);
}
void LoginSession::send_select_packet(wire::Bytes packet) {
if(state_!=State::Selecting) throw std::logic_error("select packet outside the select phase");
emit(std::move(packet),true);
}
void LoginSession::select(std::uint8_t slot) {
if(state_!=State::Selecting) throw std::logic_error("selection requires character roster and select phase");
if(slot>=characters_.size() || characters_[slot].id==0) throw std::invalid_argument("cannot select an empty character slot");
+11
View File
@@ -12,6 +12,12 @@ struct Character {
std::array<std::uint8_t,4> sungma{};
std::int32_t x=0,y=0; std::uint32_t address=0; std::uint16_t port=0;
};
// SelectPhase results for the Python phase windows (OnCreateSuccess/OnCreateFailure/OnDeleteSuccess/
// OnDeleteFailure/OnChangeName).
struct SelectResult {
enum class Kind { CreateSuccess, CreateFailure, DeleteSuccess, DeleteFailure, ChangeName } kind;
int value=0; std::string name;
};
class LoginSession {
public:
enum class State { Handshake, Authenticating, Authenticated, LoggingIn, Selecting, Entering, Loading, Failed };
@@ -27,6 +33,10 @@ public:
for(auto& c:characters_)if(c.name==player) {c.guild_id=id;c.guild_name=name;}
}
void select(std::uint8_t slot);
// SelectPhase senders: Send(...) + SendSequence() on the select connection.
void send_select_packet(wire::Bytes packet);
void set_empire(std::uint8_t empire) { empire_=empire; } // SendSelectEmpirePacket: SetEmpireID
std::vector<SelectResult> take_select_results() { auto r=std::move(select_results_);select_results_.clear();return r; }
// Windows networkmodule.py: net.SetTCPRecvBufferSize(128*1024). A full buffer pauses recv() (restSize<=0).
static constexpr std::size_t kRecvBufferSize=128*1024;
std::size_t receive_space() const { return kRecvBufferSize-(raw_.size()+plain_.size()); }
@@ -68,6 +78,7 @@ private:
wire::Bytes raw_,plain_;
std::vector<wire::Bytes> output_;
std::vector<Character> characters_;
std::vector<SelectResult> select_results_;
std::vector<wire::Bytes> pack_metadata_;
};
}
+33
View File
@@ -1,5 +1,6 @@
#include "protocol.h"
#include "eter_net/Packet.h"
#include <algorithm>
#include <stdexcept>
#include <cstring>
@@ -19,6 +20,38 @@ Bytes select_character(std::uint8_t slot) {
return {HEADER_CG_CHARACTER_SELECT, slot};
}
namespace {
// strncpy into a zeroed char[width]: at most width-1 bytes keep the terminator (CHARACTER_NAME_MAX_LEN+1 field).
void fixed(Bytes& out, const std::string& value, std::size_t width, std::size_t copy) {
auto start=out.size(); out.resize(start+width,0);
std::copy_n(value.begin(), std::min({value.size(), copy, width}), out.begin()+start);
}
}
Bytes create_character(std::uint8_t slot, const std::string& name, std::uint16_t job, std::uint8_t shape,
std::uint8_t con, std::uint8_t intelligence, std::uint8_t str, std::uint8_t dex) {
Bytes out{HEADER_CG_PLAYER_CREATE, slot};
// strncpy(name, name, CHARACTER_NAME_MAX_LEN): the 25th byte stays 0.
fixed(out, name, CHARACTER_NAME_MAX_LEN+1, CHARACTER_NAME_MAX_LEN);
u16(out, job);
out.insert(out.end(), {shape, con, intelligence, str, dex});
return out;
}
Bytes destroy_character(std::uint8_t slot, const std::string& private_code) {
Bytes out{HEADER_CG_PLAYER_DESTROY, slot};
fixed(out, private_code, 8, 8-1); // strncpy(szPrivateCode, code, PRIVATE_CODE_LENGTH-1)
return out;
}
Bytes select_empire(std::uint8_t empire) { return {HEADER_CG_EMPIRE, empire}; }
Bytes change_name(std::uint8_t slot, const std::string& name) {
Bytes out{HEADER_CG_CHANGE_NAME, slot};
fixed(out, name, CHARACTER_NAME_MAX_LEN+1, CHARACTER_NAME_MAX_LEN);
return out;
}
Bytes attack(std::uint8_t type, std::uint32_t victim, std::uint8_t process_crc, std::uint8_t file_crc) {
Bytes out{HEADER_CG_ATTACK, type};
u32(out, victim);
+6
View File
@@ -8,6 +8,12 @@ using Bytes = std::vector<std::uint8_t>;
// AVA2 Packet.h, packed Win32 wire format. Independent from host ABI.
Bytes select_character(std::uint8_t slot);
// CPythonNetworkStream SelectPhase senders (TPacketCGCreateCharacter/DestroyCharacter/Empire/ChangeName).
Bytes create_character(std::uint8_t slot, const std::string& name, std::uint16_t job, std::uint8_t shape,
std::uint8_t con, std::uint8_t intelligence, std::uint8_t str, std::uint8_t dex);
Bytes destroy_character(std::uint8_t slot, const std::string& private_code);
Bytes select_empire(std::uint8_t empire);
Bytes change_name(std::uint8_t slot, const std::string& name);
Bytes attack(std::uint8_t type, std::uint32_t victim, std::uint8_t process_crc = 0, std::uint8_t file_crc = 0);
Bytes pickup(std::uint32_t vid);
Bytes character_move(std::uint8_t func, std::uint8_t arg, std::uint8_t rot, std::int32_t x, std::int32_t y, std::uint32_t time);