Open FarmerJon1066 opened 6 months ago
Hey, if you didn't modify the shared memory mapped created by the plugin, the offset for the speed is somewhat over 700 and the truck position even has a much higher offset in the memory. If I read your code correctly, you take the first bytes from it. But there is other data located. The data starts with sdkActive
, empty space, paused
, empty space, time
... and so on.
In scs-telemetry/inc/scs-telemetry-common.hpp
offsets are given to specific areas of the memory. Or the unwrapping side scs-client/C#/SCSSdkClient/SCSSdkConvert.cs
.
To direct fetch specific values, you would need to create/calculate the offset to the specific value first. It is not that difficult, but currently there is no available table for that.
Thanks for the fast reply so something like this then? The data seems to make much better sense now, its just finding the right offset. Current truck speed: 3 m/s (6.7108197 mph) Truck cabin position: X=0, Y=0.9946883, Z=-1.8208612
using System; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; using System.Threading;
namespace ATS_API { class Program { static void Main() { var telemetryHandler = new TelemetryHandler("Local\SCSTelemetry"); telemetryHandler.Start(); } }
// Define the TelemetryData structure with explicit layout to set specific field offsets
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
public struct TelemetryData
{
// Define the speed field at offset 700
[FieldOffset(700)]
public float speed; // Truck speed in m/s
// Define the cabin position fields at offset 1640
[FieldOffset(1640)]
public FVector cabinPosition; // Truck cabin position
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct FVector
{
public float X;
public float Y;
public float Z;
}
public class TelemetryHandler
{
private MemoryMappedFile memoryMappedFile;
private MemoryMappedViewAccessor accessor;
public TelemetryHandler(string sharedMemoryName)
{
memoryMappedFile = MemoryMappedFile.OpenExisting(sharedMemoryName);
accessor = memoryMappedFile.CreateViewAccessor();
}
public void Start()
{
Console.WriteLine("Telemetry data is ready.");
while (true)
{
Console.Clear(); // Clear the console window
var telemetryData = ReadTelemetryData();
Console.WriteLine($"Current truck speed: {telemetryData.speed} m/s ({telemetryData.speed * 2.23694f} mph)");
Console.WriteLine($"Truck cabin position: X={telemetryData.cabinPosition.X}, Y={telemetryData.cabinPosition.Y}, Z={telemetryData.cabinPosition.Z}");
Thread.Sleep(1000); // Wait for 1 second before the next update
}
}
private TelemetryData ReadTelemetryData()
{
int dataSize = Marshal.SizeOf(typeof(TelemetryData));
byte[] rawData = new byte[dataSize];
// Ensure the accessor has enough capacity to read the data
if (accessor.Capacity >= dataSize)
{
accessor.ReadArray(0, rawData, 0, rawData.Length);
GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
TelemetryData data = (TelemetryData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TelemetryData));
handle.Free();
return data;
}
else
{
Console.WriteLine("Shared memory does not contain enough data.");
return new TelemetryData(); // Return a default structure
}
}
~TelemetryHandler()
{
accessor?.Dispose();
memoryMappedFile?.Dispose();
}
}
}
I want to make sure I am heading down the right path for getting the data from the memory mapped file. This is the code I have so far but it always returns crap data of (Current truck speed: 1E-45 m/s (3E-45 mph) Truck position: X=1E-45, Y=5.7172556E-31, Z=0)
using System; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; using System.Threading;
namespace ATS_API { class Program { static void Main() { var telemetryHandler = new TelemetryHandler("Local\SCSTelemetry"); telemetryHandler.Start(); } }
}