Files
AVA2/tests/itemshop_test.cpp
shenleiandClaude Opus 5.5 9077aa389a AVA2 client Windows parity passes (gr2 BitKnit2/64-bit, GamePhase handlers, python modules)
- gr2: BitKnit2 decompression, 64-bit LE files, compressed curves
- net: remaining GamePhase handlers, generated GC packet table
- python: event/chat/chr/minimap/guild/quest/messenger/shop-search and
  other native modules ported against the AVA2 Windows client
- render: D3D8 fixed-function lighting in Gr2ModelRenderer, terrain
  without baked shade; cape/NPC equipment attachment
- tests + audits updated; docs/PORT-PLAN.md and WINDOWS-PARITY.md

Batch 4 (render pipeline: background.*, app.RenderGame/UpdateGame,
camera bindings, main.cpp fake-content removal) is still in progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:43:01 +09:00

63 lines
6.2 KiB
C++

#include "python_runtime.h"
#include "eter_net/PythonNetworkStream.h"
#include <iostream>
#include <stdexcept>
using namespace ava2;
using Bytes=std::vector<uint8_t>;
void require(bool ok,const char* why) {if(!ok)throw std::runtime_error(why);}
void put(Bytes& b,uint64_t v,unsigned width=4) {for(unsigned i=0;i<width;++i)b.push_back(uint8_t(v>>(8*i)));}
void text(Bytes& b,const std::string& s,size_t width) {for(size_t i=0;i<width;++i)b.push_back(i<s.size()?s[i]:0);}
Bytes frame(uint8_t sub,const Bytes& data) {Bytes b{93};put(b,6+data.size());b.push_back(sub);b.insert(b.end(),data.begin(),data.end());return b;}
void send(uint8_t sub,const Bytes& data) {auto& s=PythonNetworkStream::Instance();s.PushRecvData(frame(sub,data));s.ProcessRecvData();}
Bytes item() {
Bytes b;put(b,0xffffffff);put(b,0x80000001);put(b,uint64_t(-900000000000LL),8);put(b,uint32_t(-4));b.push_back(255);
put(b,uint32_t(-5));put(b,123);put(b,900000000000ULL,8);put(b,222);put(b,333);put(b,uint32_t(-8));return b;
}
Bytes log(const std::string& name="Hero") {
Bytes b;put(b,100);text(b,name,25);text(b,"2026-10-06",21);put(b,uint32_t(-10));text(b,"127.0.0.1",16);
put(b,300);put(b,0xffffffff);put(b,uint32_t(-2));put(b,uint64_t(-700000000000LL),8);return b;
}
Bytes load(int categories) {Bytes b;put(b,900000000000ULL,8);put(b,uint32_t(-10));put(b,uint32_t(categories));return b;}
int main(int argc,char** argv) {
if(argc!=3)return 2;
try {
PythonRuntime python(argv[1],argv[2]);auto& s=PythonNetworkStream::Instance();s.ResetConnection();s.SetPhase(PHASE_GAME);
python.execute(R"PY(import net
class Game:
def __init__(self): self.events=[]
def __getattr__(self,name):
def event(*args): self.events.append((name,)+args)
return event
game=Game()
net.SetPhaseWindow(net.PHASE_WINDOW_GAME,game)
item=(-1,-2147483647,-900000000000,255,-5,-4,123,900000000000,-8)
log=('2026-10-06',-10,'Hero','127.0.0.1',-1,-2,-700000000000)
)PY");
require(item().size()==49 && log().size()==90,"itemshop independent fixture sizes");
auto wire=frame(6,item());wire.push_back(44);s.PushRecvData(wire.data(),wire.size()-2);s.ProcessRecvData();python.execute("assert game.events==[]\n");s.PushRecvData(wire.data()+wire.size()-2,2);s.ProcessRecvData();require(s.PopSendBytes()==Bytes{254},"itemshop lost coalesced ping");
python.execute("assert game.events==[('ItemShopUpdateItem',)+item]\ngame.events=[]\n");
Bytes coin;put(coin,uint64_t(-100000000000LL),8);coin.push_back(0);send(3,coin);python.execute("assert game.events==[('ItemShopSetDragonCoin',-100000000000)]\ngame.events=[]\n");coin.back()=2;auto row=log();coin.insert(coin.end(),row.begin(),row.end());send(3,coin);
python.execute("assert game.events==[('ItemShopAppendLog',)+log,('ItemShopSetDragonCoin',-100000000000)]\ngame.events=[]\n");
Bytes logs;put(logs,2);logs.insert(logs.end(),row.begin(),row.end());logs.insert(logs.end(),row.begin(),row.end());send(1,logs);
python.execute("assert game.events==[('ItemShopHideLoading',),('ItemShopLogClear',),('ItemShopAppendLog',)+log,('ItemShopAppendLog',)+log,('ItemShopPurchasesWindow',)]\ngame.events=[]\n");logs.clear();put(logs,0);send(1,logs);
python.execute("assert game.events==[('ItemShopHideLoading',),('ItemShopLogClear',),('ItemShopPurchasesWindow',)]\ngame.events=[]\n");
auto data=load(2);data.insert(data.end(),{3,2,4,2});auto it=item();data.insert(data.end(),it.begin(),it.end());data.insert(data.end(),it.begin(),it.end());data.insert(data.end(),{5,0,8,0});send(0,data);
python.execute("assert game.events==[('ItemShopSetDragonCoin',900000000000),('ItemShopHideLoading',),('ItemShopClear',-10),('ItemShopAppendItem',3,4)+item,('ItemShopAppendItem',3,4)+item,('ItemShopOpenMainPage',),('ItemShopOpenMainPage',)]\ngame.events=[]\n");
send(0,load(9999));python.execute("assert game.events==[('ItemShopSetDragonCoin',900000000000),('ItemShopHideLoading',),('ItemShopClear',-10),('ItemShopOpenMainPage',)]\ngame.events=[]\n");send(0,load(0));
python.execute("assert game.events==[('ItemShopSetDragonCoin',900000000000),('ItemShopHideLoading',),('ItemShopClear',-10)]\ngame.events=[]\n");
row=log(std::string(25,'x'));std::fill(row.begin()+29,row.begin()+50,'y');std::fill(row.begin()+54,row.begin()+70,'z');logs.clear();put(logs,1);logs.insert(logs.end(),row.begin(),row.end());send(1,logs);
python.execute("assert game.events[2]==('ItemShopAppendLog','y'*21,-10,'x'*25,'z'*16,-1,-2,-700000000000)\ngame.events=[]\n");
for(uint8_t sub:{2,4,5,255}) {send(sub,{44});require(s.PopSendBytes()==Bytes{254},"unhandled itemshop subheader consumed payload");}
python.execute("assert game.events==[]\n");
// Invalid arrays and fields cannot read into a following packet or allocate unbounded memory.
logs.clear();put(logs,0xffffffff);send(1,logs);python.execute("assert game.events==[('ItemShopHideLoading',),('ItemShopLogClear',)]\ngame.events=[]\n");
logs.clear();put(logs,1);auto bad=frame(1,logs);bad.push_back(44);s.PushRecvData(bad);s.ProcessRecvData();require(s.PopSendBytes().empty(),"short logs consumed next frame");python.execute("assert game.events==[('ItemShopHideLoading',),('ItemShopLogClear',)]\ngame.events=[]\n");
bad=frame(6,{1});bad.push_back(44);s.PushRecvData(bad);s.ProcessRecvData();require(s.PopSendBytes().empty(),"short update consumed next frame");python.execute("assert game.events==[]\n");
coin.clear();put(coin,1,8);coin.push_back(1);send(3,coin);python.execute("assert game.events==[]\n");
send(0,load(-1));python.execute("assert game.events==[('ItemShopSetDragonCoin',900000000000),('ItemShopHideLoading',),('ItemShopClear',-10)]\ngame.events=[]\ndef broken(*args): raise ValueError('expected callback failure')\ngame.ItemShopAppendLog=broken\n");
coin=Bytes{};put(coin,7,8);coin.push_back(1);row=log();coin.insert(coin.end(),row.begin(),row.end());send(3,coin);python.execute("assert game.events==[('ItemShopSetDragonCoin',7)]\n");
require(s.GetUnportedPackets().empty(),"itemshop still routed through skip handler");s.ResetConnection();
std::cout<<"AVA2 itemshop wire and Python callback parity passed\n";
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}
}