#include "switchbot_state.h" #include "python_runtime.h" #include "python_gameplay_modules.h" #include "eter_net/PythonNetworkStream.h" #include #include #include #include using namespace ava2; using Bytes=std::vector; 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(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> 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