68 lines
6.8 KiB
C++
68 lines
6.8 KiB
C++
#include "python_runtime.h"
|
|
#include "python_gameplay_modules.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 write(const std::filesystem::path& p,const Bytes& b) {std::filesystem::create_directories(p.parent_path());std::ofstream out(p,std::ios::binary);out.write(reinterpret_cast<const char*>(b.data()),b.size());}
|
|
void textfile(const std::filesystem::path& p,const std::string& text) {write(p,Bytes(text.begin(),text.end()));}
|
|
Bytes row(uint32_t id,const std::string& internal,const std::string& name,uint8_t type=1,uint8_t size=3) {
|
|
Bytes b;u32(b,id);b.resize(172);std::copy(internal.begin(),internal.end(),b.begin()+4);std::copy(name.begin(),name.end(),b.begin()+29);b[54]=type;b[57]=size;b[131]=123;return b;
|
|
}
|
|
void proto(const std::filesystem::path& p,const std::vector<Bytes>& rows) {
|
|
Bytes raw;for(const auto& r:rows)raw.insert(raw.end(),r.begin(),r.end());Bytes compressed;
|
|
if(raw.size()<=238)compressed.push_back(uint8_t(17+raw.size()));
|
|
else {compressed.push_back(0);size_t n=raw.size()-18;while(n>255){compressed.push_back(0);n-=255;}compressed.push_back(n);}
|
|
compressed.insert(compressed.end(),raw.begin(),raw.end());compressed.insert(compressed.end(),{17,0,0});
|
|
Bytes file{'M','I','P','X'};u32(file,1);u32(file,172);u32(file,rows.size());u32(file,20+compressed.size());
|
|
file.insert(file.end(),{'M','C','O','Z'});u32(file,0);u32(file,compressed.size());u32(file,raw.size());file.insert(file.end(),{'M','C','O','Z'});file.insert(file.end(),compressed.begin(),compressed.end());write(p,file);
|
|
}
|
|
void tga(const std::filesystem::path& p) {Bytes b(18,0);b[2]=2;b[12]=64;b[14]=96;b[16]=24;b.resize(18+64*96*3,255);write(p,b);}
|
|
int main(int argc,char** argv) {
|
|
if(argc!=3)return 2;
|
|
auto root=std::filesystem::temp_directory_path()/std::filesystem::path("ava2-item-data-"+std::to_string(getpid()));std::filesystem::create_directories(root);
|
|
auto orig_cwd = std::filesystem::current_path();
|
|
auto zip_path = std::filesystem::absolute(argv[1]);
|
|
std::filesystem::current_path(root);
|
|
try {
|
|
{
|
|
PythonRuntime python(zip_path, root);python.execute("import item\n");
|
|
python.execute(R"PY(for f in (item.GetItemName,item.GetItemDescription,item.GetItemSummary,item.GetItemSize,item.GetItemType,item.GetItemSubType,item.GetIconImage,item.GetIconImageFileName):
|
|
try: f()
|
|
except RuntimeError: pass
|
|
else: raise AssertionError('missing selected item must raise')
|
|
item.SelectItem(19)
|
|
assert item.SelectItemWiki(19)==0
|
|
assert item.SelectItemWiki(27003)==0
|
|
)PY");
|
|
int type,sub;std::string name;require(!GetItemTypeAndSubType(19,&type,&sub)&&GetWeaponModelPathByVnum(19).empty(),"fabricated seeded weapon remains");
|
|
textfile(root/"locale/en/item_names.txt","19\tFabricated Name\n");require(!GetItemNameAndType(19,&name,&type),"item_names lazy autoload remains");
|
|
textfile(root/"list","500\tWEAPON\ticon/list.tga\tmodel/explicit.gr2\n600\tARMOR\ticon/missing.tga\n700\tUSE\ticon/list.tga\textra\tinvalid\n500\tIGNORED\ticon/list2.tga\n\t800\tunused\ticon/list.tga\t\n900\tunused\t\"icon/with space.tga\"\n");
|
|
require(LoadItemList("list"),"item list fixture rejected");python.execute("item.SelectItem(500)\nassert item.GetItemName()=='' and item.GetItemType()==0 and item.GetItemSize()==(1,0), (item.GetItemName(),item.GetItemType(),item.GetItemSize())\nassert item.GetIconImageFileName()=='Noname'\nassert item.SelectItemWiki(700)==0 and item.SelectItemWiki(800)==1 and item.SelectItemWiki(900)==1\n");
|
|
require(GetWeaponModelPathByVnum(500).empty(),"three-field list did not clear previous model");
|
|
textfile(root/"desc","500\tignored name\t\"Description\"\t\"Summary\"\n600\tignored\t\"Unclosed\t\n777\tx\tShould not create\tx\n900\tignored\t\tSummary only\n");require(LoadItemDescriptions("desc"),"description fixture rejected");
|
|
python.execute("item.SelectItem(500)\nassert item.GetItemDescription()=='Description' and item.GetItemSummary()=='Summary' and item.GetItemName()=='', (item.GetItemDescription(),item.GetItemSummary(),item.GetItemName())\nitem.SelectItem(600)\nassert item.GetItemDescription()=='Unclosed' and item.GetItemSummary()==''\nassert item.SelectItemWiki(777)==0\nitem.SelectItem(900)\nassert item.GetItemDescription()=='' and item.GetItemSummary()=='Summary only'\n");
|
|
tga(root/"icon/list2.tga");tga(root/"icon/item/01002.tga");tga(root/"icon/item/02000.tga");tga(root/"icon/item/27995.tga");
|
|
// Unsorted rows: same-name icon fallback follows original source order, not map order.
|
|
proto(root/"proto",{row(1002,"Same","First"),row(1001,"Same","Second"),row(2009,"Different","Base"),row(3333,"Missing","Bowl"),row(500,"List","Listed",2,2)});
|
|
require(LoadItemTable("proto"),"prototype fixture rejected");
|
|
python.execute("item.SelectItem(1001)\nassert item.GetItemName()=='Second' and item.GetIconImageFileName()=='icon/item/01002.tga', (item.GetItemName(),item.GetIconImageFileName())\nitem.SelectItem(2009)\nassert item.GetIconImageFileName()=='icon/item/02000.tga'\nitem.SelectItem(3333)\nassert item.GetIconImageFileName()=='icon/item/27995.tga'\nitem.SelectItem(500)\nassert item.GetItemType()==2 and item.GetItemSize()==(1,2) and item.GetIconImageFileName()=='icon/list2.tga'\nassert item.GetItemDescription()=='Description' and item.GetItemSummary()=='Summary'\nassert item.GetSocket(0)==123\nitem.GetIconImage()\nassert item.GetItemSize()==(1,2)\nitem.SelectItem(999999)\nassert item.GetItemName()=='Listed'\n");
|
|
require(!LoadItemList("absent")&&!LoadItemDescriptions("absent")&&!LoadItemTable("absent"),"missing loader must fail");textfile(root/"bad","invalid proto");require(!LoadItemTable("bad"),"bad proto accepted");python.execute("assert item.GetItemName()=='Listed' and item.LoadItemTable('bad') is None\n");
|
|
textfile(root/"list","500\tignored\ticon/list2.tga\tmodel/new.gr2\n");require(LoadItemList("list"),"reload list failed");require(GetWeaponModelPathByVnum(500)=="model/new.gr2","explicit model path ignored");python.execute("assert item.GetItemType()==2 and item.GetItemName()=='Listed' and item.GetSocket(0)==0 and item.GetItemDescription()==''\n");
|
|
ResetItemDatabase();require(!GetItemTypeAndSubType(500,&type,&sub),"database reset retained records");python.execute("assert item.SelectItemWiki(500)==0\n");
|
|
}
|
|
std::filesystem::current_path(orig_cwd);
|
|
std::filesystem::remove_all(root);
|
|
std::cout<<"AVA2 item resource loading and selected data parity passed\n";
|
|
}catch(const std::exception& e){
|
|
std::filesystem::current_path(orig_cwd);
|
|
std::filesystem::remove_all(root);
|
|
std::cerr<<e.what()<<'\n';
|
|
return 1;
|
|
}
|
|
}
|