#pragma once #include "Packet.h" #include "NetworkActorManager.h" #include #include #include #include #include #include #include #include #include #include namespace ava2 { /** * PythonNetworkStream: * Partial AVA2 game-phase packet model and dispatch bridge. * Encrypted socket integration covers supported packets; full Windows parity remains pending: * - Phase transitions (PHASE_LOGIN, PHASE_SELECT, PHASE_LOADING, PHASE_GAME) * - Wire packet parser with fixed & dynamic length packet dispatching * - Full GamePhase() processor for entity updates, combat, inventory, chat, and skills * - Integration with Python callbacks, TextTailManager, SoundManager, EffectManager, FlyingManager */ class PythonNetworkStream { public: static PythonNetworkStream& Instance(); PythonNetworkStream(); ~PythonNetworkStream(); // ── Phase Management ── void SetPhaseWindow(int phase, PyObject* poWindow); void ClearPhaseWindow(int phase, PyObject* poWindow); void SetHandler(PyObject* poHandler); void SetPhase(EPhase phase); EPhase GetPhase() const { return m_phase; } bool IsGamePhase() const { return m_phase == PHASE_GAME; } void StartGame(); void ResetConnection(); void SetGuildID(uint32_t id); uint32_t GetGuildID() const {return m_guildID;} void SetClientFilename(std::string name) { m_clientFilename=std::move(name); } // ── Main Actor Attributes ── void SetMainActorVID(uint32_t vid); uint32_t GetMainActorVID() const { return m_dwMainActorVID; } uint16_t GetMainActorRace() const { return m_wMainActorRace; } uint8_t GetMainActorEmpire() const { return m_byMainActorEmpire; } uint8_t GetMainActorSkillGroup() const { return m_byMainActorSkillGroup; } const std::string& GetMainActorName() const { return m_strMainActorName; } const std::string& GetFieldMusicFileName() const { return m_strFieldMusicFileName; } float GetFieldMusicVolume() const { return m_fFieldMusicVolume; } // ── Wire Ingestion & Dispatch ── void PushRecvData(const uint8_t* data, size_t len); void PushRecvData(const std::vector& bytes); bool ProcessRecvData(); // Free bytes in the 128 KB game receive buffer (networkmodule.py SetTCPRecvBufferSize(128*1024)). size_t RecvBufferSpace() const { return m_recvBuffer.size()>=128*1024?0:128*1024-m_recvBuffer.size(); } // ── Client-to-Server Sending API ── bool SendEnterGamePacket(); bool SendPrivateShopSearchInfo(const TPacketCGShopSearch& data); bool SendSwitchbotPacket(bool start,uint8_t slot); bool SendDungeonInfoPacket(uint8_t subheader,int32_t index=0); bool SendDropItemPacket(bool search,int32_t vnum=0); bool SendBattlePassPacket(bool reward,int32_t index=0); bool SendCubeRenewalPacket(bool close,uint32_t index=0,uint32_t count=0); bool SendShopPacket(uint8_t operation,uint8_t window=0,uint8_t slot=0,uint32_t count=0); bool SendOfflineShopPacket(bool buy,uint32_t owner=0,uint8_t position=0); bool SendBuildOfflineShopPacket(const std::string& name,const std::vector& items,uint32_t vnum,uint8_t title); bool SendOfflineShopChangeDecoration(const std::string& sign,uint32_t vnum,uint32_t type); bool SendOfflineShopManagement(uint8_t operation,uint8_t position=0,uint32_t owner=0); bool SendAddOfflineShopItem(const TItemPos& bDisplayPos, uint8_t bPos, int64_t lPrice); bool SendOfflineShopChangeTitle(const std::string& title); bool SendTakeOfflineMoney() { return SendOfflineShopManagement(SHOP_SUBHEADER_CG_TAKE_MONEY); } bool SendOfflineShopCheck() { return SendOfflineShopManagement(SHOP_SUBHEADER_CG_CHECK); } bool SendOfflineShopButton() { return SendOfflineShopManagement(SHOP_SUBHEADER_CG_BUTTON); } bool SendQuickSlotAddPacket(uint8_t index,uint8_t type,uint8_t pos); bool SendQuickSlotDelPacket(uint8_t index); bool SendQuickSlotMovePacket(uint8_t source,uint8_t target); bool SendCharacterMovePacket(uint8_t func, uint8_t arg, uint8_t rot, int32_t x, int32_t y, uint32_t time); bool SendAttackPacket(uint8_t type, uint32_t victimVID); bool SendChatPacket(const std::string& text, uint8_t type = CHAT_TYPE_TALKING); bool SendWhisperPacket(const std::string& name, const std::string& text); bool SendItemUsePacket(uint8_t window, uint16_t cell); bool SendItemMovePacket(uint8_t srcWindow, uint16_t srcCell, uint8_t dstWindow, uint16_t dstCell, uint32_t count); bool SendItemPickUpPacket(uint32_t vid); bool SendItemsPickUpPacket(const std::vector& itemVector); bool SendDragonSoulRefinePacket(uint8_t bRefineType, const TItemPos* pos); bool SendItemDropPacket(uint8_t window, uint16_t cell, uint32_t gold); bool SendItemDropPacketNew(uint8_t window,uint16_t cell,uint32_t gold,uint32_t count); bool SendOnClickPacket(uint32_t vid); bool SendFlyTargetingPacket(uint32_t targetVID, int32_t x, int32_t y); bool SendAddFlyTargetingPacket(uint32_t targetVID, int32_t x, int32_t y); bool SendUseSkillPacket(uint32_t skillIndex, uint32_t targetVID = 0); bool SendTargetPacket(uint32_t vid); bool SendShootPacket(uint32_t uSkill); bool SendCharacterPositionPacket(uint8_t iPosition); bool SendSyncPositionElementPacket(uint32_t victimVID, int32_t victimX, int32_t victimY); bool SendFishingPacket(int iRotation); bool SendPointResetPacket(); bool SendSashClosePacket(); bool SendSashAddPacket(TItemPos tPos, uint8_t bPos); bool SendSashRemovePacket(uint8_t bPos); bool SendSashRefinePacket(); bool SendWarpPacket(); // ── Exchange Protocol ── bool SendExchangeStartPacket(uint32_t vid); bool SendExchangeItemAddPacket(uint8_t window, uint16_t cell, uint8_t exchangePos); bool SendExchangeItemDelPacket(uint8_t exchangePos); bool SendExchangeElkAddPacket(int64_t elk); bool SendExchangeAcceptPacket(); bool SendExchangeExitPacket(); // ── SafeBox & Mall Protocol ── bool SendSafeBoxCheckinPacket(uint8_t window, uint16_t cell, uint8_t safeboxPos); bool SendSafeBoxCheckoutPacket(uint8_t safeboxPos, uint8_t window, uint16_t cell); bool SendSafeBoxItemMovePacket(uint8_t srcPos, uint8_t dstPos, uint32_t count); bool SendSafeBoxMoneyPacket(uint8_t state, uint32_t money); bool SendMallCheckoutPacket(uint8_t safeboxPos, uint8_t window, uint16_t cell); // ── Refine & Quest Protocol ── bool SendRefinePacket(uint8_t targetItemPos, uint8_t type, uint8_t specialStorage = 0, int32_t indexSelect = -1); bool SendItemUseToItemPacket(uint16_t srcCell, uint16_t dstCell); bool SendGiveItemPacket(uint32_t targetVID, uint8_t window, uint16_t cell, uint32_t count); bool SendScriptAnswerPacket(int answer); bool SendScriptButtonPacket(int button); // CPythonNetworkStream::OnScriptEventStart: phase window OpenQuestWindow(skin, index). void OnScriptEventStart(int iSkin, int iIndex); bool SendAnswerMakeGuildPacket(const char* name); bool SendQuestInputStringPacket(const char* text); bool SendQuestInputStringLongPacket(const char* text); bool SendQuestConfirmPacket(uint8_t answer, uint32_t pid); bool SendSelectItemPacket(uint32_t itemPos); bool SendTargetInfoLoadPacket(uint32_t vid); // ── Messenger / Party / Guild Protocol (AVA2 PythonNetworkStreamPhaseGame.cpp) ── bool SendMessengerAddByVIDPacket(uint32_t vid); bool SendMessengerAddByNamePacket(const char* name); bool SendMessengerRemovePacket(const char* key, const char* name); bool SendPartyInvitePacket(uint32_t vid); bool SendPartyInviteAnswerPacket(uint32_t leaderPID, uint8_t answer); bool SendPartyRemovePacket(uint32_t pid); bool SendPartySetStatePacket(uint32_t vid, uint8_t state, uint8_t flag); bool SendPartyUseSkillPacket(uint8_t skillIndex, uint32_t vid); bool SendPartyParameterPacket(uint8_t distributeMode); bool SendGuildAddMemberPacket(uint32_t vid); bool SendGuildRemoveMemberPacket(uint32_t pid); bool SendGuildChangeGradeNamePacket(uint8_t grade, const char* name); bool SendGuildChangeGradeAuthorityPacket(uint8_t grade, uint8_t authority); bool SendGuildOfferPacket(uint32_t experience); bool SendGuildPostCommentPacket(const char* message); bool SendGuildDeleteCommentPacket(uint32_t index); bool SendGuildRefreshCommentsPacket(uint32_t highestIndex); bool SendGuildChangeMemberGradePacket(uint32_t pid, uint8_t grade); bool SendGuildUseSkillPacket(uint32_t skillID, uint32_t targetVID); bool SendGuildChangeMemberGeneralPacket(uint32_t pid, uint8_t flag); bool SendGuildInviteAnswerPacket(uint32_t guildID, uint8_t answer); bool SendGuildChargeGSPPacket(uint32_t money); bool SendGuildDepositMoneyPacket(uint32_t money); bool SendGuildWithdrawMoneyPacket(uint32_t money); void ResetGuildCommentRefreshThrottle() { m_lastGuildCommentRefresh.reset(); } // Raw outgoing bytes for network transport / unit test inspection const std::vector& GetSendQueue() const { return m_sendBuffer; } std::vector PopSendBytes(); void ClearSendQueue() { m_sendBuffer.clear();m_sendPackets.clear(); } const std::vector>& GetSendPackets() const { return m_sendPackets; } std::vector> PopSendPackets(); // Packets with whether the transport appends the sequence byte (SendSequence()). std::vector,bool>> PopSendPacketsWithSequence(); std::pair GetDungeonDestination() const { return m_dungeonDestination; } // Callback when damage occurs using DamageCallback = std::function; void SetDamageCallback(DamageCallback cb) { m_damageCallback = cb; } // Callback when chat is received // ── Chat / ServerCommand (AVA2 PythonNetworkStreamPhaseGame.cpp, PythonNetworkStreamCommand.cpp) ── void ServerCommand(const std::string& command); void SetServerCommandParserWindow(PyObject* window) { m_poSerCommandParserWnd = window; } void RegisterEmoticonString(const std::string& emoticon); bool ParseEmoticon(const char* chatMsg, uint32_t* emoticon) const; void SendEmoticon(unsigned emoticon); void EnableChatInsultFilter(bool enable) { m_isEnableChatInsultFilter = enable; } // CInsultChecker (AVA2 UserInterface/InsultChecker.cpp); text = already-read insult.txt. bool LoadInsultList(const std::string& text); bool IsChatInsultIn(const char* msg) const; bool IsInsultIn(const char* msg) const; void FilterInsult(char* line, size_t lineLen) const; void SetEmpireLanguageMode(bool enable) { m_bEmpireLanguageEnable = enable; } bool IsObserverMode() const { return m_isObserverMode; } bool HasMobilePhoneNumber() const { return m_bMobileFlag; } bool GetComboSkillFlag() const { return m_bComboSkillFlag; } // Windows calls PostQuitMessage(0); the host loop polls this instead. bool IsQuitRequested() const { return m_unknownPacketQuit; } void RequestQuit() { m_unknownPacketQuit = true; } // GamePhase() headers Windows handles but this port skips (consumed, not dispatched). const std::vector& GetUnportedPackets() const { return m_unportedPackets; } // Callback when whisper is received using WhisperCallback = std::function; void SetWhisperCallback(WhisperCallback cb) { m_whisperCallback = cb; } // Callback when ground item is added/deleted using GroundItemCallback = std::function; void SetGroundItemCallback(GroundItemCallback cb) { m_groundItemCallback = cb; } // Callback when fly projectile is created using FlyCallback = std::function; void SetFlyCallback(FlyCallback cb) { m_flyCallback = cb; } // GC_WARP: the transport (LoginClient::warp) performs CNetworkStream::Connect(lAddr, wPort). bool TakeWarpRequest(uint32_t& address,uint16_t& port) { if(!m_warpRequested) return false; m_warpRequested=false;address=m_warpAddress;port=m_warpPort;return true; } private: bool CheckPacket(uint8_t* retHeader); bool DispatchGamePhasePacket(uint8_t header,bool& ret); bool DispatchPortedGamePhasePacket(uint8_t header); // Handlers matching CPythonNetworkStreamPhaseGame bool RecvPhasePacket(); bool RecvMainCharacter(); bool RecvMainCharacter2Empire(); bool RecvMainCharacter3BGM(); bool RecvMainCharacter4BGMVolume(); bool RecvTimePacket(); bool RecvPlayerPoints(); bool RecvAffectAddPacket(); bool RecvAffectRemovePacket(); bool RecvSkillLevel(); bool RecvSkillLevelNew(); bool RecvSkillCoolTimeEnd(); void MainCharacterLoaded(int32_t x,int32_t y,int32_t z); bool RecvCharacterAppendPacket(); bool RecvCharacterAdditionalInfo(); bool RecvCharacterAppendPacketNew(); bool RecvCharacterUpdatePacketNew(); bool RecvCharacterUpdatePacket(); bool RecvCharacterDeletePacket(); bool RecvCharacterMovePacket(); bool RecvSyncPositionPacket(); bool RecvChatPacket(); bool RecvWhisperPacket(); bool RecvDamageInfoPacket(); bool RecvStunPacket(); bool RecvDeadPacket(); bool RecvPointChange(); bool m_refreshSkill=false; bool m_startGameRequested=false; std::string m_clientFilename="ava2_client"; std::vector> m_sendPackets; bool m_refreshGuildInfo=false,m_refreshGuildBoard=false,m_refreshGuildMember=false; bool m_refreshGuildCombo=false,m_refreshGuildSkill=false,m_refreshGuildGrade=false,m_refreshMessenger=false; bool m_refreshShopSearch=false; bool m_refreshInventory=false,m_refreshStatus=false,m_refreshCharacter=false; std::vector m_sendSequence; // Handshake/HandshakeOK/HybridCrypt return from GamePhase() before the refresh block. bool m_stopGamePhase=false; bool m_warpRequested=false; uint32_t m_warpAddress=0; uint16_t m_warpPort=0; uint32_t m_changeServerTime=0,m_changeClientTime=0; // CPythonNetworkStream::m_kServerTimeSync std::pair m_dungeonDestination{}; bool RecvItemSetPacket(); bool RecvItemSetPacket2(); bool RecvItemUpdatePacket(); bool RecvItemUsePacket(); bool RecvItemGroundAddPacket(); bool RecvItemGroundDelPacket(); bool RecvItemOwnership(); bool RecvQuickSlotAddPacket(); bool RecvQuickSlotDelPacket(); bool RecvQuickSlotMovePacket(); bool RecvCreateFlyPacket(); bool RecvWarpPacket(); bool RecvPingPacket(); bool RecvObserverAddPacket(); bool RecvObserverRemovePacket(); bool RecvObserverMovePacket(); bool RecvExchangePacket(); bool RecvShopPacket(); bool RecvOfflineShopPacket(); bool RecvSafeBoxSetPacket(); bool RecvSafeBoxDelPacket(); bool RecvSafeBoxWrongPasswordPacket(); bool RecvSafeBoxSizePacket(); bool RecvSafeBoxMoneyChangePacket(); bool RecvMallOpenPacket(); bool RecvMallSetPacket(); bool RecvMallDelPacket(); // Remaining GamePhase() handlers (PythonNetworkStreamPhaseGameRecv.cpp) SNetworkActorData* MainInstance(); void RefreshTargetBoardByVID(uint32_t vid); bool RecvChangeSpeedPacket(); bool RecvMotionPacket(); bool RecvEventManager(); bool RecvItemShop(); bool RecvDuelStartPacket(); bool RecvPVPPacket(); bool RecvCharacterPositionPacket(); bool RecvScriptPacket(); bool RecvShopSearchSet(); bool RecvCubeRenewalPacket(); bool RecvMessenger(); bool RecvGuild(); bool RecvBattlePassPacket(); bool RecvDropItemPacket(); bool RecvDungeonInfoPacket(); bool RecvSwitchbotPacket(); bool RecvQuestInfoPacket(); bool RecvQuestConfirmPacket(); bool RecvOfflineShopSignPacket(); bool RecvTargetInfoPacket(); bool RecvMountPacket(); bool RecvOwnerShipPacket(); bool RecvTargetPacket(); bool RecvChestDrop(); bool RecvKillLOG(); bool RecvFlyTargetingPacket(); bool RecvAddFlyTargetingPacket(); bool RecvFlyTargetingPacket(bool add); bool RecvPartyInvite(); bool RecvPartyAdd(); bool RecvPartyUpdate(); bool RecvPartyRemove(); bool RecvRequestMakeGuild(); bool RecvPartyParameter(); bool RecvFishing(); bool RecvPartyLink(); bool RecvPartyUnlink(); template void RefineInformation(const Packet& p,bool isNew); static uint8_t RefineType(const TPacketGCRefineInformation&); static uint8_t RefineType(const TPacketGCRefineInformationNew& p); bool RecvRefineInformationPacket(); bool RecvRefineInformationPacketNew(); bool RecvViewEquipPacket(); bool RecvDungeon(); bool RecvWalkModePacket(); bool RecvChangeSkillGroupPacket(); bool RecvNPCList(); bool RecvChannelPacket(); bool RecvTargetUpdatePacket(); bool RecvTargetDeletePacket(); bool RecvTargetCreatePacket(); bool RecvTargetCreatePacketNew(); bool RecvLandPacket(); bool RecvLoverInfoPacket(); bool RecvLovePointUpdatePacket(); bool RecvDigMotionPacket(); bool RecvAccountBankPacket(); bool RecvHybridCryptPacket(); bool RecvHSCheckRequest(); bool RecvXTrapVerifyRequest(); bool RecvSpecificEffect(); bool RecvSpecialEffect(); bool RecvDragonSoulRefine(); bool RecvUnk213(); bool RecvCalendarSystem(); bool RecvCalendarSystemADM(); bool RecvSashPacket(); bool RecvKingdomsWarPacket(); bool RecvSungmaAttrUpdatePacket(); bool RecvExchangeLog(); bool RecvMiniGameCatchKingPacket(); bool Recv67AttrOpenPacket(); bool RecvCheatBlacklist(); bool RecvHandshakePacket(); bool RecvHandshakeOKPacket(); bool Peek(size_t size, void* outBuf); bool Read(size_t size, void* outBuf); void Send(const void* data, size_t size); // Windows Send() without SendSequence() (XTrap ACK). void SendUnsequenced(const void* data, size_t size); private: EPhase m_phase = PHASE_OFFLINE; PyObject* m_poHandler = nullptr; PyObject* m_phaseWindows[7] = {nullptr}; uint32_t m_guildID=0; uint32_t m_dwMainActorVID = 0; uint16_t m_wMainActorRace = 0; uint8_t m_byMainActorEmpire = 0; std::optional m_lastGuildCommentRefresh; uint8_t m_byMainActorSkillGroup = 0; std::string m_strMainActorName; std::string m_strFieldMusicFileName; float m_fFieldMusicVolume = 0.02f; std::vector m_recvBuffer; uint8_t m_lastPacket[2] {}; bool m_dispatchUnported = false; bool m_unknownPacketQuit = false; std::vector m_unportedPackets; PyObject* m_poSerCommandParserWnd = nullptr; std::vector m_emoticonStrings; std::string m_strLastCommand; bool m_isEnableChatInsultFilter = false; std::vector m_insults; // CInsultChecker::m_kList_stInsult bool m_bEmpireLanguageEnable = true; // gs_bEmpireLanuageEnable bool m_isObserverMode = false; bool m_bMobileFlag = false; bool m_bComboSkillFlag = false; std::vector m_sendBuffer; // Temporary pending actor data when CharacterAdd arrives before CharacterAdditionalInfo SNetworkActorData m_pendingActorData; DamageCallback m_damageCallback; WhisperCallback m_whisperCallback; GroundItemCallback m_groundItemCallback; FlyCallback m_flyCallback; }; } // namespace ava2