aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/data/extract_information.py (4134B)


      1 import hashlib
      2 import json
      3 import pathlib
      4 import struct
      5 
      6 import ao_database
      7 import extract_items
      8 
      9 
     10 def main():
     11     client = pathlib.Path('anarchy-vanilla-14.9.1')
     12     assert (client / 'version.id').read_text().strip() == '14.9.1'
     13     database = ao_database.Database(client)
     14     items = {}
     15     placements = {}
     16     records = []
     17 
     18     # Recover placed text-only objects from their complete original contracts.
     19     for kind, playfield in sorted(database.records):
     20         if kind != 1000026:
     21             continue
     22         payload = database.read(kind, playfield)
     23         offset = 4
     24         for ordinal in range(struct.unpack_from('<I', payload)[0]):
     25             size = struct.unpack_from('<I', payload, offset)[0]
     26             offset += 4
     27             raw = payload[offset:offset + size]
     28             offset += size
     29             assert len(raw) == size
     30             if size < 64 or struct.unpack_from('<I', raw)[0] != 51005:
     31                 continue
     32             template = struct.unpack_from('<I', raw, 56)[0]
     33             if (1000020, template) not in database.records:
     34                 continue
     35             if template not in items:
     36                 items[template] = extract_items.parse_item(database.read(1000020, template), 1000020, template)
     37             item = items[template]
     38             functions = [function for section in item['sections'] for function in section.get('functions', [])]
     39             if not functions or any(function['id'] != 53044 for function in functions):
     40                 continue
     41             assert item['header_words'][0] == 51005 and item['quality'] == 1 and 'undecoded' not in item
     42             assert dict(item['stats'])[0] == 0x80023203 and dict(item['stats'])[30] == 8
     43             assert len(item['sections']) == 1 and len(functions) == 1
     44             assert (item['sections'][0]['type'], item['sections'][0]['subtype']) == (2, 0)
     45             function = functions[0]
     46             assert function['header_words'] == [0, 4] and function['argument_header'] == 9
     47             assert function['target'] == 2 and function['tick_count'] == 1 and function['tick_interval'] == 0
     48             assert function['requirements'] == []
     49             assert len(function['arguments']) == 2 and function['arguments'][0]['string']
     50             assert function['arguments'][1] == dict(bytes_hex='0' * 40)
     51             identity = struct.unpack_from('<I', raw, 4)[0]
     52             assert identity not in placements
     53             assert struct.unpack_from('<I', raw, 60)[0] == size - 64
     54             assert raw[64:] == struct.pack('>5I', 102, 103, 1009, 0, 0)
     55             placements[identity] = dict(template=template, playfield=playfield,
     56                                         position=list(struct.unpack_from('<3f', raw, 24)))
     57             records.append(dict(identity=identity, playfield=playfield, ordinal=ordinal,
     58                                 raw_hex=raw.hex(), sha256=hashlib.sha256(raw).hexdigest(),
     59                                 resource_sha256=hashlib.sha256(payload).hexdigest()))
     60         assert offset == len(payload)
     61     database.close()
     62 
     63     # Store only the template and location needed to authorize native Use.
     64     lines = ['# Generated from original 14.9.1 statels by extract_information.py.']
     65     for identity, placement in sorted(placements.items()):
     66         lines.extend([f'{identity}:', f'  template: {placement["template"]}',
     67                       f'  playfield: {placement["playfield"]}',
     68                       f'  position: {json.dumps(placement["position"])}'])
     69     output = pathlib.Path('server/gamedata/information.yaml')
     70     output.write_text('\n'.join(lines) + '\n')
     71     report = pathlib.Path('local/static-information/placements.json')
     72     report.parent.mkdir(parents=True, exist_ok=True)
     73     templates = sorted({placement['template'] for placement in placements.values()})
     74     report.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(),
     75                                       items=[items[template] for template in templates], records=records), indent=2) + '\n')
     76     print(f'{output}: {len(placements)} placements, {len(templates)} templates')
     77 
     78 
     79 if __name__ == '__main__':
     80     main()