#include "item_sound.h" #include "python_runtime.h" #include "python_gameplay_modules.h" #include "eter_net/PythonNetworkStream.h" #include "eter_net/PythonSafeBox.h" #include "eter_net/PythonExchange.h" #include "eter_sound/SoundManager.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 wav(const std::filesystem::path& path) { Bytes b{'R','I','F','F'};u32(b,38);b.insert(b.end(),{'W','A','V','E','f','m','t',' '});u32(b,16); b.insert(b.end(),{1,0,1,0});u32(b,8000);u32(b,16000);b.insert(b.end(),{2,0,16,0,'d','a','t','a'});u32(b,2);b.insert(b.end(),{0,0}); std::ofstream out(path,std::ios::binary);out.write(reinterpret_cast(b.data()),b.size()); } 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-item-sound-"+std::to_string(getpid()));std::filesystem::create_directories(root); try { { PythonRuntime python(argv[1],root); python.execute("import item\nfor i in range(8): item.SetUseSoundFileName(i,'use'+str(i))\nfor i in range(5): item.SetDropSoundFileName(i,'drop'+str(i))\nitem.SetUseSoundFileName(-1,'bad')\nitem.SetDropSoundFileName(5,'bad')\nitem.SetUseSoundFileName(8,'bad')\n"); uint32_t id=500000; for(auto [type,sub,use,drop]:{ std::tuple{1,0,3,2},std::tuple{1,1,3,2},std::tuple{1,2,4,3},std::tuple{1,3,3,2},std::tuple{1,4,3,2},std::tuple{1,5,3,2},std::tuple{1,6,1,0},std::tuple{1,7,3,2},std::tuple{1,8,3,2},std::tuple{1,9,1,0}, std::tuple{2,0,2,1},std::tuple{2,1,1,0},std::tuple{2,2,1,0},std::tuple{2,3,1,0},std::tuple{2,4,1,0},std::tuple{2,5,5,4},std::tuple{2,6,5,4},std::tuple{2,7,1,0}, std::tuple{3,0,-1,0},std::tuple{3,1,7,0},std::tuple{3,7,6,0},std::tuple{3,11,1,0},std::tuple{28,0,1,0},std::tuple{16,0,1,0}}) { proto(root/"proto",id,type,sub);python.execute("item.LoadItemTable('proto')\nitem.SelectItem("+std::to_string(id)+")\n"); python.execute("before=(item.GetItemType(),item.GetItemSubType())\n");auto selected=GetItemUseSound(id); require(use<0?!selected:selected=="use"+std::to_string(use),"original use classification mismatch"); require(GetItemDropSound(id)=="drop"+std::to_string(drop),"original drop classification mismatch"); GetItemUseSound(500000);python.execute("assert (item.GetItemType(),item.GetItemSubType())==before\n");++id; } require(!GetItemUseSound(0xffffffff)&&!GetItemDropSound(0xffffffff),"missing item must stay silent"); require(GetPotionSound()=="use6","potion uses sound enum rather than item USE_POTION subtype"); python.execute("item.SetUseSoundFileName(6,'')\nitem.SetDropSoundFileName(0,'')\n");require(GetPotionSound().empty()&&GetItemDropSound(500006)=="","empty sound filenames retained"); auto& sound=SoundManager::Instance();sound.Destroy();sound.SetSoundVolume(1); auto& net=PythonNetworkStream::Instance();net.ResetConnection(); TItemData data{};data.vnum=500002;SetPlayerItemData(1,0,data); auto& box=CPythonSafeBox::Instance();box.OpenSafeBox(1);box.OpenMall(1);box.SetItemData(0,data);box.SetMallItemData(0,data); for(unsigned op=0;op<4;++op) { auto path=root/("drop"+std::to_string(op)+".wav");wav(path); python.execute("item.SetDropSoundFileName(item.DROPSOUND_BOW,'"+path.string()+"')\n"); if(op==0)net.SendSafeBoxCheckinPacket(1,0,0); if(op==1)net.SendSafeBoxCheckoutPacket(0,1,0); if(op==2)net.SendSafeBoxItemMovePacket(0,1,2); if(op==3)net.SendMallCheckoutPacket(0,1,0); require(sound.GetCachedSoundCount()==op+1,"storage sound did not reach playback manager"); require(net.PopSendPackets().size()==1,"storage sound changed wire sending"); } net.SendSafeBoxCheckoutPacket(255,1,0);net.SendMallCheckoutPacket(255,1,0);require(sound.GetCachedSoundCount()==4,"invalid storage slot played audio");net.ClearSendQueue(); auto path=root/"potion.wav";wav(path);python.execute("item.SetUseSoundFileName(item.USESOUND_POTION,'"+path.string()+"')\n"); PlayItemUseSound(500018);require(sound.GetCachedSoundCount()==4,"potion played locally before server effect"); PlayUsePotionSound();require(sound.GetCachedSoundCount()==5,"potion playback missing"); python.execute("for method in (item.SetUseSoundFileName,item.SetDropSoundFileName):\n try: method('bad','x')\n except TypeError: pass\n else: raise AssertionError('expected original bad argument error')\n"); // Inventory requests: sound follows the original action/equipment guards. SNetworkActorData actor{};actor.m_dwVID=77;actor.m_dwRace=999999;NetworkActorManager::Instance().AppendActor(actor);net.SetMainActorVID(77); SetPlayerItemData(1,180,data);SetPlayerItemData(1,215,data);SetPlayerItemData(1,216,data); require(IsPlayerEquipmentItem(1,180)&&IsPlayerEquipmentItem(1,215)&&!IsPlayerEquipmentItem(1,216)&&!IsPlayerEquipmentItem(6,180),"original equipment cell bounds"); python.execute("import net,shop\nclass Game:\n def __init__(self): self.events=[]\n def BINARY_AppendNotifyMessage(self,message): self.events.append(message)\ngame=Game()\nnet.SetPhaseWindow(net.PHASE_WINDOW_GAME,game)\nshop.Close()\n"); auto inventoryPath=root/"inventory.wav";wav(inventoryPath);SetItemUseSound(USESOUND_BOW,inventoryPath.string());SetItemDropSound(DROPSOUND_BOW,inventoryPath.string()); auto& trade=CPythonExchange::Instance();trade.Start(); net.SendItemUsePacket(1,180);net.SendItemMovePacket(1,180,1,0,1); require(net.PopSendPackets().empty()&&sound.GetCachedSoundCount()==5,"trading equip action must reject before sound/send"); python.execute("assert game.events==['CANNOT_EQUIP_EXCHANGE']*2\ngame.events=[]\nshop.Open()\n"); trade.End();net.SendItemUsePacket(1,180);net.SendItemMovePacket(1,180,1,0,1); require(net.PopSendPackets().empty()&&sound.GetCachedSoundCount()==5,"shopping equip action must reject before sound/send"); python.execute("assert game.events==['CANNOT_EQUIP_SHOP']*2\ngame.events=[]\n"); // Source is an inventory cell: Windows does not apply the equip restriction to destination-only moves. net.SendItemMovePacket(1,0,1,180,1);require(net.PopSendPackets().size()==1&&sound.GetCachedSoundCount()==6,"inventory-to-equipment move source guard mismatch"); python.execute("assert game.events==[]\nshop.Close()\n"); net.SendItemUsePacket(1,180);net.SendItemMovePacket(1,180,1,0,1);require(net.PopSendPackets().size()==2,"allowed equipment requests missing"); auto blockedPath=root/"blocked.wav";wav(blockedPath);SetItemUseSound(USESOUND_BOW,blockedPath.string());SetItemDropSound(DROPSOUND_BOW,blockedPath.string()); NetworkActorManager::Instance().GetActorInstance(77)->Stun();net.SendItemUsePacket(1,0);net.SendItemMovePacket(1,0,1,1,1); require(net.PopSendPackets().empty()&&sound.GetCachedSoundCount()==6,"unable-to-act request must stay silent"); sound.Destroy();net.ResetConnection(); } std::filesystem::remove_all(root);std::cout<<"AVA2 item sound selection and storage playback parity passed\n"; }catch(const std::exception& e){std::filesystem::remove_all(root);std::cerr<