Files
metin2-server/tests/features/test_pet_commands_runtime.py
T

100 lines
8.0 KiB
Python

"""Execute real growing-pet commands with failing creation/placement/actor boundaries."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT=Path(__file__).resolve().parents[2]
class PetCommands(unittest.TestCase):
def test_consumption_only_after_successful_hatch_or_unlock(self):
source=(ROOT/'src/server/game/src/cmd_general.cpp').read_text(encoding='latin1')
a=source.index('static bool PortPetCommandReady');b=source.index('\n#endif',a);body=source[a:b]
harness=r'''
#include "src/server/common/utils.h"
#include "src/server/common/port_item_extra.h"
#include <cassert>
#include <cstdint>
#include <ctime>
#include <map>
#include <sstream>
#include <string>
#include <vector>
bool questLoaded=true,questRunning=false;
namespace quest {struct PC{bool IsLoaded(){return questLoaded;}bool IsRunning(){return questRunning;}};struct CQuestManager{PC pc;static CQuestManager& instance(){static CQuestManager m;return m;}PC* GetPCForce(unsigned){return &pc;}};}
using BYTE=uint8_t;using WORD=uint16_t;using DWORD=uint32_t;
constexpr int INVENTORY_MAX_NUM=225,CHARACTER_NAME_MAX_LEN=24,ITEM_PET=28,PET_LEVELABLE=1,PET_EGG=0,POINT_GOLD=11,CHAT_TYPE_INFO=1,INVENTORY=1,WEAR_PET=30,POINT_PET_SKILL_INDEX_1=20;
#define _countof(a) (sizeof(a)/sizeof(a[0]))
struct CHARACTER;struct Item;using LPITEM=Item*;using LPCHARACTER=CHARACTER*;
struct TItemPos{int cell;TItemPos(int,int c):cell(c){}};
struct TItemTable{int bType=ITEM_PET,bSubType=PET_LEVELABLE,bSize=1;};
struct Item{CHARACTER* owner=nullptr;unsigned vnum=1,count=2;int type=ITEM_PET,sub=PET_EGG;bool locked=false,exchanged=false,equipped=false,skip=false;long sockets[3]={};BYTE attr[7]={};PortItemExtra extra={};
CHARACTER* GetOwner(){return owner;}unsigned GetCount(){return count;}unsigned GetVnum(){return vnum;}int GetType(){return type;}int GetSubType(){return sub;}
bool IsEquipped(){return equipped;}bool IsExchanging(){return exchanged;}bool isLocked(){return locked;}int GetValue(int){return 53000;}
void SetCount(unsigned n){count=n;}void SetSkipSave(bool b){skip=b;}void SetNewName(const char* s){strncpy(extra.name,s,sizeof(extra.name)-1);}
void SetSocket(unsigned i,long n){if(i<3)sockets[i]=n;else extra.sockets[i-3]=n;}long GetSocket(int i){return sockets[i];}
void SetForceAttribute(unsigned i,BYTE n,int){if(i<7)attr[i]=n;else extra.types[i-7]=n;}int GetAttributeType(int i){return i<7?attr[i]:extra.types[i-7];}
void Save(){}void UpdatePacket(){}bool AddToCharacter(CHARACTER*,TItemPos);
};
struct CPetActor{Item* summon=nullptr;bool accept=true;int changes=0,evolved=0;
Item* GetSummonItem(){return summon;}void IncreaseEvolve(){++evolved;}
bool PointChange(int point,int){if(!accept)return false;++changes;summon->SetForceAttribute(point-POINT_PET_SKILL_INDEX_1,0,0);return true;}
};using LPPET=CPetActor*;
struct CPetSystem{CPetActor actor;CPetActor* GetNewPet(){return &actor;}BYTE PetItemSocketMax(DWORD){return 14;}};
struct CHARACTER{int64_t gold=500000;bool itemLoaded=true,busy=false,full=false,failAdd=false;int material=1;std::map<int,Item*> items;Item* wear=nullptr;CPetSystem system;
unsigned GetPlayerID(){return 1;}bool IsItemLoaded(){return itemLoaded;}bool IsPC(){return true;}bool GetDesc(){return true;}bool CanHandleItem(){return !busy;}bool IsDead(){return false;}bool IsStun(){return false;}bool IsWarping(){return false;}
bool GetExchange(){return false;}bool GetMyShop(){return false;}bool GetShop(){return false;}bool IsOpenSafebox(){return false;}bool IsCubeOpen(){return false;}
int64_t GetGold(){return gold;}void PointChange(int,int64_t n){assert(gold+n>=0);gold+=n;}
Item* GetInventoryItem(int p){return items.count(p)?items[p]:nullptr;}int GetEmptyInventory(int){return full?-1:2;}
template<class...T>void ChatPacket(T...){}Item* GetWear(int){return wear;}CPetSystem* GetPetSystem(){return &system;}
int CountSpecifyItem(unsigned){return material;}void RemoveSpecifyItem(unsigned,unsigned n){assert(material>=int(n));material-=n;}
};
bool Item::AddToCharacter(CHARACTER* ch,TItemPos pos){if(ch->failAdd)return false;assert(skip && extra.name[0] && sockets[1]==1);for(int j=0;j<15;++j)assert(GetAttributeType(j)==99);owner=ch;ch->items[pos.cell]=this;return true;}
struct ITEM_MANAGER{bool fail=false,validProto=true;TItemTable table;std::vector<Item*> rewards;int flushed=0;
static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;}
TItemTable* GetTable(unsigned){return validProto?&table:nullptr;}
Item* CreateItem(unsigned vnum,unsigned count){if(fail)return nullptr;Item* i=new Item;i->vnum=vnum;i->count=count;i->sub=PET_LEVELABLE;rewards.push_back(i);return i;}
void Destroy(Item* i){assert(i->skip && !i->owner);for(auto& p:rewards)if(p==i){p=nullptr;break;}delete i;}
void FlushDelayedSave(Item* i){assert(!i->skip);++flushed;}
~ITEM_MANAGER(){for(auto* i:rewards)delete i;}
};
#define M2_DESTROY_ITEM(i) ITEM_MANAGER::instance().Destroy(i)
#define ACMD(name) void name(LPCHARACTER ch,const char* argument)
bool check_name(const char* name){return strchr(name,'!')==nullptr;}
'''
main=r'''
int main(){
CHARACTER ch;Item egg,scroll;egg.owner=&ch;scroll.owner=&ch;scroll.vnum=55008;ch.items[0]=&egg;ch.items[1]=&scroll;
auto unchanged=[&](){assert(ch.gold==500000 && egg.count==2 && ch.items.size()==2 && ITEM_MANAGER::instance().flushed==0);};
for(const char* arg:{"0","0 abc extra","-1 abc","225 abc","4294967296 abc","9223372036854775808 abc","0 !bad","0 aa","0 abcdefghijklmnopqrstuvwxyz"}){do_pet_egg(&ch,arg);unchanged();}
ch.itemLoaded=false;do_pet_egg(&ch,"0 abc");unchanged();ch.itemLoaded=true;questLoaded=false;do_pet_egg(&ch,"0 abc");unchanged();questLoaded=true;questRunning=true;do_pet_egg(&ch,"0 abc");unchanged();questRunning=false;
ch.busy=true;do_pet_egg(&ch,"0 abc");unchanged();ch.busy=false;
egg.exchanged=true;do_pet_egg(&ch,"0 abc");unchanged();egg.exchanged=false;
egg.locked=true;do_pet_egg(&ch,"0 abc");unchanged();egg.locked=false;
ch.full=true;do_pet_egg(&ch,"0 abc");unchanged();ch.full=false;
auto& manager=ITEM_MANAGER::instance();manager.validProto=false;do_pet_egg(&ch,"0 abc");unchanged();manager.validProto=true;
manager.table.bSubType=PET_EGG;do_pet_egg(&ch,"0 abc");unchanged();manager.table.bSubType=PET_LEVELABLE;
manager.fail=true;do_pet_egg(&ch,"0 abc");unchanged();manager.fail=false;
ch.failAdd=true;do_pet_egg(&ch,"0 abc");unchanged();ch.failAdd=false;
do_pet_egg(&ch,"0 abc");assert(ch.gold==400000 && egg.count==1 && ch.items.size()==3 && manager.flushed==1);
Item* pet=ch.items[2];assert(!strcmp(pet->extra.name,"abc") && pet->sockets[1]==1 && pet->sockets[0]>time(0));for(unsigned j=0;j<15;++j)assert(pet->GetAttributeType(j)==99);
pet->locked=true;do_change_name(&ch,"1 2 renamed");assert(ch.gold==400000 && scroll.count==2);pet->locked=false;
for(const char* arg:{"1 2 renamed extra","-1 2 renamed","1 4294967296 renamed"})do_change_name(&ch,arg);
assert(ch.gold==400000 && scroll.count==2 && !strcmp(pet->extra.name,"abc"));
do_change_name(&ch,"1 2 renamed");assert(ch.gold==300000 && scroll.count==1 && !strcmp(pet->extra.name,"renamed"));
ch.wear=pet;pet->equipped=true;pet->sockets[1]=115;ch.system.actor.summon=pet;
ch.system.actor.accept=false;do_pet_add_slot(&ch,"3");assert(ch.material==1 && ch.system.actor.changes==0);
ch.system.actor.accept=true;for(const char* arg:{"3 extra","-1","4294967299","15"})do_pet_add_slot(&ch,arg);
assert(ch.material==1 && ch.system.actor.changes==0);
pet->locked=true;do_pet_add_slot(&ch,"3");assert(ch.material==1);pet->locked=false;
do_pet_add_slot(&ch,"3");assert(ch.material==0 && ch.system.actor.changes==1 && pet->GetAttributeType(3)==0);
pet->sockets[1]=40;ch.busy=true;do_pet_evolve(&ch,"");assert(ch.system.actor.evolved==0);ch.busy=false;
do_pet_evolve(&ch,"");assert(ch.system.actor.evolved==1);
}
'''
with tempfile.TemporaryDirectory() as tmp:
src=Path(tmp)/'probe.cpp';exe=Path(tmp)/'probe';src.write_text(harness+body+main)
subprocess.run(['c++','-std=c++17','-fsanitize=undefined,address','-fno-sanitize-recover=all','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
subprocess.run([str(exe)],check=True)
if __name__=='__main__':unittest.main()