feat: add read-only attr lock views for exchange, shop, safebox and inspect

This commit is contained in:
shen
2026-10-18 18:21:59 -07:00
parent bce34954d9
commit 5b8cc3069f
5 changed files with 153 additions and 5 deletions
+31 -5
View File
@@ -1,19 +1,45 @@
// Read-only presentation bridge: this port keeps AVA2 lock indices in an independent table.
#include "port_attr_lock.h"
#include <sstream>
#include "exchange.h"
#include "shop.h"
#include "safebox.h"
// Context IDs are presentation-only, never item window IDs or arbitrary owner IDs.
static LPITEM Ava2AttrLockView(LPCHARACTER ch,unsigned view,unsigned cell,unsigned vid) {
if (view==101 || view==102) {
CExchange* own=ch->GetExchange();
if (!own || own->GetOwner()!=ch || !own->GetCompany() || own->GetCompany()->GetCompany()!=own) return NULL;
CExchange* side=view==101?own:own->GetCompany();
LPITEM item=side->GetDisplayItem(cell);
return item && item->GetOwner()==side->GetOwner() && item->IsExchanging()?item:NULL;
}
if (view==103) return ch->GetShop()?ch->GetShop()->GetGuestItem(ch,cell):NULL;
if (view==104 || view==105) {
CSafebox* box=view==104?ch->GetSafebox():ch->GetMall();
return box && cell<SAFEBOX_MAX_NUM && box->IsValidPosition(cell)?box->Get(cell):NULL;
}
// do_view_equip is GM-only in the baseline. Never grant this to ordinary players.
if (view==106 && ch->GetGMLevel()>GM_PLAYER && cell<WEAR_MAX_NUM) {
LPCHARACTER owner=CHARACTER_MANAGER::instance().Find(vid);
return owner && owner->IsPC()?owner->GetWear(cell):NULL;
}
return NULL;
}
ACMD(do_attr_lock_info) {
if (!ch || !ch->IsPC()) return;
unsigned token=0,window=0,cell=0;std::string extra;std::istringstream args(argument);
if (!(args>>token>>window>>cell) || (args>>extra) || token>2147483647 || window>255 || cell>65535 ||
(window!=INVENTORY && window!=EQUIPMENT && window!=SWITCHBOT)) return;
unsigned token=0,window=0,cell=0,vid=0;std::string extra;std::istringstream args(argument);
if (!(args>>token>>window>>cell) || token>2147483647 || window>255 || cell>65535) return;
const bool view=window>=101 && window<=106;
if (view ? !(args>>vid) : (window!=INVENTORY && window!=EQUIPMENT && window!=SWITCHBOT)) return;
if ((args>>extra) || (window!=106 && vid)) return;
quest::PC* pc=quest::CQuestManager::instance().GetPCForce(ch->GetPlayerID());
if (!pc || !pc->IsLoaded()) return;
const int now=get_global_time();
if (ch->GetProtectTime("attr_lock_query")>now) return;
ch->SetProtectTime("attr_lock_query",now+1);
const TItemPos pos{BYTE(window),WORD(cell)};
if (window==SWITCHBOT ? cell>=SWITCHBOT_SLOT_COUNT : cell>=INVENTORY_AND_EQUIP_SLOT_MAX) return;
LPITEM target=ch->GetItem(pos);int mask=0;
if (!view && (window==SWITCHBOT ? cell>=SWITCHBOT_SLOT_COUNT : cell>=INVENTORY_AND_EQUIP_SLOT_MAX)) return;
LPITEM target=view?Ava2AttrLockView(ch,window,cell,vid):ch->GetItem(pos);int mask=0;
if (target && PortAttrFilled(target)) {
mask=PortAttrLockEffective(PortAttrLockGet(target),PortAttrFilled(target),target->GetProto() && target->GetProto()->sAddonType);
if (!target->m_portAttrLockLoaded) mask=-1; // SQL failure is not an unlocked item.
+9
View File
@@ -22,6 +22,15 @@ class CExchange
bool AddItem(TItemPos item_pos, BYTE display_pos);
bool RemoveItem(BYTE pos);
// Tooltip cells are display positions, not m_apItems array indices.
LPITEM GetDisplayItem(unsigned cell) const {
if (cell >= EXCHANGE_ITEM_MAX_NUM) return NULL;
for (unsigned i=0;i<EXCHANGE_ITEM_MAX_NUM;++i)
if (m_apItems[i] && m_abItemDisplayPos[i]==cell) return m_apItems[i];
return NULL;
}
LPCHARACTER GetOwner() { return m_pOwner; }
CExchange * GetCompany() { return m_pCompany; }
+14
View File
@@ -462,6 +462,20 @@ int CShop::Buy(LPCHARACTER ch, BYTE pos)
return (SHOP_SUBHEADER_GC_OK);
}
LPITEM CShop::GetGuestItem(LPCHARACTER guest, unsigned cell)
{
if (!guest || guest->GetShop()!=this || m_map_guest.find(guest)==m_map_guest.end() ||
!m_pkPC || cell>=m_itemVector.size()) return NULL;
const SHOP_ITEM& row=m_itemVector[cell];
// Match AddGuest's hidden high-value stock rule as well as its membership check.
if (quest::CQuestManager::instance().GetEventFlag("hivalue_item_sell")==0 &&
(row.vnum==70024 || row.vnum==70035)) return NULL;
LPITEM item=row.pkItem;
if (!item || item->GetOwner()!=m_pkPC || item->GetID()!=DWORD(row.itemid) ||
item->GetVnum()!=row.vnum || !item->GetCount()) return NULL;
return item;
}
bool CShop::AddGuest(LPCHARACTER ch, DWORD owner_vid, bool bOtherEmpire)
{
if (!ch)
+1
View File
@@ -55,6 +55,7 @@ class CShop
// 아이템이 상점에 등록되어 있는지 알려준다.
virtual bool IsSellingItem(DWORD itemID);
LPITEM GetGuestItem(LPCHARACTER guest, unsigned cell);
DWORD GetVnum() { return m_dwVnum; }
DWORD GetNPCVnum() { return m_dwNPCVnum; }
+98
View File
@@ -0,0 +1,98 @@
#!/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')