live_trail_probe.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. """Times the ropetrail history pushes of a running real WE (Wine) from its process memory.
  3. usage: live_trail_probe.py <trail length> [seconds to sample] [pid]
  4. A WE particle system keeps the countdown to the next history push at +588 and the interval (length / segments) at
  5. +592 (u16 segment count at +584, sub_1402308A0). The system is found by scanning for a float at +592 times the
  6. segment count at +584 equal to the renderer's length, with pointers where a ropetrail object has them, then +588 is read every millisecond: each frame lowers it by that frame's dt, a push resets it to the interval.
  7. Prints one line per detected frame: time (ms), dt (ms) or PUSH.
  8. """
  9. import re
  10. import struct
  11. import subprocess
  12. import sys
  13. import time
  14. import numpy as np
  15. length = float(sys.argv[1])
  16. seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 20.0
  17. pid = int(sys.argv[3]) if len(sys.argv) > 3 else int(
  18. subprocess.check_output(['pgrep', '-x', 'wallpaper64.exe'], text=True).split()[0])
  19. mem = open(f'/proc/{pid}/mem', 'rb', 0)
  20. def regions():
  21. for line in open(f'/proc/{pid}/maps'):
  22. parts = line.split()
  23. if 'rw' not in parts[1]:
  24. continue
  25. a, b = (int(x, 16) for x in parts[0].split('-'))
  26. if b - a <= 1 << 30:
  27. yield a, b
  28. def read(addr, size):
  29. mem.seek(addr)
  30. return mem.read(size)
  31. def plausible(ptr):
  32. return 0x10000 < ptr < 0x7FFFFFFFFFFF and ptr % 8 == 0
  33. def scan():
  34. found = []
  35. for a, b in regions():
  36. try:
  37. data = read(a, b - a)
  38. except OSError:
  39. continue
  40. floats = np.frombuffer(data[: len(data) // 4 * 4], dtype='<f4')
  41. with np.errstate(invalid='ignore'):
  42. likely = np.nonzero((floats > 0.001) & (floats < 1.0))[0]
  43. for i in likely:
  44. off = int(i) * 4 - 592
  45. if off < 0 or off + 616 > len(data):
  46. continue
  47. segments = struct.unpack_from('<H', data, off + 584)[0]
  48. interval = float(floats[i])
  49. timer = struct.unpack_from('<f', data, off + 588)[0]
  50. if not (2 <= segments <= 128 and abs(segments * interval - length) < 1e-3
  51. and 0.0 <= timer <= interval + 1e-4):
  52. continue
  53. pointers = [struct.unpack_from('<Q', data, off + o)[0] for o in (512, 560, 568)]
  54. if all(plausible(p) for p in pointers):
  55. found.append(a + off)
  56. return found
  57. def timer_of(addr):
  58. data = read(addr + 588, 4)
  59. return struct.unpack('<f', data)[0] if len(data) == 4 else None
  60. def lock(limit):
  61. """A system that is counting: freed ones leave stale copies behind, and a particle system that is destroyed
  62. (scene reload, all particles gone) has to be found again"""
  63. deadline = time.perf_counter() + limit
  64. while time.perf_counter() < deadline:
  65. for addr in scan():
  66. seen = set()
  67. for _ in range(150):
  68. seen.add(timer_of(addr))
  69. time.sleep(0.002)
  70. if len(seen) >= 2 and None not in seen:
  71. return addr
  72. return None
  73. base = lock(40)
  74. if base is None:
  75. sys.exit('no ropetrail system found that keeps counting')
  76. print('system at', hex(base), file=sys.stderr)
  77. last = None
  78. start = time.perf_counter()
  79. while time.perf_counter() - start < seconds:
  80. timer = timer_of(base)
  81. now = (time.perf_counter() - start) * 1000.0
  82. if timer is None or timer < 0 or timer > 10:
  83. print(f'{now:.2f} LOST', file=sys.stderr)
  84. base = lock(10)
  85. last = None
  86. if base is None:
  87. break
  88. continue
  89. if last is not None and timer != last:
  90. if timer > last:
  91. print(f'{now:.2f} PUSH {last:.5f} -> {timer:.5f}')
  92. else:
  93. print(f'{now:.2f} dt {(last - timer) * 1000:.3f}')
  94. last = timer
  95. time.sleep(0.0005)