steam_manifest_links.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. """Restores what DepotDownloader leaves out of a Steam depot: symlinks (written as empty files), the executable bit
  3. Steam gives every file on Linux, and steampipe_fixups.json (empty dirs, read-only files) when the depot has one.
  4. steam_manifest_links.py <install dir> [depot key hex] reads <install dir>/.DepotDownloader/*.manifest
  5. Link targets stay encrypted in the saved manifest (only file names get decrypted), the depot key decrypts them
  6. (Steam's symmetric scheme: AES-256-ECB encrypted IV, then AES-256-CBC). Needs pycryptodomex for that (the
  7. steam package's venv has it).
  8. """
  9. import base64, glob, os, stat, struct, subprocess, sys
  10. PAYLOAD_MAGIC = 0x71F617D0
  11. FLAG_SYMLINK = 0x200
  12. FLAG_DIRECTORY = 0x40
  13. def varint(b, i):
  14. v = s = 0
  15. while True:
  16. c = b[i]; i += 1
  17. v |= (c & 0x7f) << s; s += 7
  18. if c < 0x80:
  19. return v, i
  20. def fields(b):
  21. i = 0
  22. while i < len(b):
  23. key, i = varint(b, i)
  24. num, wt = key >> 3, key & 7
  25. if wt == 0:
  26. v, i = varint(b, i)
  27. elif wt == 2:
  28. n, i = varint(b, i)
  29. v = b[i:i + n]; i += n
  30. elif wt == 1:
  31. v = b[i:i + 8]; i += 8
  32. elif wt == 5:
  33. v = b[i:i + 4]; i += 4
  34. else:
  35. raise ValueError('wire type %d' % wt)
  36. yield num, v
  37. def mappings(path):
  38. data = open(path, 'rb').read()
  39. magic, n = struct.unpack_from('<II', data, 0)
  40. if magic != PAYLOAD_MAGIC:
  41. sys.exit('%s: not a Steam manifest payload' % path)
  42. for num, m in fields(data[8:8 + n]):
  43. if num != 1:
  44. continue
  45. f = dict(fields(m))
  46. yield f.get(1, b'').decode().replace('\\', '/'), f.get(3, 0), f.get(7, b'').decode()
  47. def decrypt(target, key):
  48. from Cryptodome.Cipher import AES
  49. from Cryptodome.Util.Padding import unpad
  50. raw = base64.b64decode(target)
  51. iv = AES.new(key, AES.MODE_ECB).decrypt(raw[:16])
  52. return unpad(AES.new(key, AES.MODE_CBC, iv).decrypt(raw[16:]), 16).rstrip(b'\0').decode()
  53. root = sys.argv[1]
  54. key = bytes.fromhex(sys.argv[2]) if len(sys.argv) > 2 else None
  55. links = 0
  56. for man in glob.glob(os.path.join(root, '.DepotDownloader', '*.manifest')):
  57. for name, flags, target in mappings(man):
  58. p = os.path.join(root, name)
  59. if flags & FLAG_SYMLINK and target:
  60. if key:
  61. target = decrypt(target, key)
  62. if os.path.islink(p) or (os.path.isfile(p) and os.path.getsize(p) == 0):
  63. os.remove(p)
  64. if not os.path.lexists(p):
  65. os.makedirs(os.path.dirname(p), exist_ok=True)
  66. os.symlink(target, p)
  67. links += 1
  68. for d, _, files in os.walk(root):
  69. for f in files:
  70. p = os.path.join(d, f)
  71. if not os.path.islink(p):
  72. os.chmod(p, os.stat(p).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
  73. fix = os.path.join(root, 'steampipe_fixups.py')
  74. if os.path.exists(fix):
  75. subprocess.run([sys.executable, '-I', fix, 'restore', root], check=True)
  76. print('%d symlinks restored' % links)