Files

100 lines
5.7 KiB
C++

#pragma once
#include "gateway/protocol.hpp"
#include <cstdint>
#include <climits>
#include <optional>
#include <span>
#include <array>
// Trade window (player<->player) — FULLY DROPPED in the gateway today (no handler
// for SMSG_TRADE_STATUS 0x120 / SMSG_TRADE_STATUS_EXTENDED 0x121, and the client's
// CMSG_*_TRADE 0x116-0x11F never reach the server). This header does the SOURCE
// half both directions:
// inbound (Source SMSG -> native): parse structs below (Codex: native serializer)
// outbound (native CMSG -> Source): Source-CMSG builders (Codex: native parser)
// GUID rule (confirmed 2026-11-18): Source operator<< / operator>> and GetGuidValue
// are FULL uint64; only GetPackGUID is packed. All trade guids here are FULL le(8).
// TRADE_SLOT_COUNT = 7; each extended slot is a FIXED 60-byte block (empty = zeros,
// entry==0 means empty) — NOT variable length.
namespace gateway {
enum : uint32_t { // TradeStatus (SharedDefines.h) — drives the window + error toasts
TRADE_STATUS_BUSY=0, TRADE_STATUS_BEGIN_TRADE=1, TRADE_STATUS_OPEN_WINDOW=2,
TRADE_STATUS_TRADE_CANCELED=3, TRADE_STATUS_TRADE_ACCEPT=4, TRADE_STATUS_BUSY_2=5,
TRADE_STATUS_NO_TARGET=6, TRADE_STATUS_BACK_TO_TRADE=7, TRADE_STATUS_TRADE_COMPLETE=8,
TRADE_STATUS_TRADE_REJECTED=9, TRADE_STATUS_TARGET_TO_FAR=10, TRADE_STATUS_WRONG_FACTION=11,
TRADE_STATUS_CLOSE_WINDOW=12, TRADE_STATUS_UNKNOWN_13=13, TRADE_STATUS_IGNORE_YOU=14,
TRADE_STATUS_YOU_STUNNED=15, TRADE_STATUS_TARGET_STUNNED=16, TRADE_STATUS_YOU_DEAD=17,
TRADE_STATUS_TARGET_DEAD=18, TRADE_STATUS_YOU_LOGOUT=19, TRADE_STATUS_TARGET_LOGOUT=20,
TRADE_STATUS_TRIAL_ACCOUNT=21, TRADE_STATUS_ONLY_CONJURED=22, TRADE_STATUS_MAX=23
};
constexpr uint8_t TRADE_SLOT_COUNT = 7;
// ---- inbound: SMSG_TRADE_STATUS 0x120 (status-dependent tail) ----
struct TurtleTradeStatus {
uint32_t status=0; uint64_t guid=0; // guid only for BEGIN_TRADE
uint32_t result=0; uint8_t isTargetError=0; uint32_t itemLimitCategory=0; // CLOSE_WINDOW
uint8_t conjuredSlot=0; // ONLY_CONJURED
};
inline std::optional<TurtleTradeStatus> turtle_trade_status(std::span<const uint8_t> body){
Reader r(body); TurtleTradeStatus d; d.status=uint32_t(r.le(4));
if(d.status>=TRADE_STATUS_MAX) return {};
switch(d.status){
case TRADE_STATUS_BEGIN_TRADE: d.guid=r.le(8); break; // other player's full guid
case TRADE_STATUS_CLOSE_WINDOW: d.result=uint32_t(r.le(4)); d.isTargetError=uint8_t(r.le(1)); d.itemLimitCategory=uint32_t(r.le(4)); break;
case TRADE_STATUS_ONLY_CONJURED: d.conjuredSlot=uint8_t(r.le(1)); break;
default: // bare status
// Octo live (2026-11-26, owner: no trade window) sends 8-byte bodies: status + u32 (Source
// TradeHandler reserves 4+4 for OPEN_WINDOW). Accept that one optional u32; it carries no field we use.
if(r.left()==4) r.le(4);
break;
}
r.end(); return d;
}
// ---- inbound: SMSG_TRADE_STATUS_EXTENDED 0x121 (the window contents) ----
struct TurtleTradeItem {
uint8_t slot=0; bool present=false;
uint32_t entry=0,displayId=0,count=0,wrapped=0; uint64_t giftCreator=0;
uint32_t permEnchant=0; uint64_t creator=0;
uint32_t charges=0,suffixFactor=0,randomPropId=0,lockId=0,maxDurability=0,durability=0;
};
struct TurtleTradeStatusExtended {
uint8_t traderState=0; uint32_t gold=0,spell=0;
std::array<TurtleTradeItem,TRADE_SLOT_COUNT> items{};
};
inline std::optional<TurtleTradeStatusExtended> turtle_trade_status_extended(std::span<const uint8_t> body){
Reader r(body); TurtleTradeStatusExtended d;
d.traderState=uint8_t(r.le(1));
auto c1=r.le(4), c2=r.le(4); if(c1!=TRADE_SLOT_COUNT||c2!=TRADE_SLOT_COUNT) return {};
d.gold=uint32_t(r.le(4)); d.spell=uint32_t(r.le(4));
for(uint8_t i=0;i<TRADE_SLOT_COUNT;++i){
auto& it=d.items[i]; it.slot=uint8_t(r.le(1)); if(it.slot!=i) return {};
it.entry=uint32_t(r.le(4)); it.displayId=uint32_t(r.le(4)); it.count=uint32_t(r.le(4));
it.wrapped=uint32_t(r.le(4)); it.giftCreator=r.le(8);
it.permEnchant=uint32_t(r.le(4)); it.creator=r.le(8);
it.charges=uint32_t(r.le(4)); it.suffixFactor=uint32_t(r.le(4)); it.randomPropId=uint32_t(r.le(4));
it.lockId=uint32_t(r.le(4)); it.maxDurability=uint32_t(r.le(4)); it.durability=uint32_t(r.le(4));
it.present = it.entry!=0; // empty slot = 60 zero bytes
}
r.end(); return d;
}
// ---- outbound: native CMSG -> Source CMSG body builders (Source 1.12 opcodes) ----
// Codex parses the native-70334 CMSG (byte layout = native RE), fills these params,
// calls the matching builder, and source_sends the Source opcode.
// 0x116 CMSG_INITIATE_TRADE : full guid 0x11A CMSG_ACCEPT_TRADE : u32 (read_skip; value unused)
// 0x117 CMSG_BEGIN_TRADE : empty 0x11B CMSG_UNACCEPT_TRADE: empty
// 0x118 CMSG_BUSY_TRADE : empty 0x11C CMSG_CANCEL_TRADE : empty
// 0x119 CMSG_IGNORE_TRADE : empty 0x11D CMSG_SET_TRADE_ITEM: u8 tradeSlot,u8 bag,u8 slot
// 0x11E CMSG_CLEAR_TRADE_ITEM: u8 tradeSlot 0x11F CMSG_SET_TRADE_GOLD: u32 gold
inline Bytes source_initiate_trade(uint64_t guid){ Bytes b; le(b,guid,8); return b; }
inline Bytes source_set_trade_item(uint8_t tradeSlot,uint8_t bag,uint8_t slot){ Bytes b; le(b,tradeSlot,1); le(b,bag,1); le(b,slot,1); return b; }
inline Bytes source_clear_trade_item(uint8_t tradeSlot){ Bytes b; le(b,tradeSlot,1); return b; }
inline Bytes source_set_trade_gold(uint32_t gold){ Bytes b; le(b,gold,4); return b; }
inline Bytes source_empty_trade(){ return Bytes{}; } // begin/busy/ignore/unaccept/cancel
// HandleAcceptTradeOpcode does recvPacket.read_skip<uint32>(): the body MUST carry 4 bytes.
inline Bytes source_accept_trade(){ Bytes b; le(b,0,4); return b; }
} // namespace gateway