Base working for damage
This commit is contained in:
+66
-14
@@ -1,6 +1,27 @@
|
||||
import logging
|
||||
|
||||
from .environment import TEST_ENV_ENABLED
|
||||
from . import environment
|
||||
from .utils import platform_utils
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InitializationResult:
|
||||
Ok = 0
|
||||
UnknownFailure = 1
|
||||
NetworkInterfaceListMissing = 2
|
||||
BackendNotInstalled = 3
|
||||
|
||||
|
||||
try:
|
||||
import pyaoaddons # type: ignore
|
||||
except:
|
||||
|
||||
BACKEND_AVAILABLE = True
|
||||
except ImportError:
|
||||
BACKEND_AVAILABLE = False
|
||||
|
||||
class pyaoaddons: # type: ignore
|
||||
@staticmethod
|
||||
def initialize():
|
||||
@@ -10,15 +31,6 @@ except:
|
||||
def subscribe(_):
|
||||
pass
|
||||
|
||||
from .environment import TEST_ENV_ENABLED
|
||||
from . import environment
|
||||
|
||||
|
||||
class InitializationResult:
|
||||
Ok = 0
|
||||
UnknownFailure = 1
|
||||
NetworkInterfaceListMissing = 2
|
||||
|
||||
|
||||
INITIALIZATION_RESULT = {
|
||||
0: InitializationResult.Ok,
|
||||
@@ -26,24 +38,64 @@ INITIALIZATION_RESULT = {
|
||||
2: InitializationResult.NetworkInterfaceListMissing
|
||||
}
|
||||
|
||||
# On Linux we ship a pure-Python Protocol18 decoder and use AF_PACKET directly,
|
||||
# so pyaoaddons is not required.
|
||||
NATIVE_BACKEND_AVAILABLE = platform_utils.is_linux()
|
||||
|
||||
_capture = None
|
||||
|
||||
|
||||
def initialize():
|
||||
if TEST_ENV_ENABLED:
|
||||
return InitializationResult.Ok
|
||||
|
||||
if NATIVE_BACKEND_AVAILABLE:
|
||||
return InitializationResult.Ok
|
||||
|
||||
if not BACKEND_AVAILABLE:
|
||||
return InitializationResult.BackendNotInstalled
|
||||
|
||||
try:
|
||||
result = pyaoaddons.initialize()
|
||||
|
||||
return INITIALIZATION_RESULT[result]
|
||||
except:
|
||||
pass
|
||||
except Exception:
|
||||
return InitializationResult.UnknownFailure
|
||||
|
||||
|
||||
def subscribe(callback):
|
||||
if TEST_ENV_ENABLED:
|
||||
return _testing_call_sequence(callback)
|
||||
_testing_call_sequence(callback)
|
||||
return True
|
||||
|
||||
pyaoaddons.subscribe(callback)
|
||||
if NATIVE_BACKEND_AVAILABLE:
|
||||
return _start_native_backend(callback)
|
||||
|
||||
if BACKEND_AVAILABLE:
|
||||
pyaoaddons.subscribe(callback)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _start_native_backend(callback):
|
||||
global _capture
|
||||
|
||||
if _capture is not None:
|
||||
return True
|
||||
|
||||
from .decode.pipeline import Pipeline
|
||||
from .net.capture import CaptureEngine
|
||||
|
||||
pipeline = Pipeline(callback)
|
||||
capture = CaptureEngine(pipeline.on_udp)
|
||||
if not capture.start():
|
||||
logger.error(
|
||||
"native capture backend could not start; "
|
||||
"check packet capture permissions")
|
||||
return False
|
||||
_capture = capture
|
||||
return True
|
||||
|
||||
|
||||
def _testing_call_sequence(callback):
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
from .photon import PhotonDecoder, PhotonMessage
|
||||
from .world import World
|
||||
@@ -0,0 +1,50 @@
|
||||
import os
|
||||
from typing import Dict, Optional
|
||||
|
||||
_ITEM_DB: Optional[Dict[int, str]] = None
|
||||
_ITEM_DB_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'static', 'item_ids.txt')
|
||||
|
||||
|
||||
def _load() -> Dict[int, str]:
|
||||
db = {}
|
||||
with open(_ITEM_DB_PATH, encoding='utf-8') as f:
|
||||
for line in f:
|
||||
parts = line.strip().split(',', 1)
|
||||
if len(parts) != 2:
|
||||
continue
|
||||
try:
|
||||
_id = int(parts[0])
|
||||
except ValueError:
|
||||
continue
|
||||
name = parts[1].strip()
|
||||
if name:
|
||||
db[_id] = name
|
||||
return db
|
||||
|
||||
|
||||
def get_db() -> Dict[int, str]:
|
||||
global _ITEM_DB
|
||||
if _ITEM_DB is None:
|
||||
_ITEM_DB = _load()
|
||||
return _ITEM_DB
|
||||
|
||||
|
||||
ITEMS_SLOTS = ('weapon', 'offhand', 'helmet', 'armor', 'boots', 'bag', 'cape', 'mount', 'potion', 'food')
|
||||
|
||||
|
||||
def decode_items(item_array) -> Dict[str, Optional[str]]:
|
||||
"""Maps an equipment number array to slot -> item unique name.
|
||||
|
||||
Always returns all slots (aoaddons' ``Items`` struct keeps every field,
|
||||
using ``None`` for empty/missing equipment) so downstream consumers can
|
||||
index ``items['weapon']`` unconditionally.
|
||||
"""
|
||||
db = get_db()
|
||||
items: Dict[str, Optional[str]] = {slot: None for slot in ITEMS_SLOTS}
|
||||
for slot, _id in enumerate(ITEMS_SLOTS):
|
||||
if item_array is None or slot >= len(item_array):
|
||||
break
|
||||
code = item_array[slot]
|
||||
if code and code in db:
|
||||
items[_id] = db[code]
|
||||
return items
|
||||
@@ -0,0 +1,380 @@
|
||||
import struct
|
||||
import logging
|
||||
from collections import namedtuple
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PROTOCOL18_SIGNAL = 0xF3
|
||||
|
||||
# Photon command types inside an eNet envelope
|
||||
CMD_SEND_RELIABLE = 0x06
|
||||
CMD_SEND_UNRELIABLE = 0x07
|
||||
CMD_SEND_FRAGMENT = 0x08
|
||||
CMD_SEND_UNRELIABLE_FRAGMENT = 0x0B
|
||||
CMD_ACKNOWLEDGE = 0x03
|
||||
CMD_PING = 0x05
|
||||
CMD_LOGOUT = 0x04
|
||||
|
||||
# Photon message types
|
||||
MSG_OPERATION_REQUEST = 0x02
|
||||
MSG_OPERATION_RESPONSE = 0x03
|
||||
MSG_EVENT_DATA = 0x04
|
||||
MSG_INTERNAL_REQUEST = 0x06
|
||||
MSG_INTERNAL_RESPONSE = 0x07
|
||||
|
||||
P18 = {
|
||||
'Boolean': 2, 'Byte': 3, 'Short': 4, 'Float': 5, 'Double': 6, 'String': 7,
|
||||
'Null': 8, 'CompressedInt': 9, 'CompressedLong': 10, 'Int1': 11, 'Int1Neg': 12,
|
||||
'Int2': 13, 'Int2Neg': 14, 'Long1': 15, 'Long1Neg': 16, 'Long2': 17, 'Long2Neg': 18,
|
||||
'Custom': 19, 'Dictionary': 20, 'Hashtable': 21, 'ObjectArray': 23,
|
||||
'OperationRequest': 24, 'OperationResponse': 25, 'EventData': 26,
|
||||
'BooleanFalse': 27, 'BooleanTrue': 28, 'ShortZero': 29, 'IntZero': 30,
|
||||
'LongZero': 31, 'FloatZero': 32, 'DoubleZero': 33, 'ByteZero': 34,
|
||||
'Array': 64, 'BooleanArray': 66, 'ByteArray': 67, 'ShortArray': 68,
|
||||
'FloatArray': 69, 'DoubleArray': 70, 'StringArray': 71, 'CompressedIntArray': 73,
|
||||
'CompressedLongArray': 74, 'CustomTypeArray': 83, 'DictionaryArray': 84,
|
||||
'HashtableArray': 85, 'CustomTypeSlim': 128,
|
||||
}
|
||||
|
||||
PhotonMessage = namedtuple(
|
||||
'PhotonMessage', ('kind', 'code', 'params', 'return_code', 'debug_message'))
|
||||
|
||||
GAME_PORT = 5056
|
||||
|
||||
FRAG_HEADER_LEN = 20 # 5 x u32
|
||||
|
||||
|
||||
class DecodeError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _need(buf: bytes, pos: int, n: int, what: str):
|
||||
if pos + n > len(buf):
|
||||
raise DecodeError(f"not enough bytes for {what} at {pos}")
|
||||
|
||||
|
||||
def rd_u8(buf: bytes, pos: int):
|
||||
_need(buf, pos, 1, "u8")
|
||||
return buf[pos], pos + 1
|
||||
|
||||
|
||||
def rd_i16le(buf: bytes, pos: int):
|
||||
_need(buf, pos, 2, "i16le")
|
||||
return struct.unpack("<h", buf[pos:pos + 2])[0], pos + 2
|
||||
|
||||
|
||||
def rd_u16le(buf: bytes, pos: int):
|
||||
_need(buf, pos, 2, "u16le")
|
||||
return struct.unpack("<H", buf[pos:pos + 2])[0], pos + 2
|
||||
|
||||
|
||||
def rd_u16be(buf: bytes, pos: int):
|
||||
_need(buf, pos, 2, "u16be")
|
||||
return struct.unpack(">H", buf[pos:pos + 2])[0], pos + 2
|
||||
|
||||
|
||||
def rd_u32be(buf: bytes, pos: int):
|
||||
_need(buf, pos, 4, "u32be")
|
||||
return struct.unpack(">I", buf[pos:pos + 4])[0], pos + 4
|
||||
|
||||
|
||||
def rd_u32le(buf: bytes, pos: int):
|
||||
_need(buf, pos, 4, "u32le")
|
||||
return struct.unpack("<I", buf[pos:pos + 4])[0], pos + 4
|
||||
|
||||
|
||||
def rd_f32le(buf: bytes, pos: int):
|
||||
_need(buf, pos, 4, "f32le")
|
||||
return struct.unpack("<f", buf[pos:pos + 4])[0], pos + 4
|
||||
|
||||
|
||||
def rd_f64le(buf: bytes, pos: int):
|
||||
_need(buf, pos, 8, "f64le")
|
||||
return struct.unpack("<d", buf[pos:pos + 8])[0], pos + 8
|
||||
|
||||
|
||||
def rd_varint(buf: bytes, pos: int):
|
||||
value = 0
|
||||
for shift in range(0, 64, 7):
|
||||
b, pos = rd_u8(buf, pos)
|
||||
if b & 0x80 == 0:
|
||||
return value | (b << shift), pos
|
||||
value |= (b & 0x7F) << shift
|
||||
raise DecodeError("varint overflow")
|
||||
|
||||
|
||||
def zigzag(v):
|
||||
return (v >> 1) ^ (-(v & 1))
|
||||
|
||||
|
||||
def read_string18(buf: bytes, pos: int):
|
||||
slen, pos = rd_varint(buf, pos)
|
||||
if slen == 0:
|
||||
return "", pos
|
||||
_need(buf, pos, slen, "string")
|
||||
return buf[pos:pos + slen].decode('utf-8', 'replace'), pos + slen
|
||||
|
||||
|
||||
def deserialize18(buf: bytes, pos: int, tc: int):
|
||||
if tc >= P18['CustomTypeSlim']:
|
||||
return ('custom', tc - P18['CustomTypeSlim']), pos
|
||||
if tc == P18['Boolean']:
|
||||
v, p = rd_u8(buf, pos)
|
||||
return bool(v), p
|
||||
if tc in (P18['Byte'], P18['ByteZero']):
|
||||
if tc == P18['ByteZero']:
|
||||
return 0, pos
|
||||
return rd_u8(buf, pos)
|
||||
if tc == P18['Short']:
|
||||
return rd_i16le(buf, pos)
|
||||
if tc == P18['Float']:
|
||||
return rd_f32le(buf, pos)
|
||||
if tc == P18['Double']:
|
||||
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']):
|
||||
return None, pos
|
||||
if tc == P18['CompressedInt']:
|
||||
v, p = rd_varint(buf, pos)
|
||||
return zigzag(v), p
|
||||
if tc == P18['CompressedLong']:
|
||||
v, p = rd_varint(buf, pos)
|
||||
return zigzag(v), p
|
||||
if tc in (P18['Int1'], P18['Long1']):
|
||||
return rd_u8(buf, pos)
|
||||
if tc == P18['Int1Neg'] or tc == P18['Long1Neg']:
|
||||
v, p = rd_u8(buf, pos)
|
||||
return -v, p
|
||||
if tc in (P18['Int2'], P18['Long2']):
|
||||
return rd_u16le(buf, pos)
|
||||
if tc == P18['Int2Neg'] or tc == P18['Long2Neg']:
|
||||
v, p = rd_u16le(buf, pos)
|
||||
return -v, p
|
||||
if tc == P18['ByteArray']:
|
||||
n, p = rd_varint(buf, pos)
|
||||
_need(buf, p, n, "byte array")
|
||||
return bytes(buf[p:p + n]), p + n
|
||||
if tc == P18['ShortArray']:
|
||||
return _read_number_array(buf, pos, lambda b, p: rd_i16le(b, p), "short array")
|
||||
if tc == P18['FloatArray']:
|
||||
return _read_number_array(buf, pos, rd_f32le, "float array")
|
||||
if tc == P18['DoubleArray']:
|
||||
return _read_number_array(buf, pos, rd_f64le, "double array")
|
||||
if tc == P18['StringArray']:
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
s, p = read_string18(buf, p)
|
||||
out.append(s)
|
||||
return out, p
|
||||
if tc == P18['CompressedIntArray']:
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
v, p = rd_varint(buf, p)
|
||||
out.append(zigzag(v))
|
||||
return out, p
|
||||
if tc == P18['CompressedLongArray']:
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
v, p = rd_varint(buf, p)
|
||||
out.append(zigzag(v))
|
||||
return out, p
|
||||
if tc == P18['BooleanArray']:
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
v, p = rd_u8(buf, p)
|
||||
out.append(bool(v))
|
||||
return out, p
|
||||
if tc in (P18['Array'], P18['ObjectArray'], P18['CustomTypeArray']):
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
et, p = rd_u8(buf, p)
|
||||
v, p = deserialize18(buf, p, et)
|
||||
out.append(v)
|
||||
return out, p
|
||||
if tc in (P18['Dictionary'], P18['Hashtable'], P18['DictionaryArray'], P18['HashtableArray']):
|
||||
return _decode_map18(buf, pos, tc)
|
||||
if tc == P18['Custom']:
|
||||
t, p = rd_u8(buf, pos)
|
||||
n, p = rd_varint(buf, p)
|
||||
_need(buf, p, n, "custom")
|
||||
return ('custom', t, bytes(buf[p:p + n])), p + n
|
||||
raise DecodeError(f"unhandled P18 type 0x{tc:02X} at {pos}")
|
||||
|
||||
|
||||
def _read_number_array(buf: bytes, pos: int, reader, what: str):
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = []
|
||||
for _ in range(n):
|
||||
v, p = reader(buf, p)
|
||||
out.append(v)
|
||||
return out, p
|
||||
|
||||
|
||||
def _decode_map18(buf: bytes, pos: int, tc: int):
|
||||
if tc in (P18['Dictionary'], P18['DictionaryArray']):
|
||||
kt, pos0 = rd_u8(buf, pos)
|
||||
vt, p = rd_u8(buf, pos0)
|
||||
n, p = rd_varint(buf, p)
|
||||
out = {}
|
||||
for _ in range(n):
|
||||
if kt == 0:
|
||||
kt2, p = rd_u8(buf, p)
|
||||
k, p = deserialize18(buf, p, kt2)
|
||||
else:
|
||||
k, p = deserialize18(buf, p, kt)
|
||||
if vt == 0:
|
||||
vt2, p = rd_u8(buf, p)
|
||||
v, p = deserialize18(buf, p, vt2)
|
||||
else:
|
||||
v, p = deserialize18(buf, p, vt)
|
||||
out[k] = v
|
||||
return out, p
|
||||
n, p = rd_varint(buf, pos)
|
||||
out = {}
|
||||
for _ in range(n):
|
||||
kt, p = rd_u8(buf, p)
|
||||
k, p = deserialize18(buf, p, kt)
|
||||
vt, p = rd_u8(buf, p)
|
||||
v, p = deserialize18(buf, p, vt)
|
||||
out[k] = v
|
||||
return out, p
|
||||
|
||||
|
||||
def decode_params18(buf: bytes, pos: int):
|
||||
size, pos = rd_u8(buf, pos)
|
||||
out = {}
|
||||
for _ in range(size):
|
||||
key, pos = rd_u8(buf, pos)
|
||||
tc, pos = rd_u8(buf, pos)
|
||||
val, pos = deserialize18(buf, pos, tc)
|
||||
out[key] = val
|
||||
return out, pos
|
||||
|
||||
|
||||
def decode_message18(buf: bytes, pos: int):
|
||||
_, pos = rd_u8(buf, pos) # signal byte 0xF3
|
||||
msg_type, pos = rd_u8(buf, pos)
|
||||
msg_type &= 0x7F
|
||||
if msg_type in (MSG_OPERATION_REQUEST, MSG_INTERNAL_REQUEST):
|
||||
code, pos = rd_u8(buf, pos)
|
||||
params, pos = decode_params18(buf, pos)
|
||||
return PhotonMessage('Request', code, params, None, None), pos
|
||||
if msg_type in (MSG_OPERATION_RESPONSE, MSG_INTERNAL_RESPONSE):
|
||||
code, pos = rd_u8(buf, pos)
|
||||
ret, pos = rd_i16le(buf, pos)
|
||||
dv_type, pos = rd_u8(buf, pos)
|
||||
dv, pos = deserialize18(buf, pos, dv_type)
|
||||
params, pos = decode_params18(buf, pos)
|
||||
return PhotonMessage('Response', code, params, ret, dv), pos
|
||||
if msg_type == MSG_EVENT_DATA:
|
||||
code, pos = rd_u8(buf, pos)
|
||||
params, pos = decode_params18(buf, pos)
|
||||
return PhotonMessage('Event', code, params, None, None), pos
|
||||
raise DecodeError(f"unknown message type 0x{msg_type:02X}")
|
||||
|
||||
|
||||
class FragmentAssembler:
|
||||
"""Reassembles eNet SendFragment sequences into whole Photon messages."""
|
||||
|
||||
def __init__(self):
|
||||
self._fragments: Dict[tuple, dict] = {}
|
||||
|
||||
def add(self, peer: int, start_seq: int, fragment_count: int,
|
||||
fragment_number: int, total_length: int, data: bytes):
|
||||
key = (peer, start_seq)
|
||||
group = self._fragments.setdefault(
|
||||
key, {'count': fragment_count, 'parts': {}, 'total': total_length})
|
||||
if group['count'] != fragment_count:
|
||||
self._fragments.pop(key, None)
|
||||
return None
|
||||
group['parts'][fragment_number] = data
|
||||
if len(group['parts']) < group['count']:
|
||||
return None
|
||||
payload = b''.join(group['parts'][i] for i in range(group['count']))
|
||||
self._fragments.pop(key, None)
|
||||
return payload
|
||||
|
||||
|
||||
class PhotonDecoder:
|
||||
def __init__(self):
|
||||
self.assembler = FragmentAssembler()
|
||||
|
||||
def decode_packet(self, payload: bytes, peer: int = 0) -> List[PhotonMessage]:
|
||||
messages: List[PhotonMessage] = []
|
||||
try:
|
||||
return self._decode_packet(payload, peer, messages)
|
||||
except DecodeError as e:
|
||||
logger.debug("dropping undecodable UDP payload: %s", e)
|
||||
return messages
|
||||
|
||||
def _decode_packet(self, payload: bytes, peer: int,
|
||||
messages: List[PhotonMessage]) -> List[PhotonMessage]:
|
||||
pos = 0
|
||||
_peer, pos = rd_u16be(payload, pos)
|
||||
crc, pos = rd_u8(payload, pos)
|
||||
count, pos = rd_u8(payload, pos)
|
||||
_ts, pos = rd_u32be(payload, pos)
|
||||
_ch, pos = rd_u32be(payload, pos)
|
||||
for _ in range(count):
|
||||
cmd_type, pos = rd_u8(payload, pos)
|
||||
_channel, pos = rd_u8(payload, pos)
|
||||
_flags, pos = rd_u8(payload, pos)
|
||||
_reserved, pos = rd_u8(payload, pos)
|
||||
length, pos = rd_u32be(payload, pos)
|
||||
_seq, pos = rd_u32be(payload, pos)
|
||||
data_len = length - 12
|
||||
if cmd_type == CMD_SEND_FRAGMENT:
|
||||
start_seq, pos = rd_u32be(payload, pos)
|
||||
fragment_count, pos = rd_u32be(payload, pos)
|
||||
fragment_number, pos = rd_u32be(payload, pos)
|
||||
total_length, pos = rd_u32be(payload, pos)
|
||||
_offset, pos = rd_u32be(payload, pos)
|
||||
data = payload[pos:pos + data_len - FRAG_HEADER_LEN]
|
||||
pos += data_len - FRAG_HEADER_LEN
|
||||
reassembled = self.assembler.add(
|
||||
peer, start_seq, fragment_count, fragment_number, total_length, data)
|
||||
if reassembled is not None:
|
||||
messages.extend(self._decode_message(reassembled, peer))
|
||||
continue
|
||||
if cmd_type in (CMD_ACKNOWLEDGE, CMD_PING):
|
||||
pos += data_len
|
||||
continue
|
||||
if cmd_type == CMD_LOGOUT:
|
||||
continue
|
||||
if cmd_type in (CMD_SEND_RELIABLE, CMD_SEND_UNRELIABLE,
|
||||
CMD_SEND_UNRELIABLE_FRAGMENT):
|
||||
if cmd_type in (CMD_SEND_UNRELIABLE,
|
||||
CMD_SEND_UNRELIABLE_FRAGMENT):
|
||||
pos += 4 # unknown/unreliable sequence trailers
|
||||
data_len -= 4
|
||||
msg_bytes = payload[pos:pos + data_len]
|
||||
pos += data_len
|
||||
messages.extend(self._decode_message(msg_bytes, peer))
|
||||
continue
|
||||
pos += data_len
|
||||
return messages
|
||||
|
||||
def _decode_message(self, msg_bytes: bytes, peer: int) -> List[PhotonMessage]:
|
||||
if not msg_bytes:
|
||||
return []
|
||||
signal = msg_bytes[0]
|
||||
if signal != PROTOCOL18_SIGNAL:
|
||||
logger.debug("dropping message with unknown signal 0x%02X", signal)
|
||||
return []
|
||||
try:
|
||||
msg, _ = decode_message18(msg_bytes, 0)
|
||||
except DecodeError as e:
|
||||
logger.debug("dropping undecodable Photon message: %s", e)
|
||||
return []
|
||||
logger.debug("decoded Photon message: %s", msg)
|
||||
return [msg]
|
||||
@@ -0,0 +1,40 @@
|
||||
import threading
|
||||
import logging
|
||||
from typing import Callable, List
|
||||
|
||||
from .photon import PhotonDecoder, PhotonMessage
|
||||
from .translate import into_game_message
|
||||
from .world import World
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Pipeline:
|
||||
"""Decodes captured UDP payloads into app events (aoaddons lib.rs flow)."""
|
||||
|
||||
def __init__(self, emit: Callable[[dict], None]):
|
||||
self.emit = emit
|
||||
self.decoder = PhotonDecoder()
|
||||
self.world = World()
|
||||
self._lock = threading.RLock()
|
||||
|
||||
def on_udp(self, payload: bytes):
|
||||
logger.debug("received UDP payload: %d bytes", len(payload))
|
||||
messages = self.decoder.decode_packet(payload)
|
||||
if not messages:
|
||||
return
|
||||
with self._lock:
|
||||
for message in messages:
|
||||
for event in self._events_from_message(message):
|
||||
self.emit(event)
|
||||
|
||||
def _events_from_message(self, message: PhotonMessage) -> List[dict]:
|
||||
game_message = into_game_message(message)
|
||||
if game_message is None:
|
||||
return []
|
||||
logger.debug("translated game message: %s", game_message)
|
||||
events = self.world.transform(game_message)
|
||||
for event in events:
|
||||
logger.debug("emitting app event: %s", event)
|
||||
return events
|
||||
@@ -0,0 +1,198 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional, List
|
||||
|
||||
from .photon import PhotonMessage
|
||||
|
||||
# Photon message code used for game traffic (aoaddons pipeline guard)
|
||||
PHOTON_GAME_CODE = 1
|
||||
|
||||
# Current opcodes (albion-lens numbering, verified against captured traffic)
|
||||
EVENT_LEAVE = 1
|
||||
EVENT_HEALTH_UPDATE = 6
|
||||
EVENT_NEW_CHARACTER = 29
|
||||
EVENT_UPDATE_FAME = 82
|
||||
EVENT_CHARACTER_EQUIPMENT_CHANGED = 90
|
||||
EVENT_REGENERATION_HEALTH_CHANGED = 91
|
||||
EVENT_KNOCKED_DOWN = 166
|
||||
EVENT_PARTY_INVITATION = 229
|
||||
EVENT_PARTY_JOIN_REQUEST = 230
|
||||
EVENT_PARTY_JOINED = 231
|
||||
EVENT_PARTY_DISBANDED = 232
|
||||
EVENT_PARTY_PLAYER_JOINED = 233
|
||||
EVENT_PARTY_CHANGED_ORDER = 234
|
||||
EVENT_PARTY_PLAYER_LEFT = 235
|
||||
|
||||
# Operation codes (response parameter 253)
|
||||
OPERATION_JOIN = 2
|
||||
OPERATION_CHANGE_CLUSTER = 41
|
||||
|
||||
|
||||
def _int(value) -> Optional[int]:
|
||||
if isinstance(value, bool):
|
||||
return int(value)
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value)
|
||||
return None
|
||||
|
||||
|
||||
def _float(value) -> Optional[float]:
|
||||
if isinstance(value, bool):
|
||||
return float(value)
|
||||
if isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
return None
|
||||
|
||||
|
||||
def _string(value) -> Optional[str]:
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
def _as_int_list(value) -> List[int]:
|
||||
if isinstance(value, bytes):
|
||||
return list(value)
|
||||
if isinstance(value, list):
|
||||
out = []
|
||||
for v in value:
|
||||
i = _int(v)
|
||||
if i is not None:
|
||||
out.append(i)
|
||||
return out
|
||||
return []
|
||||
|
||||
|
||||
@dataclass
|
||||
class GameMessage:
|
||||
name: str
|
||||
code: int
|
||||
params: dict = field(default_factory=dict)
|
||||
|
||||
def param(self, key: int):
|
||||
return self.params.get(key)
|
||||
|
||||
|
||||
def into_game_message(message: PhotonMessage) -> Optional[GameMessage]:
|
||||
if message.code != PHOTON_GAME_CODE:
|
||||
return None
|
||||
if message.kind == 'Event':
|
||||
code = _int(message.params.get(252))
|
||||
if code is None:
|
||||
return None
|
||||
name = EVENT_NAMES.get(code)
|
||||
if name is None:
|
||||
return None
|
||||
return GameMessage(name, code, message.params)
|
||||
if message.kind in ('Request', 'Response'):
|
||||
code = _int(message.params.get(253))
|
||||
if code is None:
|
||||
return None
|
||||
name = OPERATION_NAMES.get(code)
|
||||
if name is None:
|
||||
return None
|
||||
return GameMessage(name, code, message.params)
|
||||
return None
|
||||
|
||||
|
||||
EVENT_NAMES = {
|
||||
EVENT_LEAVE: 'Leave',
|
||||
EVENT_HEALTH_UPDATE: 'HealthUpdate',
|
||||
EVENT_NEW_CHARACTER: 'NewCharacter',
|
||||
EVENT_UPDATE_FAME: 'UpdateFame',
|
||||
EVENT_CHARACTER_EQUIPMENT_CHANGED: 'CharacterEquipmentChanged',
|
||||
EVENT_REGENERATION_HEALTH_CHANGED: 'RegenerationHealthChanged',
|
||||
EVENT_KNOCKED_DOWN: 'KnockedDown',
|
||||
EVENT_PARTY_INVITATION: 'PartyInvitation',
|
||||
EVENT_PARTY_JOIN_REQUEST: 'PartyJoinRequest',
|
||||
EVENT_PARTY_JOINED: 'PartyJoined',
|
||||
EVENT_PARTY_DISBANDED: 'PartyDisbanded',
|
||||
EVENT_PARTY_PLAYER_JOINED: 'PartyPlayerJoined',
|
||||
EVENT_PARTY_CHANGED_ORDER: 'PartyChangedOrder',
|
||||
EVENT_PARTY_PLAYER_LEFT: 'PartyPlayerLeft',
|
||||
}
|
||||
|
||||
OPERATION_NAMES = {
|
||||
OPERATION_JOIN: 'Join',
|
||||
OPERATION_CHANGE_CLUSTER: 'ChangeCluster',
|
||||
}
|
||||
|
||||
|
||||
# Parameter accessors used by the world state machine. Field names follow
|
||||
# aoaddons' messages.json layouts, with the current opcode space from albion-lens.
|
||||
def msg_source(msg: GameMessage) -> int:
|
||||
v = _int(msg.params.get(0))
|
||||
return v if v is not None else 0
|
||||
|
||||
|
||||
def msg_character_name(msg: GameMessage) -> str:
|
||||
v = _string(msg.params.get(2))
|
||||
return v if v is not None else ''
|
||||
|
||||
|
||||
def msg_new_character_name(msg: GameMessage) -> str:
|
||||
v = _string(msg.params.get(1))
|
||||
return v if v is not None else ''
|
||||
|
||||
|
||||
def msg_map_index(msg: GameMessage) -> Optional[str]:
|
||||
return _string(msg.params.get(8))
|
||||
|
||||
|
||||
def msg_target(msg: GameMessage) -> int:
|
||||
v = _int(msg.params.get(6))
|
||||
return v if v is not None else 0
|
||||
|
||||
|
||||
def msg_value(msg: GameMessage) -> float:
|
||||
v = _float(msg.params.get(2))
|
||||
return v if v is not None else 0.0
|
||||
|
||||
|
||||
def msg_items(msg: GameMessage) -> List[int]:
|
||||
return _as_int_list(msg.params.get(2))
|
||||
|
||||
|
||||
def msg_new_character_items(msg: GameMessage) -> List[int]:
|
||||
return _as_int_list(msg.params.get(33))
|
||||
|
||||
|
||||
def msg_regeneration_rate(msg: GameMessage) -> Optional[float]:
|
||||
if 4 not in msg.params:
|
||||
return None
|
||||
return _float(msg.params.get(4))
|
||||
|
||||
|
||||
def msg_total_fame(msg: GameMessage) -> int:
|
||||
v = _int(msg.params.get(1))
|
||||
return v if v is not None else 0
|
||||
|
||||
|
||||
def msg_fame_gained(msg: GameMessage) -> Optional[int]:
|
||||
if 2 not in msg.params:
|
||||
return None
|
||||
return _int(msg.params.get(2))
|
||||
|
||||
|
||||
def msg_party_structures(msg: GameMessage) -> List[List[int]]:
|
||||
out = []
|
||||
for item in msg.params.get(4, []) or []:
|
||||
out.append(_as_int_list(item))
|
||||
return out
|
||||
|
||||
|
||||
def msg_party_names(msg: GameMessage) -> List[str]:
|
||||
out = []
|
||||
for item in msg.params.get(5, []) or []:
|
||||
s = _string(item)
|
||||
if s is not None:
|
||||
out.append(s)
|
||||
return out
|
||||
|
||||
|
||||
def msg_party_structure(msg: GameMessage) -> List[int]:
|
||||
return _as_int_list(msg.params.get(1))
|
||||
|
||||
|
||||
def msg_party_name(msg: GameMessage) -> str:
|
||||
v = _string(msg.params.get(2))
|
||||
return v if v is not None else ''
|
||||
@@ -0,0 +1,302 @@
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from . import items as itemdb
|
||||
from . import translate
|
||||
from .translate import GameMessage
|
||||
|
||||
FIX_POINT = 10000.0
|
||||
FAME_MIN_TOTAL = 1000000 # albion-lens sanity filter for UpdateFame events
|
||||
|
||||
|
||||
class IdCache:
|
||||
"""Maps dynamic (per-session) ids to stable ids and names.
|
||||
|
||||
Mirrors aoaddons src/game/id_cache.rs: ids are assigned sequentially by
|
||||
player name, so reconnecting to a zone keeps the same id for a player.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.dyn_to_static: Dict[int, int] = {}
|
||||
self.static_to_name: Dict[int, str] = {}
|
||||
self.static_to_dyn: Dict[int, int] = {}
|
||||
self.last_id = 0
|
||||
|
||||
def save(self, dynamic_id: int, name: str):
|
||||
if not name:
|
||||
return
|
||||
static_id = self._find_static_id(name)
|
||||
if static_id is not None:
|
||||
old_dyn = self.static_to_dyn.get(static_id)
|
||||
if old_dyn is not None:
|
||||
self.dyn_to_static.pop(old_dyn, None)
|
||||
self.dyn_to_static[dynamic_id] = static_id
|
||||
self.static_to_dyn[static_id] = dynamic_id
|
||||
return
|
||||
static_id = self.last_id
|
||||
self.last_id += 1
|
||||
self.dyn_to_static[dynamic_id] = static_id
|
||||
self.static_to_dyn[static_id] = dynamic_id
|
||||
self.static_to_name[static_id] = name
|
||||
|
||||
def _find_static_id(self, name: str) -> Optional[int]:
|
||||
for sid, sid_name in self.static_to_name.items():
|
||||
if sid_name == name:
|
||||
return sid
|
||||
return None
|
||||
|
||||
def get_static_id(self, dynamic_id: int) -> Optional[int]:
|
||||
return self.dyn_to_static.get(dynamic_id)
|
||||
|
||||
def get_name(self, static_id: int) -> Optional[str]:
|
||||
return self.static_to_name.get(static_id)
|
||||
|
||||
|
||||
class Party:
|
||||
"""Tracks party membership from party events (aoaddons src/game/party.rs)."""
|
||||
|
||||
def __init__(self):
|
||||
self.main_player_name: Optional[str] = None
|
||||
self.items: List[dict] = [] # {'name', 'id'}
|
||||
|
||||
def set_main_player_name(self, name: str):
|
||||
self.main_player_name = name
|
||||
|
||||
def _game_event(self) -> dict:
|
||||
return {'name': 'UpdateParty', 'value': {
|
||||
'player_names': [item['name'] for item in self.items]}}
|
||||
|
||||
def joined(self, msg: GameMessage) -> Optional[dict]:
|
||||
structures = translate.msg_party_structures(msg)
|
||||
names = translate.msg_party_names(msg)
|
||||
self.items = []
|
||||
for structure, name in zip(structures, names):
|
||||
self.items.append({'name': name, 'id': _party_id(structure)})
|
||||
return self._game_event()
|
||||
|
||||
def disbanded(self) -> Optional[dict]:
|
||||
self.items = []
|
||||
return self._game_event()
|
||||
|
||||
def player_left(self, msg: GameMessage) -> Optional[dict]:
|
||||
if self.main_player_name is None:
|
||||
return None
|
||||
structure_id = _party_id(translate.msg_party_structure(msg))
|
||||
party_item = next((i for i in self.items if i['id'] == structure_id), None)
|
||||
if party_item is None:
|
||||
return None
|
||||
if party_item['name'] == self.main_player_name:
|
||||
return self.disbanded()
|
||||
self.items = [i for i in self.items if i['id'] != structure_id]
|
||||
return self._game_event()
|
||||
|
||||
def single_player_joined(self, msg: GameMessage) -> Optional[dict]:
|
||||
self.items.append({
|
||||
'name': translate.msg_party_name(msg),
|
||||
'id': _party_id(translate.msg_party_structure(msg)),
|
||||
})
|
||||
return self._game_event()
|
||||
|
||||
|
||||
class UnconsumedMessages:
|
||||
"""Buffers messages tagged with a dynamic id whose static id is unknown.
|
||||
|
||||
Replayed once the id is resolved (aoaddons src/game/unconsumed_messages.rs).
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._messages: Dict[int, List[GameMessage]] = {}
|
||||
|
||||
def add(self, dynamic_id: int, msg: GameMessage):
|
||||
self._messages.setdefault(dynamic_id, []).append(msg)
|
||||
|
||||
def get_for_id(self, dynamic_id: int) -> List[GameMessage]:
|
||||
return self._messages.get(dynamic_id, [])
|
||||
|
||||
|
||||
class World:
|
||||
"""Transforms translated game messages into app events (aoaddons world.rs)."""
|
||||
|
||||
def __init__(self):
|
||||
self.cache = IdCache()
|
||||
self.party = Party()
|
||||
self.unconsumed_messages = UnconsumedMessages()
|
||||
self.main_player_id: Optional[int] = None
|
||||
self._fame_totals: Dict[int, int] = {}
|
||||
|
||||
def transform(self, message: GameMessage) -> List[dict]:
|
||||
if message.name == 'NewCharacter':
|
||||
return self._on_new_character(message)
|
||||
if message.name == 'Join':
|
||||
return self._on_join(message)
|
||||
if message.name == 'ChangeCluster':
|
||||
return self._on_change_cluster(message)
|
||||
if message.name == 'Leave':
|
||||
return self._on_leave(message)
|
||||
if message.name == 'HealthUpdate':
|
||||
return self._on_health_update(message)
|
||||
if message.name == 'RegenerationHealthChanged':
|
||||
return self._on_regeneration(message)
|
||||
if message.name == 'KnockedDown':
|
||||
return self._on_knocked_down(message)
|
||||
if message.name == 'UpdateFame':
|
||||
return self._on_update_fame(message)
|
||||
if message.name == 'CharacterEquipmentChanged':
|
||||
return self._on_character_equipment_changed(message)
|
||||
if message.name == 'PartyJoined':
|
||||
return self._party_event(self.party.joined(message))
|
||||
if message.name == 'PartyDisbanded':
|
||||
return self._party_event(self.party.disbanded())
|
||||
if message.name == 'PartyPlayerJoined':
|
||||
return self._party_event(self.party.single_player_joined(message))
|
||||
if message.name == 'PartyPlayerLeft':
|
||||
return self._party_event(self.party.player_left(message))
|
||||
return []
|
||||
|
||||
def _party_event(self, event: Optional[dict]) -> List[dict]:
|
||||
return [event] if event is not None else []
|
||||
|
||||
def _on_new_character(self, msg: GameMessage) -> List[dict]:
|
||||
name = translate.msg_new_character_name(msg)
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
if not name:
|
||||
return []
|
||||
self.cache.save(dynamic_id, name)
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
result = [{
|
||||
'name': 'PlayerAppeared',
|
||||
'value': {'id': static_id, 'name': name},
|
||||
}]
|
||||
items = itemdb.decode_items(translate.msg_new_character_items(msg))
|
||||
if items:
|
||||
result.append({
|
||||
'name': 'UpdateItems',
|
||||
'value': {'source': static_id, 'value': items},
|
||||
})
|
||||
result.extend(self._replay(dynamic_id))
|
||||
if self.main_player_id is None:
|
||||
self.main_player_id = static_id
|
||||
return result
|
||||
|
||||
def _on_join(self, msg: GameMessage) -> List[dict]:
|
||||
name = translate.msg_character_name(msg)
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
if not name:
|
||||
return []
|
||||
self.cache.save(dynamic_id, name)
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
self.party.set_main_player_name(name)
|
||||
result = []
|
||||
if self.main_player_id is None:
|
||||
result.append({'name': 'ZoneChange'})
|
||||
self.main_player_id = static_id
|
||||
result.append({
|
||||
'name': 'MainPlayerAppeared',
|
||||
'value': {'id': static_id, 'name': name},
|
||||
})
|
||||
result.extend(self._replay(dynamic_id))
|
||||
return result
|
||||
|
||||
def _on_change_cluster(self, msg: GameMessage) -> List[dict]:
|
||||
if self.main_player_id is None:
|
||||
return []
|
||||
return [{'name': 'ZoneChange'}]
|
||||
|
||||
def _on_leave(self, msg: GameMessage) -> List[dict]:
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
if self.main_player_id == static_id:
|
||||
self.main_player_id = None
|
||||
return [{'name': 'ZoneChange'}]
|
||||
return []
|
||||
|
||||
def _on_health_update(self, msg: GameMessage) -> List[dict]:
|
||||
# params: [0]=target (entity whose health changed), [2]=signed delta,
|
||||
# [6]=source (attacker/healer). aoaddons emits Damage{source: params[6],
|
||||
# target: params[0]} which the app attributes to the dealer/healer.
|
||||
value = translate.msg_value(msg)
|
||||
if value == 0.0:
|
||||
return []
|
||||
actor_id = _dyn(translate.msg_target(msg)) # param 6
|
||||
victim_id = _dyn(translate.msg_source(msg)) # param 0
|
||||
actor_static = self.cache.get_static_id(actor_id)
|
||||
if actor_static is None:
|
||||
return []
|
||||
event_name = 'DamageDone' if value < 0.0 else 'HealthReceived'
|
||||
return [{
|
||||
'name': event_name,
|
||||
'value': {'source': actor_static,
|
||||
'target': self.cache.get_static_id(victim_id),
|
||||
'value': float(abs(value))},
|
||||
}]
|
||||
|
||||
def _on_regeneration(self, msg: GameMessage) -> List[dict]:
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
name = self.cache.get_name(static_id) or ''
|
||||
if not name:
|
||||
return []
|
||||
event_name = ('LeaveCombat' if translate.msg_regeneration_rate(msg) is not None
|
||||
else 'EnterCombat')
|
||||
return [{'name': event_name, 'value': {'id': static_id, 'name': name}}]
|
||||
|
||||
def _on_knocked_down(self, msg: GameMessage) -> List[dict]:
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
name = self.cache.get_name(static_id) or ''
|
||||
if not name:
|
||||
return []
|
||||
return [{'name': 'LeaveCombat', 'value': {'id': static_id, 'name': name}}]
|
||||
|
||||
def _on_update_fame(self, msg: GameMessage) -> List[dict]:
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
return []
|
||||
total = translate.msg_total_fame(msg)
|
||||
if total < FAME_MIN_TOTAL:
|
||||
return []
|
||||
prev = self._fame_totals.get(static_id)
|
||||
if prev == total:
|
||||
return []
|
||||
gained = translate.msg_fame_gained(msg)
|
||||
self._fame_totals[static_id] = total
|
||||
if gained is None:
|
||||
if prev is None or total < prev:
|
||||
return []
|
||||
gained = total - prev
|
||||
if gained <= 0:
|
||||
return []
|
||||
return [{'name': 'UpdateFame', 'value': {'value': float(gained / FIX_POINT)}}]
|
||||
|
||||
def _on_character_equipment_changed(self, msg: GameMessage) -> List[dict]:
|
||||
dynamic_id = _dyn(translate.msg_source(msg))
|
||||
static_id = self.cache.get_static_id(dynamic_id)
|
||||
if static_id is None:
|
||||
self.unconsumed_messages.add(dynamic_id, msg)
|
||||
return []
|
||||
items = itemdb.decode_items(translate.msg_items(msg))
|
||||
return [{'name': 'UpdateItems', 'value': {'source': static_id, 'value': items}}]
|
||||
|
||||
def _replay(self, dynamic_id: int) -> List[dict]:
|
||||
result: List[dict] = []
|
||||
for msg in self.unconsumed_messages.get_for_id(dynamic_id):
|
||||
result.extend(self.transform(msg))
|
||||
return result
|
||||
|
||||
|
||||
def _dyn(value) -> int:
|
||||
return int(value) & 0xFFFFFFFF
|
||||
|
||||
|
||||
def _party_id(party_structure: List[int]) -> int:
|
||||
return sum(party_structure)
|
||||
+9
-1
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
from typing import Iterable
|
||||
from dataclasses import dataclass
|
||||
from queue import Queue
|
||||
@@ -14,6 +15,9 @@ from .event_receiver import VisibilityEventReceiver
|
||||
from .stats.visibility import Visibility
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class VisibilityType:
|
||||
LastFight: str = 'last_fight'
|
||||
@@ -187,6 +191,7 @@ def get_stats(stat_type: str, combat_stat_type: str):
|
||||
|
||||
def queue_an_event(event):
|
||||
event_queue.put(event)
|
||||
logger.debug("event received: %s", event.get(ev_consts.EvKeyName))
|
||||
|
||||
|
||||
def reset_zone_stats():
|
||||
@@ -207,5 +212,8 @@ def is_ready():
|
||||
|
||||
def initialize():
|
||||
initialization_result = backend_proxy.initialize()
|
||||
backend_proxy.subscribe(queue_an_event)
|
||||
if not backend_proxy.subscribe(queue_an_event):
|
||||
logger.error("packet capture backend subscription failed")
|
||||
if initialization_result == InitializationResult.Ok:
|
||||
return InitializationResult.UnknownFailure
|
||||
return initialization_result
|
||||
|
||||
@@ -1,7 +1,37 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
TEST_ENV_ENABLED = bool(os.getenv('TESTING'))
|
||||
|
||||
# Verbose debugging flag, e.g. ``aostats --verbose``.
|
||||
VERBOSE = bool(
|
||||
os.getenv('AOSTATS_VERBOSE') or
|
||||
'--verbose' in sys.argv or
|
||||
'-v' in sys.argv
|
||||
)
|
||||
|
||||
|
||||
def is_verbose():
|
||||
return VERBOSE
|
||||
|
||||
|
||||
def setup_logging():
|
||||
"""Enable verbose logging for the app and the packet capture backend.
|
||||
|
||||
The Rust backend reads the ``BACKEND_LOG_LEVEL`` environment variable when
|
||||
it initializes, so it must be set before ``initialize()`` is called.
|
||||
"""
|
||||
if not VERBOSE:
|
||||
return
|
||||
|
||||
os.environ['BACKEND_LOG_LEVEL'] = 'debug'
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format='%(asctime)s %(levelname)-8s %(name)s: %(message)s',
|
||||
datefmt='%H:%M:%S')
|
||||
|
||||
|
||||
class TestType:
|
||||
Simple = 'simple'
|
||||
|
||||
+82
-15
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import requests
|
||||
|
||||
@@ -13,6 +14,7 @@ from PySide6.QtWidgets import QPushButton # type: ignore
|
||||
from ..engine import InitializationResult
|
||||
from ..engine import initialize
|
||||
from ..utils.config import config
|
||||
from ..environment import setup_logging
|
||||
|
||||
from .modules.main import MainWidget
|
||||
from .styling import style
|
||||
@@ -20,17 +22,40 @@ from ..utils.version import current_version as get_current_version
|
||||
from ..utils.version import latest_version as get_latest_version
|
||||
from ..utils.assets import path
|
||||
from ..utils.assets import scripts
|
||||
from ..utils import platform_utils
|
||||
from ..utils.platform_utils import capture_permissions_ok
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.abspath('__file__')))
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def fix_network():
|
||||
script_name = platform_utils.repair_script()
|
||||
if not script_name:
|
||||
sys.exit(0)
|
||||
|
||||
script_path = scripts(script_name)
|
||||
|
||||
if platform_utils.is_windows():
|
||||
os.system(script_path)
|
||||
else:
|
||||
subprocess.call(['sh', script_path, sys.executable])
|
||||
|
||||
def fix_npcap():
|
||||
os.system(scripts('fixnpcap.bat'))
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def run():
|
||||
setup_logging()
|
||||
|
||||
app_argv = [a for a in sys.argv if a not in ('--verbose', '-v')]
|
||||
|
||||
logger.info("Albion Online Stats v%s starting", get_current_version())
|
||||
logger.debug("python: %s (%s)", sys.executable, sys.version)
|
||||
logger.debug("platform: %s", platform_utils.system())
|
||||
|
||||
initialization_result = initialize()
|
||||
logger.debug("backend initialization result: %s", initialization_result)
|
||||
|
||||
conf = config()
|
||||
window_config = conf['window']
|
||||
@@ -38,7 +63,12 @@ def run():
|
||||
WIDTH = window_config['width']
|
||||
HEIGHT = window_config['height']
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
perms_ok = True
|
||||
if platform_utils.is_linux():
|
||||
perms_ok = capture_permissions_ok()
|
||||
logger.debug("capture permissions probe: %s", perms_ok)
|
||||
|
||||
app = QApplication(app_argv)
|
||||
|
||||
app.setStyleSheet(style)
|
||||
|
||||
@@ -73,21 +103,58 @@ def run():
|
||||
msg.setStandardButtons(QMessageBox.Ok)
|
||||
msg.show()
|
||||
|
||||
if initialization_result == InitializationResult.NetworkInterfaceListMissing:
|
||||
need_capture_fix = (
|
||||
initialization_result != InitializationResult.Ok
|
||||
or (platform_utils.is_linux() and not perms_ok)
|
||||
)
|
||||
|
||||
if need_capture_fix:
|
||||
msg = QMessageBox()
|
||||
msg.setIcon(QMessageBox.Critical)
|
||||
msg.setWindowTitle("Unable to track network traffic data!")
|
||||
msg.setText(
|
||||
"On windows make sure that WinPcap is installed in your system.")
|
||||
msg.setInformativeText("WinPcap can be installed from <a href='{}'>here</a> <br>\
|
||||
<b>Make sure to install with the \"Install Npcap in WinPcap API-compatible Mode\"<b> option<br><br>\
|
||||
In case where npcap is installed try to fix npcap and restart the app"
|
||||
.format('https://nmap.org/npcap/dist/npcap-0.9990.exe'))
|
||||
msg.setStandardButtons(QMessageBox.Ok)
|
||||
button = QPushButton("Fix npcap")
|
||||
|
||||
button.clicked.connect(fix_npcap)
|
||||
msg.addButton(button, QMessageBox.NoRole)
|
||||
if initialization_result == InitializationResult.BackendNotInstalled:
|
||||
msg.setText(
|
||||
"The packet capture backend (pyaoaddons) is not installed.")
|
||||
msg.setInformativeText(
|
||||
"pyaoaddons 0.2.8 ships prebuilt wheels only for CPython 3.6-3.10.<br>\
|
||||
Use one of those Python versions and reinstall the app \
|
||||
(<code>python -m pip install .</code>), or build pyaoaddons from source.<br><br>\
|
||||
See the README for details.")
|
||||
msg.setStandardButtons(QMessageBox.Ok)
|
||||
elif platform_utils.is_windows():
|
||||
msg.setText(
|
||||
"On Windows make sure that Npcap is installed in your system.")
|
||||
msg.setInformativeText("Npcap can be installed from <a href='{}'>here</a> <br>\
|
||||
<b>Make sure to install with the \"Install Npcap in WinPcap API-compatible Mode\"</b> option<br><br>\
|
||||
In case where npcap is installed try to fix npcap and restart the app"
|
||||
.format('https://nmap.org/npcap/dist/npcap-0.9990.exe'))
|
||||
msg.setStandardButtons(QMessageBox.Ok)
|
||||
button = QPushButton("Fix npcap")
|
||||
button.clicked.connect(fix_network)
|
||||
msg.addButton(button, QMessageBox.NoRole)
|
||||
else:
|
||||
permissions_missing = platform_utils.is_linux() and not perms_ok
|
||||
if permissions_missing:
|
||||
msg.setText(
|
||||
"The app does not have permission to capture network packets.")
|
||||
msg.setInformativeText(
|
||||
"Capturing packets requires elevated privileges. Use the button below \
|
||||
to grant the Python interpreter the needed capabilities \
|
||||
(or run the app with <code>sudo</code>).")
|
||||
else:
|
||||
msg.setText(
|
||||
"Make sure that libpcap is installed and network interfaces are available.")
|
||||
msg.setInformativeText(
|
||||
"Install libpcap:<br>\
|
||||
<b>Debian/Ubuntu:</b> <code>sudo apt install libpcap0.8</code><br>\
|
||||
<b>Fedora:</b> <code>sudo dnf install libpcap</code><br>\
|
||||
<b>Arch:</b> <code>sudo pacman -S libpcap</code>")
|
||||
msg.setStandardButtons(QMessageBox.Ok)
|
||||
if platform_utils.repair_script():
|
||||
button = QPushButton("Fix capture permissions")
|
||||
button.clicked.connect(fix_network)
|
||||
msg.addButton(button, QMessageBox.NoRole)
|
||||
msg.show()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
import logging
|
||||
import socket
|
||||
import struct
|
||||
import threading
|
||||
from typing import Callable, List, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
GAME_PORT = 5056
|
||||
|
||||
PROTO_IPV4 = 0x0800
|
||||
PROTO_IPV6 = 0x86DD
|
||||
PROTO_VLAN = 0x8100
|
||||
PROTO_QINQ = 0x88A8
|
||||
IPPROTO_UDP = 17
|
||||
|
||||
ETH_P_ALL = 0x0003
|
||||
|
||||
MAX_FRAME = 65535
|
||||
|
||||
|
||||
def _extract_udp_payload(frame: bytes) -> Optional[bytes]:
|
||||
"""Returns the UDP payload if the ethernet frame carries UDP on port 5056.
|
||||
|
||||
Handles Ethernet, VLAN tags, IPv4 and IPv6 with basic extension handling.
|
||||
"""
|
||||
pos = 0
|
||||
if len(frame) < 14:
|
||||
return None
|
||||
ethertype = struct.unpack(">H", frame[12:14])[0]
|
||||
pos = 14
|
||||
while ethertype in (PROTO_VLAN, PROTO_QINQ):
|
||||
if len(frame) < pos + 4:
|
||||
return None
|
||||
ethertype = struct.unpack(">H", frame[pos + 2:pos + 4])[0]
|
||||
pos += 4
|
||||
if ethertype == PROTO_IPV4:
|
||||
if len(frame) < pos + 20:
|
||||
return None
|
||||
version_ihl = frame[pos]
|
||||
header_len = (version_ihl & 0x0F) * 4
|
||||
protocol = frame[pos + 9]
|
||||
payload = frame[pos + header_len:]
|
||||
elif ethertype == PROTO_IPV6:
|
||||
if len(frame) < pos + 40:
|
||||
return None
|
||||
next_header = frame[pos + 6]
|
||||
header_len = 40
|
||||
while next_header in (0, 43, 44, 60):
|
||||
if len(frame) < pos + header_len + 8:
|
||||
return None
|
||||
hdr_ext_len = (frame[pos + header_len + 1] + 1) * 8
|
||||
if next_header == 44:
|
||||
next_header = frame[pos + header_len]
|
||||
header_len += 8
|
||||
continue
|
||||
next_header = frame[pos + header_len]
|
||||
header_len += hdr_ext_len
|
||||
protocol = next_header
|
||||
payload = frame[pos + header_len:]
|
||||
else:
|
||||
return None
|
||||
if protocol != IPPROTO_UDP:
|
||||
return None
|
||||
if len(payload) < 8:
|
||||
return None
|
||||
src_port, dst_port, length = struct.unpack(">HHH", payload[0:6])
|
||||
if src_port != GAME_PORT and dst_port != GAME_PORT:
|
||||
return None
|
||||
udp_len = length
|
||||
if udp_len < 8 or udp_len > len(payload):
|
||||
return None
|
||||
return bytes(payload[8:udp_len])
|
||||
|
||||
|
||||
class CaptureEngine:
|
||||
"""Listens for Albion Online traffic on all non-loopback interfaces.
|
||||
|
||||
Mirrors the aoaddons packet sniffer: one AF_PACKET socket per interface,
|
||||
forwarding only UDP packets on port 5056 to a shared decode pipeline.
|
||||
"""
|
||||
|
||||
def __init__(self, on_udp: Callable[[bytes], None]):
|
||||
self._on_udp = on_udp
|
||||
self._sockets: List[socket.socket] = []
|
||||
self._threads: List[threading.Thread] = []
|
||||
self._stop = threading.Event()
|
||||
|
||||
@staticmethod
|
||||
def interface_names() -> List[str]:
|
||||
names = []
|
||||
try:
|
||||
for _index, name in socket.if_nameindex():
|
||||
if name and name != 'lo':
|
||||
names.append(name)
|
||||
except OSError:
|
||||
logger.warning("unable to enumerate network interfaces")
|
||||
return names
|
||||
|
||||
def start(self) -> bool:
|
||||
names = self.interface_names()
|
||||
if not names:
|
||||
logger.warning("no network interfaces available for capture")
|
||||
return False
|
||||
for name in names:
|
||||
try:
|
||||
sock = socket.socket(
|
||||
socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
|
||||
sock.bind((name, ETH_P_ALL))
|
||||
sock.settimeout(1.0)
|
||||
except OSError as e:
|
||||
logger.warning("unable to bind %s: %s", name, e)
|
||||
continue
|
||||
self._sockets.append(sock)
|
||||
thread = threading.Thread(
|
||||
target=self._run_interface, args=(sock, name), daemon=True)
|
||||
thread.start()
|
||||
self._threads.append(thread)
|
||||
logger.info("capturing on interface %s", name)
|
||||
if not self._sockets:
|
||||
logger.error("no interfaces could be captured (permissions?)")
|
||||
return False
|
||||
return True
|
||||
|
||||
def stop(self):
|
||||
self._stop.set()
|
||||
for sock in self._sockets:
|
||||
try:
|
||||
sock.close()
|
||||
except OSError:
|
||||
pass
|
||||
for thread in self._threads:
|
||||
thread.join(timeout=2.0)
|
||||
self._sockets = []
|
||||
self._threads = []
|
||||
|
||||
def _run_interface(self, sock: socket.socket, interface_name: str):
|
||||
logger.debug("capture thread started for %s", interface_name)
|
||||
try:
|
||||
while not self._stop.is_set():
|
||||
try:
|
||||
frame = sock.recv(MAX_FRAME)
|
||||
except socket.timeout:
|
||||
continue
|
||||
except OSError:
|
||||
break
|
||||
payload = _extract_udp_payload(frame)
|
||||
if payload is None:
|
||||
continue
|
||||
try:
|
||||
self._on_udp(payload)
|
||||
except Exception:
|
||||
logger.exception("error handling UDP payload")
|
||||
finally:
|
||||
logger.debug("capture thread stopped for %s", interface_name)
|
||||
Executable
+55
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
# Grant the Python interpreter the capabilities required to capture network
|
||||
# traffic, so that Albion Online Stats does not have to run as root.
|
||||
#
|
||||
# Usage: fixlibpcap.sh [path-to-python-interpreter]
|
||||
|
||||
PYTHON="${1:-$(command -v python3)}"
|
||||
|
||||
if [ -z "$PYTHON" ]; then
|
||||
echo "Could not determine the python interpreter path." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -x "$PYTHON" ]; then
|
||||
echo "Python interpreter '$PYTHON' is not executable." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Resolve symlinks (e.g. venv/bin/python -> /usr/bin/python3). setcap has to be
|
||||
# applied to the real binary, not to a symlink.
|
||||
if readlink -f "$PYTHON" >/dev/null 2>&1; then
|
||||
PYTHON="$(readlink -f "$PYTHON")"
|
||||
elif command -v python3 >/dev/null 2>&1; then
|
||||
PYTHON="$(python3 -c 'import os, sys; print(os.path.realpath(sys.executable))' 2>/dev/null || echo "$PYTHON")"
|
||||
fi
|
||||
|
||||
if ! command -v setcap >/dev/null 2>&1; then
|
||||
echo "The 'setcap' tool was not found." >&2
|
||||
echo "Install libcap before retrying:" >&2
|
||||
echo " Debian/Ubuntu: sudo apt install libcap2-bin" >&2
|
||||
echo " Fedora: sudo dnf install libcap" >&2
|
||||
echo " Arch: sudo pacman -S libcap" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Granting packet capture capabilities to: $PYTHON"
|
||||
echo "You may be asked for a password."
|
||||
|
||||
# pkexec shows a graphical authentication dialog, which works even when the app
|
||||
# was not started from a terminal. Fall back to sudo otherwise.
|
||||
if command -v pkexec >/dev/null 2>&1; then
|
||||
pkexec setcap cap_net_raw,cap_net_admin=eip "$PYTHON"
|
||||
status=$?
|
||||
else
|
||||
sudo setcap cap_net_raw,cap_net_admin=eip "$PYTHON"
|
||||
status=$?
|
||||
fi
|
||||
|
||||
if [ "$status" -ne 0 ]; then
|
||||
echo "Failed to set capture capabilities. Try running the app with sudo:" >&2
|
||||
echo " sudo $(command -v aostats 2>/dev/null || echo 'python -m aostats')" >&2
|
||||
exit "$status"
|
||||
fi
|
||||
|
||||
echo "Done. Please restart Albion Online Stats."
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
+12
-6
@@ -1,8 +1,9 @@
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
import toml
|
||||
|
||||
from . import platform_utils
|
||||
|
||||
CFG_VERSION = '0.7'
|
||||
|
||||
default = """
|
||||
@@ -33,11 +34,12 @@ _config = None
|
||||
|
||||
def get_config_path():
|
||||
cfg_file_name = 'albion-online-stats.cfg'
|
||||
script_dir = os.path.dirname(_script_path)
|
||||
if platform.system() == 'Windows':
|
||||
return os.path.join(os.getenv('APPDATA', script_dir), cfg_file_name)
|
||||
else:
|
||||
return os.path.join(script_dir, cfg_file_name)
|
||||
|
||||
cfg_dir = platform_utils.config_dir()
|
||||
if not cfg_dir:
|
||||
cfg_dir = os.path.dirname(os.path.abspath(_script_path))
|
||||
|
||||
return os.path.join(cfg_dir, cfg_file_name)
|
||||
|
||||
|
||||
def config():
|
||||
@@ -48,6 +50,10 @@ def config():
|
||||
|
||||
conf_file = get_config_path()
|
||||
|
||||
conf_dir = os.path.dirname(conf_file)
|
||||
if conf_dir:
|
||||
os.makedirs(conf_dir, exist_ok=True)
|
||||
|
||||
try:
|
||||
with open(conf_file, "r") as cfg_file:
|
||||
cfg = toml.load(cfg_file)
|
||||
|
||||
+10
-2
@@ -1,9 +1,17 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
try:
|
||||
from pyaoaddons import localization_mapping
|
||||
except:
|
||||
localization_mapping = {}
|
||||
except Exception:
|
||||
_PATH = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
'static', 'localization_mapping.json')
|
||||
try:
|
||||
with open(_PATH, encoding='utf-8') as _f:
|
||||
localization_mapping = json.load(_f)
|
||||
except (OSError, ValueError):
|
||||
localization_mapping = {}
|
||||
|
||||
|
||||
from . import assets
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import os
|
||||
import platform
|
||||
import socket
|
||||
from typing import Optional
|
||||
|
||||
WINDOWS = 'Windows'
|
||||
LINUX = 'Linux'
|
||||
MACOS = 'Darwin'
|
||||
|
||||
APP_DIR_NAME = 'albion-online-stats'
|
||||
|
||||
# Name of the platform specific script used to repair packet capture
|
||||
# permissions. ``None`` means that no automatic repair is available.
|
||||
_REPAIR_SCRIPTS = {
|
||||
WINDOWS: 'fixnpcap.bat',
|
||||
LINUX: 'fixlibpcap.sh',
|
||||
MACOS: 'fixlibpcap.sh',
|
||||
}
|
||||
|
||||
|
||||
def system() -> str:
|
||||
return platform.system()
|
||||
|
||||
|
||||
def is_windows() -> bool:
|
||||
return system() == WINDOWS
|
||||
|
||||
|
||||
def is_linux() -> bool:
|
||||
return system() == LINUX
|
||||
|
||||
|
||||
def is_macos() -> bool:
|
||||
return system() == MACOS
|
||||
|
||||
|
||||
def repair_script() -> Optional[str]:
|
||||
return _REPAIR_SCRIPTS.get(system())
|
||||
|
||||
|
||||
def capture_permissions_ok() -> bool:
|
||||
"""Return True when the current process may capture network packets.
|
||||
|
||||
The packet capture backend starts background sniffing threads and reports
|
||||
initialization as successful even when it later fails to open a socket, so
|
||||
on Linux this explicitly probes for ``CAP_NET_RAW`` (required to open
|
||||
``AF_PACKET`` raw sockets). On other platforms there is no easy probe, so
|
||||
``True`` is returned.
|
||||
"""
|
||||
if not is_linux():
|
||||
return True
|
||||
|
||||
euid = getattr(os, 'geteuid', lambda: -1)()
|
||||
if euid == 0:
|
||||
return True
|
||||
|
||||
try:
|
||||
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
|
||||
sock.close()
|
||||
return True
|
||||
except (PermissionError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def config_dir() -> Optional[str]:
|
||||
"""Return the platform specific directory used to store the config.
|
||||
|
||||
Returns ``None`` when no standard per-user directory can be determined.
|
||||
"""
|
||||
if is_windows():
|
||||
base = os.getenv('APPDATA')
|
||||
return os.path.join(base, APP_DIR_NAME) if base else None
|
||||
|
||||
if is_macos():
|
||||
home = os.path.expanduser('~')
|
||||
if not home or home == '~':
|
||||
return None
|
||||
return os.path.join(home, 'Library', 'Application Support', APP_DIR_NAME)
|
||||
|
||||
if is_linux():
|
||||
base = os.getenv('XDG_CONFIG_HOME')
|
||||
if not base:
|
||||
home = os.path.expanduser('~')
|
||||
if not home or home == '~':
|
||||
return None
|
||||
base = os.path.join(home, '.config')
|
||||
return os.path.join(base, APP_DIR_NAME)
|
||||
|
||||
return None
|
||||
+12
-2
@@ -1,7 +1,17 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
try:
|
||||
from pyaoaddons import item_category_mapping
|
||||
except:
|
||||
item_category_mapping = {}
|
||||
except Exception:
|
||||
_PATH = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
'static', 'item_category_mapping.json')
|
||||
try:
|
||||
with open(_PATH, encoding='utf-8') as _f:
|
||||
item_category_mapping = json.load(_f)
|
||||
except (OSError, ValueError):
|
||||
item_category_mapping = {}
|
||||
|
||||
|
||||
class WeaponType:
|
||||
|
||||
Reference in New Issue
Block a user