aorecomp

Anarchy Online classic in the browser

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


tools/data/extract_clinics.py (2478B)


      1 import hashlib
      2 import json
      3 import pathlib
      4 import struct
      5 
      6 import ao_database
      7 
      8 
      9 def main():
     10     client = pathlib.Path('anarchy-vanilla-14.9.1')
     11     assert (client / 'version.id').read_text().strip() == '14.9.1'
     12     database = ao_database.Database(client)
     13     terminals = {}
     14     records = []
     15 
     16     # Original statels carry identity, transform, template and counted scripts.
     17     for kind, playfield in sorted(database.records):
     18         if kind != 1000026:
     19             continue
     20         payload = database.read(kind, playfield)
     21         offset = 4
     22         for ordinal in range(struct.unpack_from('<I', payload)[0]):
     23             size = struct.unpack_from('<I', payload, offset)[0]
     24             offset += 4
     25             raw = payload[offset:offset + size]
     26             offset += size
     27             assert len(raw) == size
     28             if size < 64 or struct.unpack_from('<I', raw, 56)[0] != 43553:
     29                 continue
     30             identity_type, identity = struct.unpack_from('<II', raw)
     31             assert identity_type == 51005 and identity not in terminals
     32             assert struct.unpack_from('<I', raw, 60)[0] == size - 64
     33             assert raw[64:] == struct.pack('>5I', 102, 103, 1009, 0, 0)
     34             terminals[identity] = dict(playfield=playfield, position=list(struct.unpack_from('<3f', raw, 24)))
     35             records.append(dict(playfield=playfield, ordinal=ordinal, identity=identity,
     36                                 raw_hex=raw.hex(), sha256=hashlib.sha256(raw).hexdigest(),
     37                                 resource_sha256=hashlib.sha256(payload).hexdigest()))
     38         assert offset == len(payload)
     39     database.close()
     40 
     41     # Retain only the location needed to authorize an actual terminal use.
     42     lines = ['# Generated from original 14.9.1 statels by extract_clinics.py.']
     43     for identity, terminal in sorted(terminals.items()):
     44         lines.extend([f'{identity}:', f'  playfield: {terminal["playfield"]}',
     45                       f'  position: {json.dumps(terminal["position"])}'])
     46     output = pathlib.Path('server/gamedata/clinics.yaml')
     47     output.write_text('\n'.join(lines) + '\n')
     48     report = pathlib.Path('local/stationary-clinic/placements.json')
     49     report.parent.mkdir(parents=True, exist_ok=True)
     50     report.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(),
     51                                       records=records), indent=2) + '\n')
     52     print(f'{output}: {len(terminals)} terminals')
     53 
     54 
     55 if __name__ == '__main__':
     56     main()