Added replay ability

This commit is contained in:
2026-10-10 23:00:04 -06:00
parent 13b1dcdd97
commit fbfa90edec
16 changed files with 700 additions and 39 deletions
+13 -1
View File
@@ -1,4 +1,16 @@
from .gui.app import run
import sys
from .environment import replay_path, replay_headless
def run():
if replay_path() and replay_headless():
from .replay_cli import run_headless
sys.exit(run_headless())
from .gui.app import run as run_gui
run_gui()
if __name__ == '__main__':
run()
+25
View File
@@ -43,9 +43,13 @@ INITIALIZATION_RESULT = {
NATIVE_BACKEND_AVAILABLE = platform_utils.is_linux()
_capture = None
_replay = None
def initialize():
if environment.replay_path():
return InitializationResult.Ok
if TEST_ENV_ENABLED:
return InitializationResult.Ok
@@ -68,6 +72,9 @@ def subscribe(callback):
_testing_call_sequence(callback)
return True
if environment.replay_path():
return _start_replay_backend(callback)
if NATIVE_BACKEND_AVAILABLE:
return _start_native_backend(callback)
@@ -98,6 +105,24 @@ def _start_native_backend(callback):
return True
def _start_replay_backend(callback):
global _replay
if _replay is not None:
return True
from .decode.pipeline import Pipeline
from .net.replay import ReplayEngine
pipeline = Pipeline(callback)
replay = ReplayEngine(pipeline.on_udp)
if not replay.start():
logger.error("replay backend could not start; check the log path")
return False
_replay = replay
return True
def _testing_call_sequence(callback):
def add_player(id, name, weapon, event_name='PlayerAppeared'):
return [
+13 -1
View File
@@ -4,7 +4,7 @@ import threading
from datetime import datetime, timezone
from typing import Any, Dict, List
from ..environment import discovery_log_path
from ..environment import discovery_log_path, replay_path
from . import items as itemdb
@@ -16,11 +16,23 @@ def enabled() -> bool:
return bool(discovery_log_path())
def _same_file(a: str, b: str) -> bool:
try:
return os.path.abspath(a) == os.path.abspath(b)
except (TypeError, ValueError):
return False
def log(record_type: str, **fields):
path = discovery_log_path()
if not path:
return
# Never clobber the log we are replaying from.
source = replay_path()
if source and _same_file(path, source):
return
global _initialized
with _lock:
log_dir = os.path.dirname(path)
+9 -3
View File
@@ -135,10 +135,16 @@ def deserialize18(buf: bytes, pos: int, tc: int):
return rd_f64le(buf, pos)
if tc == P18['String']:
return read_string18(buf, pos)
if tc in (P18['Null'], P18['BooleanFalse'], P18['BooleanTrue'],
P18['ShortZero'], P18['IntZero'], P18['LongZero'],
P18['FloatZero'], P18['DoubleZero']):
if tc == P18['Null']:
return None, pos
if tc == P18['BooleanFalse']:
return False, pos
if tc == P18['BooleanTrue']:
return True, pos
if tc in (P18['ShortZero'], P18['IntZero'], P18['LongZero']):
return 0, pos
if tc in (P18['FloatZero'], P18['DoubleZero']):
return 0.0, pos
if tc == P18['CompressedInt']:
v, p = rd_varint(buf, pos)
return zigzag(v), p
+33 -6
View File
@@ -11,10 +11,10 @@ EVENT_LEAVE = 1
EVENT_HEALTH_UPDATE = 6
EVENT_NEW_CHARACTER = 29
EVENT_ITEM_GAINED = 32
EVENT_TAKE_SILVER = 62
EVENT_UPDATE_FAME = 82
EVENT_CHARACTER_EQUIPMENT_CHANGED = 90
EVENT_REGENERATION_HEALTH_CHANGED = 91
EVENT_SILVER_GAINED = 153
EVENT_KNOCKED_DOWN = 166
EVENT_PARTY_INVITATION = 229
EVENT_PARTY_JOIN_REQUEST = 230
@@ -115,10 +115,10 @@ EVENT_NAMES = {
EVENT_HEALTH_UPDATE: 'HealthUpdate',
EVENT_NEW_CHARACTER: 'NewCharacter',
EVENT_ITEM_GAINED: 'ItemGained',
EVENT_TAKE_SILVER: 'TakeSilver',
EVENT_UPDATE_FAME: 'UpdateFame',
EVENT_CHARACTER_EQUIPMENT_CHANGED: 'CharacterEquipmentChanged',
EVENT_REGENERATION_HEALTH_CHANGED: 'RegenerationHealthChanged',
EVENT_SILVER_GAINED: 'SilverGained',
EVENT_KNOCKED_DOWN: 'KnockedDown',
EVENT_PARTY_INVITATION: 'PartyInvitation',
EVENT_PARTY_JOIN_REQUEST: 'PartyJoinRequest',
@@ -191,6 +191,22 @@ def msg_fame_gained(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(2))
def msg_fame_is_premium(msg: GameMessage) -> bool:
return msg.params.get(5) is True
def msg_fame_satchel(msg: GameMessage) -> int:
v = _int(msg.params.get(10))
return v if v is not None else 0
def msg_fame_bonus_factor(msg: GameMessage) -> float:
if 17 not in msg.params:
return 0.0
v = _float(msg.params.get(17))
return v if v is not None else 0.0
def msg_collected_item_id(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(1))
@@ -203,12 +219,23 @@ def msg_collected_item_value(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(4))
def msg_silver_source_type(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(1))
def msg_silver_yield_pre_tax(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(3))
def msg_silver_amount(msg: GameMessage) -> Optional[int]:
return _int(msg.params.get(2))
def msg_silver_cluster_tax(msg: GameMessage) -> int:
v = _int(msg.params.get(4))
return v if v is not None else 0
def msg_silver_guild_tax(msg: GameMessage) -> int:
v = _int(msg.params.get(5))
return v if v is not None else 0
def msg_silver_alliance_penalty(msg: GameMessage) -> int:
v = _int(msg.params.get(6))
return v if v is not None else 0
def msg_party_structures(msg: GameMessage) -> List[List[int]]:
+24 -13
View File
@@ -9,7 +9,6 @@ from .translate import GameMessage
FIX_POINT = 10000.0
FAME_MIN_TOTAL = 1000000 # albion-lens sanity filter for UpdateFame events
SILVER_SOURCE_LOOTED = 54
logger = logging.getLogger(__name__)
@@ -180,8 +179,8 @@ class World:
return self._on_update_fame(message)
if message.name == 'ItemGained':
return self._on_item_gained(message)
if message.name == 'SilverGained':
return self._on_silver_gained(message)
if message.name == 'TakeSilver':
return self._on_take_silver(message)
if message.name == 'CharacterEquipmentChanged':
return self._on_character_equipment_changed(message)
if message.name == 'PartyJoined':
@@ -339,7 +338,14 @@ class World:
gained = total - prev
if gained <= 0:
return []
return [{'name': 'UpdateFame', 'value': {'value': float(gained / FIX_POINT)}}]
# Match the in-game display: zone/event bonus factor and premium are
# applied on top of the base fame, plus any satchel (insight bag) fame.
value = float(gained)
if translate.msg_fame_is_premium(msg):
value *= 1.5
value += float(translate.msg_fame_satchel(msg))
value *= 1.0 + translate.msg_fame_bonus_factor(msg)
return [{'name': 'UpdateFame', 'value': {'value': value / FIX_POINT}}]
def _on_item_gained(self, msg: GameMessage) -> List[dict]:
if not self._main_player_joined:
@@ -364,20 +370,25 @@ class World:
value['estimated_value'] = estimated_value
return [{'name': 'ItemCollected', 'value': value}]
def _on_silver_gained(self, msg: GameMessage) -> List[dict]:
def _on_take_silver(self, msg: GameMessage) -> List[dict]:
if not self._main_player_joined:
return []
amount = translate.msg_silver_amount(msg)
source_type = translate.msg_silver_source_type(msg)
if amount is None or amount <= 0:
dynamic_id = _dyn(translate.msg_source(msg))
static_id = self.cache.get_static_id(dynamic_id)
if static_id is None or static_id != self.main_player_id:
return []
if source_type == SILVER_SOURCE_LOOTED:
silver_type = 'looted'
else:
yield_pre_tax = translate.msg_silver_yield_pre_tax(msg)
if yield_pre_tax is None or yield_pre_tax <= 0:
return []
taxes = (translate.msg_silver_cluster_tax(msg)
+ translate.msg_silver_guild_tax(msg)
+ translate.msg_silver_alliance_penalty(msg))
gained = max(0, yield_pre_tax - taxes)
if gained <= 0:
return []
return [{'name': 'SilverGained', 'value': {
'value': float(amount),
'silver_type': silver_type,
'value': float(gained / FIX_POINT),
'silver_type': 'looted',
}}]
def _on_character_equipment_changed(self, msg: GameMessage) -> List[dict]:
+48
View File
@@ -35,6 +35,35 @@ REMEMBER_LAST_PARTY = bool(
'--remember-last-party' in sys.argv
)
# Replay a captured discovery log instead of capturing live packets, e.g.
# ``aostats --replay /tmp/capture.jsonl``.
REPLAY_PATH = (
os.getenv('AOSTATS_REPLAY') or
_arg_value('--replay')
)
def _float_arg(name: str) -> Optional[float]:
value = _arg_value(name)
if value is None:
return None
try:
return float(value)
except ValueError:
return None
# Replay pacing: 0 means as fast as possible (default), 1.0 means real time
# (honour the timestamps in the log), N means N times faster than real time.
REPLAY_SPEED = _float_arg('--replay-speed') or 0.0
# When replaying, ``--replay-headless`` prints a summary and exits instead of
# opening the GUI.
REPLAY_HEADLESS = bool(
os.getenv('AOSTATS_REPLAY_HEADLESS') or
'--replay-headless' in sys.argv
)
_fault_file = None
@@ -50,6 +79,18 @@ def remember_last_party():
return REMEMBER_LAST_PARTY
def replay_path() -> Optional[str]:
return REPLAY_PATH
def replay_speed() -> float:
return REPLAY_SPEED
def replay_headless() -> bool:
return REPLAY_HEADLESS
def app_log_path() -> Optional[str]:
if not DISCOVERY_LOG_PATH:
return None
@@ -73,6 +114,13 @@ def qt_args(argv: List[str]) -> List[str]:
continue
if arg == '--remember-last-party':
continue
if arg == '--replay' or arg == '--replay-speed':
skip_next = True
continue
if arg.startswith('--replay=') or arg.startswith('--replay-speed='):
continue
if arg == '--replay-headless':
continue
result.append(arg)
return result
+9 -4
View File
@@ -19,7 +19,7 @@ from PySide6.QtWidgets import QPushButton # type: ignore
from ..engine import InitializationResult
from ..engine import initialize
from ..utils.config import config
from ..environment import is_verbose, qt_args, remember_last_party, setup_logging
from ..environment import is_verbose, qt_args, remember_last_party, replay_path, setup_logging
from .modules.main import MainWidget
from .styling import style
@@ -82,6 +82,8 @@ def run():
logger.debug("python: %s (%s)", sys.executable, sys.version)
logger.debug("platform: %s", platform_utils.system())
replaying = bool(replay_path())
conf = config()
window_config = conf['window']
@@ -93,7 +95,7 @@ def run():
_install_heartbeat(app)
perms_ok = True
if platform_utils.is_linux():
if platform_utils.is_linux() and not replaying:
perms_ok = capture_permissions_ok()
logger.debug("capture permissions probe: %s", perms_ok)
@@ -107,7 +109,10 @@ def run():
widget = MainWidget(clipboard)
widget.setWindowOpacity(window_config['opacity'])
widget.setWindowTitle('Albion Online Stats')
title = 'Albion Online Stats'
if replaying:
title += ' (replay)'
widget.setWindowTitle(title)
widget.setWindowIcon(QtGui.QIcon(path('albion-stats-icon.png')))
if window_config['always_on_top']:
@@ -134,7 +139,7 @@ def run():
msg.setStandardButtons(QMessageBox.Ok)
msg.show()
need_capture_fix = (
need_capture_fix = not replaying and (
initialization_result != InitializationResult.Ok
or (platform_utils.is_linux() and not perms_ok)
)
+139
View File
@@ -0,0 +1,139 @@
import json
import logging
import os
import re
import threading
from datetime import datetime
from typing import Callable, Iterator, List, Optional, Tuple
from .. import environment
logger = logging.getLogger(__name__)
_RAW_PAYLOAD_RE = re.compile(r'Raw payload:\s*\[([0-9,\s]*)\]')
def _record_timestamp(record: dict) -> Optional[float]:
raw = record.get('timestamp')
if not raw:
return None
try:
return datetime.fromisoformat(raw).timestamp()
except ValueError:
return None
def _json_payload(record: dict) -> Optional[bytes]:
if record.get('type') != 'udp_payload':
return None
payload_hex = record.get('payload_hex')
if not payload_hex:
return None
return bytes.fromhex(payload_hex)
def _raw_payload(line: str) -> Optional[bytes]:
match = _RAW_PAYLOAD_RE.search(line)
if not match:
return None
parts = [part.strip() for part in match.group(1).split(',')]
numbers = [int(part) for part in parts if part]
if not numbers:
return None
return bytes(numbers)
def iter_records(path: str) -> Iterator[Tuple[bytes, Optional[float]]]:
"""Yield ``(payload, timestamp)`` for every captured packet in ``path``.
Supports both the discovery JSONL format (records of ``type``
``udp_payload`` with a ``payload_hex`` field) and the legacy aoaddons
``Raw payload: [...]`` text format. All other record types are ignored.
"""
with open(path, encoding='utf-8', errors='replace') as log:
for line in log:
line = line.strip()
if not line:
continue
if line.startswith('{'):
try:
record = json.loads(line)
except json.JSONDecodeError:
record = None
if isinstance(record, dict):
payload = _json_payload(record)
if payload is not None:
yield payload, _record_timestamp(record)
continue
payload = _raw_payload(line)
if payload is not None:
yield payload, None
def load_payloads(path: str) -> List[bytes]:
"""Return every captured UDP payload from ``path`` in order."""
return [payload for payload, _ in iter_records(path)]
class ReplayEngine:
"""Feeds previously captured UDP payloads into a decode callback.
Mirrors :class:`src.net.capture.CaptureEngine` so the rest of the app can
treat a replayed log exactly like live traffic.
"""
def __init__(self, on_udp: Callable[[bytes], None],
path: Optional[str] = None,
speed: Optional[float] = None):
self._on_udp = on_udp
self._path = path if path is not None else environment.replay_path()
self._speed = speed if speed is not None else environment.replay_speed()
self._stop = threading.Event()
self._thread: Optional[threading.Thread] = None
self.packet_count = 0
def start(self) -> bool:
if self._thread is not None:
return True
if not self._path:
logger.error("replay requested but no log path was given")
return False
if not os.path.isfile(self._path):
logger.error("replay log does not exist: %s", self._path)
return False
self._thread = threading.Thread(
target=self._run, name='aostats-replay', daemon=True)
self._thread.start()
return True
def stop(self):
self._stop.set()
def wait(self, timeout: Optional[float] = None):
if self._thread is not None:
self._thread.join(timeout)
def _run(self):
logger.info("replaying %s (speed=%s)", self._path, self._speed)
previous: Optional[float] = None
try:
for payload, timestamp in iter_records(self._path):
if self._stop.is_set():
return
if self._speed and self._speed > 0 \
and timestamp is not None and previous is not None:
delay = (timestamp - previous) / self._speed
if delay > 0 and self._stop.wait(delay):
return
if timestamp is not None:
previous = timestamp
try:
self._on_udp(payload)
except Exception:
logger.exception("error replaying payload")
self.packet_count += 1
except OSError:
logger.exception("unable to read replay log: %s", self._path)
return
logger.info("replay finished: %d packets", self.packet_count)
+80
View File
@@ -0,0 +1,80 @@
import sys
from collections import Counter
from typing import List, Optional
from .consts import events as consts
from .decode.pipeline import Pipeline
from .net.replay import load_payloads
from .environment import replay_path, setup_logging
def _fmt(value: float) -> str:
return f'{value:.2f}'
def _event_value(event: dict) -> dict:
return event.get(consts.EvKeyValue) or {}
def summarize(events: List[dict], path: Optional[str] = None,
packet_count: Optional[int] = None) -> str:
"""Build a human readable summary of the events produced by a replay."""
counts = Counter(event[consts.EvKeyName] for event in events)
fame = sum(_event_value(e).get('value', 0.0)
for e in events if e[consts.EvKeyName] == consts.EvNameUpdateFame)
silver = sum(_event_value(e).get('value', 0.0)
for e in events
if e[consts.EvKeyName] == consts.EvNameSilverGained)
damage = sum(_event_value(e).get('value', 0.0)
for e in events
if e[consts.EvKeyName] == consts.EvNameDamageDone)
main_player = None
for event in events:
if event[consts.EvKeyName] == consts.EvNameMainPlayerAppeared:
main_player = _event_value(event).get('name')
break
lines = ['Replay summary']
if path:
lines.append(f' log: {path}')
if packet_count is not None:
lines.append(f' packets: {packet_count}')
lines.append(f' events: {len(events)}')
lines.append(f' main player: {main_player or "(unknown)"}')
lines.append(f' zone changes: {counts.get(consts.EvNameZoneChange, 0)}')
lines.append(f' fame gained: {_fmt(fame)}')
lines.append(f' silver looted: {_fmt(silver)}')
lines.append(f' damage done: {_fmt(damage)}')
top = counts.most_common(8)
if top:
lines.append(' top events:')
for name, count in top:
lines.append(f' {name}: {count}')
return '\n'.join(lines)
def run_headless() -> int:
"""Replay the configured log through the pipeline and print a summary."""
setup_logging()
path = replay_path()
if not path:
print('--replay requires a log path', file=sys.stderr)
return 2
try:
packets = load_payloads(path)
except OSError as error:
print(f'unable to read replay log {path}: {error}', file=sys.stderr)
return 1
events: List[dict] = []
pipeline = Pipeline(events.append)
for payload in packets:
pipeline.on_udp(payload)
print(summarize(events, path=path, packet_count=len(packets)))
return 0