|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Sockets; |
| 8 | +using Syroot.BinaryData.Memory; |
| 9 | +using System.Numerics; |
| 10 | + |
| 11 | +using PDTools.Crypto.SimulationInterface; |
| 12 | + |
| 13 | +namespace SimulatorInterface |
| 14 | +{ |
| 15 | + public class SimulatorInterface |
| 16 | + { |
| 17 | + private ISimulationInterfaceCryptor _cryptor; |
| 18 | + private IPEndPoint _endpoint; |
| 19 | + |
| 20 | + public const int SendDelaySeconds = 10; |
| 21 | + |
| 22 | + public const int ReceivePort = 33739; |
| 23 | + public const int BindPort = 33740; |
| 24 | + |
| 25 | + public SimulatorInterface(string address) |
| 26 | + { |
| 27 | + if (!IPAddress.TryParse(address, out IPAddress addr)) |
| 28 | + throw new ArgumentException("Could not parse IP Address."); |
| 29 | + |
| 30 | + _endpoint = new IPEndPoint(addr, ReceivePort); |
| 31 | + _cryptor = new SimulatorInterfaceCryptorGT7(); |
| 32 | + } |
| 33 | + |
| 34 | + public bool Start() |
| 35 | + { |
| 36 | + Console.WriteLine($"- Starting Simulator Interface to connect at endpoint: {_endpoint}"); |
| 37 | + const int SendDelaySeconds = 10; |
| 38 | + |
| 39 | + UdpClient udpClient; |
| 40 | + try |
| 41 | + { |
| 42 | + Console.WriteLine($"- Attempting to bind port: {BindPort}"); |
| 43 | + udpClient = new UdpClient(BindPort); |
| 44 | + |
| 45 | + Console.WriteLine("- Sending heartbeat packet.."); |
| 46 | + udpClient.Send(new byte[1] { (byte)'A' }, _endpoint); |
| 47 | + } |
| 48 | + catch (Exception e) |
| 49 | + { |
| 50 | + Console.WriteLine($"Error: {e.Message}"); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + DateTime lastSent = DateTime.UtcNow; |
| 55 | + IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); |
| 56 | + |
| 57 | + Console.WriteLine("- Done. Waiting on packets.. (If this gets stuck, it failed to connect.)"); |
| 58 | + |
| 59 | + // Will send a packet per tick - 60fps |
| 60 | + while (true) |
| 61 | + { |
| 62 | + if ((DateTime.UtcNow - lastSent).TotalSeconds > SendDelaySeconds) |
| 63 | + udpClient.Send(new byte[1] { (byte)'A' }, _endpoint); |
| 64 | + |
| 65 | + byte[] data = udpClient.Receive(ref RemoteIpEndPoint); |
| 66 | + if (data.Length != 0x128) |
| 67 | + throw new InvalidDataException($"Expected packet size to be 0x128. Was {data.Length:X4} bytes."); |
| 68 | + |
| 69 | + _cryptor.Decrypt(data); |
| 70 | + |
| 71 | + SpanReader sr = new SpanReader(data); |
| 72 | + int magic = sr.ReadInt32(); |
| 73 | + if (magic != 0x47375330) // 0S7G - G7S0 |
| 74 | + throw new InvalidDataException($"Unexpected packet magic '{magic}'."); |
| 75 | + |
| 76 | + var dataPacket = new SimulatorPacketGT7(); |
| 77 | + |
| 78 | + dataPacket.Position = new Vector3(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()); // Coords to track |
| 79 | + dataPacket.Acceleration = new Vector3(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()); // Accel in track pixels |
| 80 | + dataPacket.Rotation = new Vector3(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()); // Pitch/Yaw/Roll all -1 to 1 |
| 81 | + dataPacket.Unknown_0x28 = sr.ReadSingle(); |
| 82 | + dataPacket.Unknown_0x2C = new Vector3(sr.ReadSingle(), sr.ReadSingle(), sr.ReadSingle()); |
| 83 | + dataPacket.Unknown_0x38 = sr.ReadSingle(); |
| 84 | + dataPacket.RPM = sr.ReadSingle(); |
| 85 | + |
| 86 | + // Skip IV |
| 87 | + sr.Position += 8; |
| 88 | + |
| 89 | + dataPacket.Unknown_0x48 = sr.ReadSingle(); |
| 90 | + dataPacket.MetersPerSecond = sr.ReadSingle(); |
| 91 | + dataPacket.TurboBoost = sr.ReadSingle(); |
| 92 | + dataPacket.Unknown_0x54 = sr.ReadSingle(); |
| 93 | + dataPacket.Unknown_Always85_0x58 = sr.ReadSingle(); |
| 94 | + dataPacket.Unknown_Always110_0x5C = sr.ReadSingle(); |
| 95 | + dataPacket.TireSurfaceTemperatureFL = sr.ReadSingle(); |
| 96 | + dataPacket.TireSurfaceTemperatureFR = sr.ReadSingle(); |
| 97 | + dataPacket.TireSurfaceTemperatureRL = sr.ReadSingle(); |
| 98 | + dataPacket.TireSurfaceTemperatureRR = sr.ReadSingle(); |
| 99 | + dataPacket.TotalTimeTicks = sr.ReadInt32(); // can't be more than MAX_LAPTIME1000 - which is 1209599999, or else it's set to -1 |
| 100 | + dataPacket.CurrentLap = sr.ReadInt32(); |
| 101 | + dataPacket.BestLapTime = TimeSpan.FromMilliseconds(sr.ReadInt32()); |
| 102 | + dataPacket.LastLapTime = TimeSpan.FromMilliseconds(sr.ReadInt32()); |
| 103 | + dataPacket.DayProgressionMS = sr.ReadInt32(); |
| 104 | + dataPacket.PreRaceStartPositionOrQualiPos = sr.ReadInt16(); |
| 105 | + dataPacket.NumCarsAtPreRace = sr.ReadInt16(); |
| 106 | + dataPacket.MinAlertRPM = sr.ReadInt16(); |
| 107 | + dataPacket.MaxAlertRPM = sr.ReadInt16(); |
| 108 | + dataPacket.CalculatedMaxSpeed = sr.ReadInt16(); |
| 109 | + dataPacket.Flags = (SimulatorFlags)sr.ReadInt16(); |
| 110 | + |
| 111 | + int bits = sr.ReadByte(); |
| 112 | + dataPacket.CurrentGear = (byte)(bits & 0b1111); |
| 113 | + dataPacket.SuggestedGear = (byte)(bits >> 4); |
| 114 | + |
| 115 | + dataPacket.Throttle = sr.ReadByte(); |
| 116 | + dataPacket.Brake = sr.ReadByte(); |
| 117 | + |
| 118 | + //short throttleAndBrake = sr.ReadInt16(); |
| 119 | + byte unknown = sr.ReadByte(); |
| 120 | + |
| 121 | + dataPacket.TireFL_Unknown0x94_0 = sr.ReadSingle(); |
| 122 | + dataPacket.TireFR_Unknown0x94_1 = sr.ReadSingle(); |
| 123 | + dataPacket.TireRL_Unknown0x94_2 = sr.ReadSingle(); |
| 124 | + dataPacket.TireRR_Unknown0x94_3 = sr.ReadSingle(); |
| 125 | + dataPacket.TireFL_Accel = sr.ReadSingle(); |
| 126 | + dataPacket.TireFR_Accel = sr.ReadSingle(); |
| 127 | + dataPacket.TireRL_Accel = sr.ReadSingle(); |
| 128 | + dataPacket.TireRR_Accel = sr.ReadSingle(); |
| 129 | + dataPacket.TireFL_UnknownB4 = sr.ReadSingle(); |
| 130 | + dataPacket.TireFR_UnknownB4 = sr.ReadSingle(); |
| 131 | + dataPacket.TireRL_UnknownB4 = sr.ReadSingle(); |
| 132 | + dataPacket.TireRR_UnknownB4 = sr.ReadSingle(); |
| 133 | + dataPacket.TireFL_UnknownC4 = sr.ReadSingle(); |
| 134 | + dataPacket.TireFR_UnknownC4 = sr.ReadSingle(); |
| 135 | + dataPacket.TireRL_UnknownC4 = sr.ReadSingle(); |
| 136 | + dataPacket.TireRR_UnknownC4 = sr.ReadSingle(); |
| 137 | + |
| 138 | + sr.Position += sizeof(int) * 8; // Seems to be reserved - server does not set that |
| 139 | + |
| 140 | + dataPacket.Unknown_0xF4 = sr.ReadSingle(); |
| 141 | + dataPacket.Unknown_0xF8 = sr.ReadSingle(); |
| 142 | + dataPacket.RPMUnknown_0xFC = sr.ReadSingle(); |
| 143 | + |
| 144 | + for (var i = 0; i < 7; i++) |
| 145 | + dataPacket.Unknown_0x100[i] = sr.ReadSingle(); |
| 146 | + |
| 147 | + sr.Position += 8; |
| 148 | + dataPacket.CarCode = sr.ReadInt32(); |
| 149 | + |
| 150 | + PrintStatus(dataPacket); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void PrintStatus(SimulatorPacketGT7 packet) |
| 155 | + { |
| 156 | + Console.Clear(); |
| 157 | + Console.WriteLine($"Simulator Interface Packet"); |
| 158 | + Console.WriteLine("[Car Data]"); |
| 159 | + Console.WriteLine($"- Car Code: {packet.CarCode}"); |
| 160 | + Console.WriteLine($"- Throttle: {packet.Throttle}"); |
| 161 | + Console.WriteLine($"- Brake: {packet.Brake}"); |
| 162 | + Console.WriteLine($"- RPM: {packet.RPM} - KPH: {Math.Round(packet.MetersPerSecond * 3.6, 2)}"); |
| 163 | + Console.WriteLine($"- Turbo Boost: {packet.TurboBoost}"); |
| 164 | + |
| 165 | + if (packet.SuggestedGear == 15) |
| 166 | + Console.WriteLine($"- Gear: {packet.CurrentGear}"); |
| 167 | + else |
| 168 | + Console.WriteLine($"- Gear: {packet.CurrentGear} (Suggested: {packet.SuggestedGear})"); |
| 169 | + |
| 170 | + Console.WriteLine($"- Flags: {packet.Flags}"); |
| 171 | + Console.WriteLine($"- Tires"); |
| 172 | + Console.WriteLine($" FL:{Math.Round(packet.TireSurfaceTemperatureFL, 2)} FR:{Math.Round(packet.TireSurfaceTemperatureFR, 2)}"); |
| 173 | + Console.WriteLine($" RL:{Math.Round(packet.TireSurfaceTemperatureRL, 2)} RR:{Math.Round(packet.TireSurfaceTemperatureRR, 2)}"); |
| 174 | + |
| 175 | + Console.WriteLine(); |
| 176 | + Console.WriteLine("[Race Data]"); |
| 177 | + |
| 178 | + int a = (int)(packet.TotalTimeTicks * 0.16667); |
| 179 | + TimeSpan.FromSeconds(a / 10); |
| 180 | + Console.WriteLine($"- Total Session Time: {TimeSpan.FromSeconds(packet.TotalTimeTicks / 60)}"); |
| 181 | + Console.WriteLine($"- Current Lap: {packet.CurrentLap}"); |
| 182 | + Console.WriteLine($"- Best: {packet.BestLapTime}"); |
| 183 | + Console.WriteLine($"- Last: {packet.LastLapTime}"); |
| 184 | + Console.WriteLine($"- Time of Day: {TimeSpan.FromMilliseconds(packet.DayProgressionMS)}"); |
| 185 | + |
| 186 | + Console.WriteLine(); |
| 187 | + Console.WriteLine("[Positional Information]"); |
| 188 | + Console.WriteLine($"- Position: {packet.Position}"); |
| 189 | + Console.WriteLine($"- Accel: {packet.Acceleration}"); |
| 190 | + Console.WriteLine($"- Rotation: {packet.Rotation}"); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments