Files

99 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""Compile the actual tooltip resolver and command with isolated state/ownership fixtures."""
from pathlib import Path
import subprocess,tempfile
root=Path(__file__).resolve().parents[2]/'src/server/game/src'
source='\n'.join(l for l in (root/'ava2_attr_lock_commands.h').read_text().splitlines() if not l.startswith('#include'))
exchange=(root/'exchange.h').read_bytes().decode('latin1');a=exchange.index(' LPITEM GetDisplayItem(');b=exchange.index('\n }',a)+len('\n }');display=exchange[a:b]
shop=(root/'shop.cpp').read_bytes().decode('latin1');a=shop.index('LPITEM CShop::GetGuestItem(');b=shop.index('\nbool CShop::AddGuest',a);guest=shop[a:b]
code=r'''
#include <cassert>
#include <cstdio>
#include <map>
#include <string>
#include <sstream>
#include <vector>
#include <cstdarg>
#define INVENTORY 1
#define EQUIPMENT 2
#define SWITCHBOT 7
#define SWITCHBOT_SLOT_COUNT 6
#define INVENTORY_AND_EQUIP_SLOT_MAX 300
#define SAFEBOX_MAX_NUM 180
#define WEAR_MAX_NUM 32
#define GM_PLAYER 0
#define EXCHANGE_ITEM_MAX_NUM 12
#define CHAT_TYPE_COMMAND 1
#define ACMD(name) void name(CHARACTER* ch,const char* argument)
using BYTE=unsigned char;using WORD=unsigned short;using DWORD=unsigned;
struct CHARACTER;struct CExchange;struct CShop;
using LPCHARACTER=CHARACTER*;
struct Proto {int sAddonType=0;};
struct CItem {CHARACTER* owner=nullptr;unsigned id=99,vnum=100,count=1,mask=8;bool exchanging=true,m_portAttrLockLoaded=true;Proto proto;
CHARACTER* GetOwner(){return owner;}bool IsExchanging(){return exchanging;}
unsigned GetID(){return id;}unsigned GetVnum(){return vnum;}unsigned GetCount(){return count;}const Proto* GetProto(){return &proto;}
unsigned GetAttributeType(unsigned i){return i<5?i+1:0;}
};
using LPITEM=CItem*;
#include "port_attr_lock.h"
unsigned PortAttrLockGet(CItem* i){return i->mask;}
struct TItemPos {BYTE window_type;WORD cell;};
struct CSafebox {CItem* item=nullptr;bool IsValidPosition(unsigned c){return c<45;}LPITEM Get(unsigned c){return c==3?item:nullptr;}};
struct CHARACTER {bool pc=true;unsigned gm=0;int protect=0;CExchange* exchange=nullptr;CShop* shop=nullptr;CSafebox* safe=nullptr;CSafebox* mall=nullptr;CItem* item=nullptr;std::string reply;
bool IsPC(){return pc;}unsigned GetGMLevel(){return gm;}unsigned GetPlayerID(){return 1;}
CExchange* GetExchange(){return exchange;}CShop* GetShop(){return shop;}CSafebox* GetSafebox(){return safe;}CSafebox* GetMall(){return mall;}
CItem* GetWear(unsigned c){return c==2?item:nullptr;}CItem* GetItem(TItemPos p){return p.cell==4?item:nullptr;}
int GetProtectTime(const char*){return protect;}void SetProtectTime(const char*,int n){protect=n;}
void ChatPacket(int,const char* fmt,...){char b[512];va_list a;va_start(a,fmt);vsnprintf(b,sizeof(b),fmt,a);va_end(a);reply=b;}
};
struct CExchange {CExchange* company=nullptr;CHARACTER* owner=nullptr;CItem* m_apItems[12]{};unsigned m_abItemDisplayPos[12]{};
CHARACTER* GetOwner(){return owner;}CExchange* GetCompany(){return company;}
'''+display+r'''
};
namespace quest {struct PC {bool IsLoaded(){return true;}};struct CQuestManager {int flag=0;static CQuestManager& instance(){static CQuestManager q;return q;}PC* GetPCForce(unsigned){static PC p;return &p;}int GetEventFlag(const char*){return flag;}};}
struct CShop {struct SHOP_ITEM {LPITEM pkItem;int itemid;unsigned vnum;};CHARACTER* m_pkPC=nullptr;std::map<CHARACTER*,bool> m_map_guest;std::vector<SHOP_ITEM> m_itemVector;LPITEM GetGuestItem(CHARACTER*,unsigned);};
'''+guest+r'''
struct CHARACTER_MANAGER {std::map<unsigned,CHARACTER*> chars;static CHARACTER_MANAGER& instance(){static CHARACTER_MANAGER m;return m;}CHARACTER* Find(unsigned v){return chars.count(v)?chars[v]:nullptr;}};
int get_global_time(){return 100;}
'''+source+r'''
int main(){
CHARACTER viewer,seller;CItem mine,theirs;mine.owner=&viewer;theirs.owner=&seller;viewer.item=&mine;seller.item=&theirs;
CExchange a,b;a.owner=&viewer;b.owner=&seller;a.company=&b;b.company=&a;viewer.exchange=&a;
a.m_apItems[0]=&mine;a.m_abItemDisplayPos[0]=8;b.m_apItems[5]=&theirs;b.m_abItemDisplayPos[5]=2;
assert(Ava2AttrLockView(&viewer,101,8,0)==&mine);assert(!Ava2AttrLockView(&viewer,101,0,0));
assert(Ava2AttrLockView(&viewer,102,2,0)==&theirs);assert(!Ava2AttrLockView(&viewer,102,5,0));
theirs.exchanging=false;assert(!Ava2AttrLockView(&viewer,102,2,0));theirs.exchanging=true;
b.company=nullptr;assert(!Ava2AttrLockView(&viewer,102,2,0));b.company=&a;
viewer.exchange=nullptr;assert(!Ava2AttrLockView(&viewer,102,2,0));
CShop shop;shop.m_pkPC=&seller;shop.m_itemVector.push_back({&theirs,99,100});viewer.shop=&shop;
assert(!Ava2AttrLockView(&viewer,103,0,0));shop.m_map_guest[&viewer]=false;
assert(Ava2AttrLockView(&viewer,103,0,0)==&theirs);assert(!Ava2AttrLockView(&viewer,103,1,0));
theirs.vnum=70024;shop.m_itemVector[0].vnum=70024;assert(!Ava2AttrLockView(&viewer,103,0,0));
quest::CQuestManager::instance().flag=1;assert(Ava2AttrLockView(&viewer,103,0,0)==&theirs);quest::CQuestManager::instance().flag=0;
theirs.vnum=100;shop.m_itemVector[0].vnum=100;
shop.m_itemVector[0].itemid=100;assert(!Ava2AttrLockView(&viewer,103,0,0));shop.m_itemVector[0].itemid=99;
theirs.owner=&viewer;assert(!Ava2AttrLockView(&viewer,103,0,0));theirs.owner=&seller;
CSafebox safe;safe.item=&mine;viewer.safe=&safe;viewer.mall=&safe;
assert(Ava2AttrLockView(&viewer,104,3,0)==&mine && Ava2AttrLockView(&viewer,105,3,0)==&mine);
assert(!Ava2AttrLockView(&viewer,104,256,0));viewer.safe=nullptr;assert(!Ava2AttrLockView(&viewer,104,3,0));
CHARACTER_MANAGER::instance().chars[123]=&seller;
assert(!Ava2AttrLockView(&viewer,106,2,123));viewer.gm=1;
assert(Ava2AttrLockView(&viewer,106,2,123)==&theirs);assert(!Ava2AttrLockView(&viewer,106,32,123));
seller.pc=false;assert(!Ava2AttrLockView(&viewer,106,2,123));seller.pc=true;
auto query=[&](const char* s){viewer.protect=0;viewer.reply.clear();do_attr_lock_info(&viewer,s);return viewer.reply;};
assert(query("5 1 4")=="AttrLockInfo 5 1 4 100 8");
assert(query("6 103 0 0")=="AttrLockInfo 6 103 0 100 8");
for(const char* s:{"1 103 0","1 103 0 123","1 103 0 0 trailing","1 255 0","2147483648 1 4","1 104 65536 0"})assert(query(s).empty());
viewer.gm=0;assert(query("7 106 2 123")=="AttrLockInfo 7 106 2 0 0");
viewer.gm=1;assert(query("8 106 2 123")=="AttrLockInfo 8 106 2 100 8");
viewer.protect=101;viewer.reply.clear();do_attr_lock_info(&viewer,"9 1 4");assert(viewer.reply.empty());
mine.m_portAttrLockLoaded=false;assert(query("10 1 4")=="AttrLockInfo 10 1 4 100 -1");
}
'''
with tempfile.TemporaryDirectory() as d:
p=Path(d);(p/'test.cpp').write_text(code)
subprocess.run(['c++','-std=c++17','-I'+str(root),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
subprocess.run([str(p/'test')],check=True)
print('Attr lock views: display cells, reciprocal exchange, owner/guest/item identity, closed containers, GM permissions, parser/throttle/SQL failure: PASS')