aorecomp

Anarchy Online classic in the browser

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


tools/client/package_runtime.py (2887B)


      1 """Copy static game resources into the Go server's asset directory."""
      2 import hashlib
      3 import json
      4 import pathlib
      5 import shutil
      6 import subprocess
      7 
      8 
      9 root = pathlib.Path(__file__).resolve().parents[2]
     10 assets = root / 'anarchy-assets-14.9.1'
     11 destination = root / 'server/assets'
     12 destination.mkdir(parents=True, exist_ok=True)
     13 
     14 # Keep a fixed list of static inputs; writable server state is never bundled.
     15 files = []
     16 for name in ['cd_image', 'Setupf', 'fonts']:
     17     files.extend((path, path.relative_to(root).as_posix())
     18                  for path in (assets / name).rglob('*') if path.is_file())
     19 files.extend((assets / name, 'anarchy-assets-14.9.1/' + name)
     20              for name in ['Prefs/Main.cfg', 'Prefs/localtest/Login.cfg', 'version.id',
     21                           'CharCreateCamera.dat'])
     22 files.extend((root / 'data' / name, name) for name in [
     23     'terrain.bin', 'zone_links.yaml',
     24 ])
     25 files.extend((path, path.relative_to(root / 'data').as_posix())
     26              for path in (root / 'data/maps').rglob('*') if path.is_file())
     27 
     28 # Copy ordinary files, preserving the portable client's machine-independent defaults.
     29 versions = []
     30 names = []
     31 for path, name in sorted(files, key=lambda item: item[1]):
     32     # Go embed rejects apostrophes; retain the original name for the guest filesystem.
     33     embedded_name = name.replace("'", "_")
     34     target = destination / embedded_name
     35     if embedded_name != name:
     36         names.append('\t' + json.dumps(embedded_name) + ': ' + json.dumps(name) + ',')
     37         (destination / name).unlink(missing_ok=True)
     38     target.parent.mkdir(parents=True, exist_ok=True)
     39     if path.is_relative_to(assets):
     40         assert path.suffix.lower() not in ['.exe', '.dll', '.com', '.asi', '.flt', '.m3d'], path
     41         with path.open('rb') as source:
     42             prefix = source.read(4)
     43         assert prefix[:2] != b'MZ' and prefix != b'\x7fELF', path
     44     if path == assets / 'Prefs/Main.cfg':
     45         target.write_bytes(b'\n'.join(line for line in path.read_bytes().splitlines()
     46                                     if not line.startswith(b'GfxDeviceName ')) + b'\n')
     47     else:
     48         shutil.copyfile(path, target)
     49 
     50     # Precompute asset ETags so serving a manifest does not scan the game database.
     51     if path.is_relative_to(assets):
     52         with target.open('rb') as source:
     53             version = '"' + hashlib.file_digest(source, 'sha256').hexdigest() + '"'
     54         versions.append('\t' + json.dumps(embedded_name) + ': ' + json.dumps(version) + ',')
     55 
     56 (destination / 'versions.go').write_text(
     57     '// Code generated by tools/client/package_runtime.py; DO NOT EDIT.\n\n'
     58     'package assets\n\nvar Versions = map[string]string{\n' + '\n'.join(versions) + '\n}\n\n'
     59     'var Names = map[string]string{\n' + '\n'.join(names) + '\n}\n')
     60 subprocess.run(['gofmt', '-w', str(destination / 'versions.go')], check=True)
     61 print('Copied %d static resource files' % len(files))