rFactor-2-Message-Center/rFactor2MiscData.cs
Kilian Hofmann 1ac3ef7b86 Renamed
Select vehivle telemetry
2022-11-13 03:50:41 +01:00

114 lines
5.5 KiB
C#

using CrewChiefV4.rFactor2_V2.rFactor2Data;
using GameReaderCommon;
using RfactorReader.RF2;
using SimHub.Plugins;
using System;
namespace rFactor2MiscData
{
[PluginDescription("Read Access to miscellaneous rFactor 2 Data")]
[PluginAuthor("Kilian Kurt Hofmann")]
[PluginName("rFactor 2 Misc Data")]
public class rFactor2MiscData : IPlugin, IDataPlugin
{
private const string LAST_MESSAGE = "rF2MiscData.LastMessage";
private const string VEHICLE_BASE = "rF2MiscData.Position";
private const string VEHICLE_FUEL = ".Fuel";
private const string VEHICLE_THROTTLE= ".Throttle";
private const string VEHICLE_BRAKE = ".Brake";
private const string VEHICLE_PLACE = ".Place";
private const string VEHICLE_DRIVERNAME = ".DriverName";
/// <summary>
/// Instance of the current plugin manager
/// </summary>
public PluginManager PluginManager { get; set; }
/// <summary>
/// Gets a short plugin title to show in left menu. Return null if you want to use the title as defined in PluginName attribute.
/// </summary>
public string LeftMenuTitle => null;
/// <summary>
/// Called one time per game data update, contains all normalized game data,
/// raw data are intentionnally "hidden" under a generic object type (A plugin SHOULD NOT USE IT)
///
/// This method is on the critical path, it must execute as fast as possible and avoid throwing any error
///
/// </summary>
/// <param name="pluginManager"></param>
/// <param name="data">Current game data, including current and previous data frame.</param>
public void DataUpdate(PluginManager pluginManager, ref GameData data)
{
// Define the value of our property (declared in init)
if (data.GameRunning && data is GameData<WrapV2> _data)
{
WrapV2 newData = _data.GameNewData.Raw;
// Last Message
string message = System.Text.Encoding.Default.GetString(newData.extended.mLastHistoryMessage);
int index = message.IndexOf('\0');
if( index >= 0)
{
message = message.Remove(index);
}
pluginManager.SetPropertyValue(LAST_MESSAGE, GetType(), message);
int maxVehicles = Math.Min(newData.Scoring.mVehicles.Length, newData.telemetry.mVehicles.Length);
for(int i = 0; i < maxVehicles; i++)
{
rF2VehicleTelemetry telemetry = newData.telemetry.mVehicles[i];
rF2VehicleScoring scoring = newData.Scoring.mVehicles[i];
// Vehicle Fuel
pluginManager.SetPropertyValue($"{VEHICLE_BASE}{scoring.mPlace:000}{VEHICLE_FUEL}", GetType(), telemetry.mFuel);
// Vehicle Throttle
pluginManager.SetPropertyValue($"{VEHICLE_BASE}{scoring.mPlace:000}{VEHICLE_THROTTLE}", GetType(), telemetry.mFilteredThrottle);
// Vehicle Brake
pluginManager.SetPropertyValue($"{VEHICLE_BASE}{scoring.mPlace:000}{VEHICLE_BRAKE}", GetType(), telemetry.mFilteredBrake);
// Vehicle Position
pluginManager.SetPropertyValue($"{VEHICLE_BASE}{scoring.mPlace:000}{VEHICLE_PLACE}", GetType(), scoring.mPlace);
// Vehicle Driver Name
string driverName = System.Text.Encoding.Default.GetString(scoring.mDriverName);
index = message.IndexOf('\0');
if (index >= 0)
{
message = message.Remove(index);
}
pluginManager.SetPropertyValue($"{VEHICLE_BASE}{scoring.mPlace:000}{VEHICLE_DRIVERNAME}", GetType(), driverName);
}
}
}
/// <summary>
/// Called at plugin manager stop, close/dispose anything needed here !
/// Plugins are rebuilt at game change
/// </summary>
/// <param name="pluginManager"></param>
public void End(PluginManager pluginManager)
{
SimHub.Logging.Current.Info("Stopping plugin");
pluginManager.ClearProperties(GetType());
}
/// <summary>
/// Called once after plugins startup
/// Plugins are rebuilt at game change
/// </summary>
/// <param name="pluginManager"></param>
public void Init(PluginManager pluginManager)
{
SimHub.Logging.Current.Info("Starting plugin");
pluginManager.AddProperty(LAST_MESSAGE, GetType(), "No Data", "Last Message Center Entry");
for (int i = 1; i <= 104; i++)
{
pluginManager.AddProperty($"{VEHICLE_BASE}{i:000}{VEHICLE_FUEL}", GetType(), "No Data", $"Vehicle at position {i}: Fuel");
pluginManager.AddProperty($"{VEHICLE_BASE}{i:000}{VEHICLE_THROTTLE}", GetType(), "No Data", $"Vehicle at position {i}: Throttle");
pluginManager.AddProperty($"{VEHICLE_BASE}{i:000}{VEHICLE_BRAKE}", GetType(), "No Data", $"Vehicle at position {i}: Brake");
pluginManager.AddProperty($"{VEHICLE_BASE}{i:000}{VEHICLE_PLACE}", GetType(), "No Data", $"Vehicle at position {i}: Place");
pluginManager.AddProperty($"{VEHICLE_BASE}{i:000}{VEHICLE_DRIVERNAME}", GetType(), "No Data", $"Vehicle at position {i}: Driver Name");
}
}
}
}