1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-24 23:00:12 +00:00
msfs-popout-panel-manager/SimconnectAgent/SimConnector.cs

341 lines
13 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using Microsoft.FlightSimulator.SimConnect;
using MSFSPopoutPanelManager.Shared;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
2022-08-13 06:14:49 +00:00
using System.Threading.Tasks;
2022-07-23 19:23:32 +00:00
using System.Timers;
namespace MSFSPopoutPanelManager.SimConnectAgent
{
public class SimConnector
{
private const int MSFS_CONNECTION_RETRY_TIMEOUT = 2000;
private const int WM_USER_SIMCONNECT = 0x0402;
private SimConnect _simConnect;
private Timer _connectionTimer;
2022-08-13 06:14:49 +00:00
private bool _isDisabledReconnect;
2022-07-23 19:23:32 +00:00
public event EventHandler<string> OnException;
public event EventHandler<List<SimConnectDataDefinition>> OnReceivedData;
public event EventHandler OnConnected;
public event EventHandler OnDisconnected;
public event EventHandler<SimConnectSystemEvent> OnReceiveSystemEvent;
public bool Connected { get; set; }
public List<SimConnectDataDefinition> SimConnectDataDefinitions;
public void Start()
{
_connectionTimer = new Timer();
_connectionTimer.Interval = MSFS_CONNECTION_RETRY_TIMEOUT;
_connectionTimer.Enabled = true;
_connectionTimer.Elapsed += (source, e) =>
{
try
{
InitializeSimConnect();
}
catch
{
// handle SimConnect instantiation error when MSFS is not connected
}
};
}
public void Restart()
{
_connectionTimer.Enabled = true;
}
private void HandleOnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
ReceiveMessage();
}
public void StopAndReconnect()
{
Stop();
InitializeSimConnect();
}
public void Stop()
{
if (_simConnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
_simConnect.Dispose();
_simConnect = null;
}
Connected = false;
}
public void RequestData()
{
if (_simConnect == null || !Connected)
return;
foreach (var definition in SimConnectDataDefinitions)
{
2022-08-13 06:14:49 +00:00
if (definition.DataDefinitionType == DataDefinitionType.SimConnect)
_simConnect.RequestDataOnSimObjectType(definition.RequestId, definition.DefineId, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
2022-07-23 19:23:32 +00:00
}
}
public void ReceiveMessage()
{
if (_simConnect == null)
return;
try
{
2022-08-13 06:14:49 +00:00
if (!_isDisabledReconnect)
_simConnect.ReceiveMessage();
2022-07-23 19:23:32 +00:00
}
catch (Exception ex)
{
if (ex.Message != "0xC00000B0")
{
FileLogger.WriteLog($"SimConnector: SimConnect receive message exception - {ex.Message}", StatusMessageType.Error);
2022-08-13 06:14:49 +00:00
}
2022-07-23 19:23:32 +00:00
2022-08-13 06:14:49 +00:00
if (!_isDisabledReconnect)
{
// Prevent multiple reconnects from running
_isDisabledReconnect = true;
2022-07-23 19:23:32 +00:00
// Need to stop and reconnect server since the data is SimConnect connection or data is probably corrupted.
StopAndReconnect();
}
}
}
public void TransmitActionEvent(ActionEvent eventID, uint data)
{
if (_simConnect != null)
{
try
{
_simConnect.TransmitClientEvent(0U, eventID, data, NotificationGroup.GROUP0, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
}
catch (Exception ex)
{
var eventName = eventID.ToString()[4..]; // trim out KEY_ prefix
FileLogger.WriteLog($"SimConnector: SimConnect transmit event exception - EventName: {eventName} - {ex.Message}", StatusMessageType.Error);
}
}
}
public void SetDataObject(SIMCONNECT_DEFINE_ID defineId, object dValue)
{
if (_simConnect != null)
{
try
{
_simConnect.SetDataOnSimObject(defineId, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, dValue);
}
catch (Exception ex)
{
FileLogger.WriteLog($"SimConnector: Unable to set SimConnect variable - {ex.Message}", StatusMessageType.Error);
}
}
}
private int _connectRetryCount = 0;
private void InitializeSimConnect()
{
StatusMessageWriter.WriteMessage($"Connecting to MSFS {new StringBuilder().Insert(0, ". ", _connectRetryCount++ % 20).ToString()}", StatusMessageType.Info, false);
_simConnect = new SimConnect("MSFS Pop Out Panel Manager", Process.GetCurrentProcess().MainWindowHandle, WM_USER_SIMCONNECT, null, 0);
_connectRetryCount = 0;
_connectionTimer.Enabled = false;
// Listen to connect and quit msgs
_simConnect.OnRecvOpen += HandleOnRecvOpen;
_simConnect.OnRecvQuit += HandleOnRecvQuit;
_simConnect.OnRecvException += HandleOnRecvException;
_simConnect.OnRecvEvent += HandleOnReceiveSystemEvent;
_simConnect.OnRecvSimobjectDataBytype += HandleOnRecvSimobjectDataBytype;
2022-08-01 23:21:42 +00:00
_simConnect.OnRecvEventFilename += HandleOnRecvEventFilename;
2022-08-02 15:51:28 +00:00
_simConnect.OnRecvSystemState += HandleOnRecvSystemState;
2022-07-23 19:23:32 +00:00
// Register simConnect system events
_simConnect.UnsubscribeFromSystemEvent(SimConnectSystemEvent.SIMSTART);
_simConnect.SubscribeToSystemEvent(SimConnectSystemEvent.SIMSTART, "SimStart");
_simConnect.UnsubscribeFromSystemEvent(SimConnectSystemEvent.SIMSTOP);
_simConnect.SubscribeToSystemEvent(SimConnectSystemEvent.SIMSTOP, "SimStop");
_simConnect.UnsubscribeFromSystemEvent(SimConnectSystemEvent.VIEW);
_simConnect.SubscribeToSystemEvent(SimConnectSystemEvent.VIEW, "View");
2022-08-01 23:21:42 +00:00
_simConnect.UnsubscribeFromSystemEvent(SimConnectSystemEvent.AIRCRAFTLOADED);
_simConnect.SubscribeToSystemEvent(SimConnectSystemEvent.AIRCRAFTLOADED, "AircraftLoaded");
2022-07-23 19:23:32 +00:00
AddDataDefinitions();
2022-08-02 15:51:28 +00:00
_simConnect.RequestSystemState(SystemStateRequestId.AIRCRAFTPATH, "AircraftLoaded");
2022-08-13 06:14:49 +00:00
_isDisabledReconnect = false;
Task.Run(() =>
{
for (var i = 0; i < 5; i++)
{
System.Threading.Thread.Sleep(1000);
ReceiveMessage();
}
});
2022-07-23 19:23:32 +00:00
OnConnected?.Invoke(this, null);
2022-08-13 06:14:49 +00:00
Connected = true;
2022-07-23 19:23:32 +00:00
StatusMessageWriter.WriteMessage("MSFS is connected", StatusMessageType.Info, false);
}
2022-08-02 15:51:28 +00:00
private void HandleOnRecvSystemState(SimConnect sender, SIMCONNECT_RECV_SYSTEM_STATE data)
{
switch ((SystemStateRequestId)Enum.Parse(typeof(SystemStateRequestId), data.dwRequestID.ToString()))
{
case SystemStateRequestId.AIRCRAFTPATH:
SetActiveAircraftTitle(data.szString);
break;
}
}
2022-08-01 23:21:42 +00:00
private void HandleOnRecvEventFilename(SimConnect sender, SIMCONNECT_RECV_EVENT_FILENAME data)
{
switch (data.uEventID)
{
case (uint)SimConnectSystemEvent.AIRCRAFTLOADED:
2022-08-02 15:51:28 +00:00
SetActiveAircraftTitle(data.szFileName);
2022-08-01 23:21:42 +00:00
break;
}
}
2022-07-23 19:23:32 +00:00
private void AddDataDefinitions()
{
int definitionId = 1;
int requestId = 1;
SimConnectDataDefinitions = DefaultSimConnectDataDefinition.GetDefinitions();
foreach (var definition in SimConnectDataDefinitions)
{
2022-08-01 23:21:42 +00:00
if (definition.DataDefinitionType == DataDefinitionType.SimConnect)
2022-07-23 19:23:32 +00:00
{
2022-08-01 23:21:42 +00:00
SIMCONNECT_DATATYPE simmConnectDataType;
switch (definition.DataType)
{
case DataType.String:
simmConnectDataType = SIMCONNECT_DATATYPE.STRING256;
break;
case DataType.Float64:
simmConnectDataType = SIMCONNECT_DATATYPE.FLOAT64;
break;
default:
simmConnectDataType = SIMCONNECT_DATATYPE.FLOAT64;
break;
}
_simConnect.AddToDataDefinition((SIMCONNECT_DEFINE_ID)definitionId, definition.VariableName, definition.SimConnectUnit, simmConnectDataType, 0.0f, SimConnect.SIMCONNECT_UNUSED);
// Assign definition id and request id back into definition object
definition.DefineId = (SIMCONNECT_DEFINE_ID)definitionId;
definition.RequestId = (SIMCONNECT_REQUEST)requestId;
if (definition.DataType == DataType.String)
_simConnect.RegisterDataDefineStruct<SimConnectStruct>(definition.DefineId);
else
_simConnect.RegisterDataDefineStruct<double>(definition.DefineId);
definitionId++;
requestId++;
2022-07-23 19:23:32 +00:00
}
}
// Setup SimEvent mapping
foreach (var item in Enum.GetValues(typeof(ActionEvent)))
{
if (item.ToString().StartsWith("KEY_"))
_simConnect.MapClientEventToSimEvent((ActionEvent)item, item.ToString()[4..]);
}
}
private void HandleOnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
Stop();
OnDisconnected?.Invoke(this, null);
}
private void HandleOnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
SIMCONNECT_EXCEPTION e = (SIMCONNECT_EXCEPTION)data.dwException;
switch (e)
{
case SIMCONNECT_EXCEPTION.DATA_ERROR:
case SIMCONNECT_EXCEPTION.NAME_UNRECOGNIZED:
case SIMCONNECT_EXCEPTION.ALREADY_CREATED:
case SIMCONNECT_EXCEPTION.UNRECOGNIZED_ID:
case SIMCONNECT_EXCEPTION.EVENT_ID_DUPLICATE:
break;
default:
OnException?.Invoke(this, $"SimConnector: SimConnect on recieve exception - {e}");
break;
}
}
private void HandleOnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
{
if (_simConnect == null || !Connected || SimConnectDataDefinitions == null)
return;
try
{
foreach (var definition in SimConnectDataDefinitions)
{
if (data.dwRequestID == (uint)definition.RequestId)
{
if (definition.DataType == DataType.String)
{
definition.Value = ((SimConnectStruct)data.dwData[0]).SValue;
}
else
{
definition.Value = (double)data.dwData[0];
}
}
}
OnReceivedData?.Invoke(this, SimConnectDataDefinitions);
}
catch (Exception ex)
{
FileLogger.WriteException($"SimConnector: SimConnect receive data exception - {ex.Message}", ex);
}
}
private void HandleOnReceiveSystemEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data)
{
var systemEvent = ((SimConnectSystemEvent)data.uEventID);
OnReceiveSystemEvent?.Invoke(this, systemEvent);
}
2022-08-02 15:51:28 +00:00
private void SetActiveAircraftTitle(string aircraftFilePath)
{
var filePathToken = aircraftFilePath.Split(@"\");
2022-08-13 06:14:49 +00:00
if (filePathToken.Length > 1)
{
var aircraftName = filePathToken[filePathToken.Length - 2];
aircraftName = aircraftName.Replace("_", " ").ToUpper();
SimConnectDataDefinitions.Find(s => s.PropName == "AircraftName").Value = aircraftName;
OnReceivedData?.Invoke(this, SimConnectDataDefinitions);
}
2022-08-02 15:51:28 +00:00
}
2022-07-23 19:23:32 +00:00
}
}