tools/data/test_extract_items.py (5728B)
1 import collections 2 import pathlib 3 import struct 4 import unittest 5 6 import ao_database 7 import extract_items 8 9 10 class ItemTests(unittest.TestCase): 11 def test_ammunition_text_quality_and_combat_stats(self): 12 payload = bytes.fromhex( 13 '3dc70000040000000f000000170000002e3700004c000000000000001e00000005020000' 14 '00000000030440802a01000000000000580000000a000000d4000000640000000c000000' 15 '306600004f000000a87d000036000000010000004a00000050000000d300000096000000' 16 'a4010000030000000200000028000000150000002100000017002900466c616d65746872' 17 '6f77657220416d6d756e6974696f6e5468697320697320416d6d756e6974696f6e20666f' 18 '722074686520666c616d657468726f776572732e0400000004000000d30b00000d000000' 19 'e207000099000000640000000c000000e20700007400000064000000060000001b000000' 20 'd30b00000d000000000000000c00000000000000') 21 22 item = extract_items.parse_item(payload, 1000020, 21601) 23 24 self.assertEqual(item['name'], 'Flamethrower Ammunition') 25 self.assertEqual(item['description'], 'This is Ammunition for the flamethrowers.') 26 self.assertEqual(item['quality'], 1) 27 self.assertEqual(dict(item['stats'])[79], 32168) 28 self.assertEqual(item['sections'][0]['entries'], [ 29 {'type': 13, 'stats': [[153, 100]]}, 30 {'type': 12, 'stats': [[116, 100]]}]) 31 self.assertNotIn('undecoded', item) 32 33 def test_empty_function_string_does_not_consume_next_argument(self): 34 payload = bytes.fromhex( 35 '3dc70000030000000f00000017000000a61700001e000000080000000000000001140080' 36 '26010000020000000c0000007d2a0000360000000100000015000000210000001c000b00' 37 '436c69636b206d6520746f2063726173682074686520636c69656e746372617368657249' 38 '74656d0200000000000000e20700008ecf000000000000040000000000000001000000' 39 '0000000002000000090000000000000020000000596f752068617665206265656e206769' 40 '76656e2061206e6577206e616d652e000a000000') 41 42 item = extract_items.parse_item(payload, 1000020, 120827) 43 44 self.assertEqual(item['sections'][0]['functions'][0]['arguments'], [ 45 {'string': ''}, {'string': 'You have been given a new name.'}, 46 {'bytes_hex': '0a0000'}]) 47 self.assertEqual(item['undecoded'], { 48 'offset': 198, 'reason': 'trailing bytes', 'bytes_hex': '00'}) 49 50 def test_unknown_section_is_preserved_without_scanning_for_another_marker(self): 51 payload = (struct.pack('<5I', 51005, 3, 15, 23, 1009) 52 + struct.pack('<IIHH', 21, 33, 1, 0) + b'A' 53 + struct.pack('<III', 99, 22, 1009)) 54 55 item = extract_items.parse_item(payload, 1000020, 1) 56 57 self.assertEqual(item['sections'], []) 58 self.assertEqual(item['undecoded'], { 59 'offset': 33, 'reason': 'unknown section type 99', 60 'bytes_hex': '6300000016000000f1030000'}) 61 62 def test_invalid_count_is_an_error(self): 63 payload = struct.pack('<5I', 51005, 3, 15, 23, 1010) 64 65 with self.assertRaisesRegex(ValueError, 'invalid serialized count'): 66 extract_items.parse_item(payload, 1000020, 1) 67 68 # assertRaises verifies corrupt framing is not accepted as a definition. 69 70 def test_selected_database_coverage_and_pet_nano_requirements(self): 71 if not pathlib.Path('anarchy-vanilla-14.9.1/cd_image/data/db/ResourceDatabase.idx').exists(): 72 self.skipTest('selected client database is not installed') 73 database = ao_database.Database('anarchy-vanilla-14.9.1') 74 counts = collections.Counter() 75 undecoded = [] 76 77 for kind, identity in sorted(database.records): 78 if kind not in (1000020, 1040005): 79 continue 80 payload = database.read(kind, identity) 81 item = extract_items.parse_item(payload, kind, identity) 82 counts[kind] += 1 83 offset = 32 + len(item['stats']) * 8 84 offset += len(item['name']) + len(item['description']) 85 for section in item['sections']: 86 self.assertEqual(section['offset'], offset, (kind, identity)) 87 offset += section['size'] 88 if 'undecoded' in item: 89 self.assertEqual(item['undecoded']['offset'], offset, (kind, identity)) 90 undecoded.append((kind, identity, item['undecoded']['bytes_hex'])) 91 offset += len(bytes.fromhex(item['undecoded']['bytes_hex'])) 92 self.assertEqual(offset, len(payload), (kind, identity)) 93 nano = extract_items.parse_item(database.read(1040005, 46381), 1040005, 46381) 94 shop = extract_items.parse_item(database.read(1000020, 81799), 1000020, 81799) 95 database.close() 96 97 self.assertEqual(counts, {1000020: 83245, 1040005: 4366}) 98 self.assertEqual(undecoded, [(1000020, 120802, '00'), (1000020, 120827, '00'), 99 (1000020, 203911, '00')]) 100 self.assertEqual(nano['name'], 'Executive-Grade Bodyguard') 101 self.assertEqual(nano['sections'][2]['functions'][0]['arguments'][0], {'code': 'BUBO'}) 102 self.assertEqual(nano['sections'][3]['actions'][0]['requirements'], [ 103 {'stat': 130, 'value': 778, 'operator': 2}, 104 {'stat': 131, 'value': 778, 'operator': 2}, 105 {'stat': 0, 'value': 0, 'operator': 4}, 106 {'stat': 60, 'value': 8, 'operator': 0}, 107 {'stat': 0, 'value': 0, 'operator': 4}]) 108 self.assertEqual(len(shop['sections'][0]['entries']), 12) 109 self.assertEqual(shop['sections'][0]['entries'][-1]['byte_pair'], [0, 0]) 110 self.assertNotIn('undecoded', shop) 111 112 113 if __name__ == '__main__': 114 unittest.main()