Class RF2MemoryReader
- Namespace
- rF2SharedMemoryNet
- Assembly
- rF2SharedMemoryNet.dll
Provides functionality to read various types of data from memory-mapped files used by rFactor2.
public sealed class RF2MemoryReader : IDisposable
- Inheritance
-
RF2MemoryReader
- Implements
- Inherited Members
Remarks
The RF2MemoryReader class is designed to interface with rFactor2's shared memory plugin, allowing access to telemetry, scoring, rules, force feedback, graphics, pit information, weather, and other data. It attempts to open the necessary memory-mapped files upon instantiation and provides methods to read data synchronously and asynchronously. The class implements IDisposable to ensure proper release of resources.
Important: Always call Dispose() when you are done using this class to release memory-mapped file resources. If you need to read data from a new instance, create a new RF2MemoryReader object.
Constructors
RF2MemoryReader(ILogger?, bool)
Initializes a new instance of the RF2MemoryReader class, which reads memory-mapped files for rFactor2 or Le Mans Ultimate telemetry data.
public RF2MemoryReader(ILogger? logger = null, bool enableDMA = false)
Parameters
logger
ILoggerAn optional logger for capturing error messages and operational logs. Can be null if logging is not required.
enableDMA
boolA boolean value indicating whether to enable Direct Memory Access (DMA) for reading data. If true, attempts to initialize the LMU Memory Reader.
Remarks
This constructor attempts to open several memory-mapped files required for reading telemetry and other data from the rFactor2 or Le Mans Ultimate game. If the game is not running or the necessary plugins are not installed, errors will be logged if a logger is provided.
Exceptions
- InvalidOperationException
Thrown if the LMU Memory Reader fails to initialize when
enableDMA
is true. Ensure the game is running and the LMU plugin is installed.
Properties
IsDisposed
Gets a value indicating whether the object has been disposed.
public bool IsDisposed { get; }
Property Value
Methods
Dispose()
Disposes of the resources used by the RF2MemoryReader.
public void Dispose()
Remarks
Important: Call this method when you are done using the RF2MemoryReader to release memory-mapped file resources. Failure to do so may result in resource leaks.
If you need to read data from a new instance, create a new RF2MemoryReader object after disposing of the current one.
GetExtended()
Retrieves the extended telemetry data.
public ExtendedTelemetry? GetExtended()
Returns
- ExtendedTelemetry?
An instance of ExtendedTelemetry containing the extended telemetry data, or null if the data is unavailable.
GetExtendedAsync(CancellationToken)
Asynchronously retrieves extended telemetry data.
public Task<ExtendedTelemetry?> GetExtendedAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<ExtendedTelemetry?>
A task that represents the asynchronous operation. The task result contains the extended telemetry data, or null if the data is not available.
Remarks
This method fetches data from a predefined source and returns it as an ExtendedTelemetry object. The operation can be canceled by passing a cancellation token.
GetForceFeedback()
Retrieves the force feedback configuration data.
public ForceFeedback? GetForceFeedback()
Returns
- ForceFeedback?
An instance of ForceFeedback containing the configuration data, or null if the data is unavailable.
GetForceFeedbackAsync(CancellationToken)
Asynchronously retrieves the force feedback data.
public Task<ForceFeedback?> GetForceFeedbackAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<ForceFeedback?>
A task that represents the asynchronous operation. The task result contains the force feedback data, or null if the data is not available.
GetGraphics()
Retrieves the graphics data from the specified graphics file.
public Graphics? GetGraphics()
Returns
- Graphics?
An instance of Graphics containing the graphics data, or null if the data cannot be retrieved.
GetGraphicsAsync(CancellationToken)
Asynchronously retrieves graphics data from a specified source.
public Task<Graphics?> GetGraphicsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<Graphics?>
A task that represents the asynchronous operation. The task result contains the graphics data if available; otherwise, null.
Remarks
This method fetches graphics data asynchronously and can be canceled using the
provided cancellationToken
.
GetHWControl()
Retrieves the hardware control configuration from the specified data source.
public HardwareControl? GetHWControl()
Returns
- HardwareControl?
An instance of HardwareControl representing the current hardware control settings, or null if the configuration cannot be retrieved.
GetHWControlAsync(CancellationToken)
Asynchronously retrieves the hardware control configuration.
public Task<HardwareControl?> GetHWControlAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<HardwareControl?>
A task that represents the asynchronous operation. The task result contains the hardware control configuration, or null if the configuration is not available.
GetLMUElectronics()
Retrieves the electronics configuration from the LMU memory reader.
public Electronics GetLMUElectronics()
Returns
- Electronics
The Electronics object representing the current configuration of car electronics.
Exceptions
- InvalidOperationException
Thrown if the LMU memory reader is not initialized. Ensure that the DMA is enabled when creating this object.
GetPitInfo()
Retrieves information about the pit from a data source.
public PitInfo? GetPitInfo()
Returns
GetPitInfoAsync(CancellationToken)
Asynchronously retrieves the pit information from the specified data source.
public Task<PitInfo?> GetPitInfoAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<PitInfo?>
A task that represents the asynchronous operation. The task result contains the PitInfo object if the data is successfully retrieved; otherwise, null.
Remarks
This method fetches the pit information using an asynchronous operation. It is designed to be non-blocking and can be cancelled by passing a cancellation token.
GetPluginControl()
Retrieves the plugin control configuration.
public PluginControl? GetPluginControl()
Returns
- PluginControl?
An instance of PluginControl representing the plugin control configuration, or null if the configuration is not available.
GetPluginControlAsync(CancellationToken)
Asynchronously retrieves the plugin control data.
public Task<PluginControl?> GetPluginControlAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<PluginControl?>
A task that represents the asynchronous operation. The task result contains the PluginControl object if available; otherwise, null.
Remarks
This method fetches the plugin control data from a predefined source. If the data is not available, the method returns null.
GetRules()
Retrieves the rules configuration from the specified data source.
public Rules? GetRules()
Returns
- Rules?
An instance of Rules containing the rules configuration, or null if the rules cannot be retrieved.
GetRulesAsync(CancellationToken)
Asynchronously retrieves the rules from the specified data source.
public Task<Rules?> GetRulesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<Rules?>
A task that represents the asynchronous operation. The task result contains the retrieved Rules object, or null if the rules cannot be found.
Remarks
This method fetches the rules data asynchronously, allowing for cancellation through
the provided cancellationToken
.
GetRulesControl()
Retrieves the RulesControl object from the specified data source.
public RulesControl? GetRulesControl()
Returns
- RulesControl?
An instance of RulesControl if the data is successfully retrieved; otherwise, null.
Remarks
This method attempts to load the RulesControl from a predefined file. If the file does not exist or the data cannot be parsed, the method returns null.
GetRulesControlAsync(CancellationToken)
Asynchronously retrieves the rules control data.
public Task<RulesControl?> GetRulesControlAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<RulesControl?>
A task that represents the asynchronous operation. The task result contains the RulesControl object if available; otherwise, null.
GetScoring()
Retrieves the scoring data from the specified scoring file.
public Scoring? GetScoring()
Returns
- Scoring?
An instance of Scoring containing the scoring data, or null if the data cannot be retrieved.
GetScoringAsync(CancellationToken)
Asynchronously retrieves the scoring data.
public Task<Scoring?> GetScoringAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests.
Returns
- Task<Scoring?>
A task representing the asynchronous operation. The task result contains the scoring data, or null if the data is not available.
GetTelemetry()
Retrieves the telemetry data from the specified file.
public Telemetry? GetTelemetry()
Returns
Remarks
This method attempts to read telemetry data from a predefined file. If the file does not exist or the data cannot be read, the method returns null.
GetTelemetryAsync(CancellationToken)
Asynchronously retrieves telemetry data from a specified source.
public Task<Telemetry?> GetTelemetryAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<Telemetry?>
A task that represents the asynchronous operation. The task result contains the telemetry data, or null if no data is available.
Remarks
This method fetches telemetry data, which may include performance metrics or usage statistics, from a predefined source. The operation can be canceled by passing a cancellation token.
GetWeather()
Retrieves the current weather data.
public Weather? GetWeather()
Returns
- Weather?
An instance of Weather containing the current weather information, or null if the data is unavailable.
GetWeatherAsync(CancellationToken)
Asynchronously retrieves the current weather data.
public Task<Weather?> GetWeatherAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<Weather?>
A task representing the asynchronous operation. The task result contains the current weather data, or null if the data is unavailable.
Remarks
This method fetches weather data from a predefined source. It supports cancellation
through the provided cancellationToken
.
GetWeatherControl()
Retrieves the current weather control settings.
public WeatherControl? GetWeatherControl()
Returns
- WeatherControl?
An instance of WeatherControl representing the current weather control settings, or null if the settings cannot be retrieved.
GetWeatherControlAsync(CancellationToken)
Asynchronously retrieves the current weather control settings.
public Task<WeatherControl?> GetWeatherControlAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA token to monitor for cancellation requests. The default value is None.
Returns
- Task<WeatherControl?>
A task that represents the asynchronous operation. The task result contains the current WeatherControl settings, or null if the settings could not be retrieved.
Remarks
This method fetches the weather control data from a predefined source. If the operation is canceled, the task will be completed with a TaskCanceledException.