Files
metin2-client/tests/wiki/test_quiver_runtime.py

56 lines
2.0 KiB
Python

"""Compile the actual client arrow-slot check without invoking the game UI."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
class QuiverClientTests(unittest.TestCase):
def test_actual_client_ammunition_gate(self):
text = (ROOT/'src/port/UserInterface/PythonPlayerSkill.cpp').read_text()
body = text[text.index('bool CPythonPlayer::__HasEnoughArrow()'):text.index('bool CPythonPlayer::__HasItem(')]
code = r'''
#include <cassert>
enum {INVENTORY,c_Equipment_Arrow};
struct TItemPos{TItemPos(int,int){}};
struct CItemData{
enum {ITEM_TYPE_WEAPON=1,WEAPON_ARROW=6,WEAPON_QUIVER=9};
int type=ITEM_TYPE_WEAPON,subtype=WEAPON_ARROW;
int GetType(){return type;}int GetSubType(){return subtype;}
};
struct CItemManager{
CItemData* item=nullptr;static CItemManager& Instance(){static CItemManager m;return m;}
bool GetItemDataPointer(int,CItemData** out){*out=item;return item!=nullptr;}
};
int errors=0;
#define Py_BuildValue(...) nullptr
void PyCallClassMemberFunc(void*,const char*,void*){++errors;}
struct CPythonPlayer{
void* m_ppyGameWindow=nullptr;
int GetItemIndex(TItemPos){return 0;}bool __HasEnoughArrow();
};
''' + body + r'''
int main(){
CPythonPlayer player;CItemData item;
assert(!player.__HasEnoughArrow() && errors==1);
CItemManager::Instance().item=&item;
assert(player.__HasEnoughArrow());
item.subtype=CItemData::WEAPON_QUIVER;assert(player.__HasEnoughArrow());
item.subtype=2;assert(!player.__HasEnoughArrow());
item.subtype=CItemData::WEAPON_QUIVER;item.type=3;
assert(!player.__HasEnoughArrow() && errors==3);
}
'''
with tempfile.TemporaryDirectory(prefix='ch5-quiver-client-') as folder:
root = Path(folder)
(root/'probe.cpp').write_text(code)
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined',
str(root/'probe.cpp'),'-o',str(root/'probe')],check=True)
subprocess.run([str(root/'probe')],check=True)
if __name__ == '__main__':
unittest.main()