rFactor-2-Message-Center/rFactor2MessageCenter.cs
2022-11-11 02:41:59 +01:00

73 lines
2.8 KiB
C#

using GameReaderCommon;
using RfactorReader.RF2;
using SimHub.Plugins;
namespace rFactor2MessageCenter
{
[PluginDescription("Read Access to the rFactor 2 Message Center")]
[PluginAuthor("Kilian Kurt Hofmann")]
[PluginName("rFactor 2 Message Center")]
public class rFactor2MessageCenter : IPlugin, IDataPlugin
{
private const string LAST_MESSAGE = "LastMessage";
/// <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)
{
StatusData<WrapV2> newData = _data.GameNewData;
string message = System.Text.Encoding.Default.GetString(newData.Raw.extended.mLastHistoryMessage);
int index = message.IndexOf('\0');
if( index >= 0)
{
message = message.Remove(index);
}
pluginManager.SetPropertyValue(LAST_MESSAGE, GetType(), message);
}
}
/// <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");
}
}
}