Files
AVA2/tests/switchbot_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

92 lines
7.8 KiB
C++

#include "switchbot_state.h"
#include "python_runtime.h"
#include "python_gameplay_modules.h"
#include "eter_net/PythonNetworkStream.h"
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
using namespace ava2;
using Bytes=std::vector<uint8_t>;
void require(bool ok,const char* why) {if(!ok)throw std::runtime_error(why);}
void u32(Bytes& b,uint32_t v) {for(unsigned i=0;i<4;++i)b.push_back(uint8_t(v>>(8*i)));}
void u16(Bytes& b,uint16_t v) {b.push_back(uint8_t(v));b.push_back(uint8_t(v>>8));}
Bytes frame(uint8_t sub,const Bytes& data) {Bytes b{171};u32(b,uint32_t(7+data.size()));b.insert(b.end(),{sub,0});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();}
void attr(Bytes& data,uint8_t set,int type,int value) {data.push_back(set);u32(data,uint32_t(type));u32(data,uint32_t(value));}
void proto(const std::filesystem::path& path,uint32_t id,uint8_t type,uint8_t sub) {
// Independent172-byte AVA2 r152 record and literal LZO stream, no source assets.
Bytes raw;u32(raw,id);raw.resize(172);raw[4]='F';raw[29]='F';raw[54]=type;raw[55]=sub;raw[57]=1;
Bytes compressed{189};compressed.insert(compressed.end(),raw.begin(),raw.end());compressed.insert(compressed.end(),{17,0,0});
Bytes payload{'M','C','O','Z'};payload.insert(payload.end(),compressed.begin(),compressed.end());
Bytes file{'M','I','P','X'};u32(file,1);u32(file,172);u32(file,1);u32(file,uint32_t(16+payload.size()));
file.insert(file.end(),{'M','C','O','Z'});u32(file,0);u32(file,uint32_t(compressed.size()));u32(file,172);file.insert(file.end(),payload.begin(),payload.end());
std::ofstream out(path,std::ios::binary);out.write(reinterpret_cast<const char*>(file.data()),file.size());
}
int main(int argc,char** argv) {
if(argc!=3)return 2;
auto root=std::filesystem::temp_directory_path()/std::filesystem::path("ava2-switchbot-"+std::to_string(getpid()));std::filesystem::create_directories(root);
try {
{
PythonRuntime python(argv[1],root);auto& s=PythonNetworkStream::Instance();s.ResetConnection();s.SetPhase(PHASE_GAME);
python.execute(R"PY(import switchbot as sw,player,net,item
assert sw.SLOT_COUNT==5 and sw.ALTERNATIVE_COUNT==2
assert sw.IsActive(5)==0 and sw.IsFinished(5)==0
assert sw.GetAttribute(5,1,4)==(0,0)
assert sw.IsActive() is None and sw.SetAttribute() is None
assert sw.GetAttributesForSet(0)==[] and sw.GetAttributeMaxValue(0,1)==0
class Game:
def __init__(self): self.events=[]
def RefreshSwitchbotWindow(self): self.events.append('window')
def RefreshInventory(self):
assert player.GetItemCount(11,5)==700
assert player.GetItemMetinSocket(11,5,7)==0
self.events.append('inventory')
def RefreshSwitchbotItem(self,slot): self.events.append(('item',slot))
game=Game()
net.SetPhaseWindow(net.PHASE_WINDOW_GAME,game)
player.SetGameWindow(game)
sw.SetAttribute(256,256,256,257,65535)
assert sw.GetAttribute(0,0,0)==(1,-1)
sw.SetAttribute(5,1,4,9,40000)
assert sw.GetAttribute(5,1,4)==(9,-25536)
assert sw.GetAttribute(0,0,5)==(0,0) and sw.GetAttribute(-1,0,0)==(0,0)
sw.Start(0)
assert sw.IsActive(0)==0
sw.Stop(0)
)PY");
auto packets=s.PopSendPacketsWithSequence();Bytes expected{171,37,0,0,0,0,0,1,255,255};expected.resize(37);
require(packets.size()==2 && packets[0].first==expected && packets[1].first==Bytes{171,7,0,0,0,1,0} && packets[0].second && packets[1].second,"switchbot native-less send/sequence mismatch");
Bytes table;u32(table,123);table.insert(table.end(),{0,0,0,0,0,2,0,0,0,0,0,255});for(unsigned i=0;i<6;++i)u32(table,100+i);
for(unsigned i=0;i<60;++i) {table.push_back(uint8_t(i+1));u16(table,uint16_t(-int(i+1)));}
require(table.size()==220,"literal table size");auto wire=frame(0,table);wire.push_back(44);
s.PushRecvData(wire.data(),226);s.ProcessRecvData();python.execute("assert game.events==[]\n");s.PushRecvData(wire.data()+226,2);s.ProcessRecvData();require(s.PopSendBytes()==Bytes{254},"switchbot table consumed next ping");
python.execute("assert sw.IsActive(5)==1 and sw.IsFinished(5)==1\nassert sw.GetAttribute(5,1,4)==(60,-60) and game.events==['window']\nsw.Stop(5)\nassert sw.IsActive(5)==1\nsw.ClearSlot(5)\nassert sw.IsActive(5)==0 and sw.IsFinished(5)==1 and sw.GetAttribute(5,1,4)==(0,0)\n");s.PopSendBytes();
require(GetSwitchbotState().table.items[5]==0 && GetSwitchbotState().table.player_id==123,"clear slot/table state mismatch");
TItemData item{};item.vnum=555;item.count=1;item.flags=123;SetPlayerItemData(11,5,item);
Bytes update{5};u32(update,999);u16(update,700);for(unsigned i=0;i<8;++i)u32(update,uint32_t(-int(i+1)));for(unsigned i=0;i<15;++i){update.push_back(uint8_t(i+1));u16(update,uint16_t(-int(i+10)));}send(1,update);
auto got=GetPlayerItemData(11,5);require(got.vnum==555 && got.count==700 && got.flags==123 && got.alSockets[7]==-8 && got.aAttr[14].bType==15 && got.aAttr[14].sValue==-24,"switchbot item update replaced vnum or lost extended arrays");
python.execute("assert game.events[-2:]==['inventory',('item',5)]\n");
Bytes attributes;for(int set=0;set<15;++set)attr(attributes,uint8_t(set),1,100+set);attr(attributes,0,4,40000);attr(attributes,0,257,999);send(2,attributes);
// Load small independent prototypes to verify actual equipped-item classification.
const std::vector<std::pair<int,int>> types{{1,0},{1,6},{36,0},{2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,8},{2,7},{28,0},{28,1},{28,2},{28,3},{0,0}};
const int sets[]{0,-1,11,1,5,6,2,3,4,7,13,-1,1,5,6,2,-1};
for(size_t i=0;i<types.size();++i) {
const uint32_t id=600000+uint32_t(i);proto(root/"fixture_proto",id,uint8_t(types[i].first),uint8_t(types[i].second));python.execute("item.LoadItemTable('fixture_proto')\n");TItemData item{};item.vnum=id;SetPlayerItemData(11,0,item);
require(GetSwitchbotState().AttributeSet(0)==sets[i],"item attribute-set classification mismatch");
python.execute("assert sw.GetAttributeMaxValue(0,1)=="+std::to_string(sets[i]<0?0:sets[i]==0?999:100+sets[i])+"\n");
}
TItemData weapon{};weapon.vnum=600000;SetPlayerItemData(11,0,weapon);
python.execute("assert sw.GetAttributesForSet(0)==[(1,999),(4,-25536)]\nassert sw.GetAttributeMaxValue(0,4)==40000\nassert sw.GetAttributesForSet(6)==[]\nsw.Start(6)\nsw.Stop(6)\nsw.Start(7)\nsw.Stop(-1)\n");packets=s.PopSendPacketsWithSequence();require(packets.size()==2 && packets[0].first==Bytes{171,37,0,0,0,0,6} && packets[1].first==Bytes{171,7,0,0,0,1,6},"slot6 header-only original quirk changed");
send(2,{});python.execute("assert sw.GetAttributesForSet(0)==[] and sw.GetAttributeMaxValue(0,1)==0\n");
// Original leaves unknown-subheader and short attribute tails for the dispatcher.
send(255,{44});require(s.PopSendBytes()==Bytes{254},"unknown switchbot subheader consumed payload");send(2,{44});require(s.PopSendBytes()==Bytes{254},"attribute tail consumed");
auto bad=frame(0,{1});bad.push_back(44);s.PushRecvData(bad);s.ProcessRecvData();require(s.PopSendBytes().empty(),"bad table consumed next packet");
python.execute("assert sw.GetAttribute(0,0,0)==(1,-1)\ndef broken(): raise ValueError('expected failure')\ngame.RefreshSwitchbotWindow=broken\n");send(0,table);python.execute("assert sw.IsActive(5)==1\n");
require(s.GetUnportedPackets().empty(),"switchbot still uses skip handler");s.ResetConnection();
}
{PythonRuntime python(argv[1],root);python.execute("import switchbot as sw\nassert sw.IsActive(5)==0 and sw.IsFinished(5)==0 and sw.GetAttribute(0,0,0)==(0,0)\n");}
std::filesystem::remove_all(root);std::cout<<"AVA2 switchbot state, wire and Python parity passed\n";
}catch(const std::exception& e){std::filesystem::remove_all(root);std::cerr<<e.what()<<'\n';return 1;}
}