1649f91318
- Speed: offset 256 (m/s * 3.6 = km/h), was incorrectly at 244 (PositionX) - Gear: offset 319 (U8), was at 264/298 - Throttle/Brake: offsets 315/316 (U8, 0-255) - Steering: offset 320 (S8, -127~127) - RPM: offset 16 ✓, MaxRPM: offset 8 ✓ - Lap times: BestLap=296, LastLap=300, CurrentLap=304 - LapNumber: offset 312 (U16) - Boost: 284, Fuel: 288, Position: 244/248/252 - Updated all Forza parsers (FH4, FH5) and fallback parser - Ref: Forza Data Out official format (324 bytes)
52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
import struct
|
|
import time
|
|
from server.telemetry.data import TelemetryData
|
|
|
|
|
|
def get_parser():
|
|
return ForzaHorizon4Parser()
|
|
|
|
|
|
class ForzaHorizon4Parser:
|
|
def game_id(self) -> str:
|
|
return "forza_horizon_4"
|
|
|
|
def parse(self, data: bytes, addr: tuple) -> TelemetryData:
|
|
td = TelemetryData(game_id="forza_horizon_4", timestamp=time.time())
|
|
td.raw = {"raw_hex": data.hex(), "length": len(data), "addr": f"{addr[0]}:{addr[1]}"}
|
|
|
|
if len(data) < 312:
|
|
return td
|
|
|
|
try:
|
|
td.raw["is_race_on"] = struct.unpack_from("<i", data, 0)[0]
|
|
td.max_rpm = struct.unpack_from("<f", data, 8)[0]
|
|
td.rpm = struct.unpack_from("<f", data, 16)[0]
|
|
td.acceleration_x = struct.unpack_from("<f", data, 20)[0]
|
|
td.acceleration_y = struct.unpack_from("<f", data, 24)[0]
|
|
td.acceleration_z = struct.unpack_from("<f", data, 28)[0]
|
|
td.position_x = struct.unpack_from("<f", data, 244)[0]
|
|
td.position_y = struct.unpack_from("<f", data, 248)[0]
|
|
td.position_z = struct.unpack_from("<f", data, 252)[0]
|
|
td.speed_kmh = struct.unpack_from("<f", data, 256)[0] * 3.6
|
|
td.speed_mph = td.speed_kmh * 0.621371
|
|
td.horsepower = struct.unpack_from("<f", data, 260)[0] / 745.7
|
|
td.torque = struct.unpack_from("<f", data, 264)[0]
|
|
td.boost = struct.unpack_from("<f", data, 284)[0]
|
|
td.fuel = struct.unpack_from("<f", data, 288)[0]
|
|
td.best_lap = struct.unpack_from("<f", data, 296)[0]
|
|
td.last_lap = struct.unpack_from("<f", data, 300)[0]
|
|
td.lap_time = struct.unpack_from("<f", data, 304)[0]
|
|
td.lap_number = struct.unpack_from("<H", data, 312)[0]
|
|
td.raw["race_position"] = struct.unpack_from("<B", data, 314)[0]
|
|
td.throttle = struct.unpack_from("<B", data, 315)[0] / 255.0
|
|
td.brake = struct.unpack_from("<B", data, 316)[0] / 255.0
|
|
td.clutch = struct.unpack_from("<B", data, 317)[0] / 255.0
|
|
td.handbrake = struct.unpack_from("<B", data, 318)[0] / 255.0
|
|
td.gear = struct.unpack_from("<B", data, 319)[0]
|
|
td.steering = max(-1.0, min(1.0, struct.unpack_from("<b", data, 320)[0] / 127.0))
|
|
except struct.error:
|
|
pass
|
|
|
|
return td
|