From 64585e3ba697e3b38a3698cb4be288ad98fc41e2 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Mon, 27 Jan 2025 16:41:03 +0530 Subject: Initial Commit --- lib/YSchat.py | 22 +++++++++++++++++ lib/YSendFlight.py | 6 +++++ lib/YSplayer.py | 27 ++++++++++++++++++++ lib/parseFlightData.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 lib/YSchat.py create mode 100644 lib/YSendFlight.py create mode 100644 lib/YSplayer.py create mode 100644 lib/parseFlightData.py (limited to 'lib') diff --git a/lib/YSchat.py b/lib/YSchat.py new file mode 100644 index 0000000..fb23208 --- /dev/null +++ b/lib/YSchat.py @@ -0,0 +1,22 @@ +from struct import pack, unpack + +def send(buffer: bytes): + """ + Add to a packet the 'size' information + """ + return pack("I", len(buffer)) + buffer + +def reply(type, buffer:bytes): + """ + Generate packets to send + """ + return send(pack("I", type) + buffer) + +def message(msg: str): + """ + Generate packets for sending messages + """ + decode = "l" + str(len(msg) + 2) + "s" + msg_buffer = bytes(msg, 'utf-8') + buffer = pack(decode, 0, msg_buffer) + return reply(32, buffer) diff --git a/lib/YSendFlight.py b/lib/YSendFlight.py new file mode 100644 index 0000000..c8456cf --- /dev/null +++ b/lib/YSendFlight.py @@ -0,0 +1,6 @@ +from struct import pack +from lib.YSchat import reply + +def endFlight(id: int): + buffer = pack("Ih", id, 0) + return reply(12, buffer) diff --git a/lib/YSplayer.py b/lib/YSplayer.py new file mode 100644 index 0000000..c71f12e --- /dev/null +++ b/lib/YSplayer.py @@ -0,0 +1,27 @@ +class Player: + def __init__(self,username, playerId, x, y, z, throttle, aam, agm, + gunAmmo, rktAmmo, fuel, ipAddr, life, vx, vy, vz, gValue): + self.username = username + self.ip = ipAddr + self.playerId = playerId + self.x = x + self.y = y + self.z = z + self.throttle = throttle + self.aam = aam + self.agm = agm + self.gunAmmo = gunAmmo + self.rktAmmo = rktAmmo + self.fuel = fuel + self.life = life + self.vx = vx + self.vy = vy + self.vz = vz + self.gValue = gValue + + def __str__(self): + return f"Username: {self.username}, IP: {self.ip}, " \ + f"Player ID: {self.playerId}, X: {self.x}, Y: {self.y}, Z: {self.z}, " \ + f"Throttle: {self.throttle}, AAM: {self.aam}, AGM: {self.agm}, " \ + f"Gun Ammo: {self.gunAmmo}, Rocket Ammo: {self.rktAmmo}, Fuel: {self.fuel}" \ + f"Life: {self.life}, gValue: {self.gValue}" diff --git a/lib/parseFlightData.py b/lib/parseFlightData.py new file mode 100644 index 0000000..35aa3b3 --- /dev/null +++ b/lib/parseFlightData.py @@ -0,0 +1,67 @@ +from math import pi +from struct import unpack as up + +# Authored by https://theindiandev.in +# Date : Jan 26 2025 +# Not more than 80 characters per line + +def parseFlightData(data: bytes): + """ + Input : Bytecode, Type 11 datapackets from ysflight server, + do NOT strip the heading(first 8 octets) + Output : tuple + + Tested only on 20150425 version of ysflight + """ + # Assuming version(info1) = 5,4 when speed < 400kts + # version(info1) = 3 when speed > 400kts OR just spawning in the server + version = up("h", data[16:18])[0] + if version == 5 or version == 4: + tRemote = up("f", data[8:12])[0] # Remote Timer from client + playerId = up("I", data[12:16])[0] + x,y,z = up("fff", data[18:30]) + # Heading, Pitch and Bank values are correct but cannot be interepeted + # correctly + # FIXME + h, p, b = up("hhh", data[30:36]) + heading = (h*pi/32768.0) + aoa = (p*pi/32768.0) + bank = (b*pi/32760.0) + vx, vy, vz = up("hhh", data[36:42]) + # FIXME : Unkown padding of 2 bytes, it seems to be from the fuel as a + # integer however correct values only with a short + smokeOil, fuel, payload, _ = up("hhhh", data[48:56]) + flightState, vgw = up("BB", data[56:58]) + gunAmmo, rktAmmo = up("hh", data[62:66]) + # FIXME : aam count incorrect + aam, agm, bomb, life = up("BBBB", data[66:70]) # aam, agm, bomb, life + gValue = (up("B", data[70:71])[0])/10 + throttle, elev, ail, rud, trim, bombBayInfo = up("BBBBBB", data[71:77]) + elif version == 3: + tRemote = up("f", data[8:12])[0] + playerId = up("I", data[12:16])[0] + x,y,z = up("fff", data[20:32]) + h, p, b = up("hhh", data[32:38]) + heading = (h*pi/32768.0) + aoa = (p*pi/32768.0) + bank = (b*pi/32760.0) + vx, vy, vz = up("hhh", data[38:44]) + gValue = (up("h", data[50:52])[0])/100 + gunAmmo, aam, agm, bomb, smokeOil = up("5h", data[52:62]) + fuel, payload = up("2f", data[62:70]) + life = up("h", data[70:72])[0] + flightState, vgw = up("BB", data[72:74]) + throttle, elev, ail, rud, trim = up("B4c", data[80:85]) + rktAmmo = up("h", data[85:87])[0] + bombBayInfo = up("B", data[89:90])[0] + else: + print(f"Unkown version {version}") + print("Payload Dump:") + print(data) + raise ValueError("Unknown version of ysflight") + + + return (tRemote, playerId, x, y, z, heading, aoa, bank, vx, vy, vz, + smokeOil, fuel, payload, flightState, vgw, gunAmmo, rktAmmo, + aam, agm, bomb, life, throttle, elev, ail, rud, trim, + bombBayInfo, gValue) -- cgit v1.2.3