AVA2 cross-platform client parity baseline

This commit is contained in:
shen
2026-10-06 00:53:42 -07:00
commit 9f748eb05a
3271 changed files with 1275528 additions and 0 deletions
+731
View File
@@ -0,0 +1,731 @@
#include "python_net.h"
#include "login_client.h"
#include "protocol.h"
#include "eter_net/PythonNetworkStream.h"
#include "eter_net/NetworkActorManager.h"
#include <Python.h>
#include <array>
#include <chrono>
#include <stdexcept>
#include <algorithm>
namespace ava2 {
namespace {
LoginClient client;
PyObject* handler=nullptr;
PyObject* account_handler=nullptr;
std::array<PyObject*,7> phases{};
std::string account,password;
std::array<Character, 5> mock_characters{};
bool has_mock_characters = false;
std::uint8_t mock_empire = 0;
bool has_mock_empire = false;
void replace(PyObject*& old,PyObject* next) {
if(next==Py_None || (next && PyInt_Check(next) && PyInt_AsLong(next)==0)) next=nullptr;
Py_XINCREF(next);
auto* previous=old;
old=next;
Py_XDECREF(previous);
}
void callback(PyObject* target,const char* name,const char* text=nullptr) {
if(!target) return;
Py_INCREF(target);
auto* method=PyObject_GetAttrString(target,name);
if(!method) {
Py_DECREF(target);
throw std::runtime_error(std::string("missing Python callback: ")+name);
}
auto* result=text?PyObject_CallFunction(method,const_cast<char*>("s"),text):PyObject_CallObject(method,nullptr);
Py_DECREF(method);
Py_DECREF(target);
if(!result) throw std::runtime_error(std::string("Python callback failed: ")+name);
Py_DECREF(result);
}
PyObject* set_handler(PyObject*,PyObject* args) { PyObject* value;if(!PyArg_ParseTuple(args,"O",&value)) return nullptr;replace(handler,value);PythonNetworkStream::Instance().SetHandler(value);Py_RETURN_NONE; }
PyObject* set_account_handler(PyObject*,PyObject* args) { PyObject* value;if(!PyArg_ParseTuple(args,"O",&value)) return nullptr;replace(account_handler,value);Py_RETURN_NONE; }
PyObject* set_phase(PyObject*,PyObject* args) { int phase;PyObject* value;if(!PyArg_ParseTuple(args,"iO",&phase,&value)) return nullptr;if(phase<0 || phase>=7) { PyErr_SetString(PyExc_ValueError,"invalid phase window");return nullptr; }replace(phases[phase],value);PythonNetworkStream::Instance().SetPhaseWindow(phase, value);Py_RETURN_NONE; }
PyObject* clear_phase(PyObject*,PyObject* args) { int phase;PyObject* value;if(!PyArg_ParseTuple(args,"iO",&phase,&value)) return nullptr;if(phase<0 || phase>=7) { PyErr_SetString(PyExc_ValueError,"invalid phase window");return nullptr; }if(phases[phase]==value) { replace(phases[phase],nullptr);PythonNetworkStream::Instance().ClearPhaseWindow(phase, value); }Py_RETURN_NONE; }
PyObject* set_login(PyObject*,PyObject* args) { const char* id;const char* pass;if(!PyArg_ParseTuple(args,"ss",&id,&pass)) return nullptr;account=id;password=pass;Py_RETURN_NONE; }
PyObject* connect(PyObject*,PyObject* args) {
const char* game;const char* auth;int game_port,auth_port;
if(!PyArg_ParseTuple(args,"sisi",&game,&game_port,&auth,&auth_port)) return nullptr;
try { client.connect({auth,auth_port},{game,game_port},account,password);PythonNetworkStream::Instance().ResetConnection();has_mock_characters=has_mock_empire=false;mock_characters.fill({});mock_empire=0;std::fill(password.begin(),password.end(),0);password.clear();Py_RETURN_NONE; }
catch(const std::exception& e) { PyErr_SetString(PyExc_ValueError,e.what());return nullptr; }
}
PyObject* disconnect(PyObject*,PyObject*) { client.cancel();PythonNetworkStream::Instance().ResetConnection();std::fill(password.begin(),password.end(),0);password.clear();Py_RETURN_NONE; }
PyObject* sequence_mode(PyObject*,PyObject*) { Py_RETURN_NONE; }
PyObject* select(PyObject*,PyObject* args) {
int slot;
if(!PyArg_ParseTuple(args,"i",&slot)) return nullptr;
if(slot<0 || slot>=5) { PyErr_SetString(PyExc_ValueError,"invalid character slot");return nullptr; }
if(has_mock_characters) Py_RETURN_NONE;
try { client.select(slot);Py_RETURN_NONE; }
catch(const std::exception& e) { PyErr_SetString(PyExc_ValueError,e.what());return nullptr; }
}
PyObject* direct_enter(PyObject*,PyObject* args) {
int slot;
if(!PyArg_ParseTuple(args,"i",&slot)) return nullptr;
if(slot<0 || slot>=5) { PyErr_SetString(PyExc_ValueError,"invalid character slot");return nullptr; }
if(has_mock_characters) { PyErr_SetString(PyExc_RuntimeError,"DirectEnter unavailable for preview characters");return nullptr; }
try { client.direct_enter(static_cast<std::uint8_t>(slot));Py_RETURN_NONE; }
catch(const std::exception& e) { PyErr_SetString(PyExc_ValueError,e.what());return nullptr; }
}
const Character* character(int index) {
if(index<0 || index>=5) { PyErr_SetString(PyExc_ValueError,"invalid character slot");return nullptr; }
if(has_mock_characters) return &mock_characters[index];
auto* session=client.session();
if(!session || session->characters().size()!=5) return nullptr;
return &session->characters()[index];
}
PyObject* get_integer(PyObject*,PyObject* args) {
int index,type;if(!PyArg_ParseTuple(args,"ii",&index,&type)) return nullptr;auto* c=character(index);if(PyErr_Occurred()) return nullptr;
if(type<0 || type>21 || type==1 || type==13) { PyErr_SetString(PyExc_ValueError,"invalid integer character field");return nullptr; }
if(!c) return PyLong_FromLong(0);
std::uint32_t value=0;
switch(type) {
case 0:value=c->id;break;case 2:value=c->job;break;case 3:value=c->level;break;case 4:value=c->strength;break;
case 5:value=c->dexterity;break;case 6:value=c->health;break;case 7:value=c->intelligence;break;case 8:value=c->play_minutes;break;
case 9:value=c->main_part;break;case 10:value=c->address;break;case 11:value=c->port;break;case 12:value=c->guild_id;break;
case 14:value=c->change_name;break;case 15:value=c->hair_part;break;case 16:value=c->sash_part;break;case 17:value=c->conqueror_level;break;
default:value=c->sungma[type-18];break;
}
return PyLong_FromUnsignedLong(value);
}
PyObject* get_string(PyObject*,PyObject* args) { int index,type;if(!PyArg_ParseTuple(args,"ii",&index,&type)) return nullptr;auto* c=character(index);if(PyErr_Occurred()) return nullptr;if(type!=1 && type!=13) { PyErr_SetString(PyExc_ValueError,"invalid string character field");return nullptr; }return PyString_FromString(c?(type==1?c->name.c_str():c->guild_name.c_str()):""); }
PyObject* empire(PyObject*,PyObject*) { return PyInt_FromLong(has_mock_empire ? mock_empire : (client.session()?client.session()->empire():0)); }
PyObject* update(PyObject*,PyObject*) { try { pump_python_net();Py_RETURN_NONE; } catch(const std::exception& e) { if(!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError,e.what());return nullptr; } }
PyObject* status(PyObject*,PyObject*) { const char* names[]={"idle","resolving","connecting","authenticating","selecting","entering","loading","failed"};return PyString_FromString(names[static_cast<int>(client.status())]); }
PyObject* is_connected(PyObject*,PyObject*) { return PyInt_FromLong(client.is_connected()); }
PyObject* net_dummy_none(PyObject*, PyObject*) { Py_RETURN_NONE; }
PyObject* net_dummy_zero(PyObject*, PyObject*) { return PyInt_FromLong(0); }
PyObject* net_dummy_one(PyObject*, PyObject*) { return PyInt_FromLong(1); }
PyObject* net_dummy_str(PyObject*, PyObject*) { return PyString_FromString(""); }
PyObject* net_dummy_tuple4(PyObject*, PyObject*) { return Py_BuildValue("iiii", 0, 0, 0, 0); }
PyObject* net_get_main_actor_vid(PyObject*, PyObject*) {
uint32_t vid = PythonNetworkStream::Instance().GetMainActorVID();
if (vid == 0 && has_mock_characters) vid = 1001;
return PyLong_FromUnsignedLong(vid);
}
PyObject* net_get_main_actor_race(PyObject*, PyObject*) {
return PyInt_FromLong(PythonNetworkStream::Instance().GetMainActorRace());
}
PyObject* net_get_main_actor_empire(PyObject*, PyObject*) {
return PyInt_FromLong(PythonNetworkStream::Instance().GetMainActorEmpire());
}
PyObject* net_get_main_actor_skill_group(PyObject*, PyObject*) {
return PyInt_FromLong(PythonNetworkStream::Instance().GetMainActorSkillGroup());
}
PyObject* net_get_field_music_name(PyObject*, PyObject*) {
return PyString_FromString(PythonNetworkStream::Instance().GetFieldMusicFileName().c_str());
}
PyObject* net_get_field_music_volume(PyObject*, PyObject*) {
return PyFloat_FromDouble(PythonNetworkStream::Instance().GetFieldMusicVolume());
}
PyObject* net_start_game(PyObject*, PyObject*) {
PythonNetworkStream::Instance().StartGame();
Py_RETURN_NONE;
}
PyObject* net_send_enter_game(PyObject*, PyObject*) {
PythonNetworkStream::Instance().SendEnterGamePacket();
Py_RETURN_NONE;
}
PyObject* net_send_chat(PyObject*, PyObject* args) {
char* text = nullptr;
int type = 0;
if (PyArg_ParseTuple(args, "s|i", &text, &type)) {
if (text) {
PythonNetworkStream::Instance().SendChatPacket(text, static_cast<uint8_t>(type));
}
}
Py_RETURN_NONE;
}
PyObject* net_send_whisper(PyObject*, PyObject* args) {
char* name = nullptr;
char* text = nullptr;
if (PyArg_ParseTuple(args, "ss", &name, &text)) {
if (name && text) {
PythonNetworkStream::Instance().SendWhisperPacket(name, text);
}
}
Py_RETURN_NONE;
}
PyObject* net_send_item_pickup(PyObject*, PyObject* args) {
int vid = 0;
if (PyArg_ParseTuple(args, "i", &vid)) {
PythonNetworkStream::Instance().SendItemPickUpPacket(static_cast<uint32_t>(vid));
}
Py_RETURN_NONE;
}
PyObject* net_send_item_drop(PyObject*, PyObject* args) {
int win=1,slot=0;
if(PyTuple_Size(args)==1) {if(!PyArg_ParseTuple(args,"i",&slot)) return nullptr;}
else if(!PyArg_ParseTuple(args,"ii",&win,&slot)) return nullptr;
try {PythonNetworkStream::Instance().SendItemDropPacket(uint8_t(win),uint16_t(slot),0);}
catch(const std::exception& e) {PyErr_SetString(PyExc_RuntimeError,e.what());return nullptr;}
Py_RETURN_NONE;
}
template<bool New> PyObject* net_send_gold_drop(PyObject*,PyObject* args) {
int gold;if(!PyArg_ParseTuple(args,"i",&gold)) return nullptr;
try {
if constexpr(New) PythonNetworkStream::Instance().SendItemDropPacketNew(0,0,uint32_t(gold),0);
else PythonNetworkStream::Instance().SendItemDropPacket(0,0,uint32_t(gold));
} catch(const std::exception& e) {PyErr_SetString(PyExc_RuntimeError,e.what());return nullptr;}
Py_RETURN_NONE;
}
PyObject* net_send_item_drop_new(PyObject*, PyObject* args) {
int win=1,slot=0,count=0;
if(PyTuple_Size(args)==2) {if(!PyArg_ParseTuple(args,"ii",&slot,&count)) return nullptr;}
else if(!PyArg_ParseTuple(args,"iii",&win,&slot,&count)) return nullptr;
try {PythonNetworkStream::Instance().SendItemDropPacketNew(uint8_t(win),uint16_t(slot),0,uint32_t(count));}
catch(const std::exception& e) {PyErr_SetString(PyExc_RuntimeError,e.what());return nullptr;}
Py_RETURN_NONE;
}
PyObject* net_send_onclick(PyObject*, PyObject* args) {
int vid = 0;
if (PyArg_ParseTuple(args, "i", &vid)) {
PythonNetworkStream::Instance().SendOnClickPacket(static_cast<uint32_t>(vid));
}
Py_RETURN_NONE;
}
PyObject* net_warp(PyObject*, PyObject*) {
PythonNetworkStream::Instance().SendWarpPacket();
Py_RETURN_NONE;
}
PyObject* send_create_character(PyObject*, PyObject* args) {
if(!has_mock_characters) { PyErr_SetString(PyExc_NotImplementedError,"AVA2 character creation server exchange has not been ported");return nullptr; }
int slot = 0, race = 0, shape = 0, con = 0, i_stat = 0, str = 0, dex = 0;
char* name = nullptr;
if (!PyArg_ParseTuple(args, "isiiiiii", &slot, &name, &race, &shape, &con, &i_stat, &str, &dex))
return nullptr;
if (slot >= 0 && slot < 5) {
Character chr;
chr.id = 1000 + slot;
chr.name = name ? name : "";
chr.job = race;
chr.level = 1;
chr.health = con;
chr.intelligence = i_stat;
chr.strength = str;
chr.dexterity = dex;
chr.play_minutes = 0;
set_mock_character(slot, chr);
}
if (phases[3]) { // PHASE_WINDOW_CREATE
PyObject* res = PyObject_CallMethod(phases[3], const_cast<char*>("OnCreateSuccess"), nullptr);
if (res) {
Py_DECREF(res);
} else {
PyErr_Clear();
}
}
Py_RETURN_NONE;
}
PyObject* send_destroy_character(PyObject*, PyObject* args) {
if(!has_mock_characters) { PyErr_SetString(PyExc_NotImplementedError,"AVA2 character deletion server exchange has not been ported");return nullptr; }
int slot = 0;
char* privateCode = nullptr;
if (!PyArg_ParseTuple(args, "is", &slot, &privateCode))
return nullptr;
if (slot >= 0 && slot < 5) {
mock_characters[slot] = Character{};
}
if (phases[2]) { // PHASE_WINDOW_SELECT
PyObject* res = PyObject_CallMethod(phases[2], const_cast<char*>("OnDeleteSuccess"), const_cast<char*>("i"), slot);
if (res) {
Py_DECREF(res);
} else {
PyErr_Clear();
}
}
Py_RETURN_NONE;
}
CharacterMoveState last_move_state{};
PyObject* send_character_move_packet(PyObject*, PyObject* args) {
int func = 0;
int arg = 0;
float rot = 0.0f;
int x = 0;
int y = 0;
unsigned int time = 0;
if (PyTuple_Size(args) >= 5) {
if (!PyArg_ParseTuple(args, "iifii|I", &func, &arg, &rot, &x, &y, &time)) {
PyErr_Clear();
int irot = 0;
if (!PyArg_ParseTuple(args, "iiiii|I", &func, &arg, &irot, &x, &y, &time)) {
return nullptr;
}
rot = static_cast<float>(irot);
}
} else if (PyTuple_Size(args) == 2) {
if (!PyArg_ParseTuple(args, "ii", &x, &y)) return nullptr;
func = 1;
}
uint8_t bRot = static_cast<uint8_t>(std::clamp(static_cast<int>(rot / 5.0f), 0, 255));
if (time == 0) {
time = static_cast<unsigned int>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
}
last_move_state.func = static_cast<uint8_t>(func);
last_move_state.arg = static_cast<uint8_t>(arg);
last_move_state.rot = bRot;
last_move_state.x = x;
last_move_state.y = y;
last_move_state.time = time;
last_move_state.valid = true;
auto wire_bytes = wire::character_move(last_move_state.func, last_move_state.arg, last_move_state.rot,
last_move_state.x, last_move_state.y, last_move_state.time);
(void)wire_bytes;
PythonNetworkStream::Instance().SendCharacterMovePacket(last_move_state.func, last_move_state.arg, last_move_state.rot,
last_move_state.x, last_move_state.y, last_move_state.time);
Py_RETURN_NONE;
}
PyObject* send_character_position_packet(PyObject*, PyObject* args) {
int pos = 0;
if (PyArg_ParseTuple(args, "i", &pos)) {
last_move_state.arg = static_cast<uint8_t>(pos);
}
Py_RETURN_NONE;
}
ItemUsePacketCallback s_itemUseCallback = nullptr;
ItemMovePacketCallback s_itemMoveCallback = nullptr;
PyObject* shop_end(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendShopPacket(0);Py_RETURN_NONE; }
PyObject* offline_shop_end(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendOfflineShopPacket(false);Py_RETURN_NONE; }
PyObject* offline_shop_remove(PyObject*,PyObject* args) { int position=0;if(!PyArg_ParseTuple(args,"i",&position))return nullptr;PythonNetworkStream::Instance().SendOfflineShopManagement(5,static_cast<uint8_t>(position));Py_RETURN_NONE; }
PyObject* offline_shop_destroy(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendOfflineShopManagement(6);Py_RETURN_NONE; }
PyObject* offline_shop_logs(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendOfflineShopManagement(11);Py_RETURN_NONE; }
PyObject* offline_shop_slot(PyObject*,PyObject* args) { int position=0;if(!PyArg_ParseTuple(args,"i",&position))return nullptr;PythonNetworkStream::Instance().SendOfflineShopManagement(13,static_cast<uint8_t>(position));Py_RETURN_NONE; }
PyObject* offline_shop_get_items(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendOfflineShopManagement(14);Py_RETURN_NONE; }
PyObject* offline_shop_add_time(PyObject*,PyObject*) { PythonNetworkStream::Instance().SendOfflineShopManagement(15);Py_RETURN_NONE; }
PyObject* offline_shop_open_vid(PyObject*,PyObject* args) { int owner=0;if(!PyArg_ParseTuple(args,"i",&owner))return nullptr;PythonNetworkStream::Instance().SendOfflineShopManagement(16,0,static_cast<uint32_t>(owner));Py_RETURN_NONE; }
PyObject* offline_shop_buy(PyObject*,PyObject* args) { int owner=0,position=0;if(!PyArg_ParseTuple(args,"ii",&owner,&position))return nullptr;PythonNetworkStream::Instance().SendOfflineShopPacket(true,static_cast<uint32_t>(owner),static_cast<uint8_t>(position));Py_RETURN_NONE; }
PyObject* shop_buy(PyObject*,PyObject* args) { int slot=0;if(!PyArg_ParseTuple(args,"i",&slot))return nullptr;PythonNetworkStream::Instance().SendShopPacket(1,0,static_cast<uint8_t>(slot));Py_RETURN_NONE; }
PyObject* shop_sell(PyObject*,PyObject* args) { int slot=0;if(!PyArg_ParseTuple(args,"i",&slot))return nullptr;PythonNetworkStream::Instance().SendShopPacket(2,0,static_cast<uint8_t>(slot));Py_RETURN_NONE; }
PyObject* shop_sell_new(PyObject*,PyObject* args) { int window=0,slot=0,count=0;if(!PyArg_ParseTuple(args,"iii",&window,&slot,&count))return nullptr;PythonNetworkStream::Instance().SendShopPacket(3,static_cast<uint8_t>(window),static_cast<uint8_t>(slot),static_cast<uint32_t>(count));Py_RETURN_NONE; }
PyObject* send_item_use_packet(PyObject*, PyObject* args) {
int window = 1;
int slot = 0;
if (!PyArg_ParseTuple(args, "ii", &window, &slot)) {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "i", &slot)) {
Py_RETURN_NONE;
}
}
PythonNetworkStream::Instance().SendItemUsePacket(static_cast<uint8_t>(window), static_cast<uint16_t>(slot));
if (s_itemUseCallback) {
s_itemUseCallback(window, slot);
}
Py_RETURN_NONE;
}
PyObject* send_item_move_packet(PyObject*, PyObject* args) {
int srcWindow = 1, srcSlot = 0, dstWindow = 1, dstSlot = 0, count = 1;
if (!PyArg_ParseTuple(args, "iiiii", &srcWindow, &srcSlot, &dstWindow, &dstSlot, &count)) {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "iii", &srcSlot, &dstSlot, &count)) {
Py_RETURN_NONE;
}
}
PythonNetworkStream::Instance().SendItemMovePacket(static_cast<uint8_t>(srcWindow), static_cast<uint16_t>(srcSlot),
static_cast<uint8_t>(dstWindow), static_cast<uint16_t>(dstSlot),
static_cast<uint32_t>(count));
if (s_itemMoveCallback) {
s_itemMoveCallback(srcWindow, srcSlot, dstWindow, dstSlot, count);
}
Py_RETURN_NONE;
}
} // namespace
const CharacterMoveState& get_last_character_move() {
return last_move_state;
}
void set_character_move(const CharacterMoveState& move) {
last_move_state = move;
}
void SetItemUsePacketCallback(ItemUsePacketCallback cb) {
s_itemUseCallback = cb;
}
void SetItemMovePacketCallback(ItemMovePacketCallback cb) {
s_itemMoveCallback = cb;
}
static PyObject* net_send_refine_packet(PyObject*, PyObject* args) {
int targetPos = 0, type = 0, specialStorage = 0, indexSelect = -1;
if (PyArg_ParseTuple(args, "iiii", &targetPos, &type, &specialStorage, &indexSelect)) {
PythonNetworkStream::Instance().SendRefinePacket((uint8_t)targetPos, (uint8_t)type, (uint8_t)specialStorage, indexSelect);
} else {
PyErr_Clear();
if (PyArg_ParseTuple(args, "iii", &targetPos, &type, &specialStorage)) {
PythonNetworkStream::Instance().SendRefinePacket((uint8_t)targetPos, (uint8_t)type, (uint8_t)specialStorage, -1);
} else {
PyErr_Clear();
if (PyArg_ParseTuple(args, "ii", &targetPos, &type)) {
PythonNetworkStream::Instance().SendRefinePacket((uint8_t)targetPos, (uint8_t)type, 0, -1);
}
}
}
Py_RETURN_NONE;
}
static PyObject* net_send_item_use_to_item_packet(PyObject*, PyObject* args) {
int srcCell = 0, dstCell = 0;
if (PyArg_ParseTuple(args, "ii", &srcCell, &dstCell)) {
PythonNetworkStream::Instance().SendItemUseToItemPacket((uint16_t)srcCell, (uint16_t)dstCell);
}
Py_RETURN_NONE;
}
static PyObject* net_send_give_item_packet(PyObject*, PyObject* args) {
int targetVID = 0, window = 0, cell = 0, count = 0;
if (PyArg_ParseTuple(args, "iiii", &targetVID, &window, &cell, &count)) {
PythonNetworkStream::Instance().SendGiveItemPacket((uint32_t)targetVID, (uint8_t)window, (uint16_t)cell, (uint32_t)count);
}
Py_RETURN_NONE;
}
static PyObject* net_send_script_answer_packet(PyObject*, PyObject* args) {
int answer = 0;
if (PyArg_ParseTuple(args, "i", &answer)) {
PythonNetworkStream::Instance().SendScriptAnswerPacket(answer);
}
Py_RETURN_NONE;
}
static PyObject* net_send_script_button_packet(PyObject*, PyObject* args) {
int button = 0;
if (PyArg_ParseTuple(args, "i", &button)) {
PythonNetworkStream::Instance().SendScriptButtonPacket(button);
}
Py_RETURN_NONE;
}
void initnet() {
static PyMethodDef methods[] = {
{"SetHandler", set_handler, METH_VARARGS, nullptr},
{"SetAccountConnectorHandler", set_account_handler, METH_VARARGS, nullptr},
{"SetPhaseWindow", set_phase, METH_VARARGS, nullptr},
{"ClearPhaseWindow", clear_phase, METH_VARARGS, nullptr},
{"SetLoginInfo", set_login, METH_VARARGS, nullptr},
{"ConnectToAccountServer", connect, METH_VARARGS, nullptr},
{"Disconnect", disconnect, METH_NOARGS, nullptr},
{"SetPacketSequenceMode", sequence_mode, METH_NOARGS, nullptr},
{"SendSelectCharacterPacket", select, METH_VARARGS, nullptr},
{"GetAccountCharacterSlotDataInteger", get_integer, METH_VARARGS, nullptr},
{"GetAccountCharacterSlotDataString", get_string, METH_VARARGS, nullptr},
{"GetEmpireID", empire, METH_NOARGS, nullptr},
{"Update", update, METH_NOARGS, nullptr},
{"GetConnectionState", status, METH_NOARGS, nullptr},
{"GetBettingGuildWarValue", net_dummy_zero, METH_VARARGS, nullptr},
{"EnableChatInsultFilter", net_dummy_none, METH_VARARGS, nullptr},
{"SetServerInfo", net_dummy_none, METH_VARARGS, nullptr},
{"GetServerInfo", net_dummy_str, METH_VARARGS, nullptr},
{"PreserveServerCommand", net_dummy_none, METH_VARARGS, nullptr},
{"GetPreservedServerCommand", net_dummy_str, METH_VARARGS, nullptr},
{"StartGame", net_start_game, METH_VARARGS, nullptr},
{"Warp", net_warp, METH_VARARGS, nullptr},
{"IsTest", net_dummy_zero, METH_VARARGS, nullptr},
{"SetMarkServer", net_dummy_none, METH_VARARGS, nullptr},
{"IsChatInsultIn", net_dummy_zero, METH_VARARGS, nullptr},
{"IsInsultIn", net_dummy_zero, METH_VARARGS, nullptr},
{"LoadInsultList", net_dummy_none, METH_VARARGS, nullptr},
{"UploadMark", net_dummy_none, METH_VARARGS, nullptr},
{"UploadSymbol", net_dummy_none, METH_VARARGS, nullptr},
{"GetGuildID", net_dummy_zero, METH_VARARGS, nullptr},
{"GetMainActorVID", net_get_main_actor_vid, METH_VARARGS, nullptr},
{"GetMainActorRace", net_get_main_actor_race, METH_VARARGS, nullptr},
{"GetMainActorEmpire", net_get_main_actor_empire, METH_VARARGS, nullptr},
{"GetMainActorSkillGroup", net_get_main_actor_skill_group, METH_VARARGS, nullptr},
{"SendTargetInfoLoad", net_dummy_none, METH_VARARGS, nullptr},
{"GetFieldMusicFileName", net_get_field_music_name, METH_VARARGS, nullptr},
{"GetFieldMusicVolume", net_get_field_music_volume, METH_VARARGS, nullptr},
{"ToggleGameDebugInfo", net_dummy_none, METH_VARARGS, nullptr},
{"SetServerCommandParserWindow", net_dummy_none, METH_VARARGS, nullptr},
{"SetTCPRecvBufferSize", net_dummy_none, METH_VARARGS, nullptr},
{"SetTCPSendBufferSize", net_dummy_none, METH_VARARGS, nullptr},
{"SetUDPRecvBufferSize", net_dummy_none, METH_VARARGS, nullptr},
{"DirectEnter", direct_enter, METH_VARARGS, nullptr},
{"LogOutGame", net_dummy_none, METH_VARARGS, nullptr},
{"ExitGame", net_dummy_none, METH_VARARGS, nullptr},
{"ExitApplication", net_dummy_none, METH_VARARGS, nullptr},
{"ConnectTCP", net_dummy_none, METH_VARARGS, nullptr},
{"ConnectUDP", net_dummy_none, METH_VARARGS, nullptr},
{"SendLoginPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendChinaMatrixCardPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendRunupMatrixCardPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendNEWCIBNPasspodAnswerPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSelectEmpirePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendChangeNamePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendCreateCharacterPacket", send_create_character, METH_VARARGS, nullptr},
{"SendDestroyCharacterPacket", send_destroy_character, METH_VARARGS, nullptr},
{"SendEnterGamePacket", net_send_enter_game, METH_VARARGS, nullptr},
{"SendItemUsePacket", send_item_use_packet, METH_VARARGS, nullptr},
{"SendItemUseToItemPacket", net_send_item_use_to_item_packet, METH_VARARGS, nullptr},
{"SendItemDropPacket", net_send_item_drop, METH_VARARGS, nullptr},
{"SendItemDropPacketNew", net_send_item_drop_new, METH_VARARGS, nullptr},
{"SendElkDropPacket", net_send_gold_drop<false>, METH_VARARGS, nullptr},
{"SendGoldDropPacketNew", net_send_gold_drop<true>, METH_VARARGS, nullptr},
{"SendItemMovePacket", send_item_move_packet, METH_VARARGS, nullptr},
{"SendItemPickUpPacket", net_send_item_pickup, METH_VARARGS, nullptr},
{"SendGiveItemPacket", net_send_give_item_packet, METH_VARARGS, nullptr},
{"SetOfflinePhase", net_dummy_none, METH_VARARGS, nullptr},
{"IsConnect", is_connected, METH_VARARGS, nullptr},
{"SendChatPacket", net_send_chat, METH_VARARGS, nullptr},
{"SendEmoticon", net_dummy_none, METH_VARARGS, nullptr},
{"SendWhisperPacket", net_send_whisper, METH_VARARGS, nullptr},
{"SendMobileMessagePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendCharacterPositionPacket", send_character_position_packet, METH_VARARGS, nullptr},
{"SendCharacterMovePacket", send_character_move_packet, METH_VARARGS, nullptr},
{"SendCharacterStatePacket", send_character_move_packet, METH_VARARGS, nullptr},
{"SendShopEndPacket", shop_end, METH_VARARGS, nullptr},
{"SendShopBuyPacket", shop_buy, METH_VARARGS, nullptr},
{"SendShopSellPacket", shop_sell, METH_VARARGS, nullptr},
{"SendShopSellPacketNew", shop_sell_new, METH_VARARGS, nullptr},
{"SendExchangeStartPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendExchangeItemAddPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendExchangeItemDelPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendExchangeElkAddPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendExchangeAcceptPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendExchangeExitPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendOnClickPacket", net_send_onclick, METH_VARARGS, nullptr},
{"RegisterEmoticonString", net_dummy_none, METH_VARARGS, nullptr},
{"SendMessengerAddByVIDPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendMessengerAddByNamePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendMessengerRemovePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyInvitePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyInviteAnswerPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyExitPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyRemovePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyRemovePacketPID", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyRemovePacketVID", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartySetStatePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyUseSkillPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendPartyParameterPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSafeboxSaveMoneyPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSafeboxWithdrawMoneyPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSafeboxCheckinPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSafeboxCheckoutPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendSafeboxItemMovePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendMallCheckoutPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendAnswerMakeGuildPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendQuestInputStringPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendQuestInputLongStringPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendQuestConfirmPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildAddMemberPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildRemoveMemberPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildChangeGradeNamePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildChangeGradeAuthorityPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildOfferPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildPostCommentPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildDeleteCommentPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildRefreshCommentsPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildChangeMemberGradePacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildUseSkillPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildChangeMemberGeneralPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildInviteAnswerPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildChargeGSPPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildDepositMoneyPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildWithdrawMoneyPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendRequestRefineInfoPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SendRefinePacket", net_send_refine_packet, METH_VARARGS, nullptr},
{"SendScriptAnswerPacket", net_send_script_answer_packet, METH_VARARGS, nullptr},
{"SendScriptButtonPacket", net_send_script_button_packet, METH_VARARGS, nullptr},
{"SendSelectItemPacket", net_dummy_none, METH_VARARGS, nullptr},
{"SetEmpireLanguageMode", net_dummy_none, METH_VARARGS, nullptr},
{"SetSkillGroupFake", net_dummy_none, METH_VARARGS, nullptr},
{"SendGuildSymbol", net_dummy_none, METH_VARARGS, nullptr},
{"DisconnectUploader", net_dummy_none, METH_VARARGS, nullptr},
{"RecvGuildSymbol", net_dummy_none, METH_VARARGS, nullptr},
{"SendOfflineShopEndPacket", offline_shop_end, METH_VARARGS, nullptr},
{"SendOfflineShopBuyPacket", offline_shop_buy, METH_VARARGS, nullptr},
{"SendAddOfflineShopItem", net_dummy_none, METH_VARARGS, nullptr},
{"SendRemoveOfflineShopItem", offline_shop_remove, METH_VARARGS, nullptr},
{"SendDestroyOfflineShop", offline_shop_destroy, METH_VARARGS, nullptr},
{"SendTakeOfflineMoney", net_dummy_none, METH_VARARGS, nullptr},
{"SendOfflineShopChangeTitle", net_dummy_none, METH_VARARGS, nullptr},
{"SendOfflineShopCheck", net_dummy_none, METH_VARARGS, nullptr},
{"SendOfflineShopButton", net_dummy_none, METH_VARARGS, nullptr},
{"SendOfflineShopRemoveLogs", offline_shop_logs, METH_VARARGS, nullptr},
{"SendOpenShopSlot", offline_shop_slot, METH_VARARGS, nullptr},
{"SendGetBackItems", offline_shop_get_items, METH_VARARGS, nullptr},
{"SendAddTime", offline_shop_add_time, METH_VARARGS, nullptr},
{"SendOpenWithVID", offline_shop_open_vid, METH_VARARGS, nullptr},
{"RegisterErrorLog", net_dummy_none, METH_VARARGS, nullptr},
{"ElementsSpellClose", net_dummy_none, METH_VARARGS, nullptr},
{"ElementsSpellAdd", net_dummy_none, METH_VARARGS, nullptr},
{"ElementsSpellChange", net_dummy_none, METH_VARARGS, nullptr},
{"Send67AttrPacket", net_dummy_none, METH_VARARGS, nullptr},
{"Send67AttrClosePacket", net_dummy_none, METH_VARARGS, nullptr},
{"CalendarOpen", net_dummy_none, METH_VARARGS, nullptr},
{"CalendarClose", net_dummy_none, METH_VARARGS, nullptr},
{"CalendarSend", net_dummy_none, METH_VARARGS, nullptr},
{"SendPrivateShopSearchInfo", net_dummy_none, METH_VARARGS, nullptr},
{"SendMiniGameCatchKingStart", net_dummy_none, METH_VARARGS, nullptr},
{"SendMiniGameCatchKingDeckCardClick", net_dummy_none, METH_VARARGS, nullptr},
{"SendMiniGameCatchKingFieldCardClick", net_dummy_none, METH_VARARGS, nullptr},
{"SendMiniGameCatchKingReward", net_dummy_none, METH_VARARGS, nullptr},
{"OpenCatchKing", net_dummy_none, METH_VARARGS, nullptr},
{"SendCombatZoneRequestActionPacket", net_dummy_none, METH_VARARGS, nullptr},
{"IsRunning", net_dummy_zero, METH_VARARGS, nullptr},
{"GetRankings", net_dummy_tuple4, METH_VARARGS, nullptr},
{"GetDay", net_dummy_tuple4, METH_VARARGS, nullptr},
{"GetDayActivate", net_dummy_zero, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
auto* module = Py_InitModule("net", methods);
constexpr const char* field_names[] = {"ID","NAME","RACE","LEVEL","STR","DEX","HTH","INT","PLAYTIME","FORM","ADDR","PORT","GUILD_ID","GUILD_NAME","CHANGE_NAME_FLAG","HAIR","SASH","CONQUEROR_LEVEL","SUNGMA_ST","SUNGMA_HP","SUNGMA_MOVE","SUNGMA_INMUNE"};
for(int i=0; i<22; ++i) PyModule_AddIntConstant(module, (std::string("ACCOUNT_CHARACTER_SLOT_")+field_names[i]).c_str(), i);
constexpr const char* phase_names[] = {"LOGO","LOGIN","SELECT","CREATE","LOAD","GAME","EMPIRE"};
for(int i=0; i<7; ++i) PyModule_AddIntConstant(module, (std::string("PHASE_WINDOW_")+phase_names[i]).c_str(), i);
PyModule_AddIntConstant(module, "EMPIRE_A", 1);
PyModule_AddIntConstant(module, "EMPIRE_B", 2);
PyModule_AddIntConstant(module, "EMPIRE_C", 3);
// CPythonNetworkStream error codes
PyModule_AddIntConstant(module, "ERROR_NONE", 0);
PyModule_AddIntConstant(module, "ERROR_UNKNOWN", 1);
PyModule_AddIntConstant(module, "ERROR_CONNECT_MARK_SERVER", 2);
PyModule_AddIntConstant(module, "ERROR_LOAD_MARK", 3);
PyModule_AddIntConstant(module, "ERROR_MARK_WIDTH", 4);
PyModule_AddIntConstant(module, "ERROR_MARK_HEIGHT", 5);
PyModule_AddIntConstant(module, "ERROR_MARK_UPLOAD_NEED_RECONNECT", 6);
PyModule_AddIntConstant(module, "ERROR_MARK_CHECK_NEED_RECONNECT", 7);
// Server commands
PyModule_AddIntConstant(module, "SERVER_COMMAND_LOG_OUT", 0);
PyModule_AddIntConstant(module, "SERVER_COMMAND_RETURN_TO_SELECT_CHARACTER", 1);
PyModule_AddIntConstant(module, "SERVER_COMMAND_QUIT", 2);
// Dragon Soul Subheaders
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL", 5);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL_MAX_REFINE", 6);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL_INVALID_MATERIAL", 7);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL_NOT_ENOUGH_MONEY", 8);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL_NOT_ENOUGH_MATERIAL", 9);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_FAIL_TOO_MUCH_MATERIAL", 10);
PyModule_AddIntConstant(module, "DS_SUB_HEADER_REFINE_SUCCEED", 11);
// Combat Zone
PyModule_AddIntConstant(module, "COMBAT_ZONE_MAX_ROWS_RANKING", 11);
PyModule_AddIntConstant(module, "COMBAT_ZONE_EMPTY_DATA", 999);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ACTION_OPEN_RANKING", 1);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ACTION_CHANGE_PAGE_RANKING", 2);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ACTION_PARTICIPATE", 3);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ACTION_LEAVE", 4);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ACTION_REQUEST_POTION", 5);
PyModule_AddIntConstant(module, "COMBAT_ZONE_TYPE_RANKING_WEEKLY", 1);
PyModule_AddIntConstant(module, "COMBAT_ZONE_TYPE_RANKING_ALL", 2);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ICON_SLOT", 27125);
PyModule_AddIntConstant(module, "COMBAT_ZONE_ICON_SLOT_COUNT", 3);
}
void register_python_net() {
if(PyImport_AppendInittab("net", initnet) != 0) throw std::runtime_error("net module registration failed");
}
void reset_python_net() {
client.cancel();
PythonNetworkStream::Instance().SetHandler(nullptr);
for(int i=0;i<7;++i) PythonNetworkStream::Instance().SetPhaseWindow(i,nullptr);
PythonNetworkStream::Instance().ResetConnection();
replace(handler, nullptr);
replace(account_handler, nullptr);
for(auto*& phase : phases) replace(phase, nullptr);
account.clear();
std::fill(password.begin(), password.end(), 0);
password.clear();
mock_characters.fill(Character{});
has_mock_characters = false;
mock_empire = 0;
has_mock_empire = false;
}
void set_mock_character(int slot, const Character& chr) {
if(slot >= 0 && slot < 5) {
mock_characters[slot] = chr;
has_mock_characters = true;
}
}
void set_mock_empire(std::uint8_t empire) {
mock_empire = empire;
has_mock_empire = true;
}
void pump_python_net() {
client.pump();
auto events = client.take_events();
for(const auto& event : events) {
if(event.generation != client.generation()) continue;
if(event.type==LoginClient::EventType::Handshake) callback(phases[1],"OnHandShake");
else if(event.type==LoginClient::EventType::RemoteDisconnect) { PythonNetworkStream::Instance().ResetConnection();callback(handler,"SetLoginPhase"); }
else if(event.type == LoginClient::EventType::LoginStart) callback(phases[1], "OnLoginStart");
else if(event.type == LoginClient::EventType::SelectReady) {
callback(handler, client.session()->empire() ? "SetSelectCharacterPhase" : "SetSelectEmpirePhase");
if(event.generation == client.generation()) callback(phases[2], "Refresh");
} else if(event.type == LoginClient::EventType::DirectEnterReady) callback(handler, "SetLoadingPhase");
else if(event.type == LoginClient::EventType::Loading) {
PythonNetworkStream::Instance().SetPhase(PHASE_LOADING);
if(!client.is_direct_enter()) callback(handler,"SetLoadingPhase");
}
else {
auto* target = account_handler ? account_handler : phases[1];
callback(target, event.connection_failure ? "OnConnectFailure" : "OnLoginFailure", event.connection_failure ? nullptr : event.reason.c_str());
}
}
auto& stream=PythonNetworkStream::Instance();
stream.PushRecvData(client.take_game_input());
stream.ProcessRecvData();
if(client.status()==LoginClient::Status::Loading) {
for(auto& packet:stream.PopSendPackets()) client.send_game_packet(std::move(packet));
}
}
} // namespace ava2