Skip to content

Commit

Permalink
Fix #4 - Improved handling of SimConnect.cfg (#5)
Browse files Browse the repository at this point in the history
* Specify location of SimConnect.cfg, option for using predefined config.

* Added support for specifying config index in SimConnect.cfg
  • Loading branch information
TimianHeber authored Oct 16, 2020
1 parent da00377 commit 45c8fce
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 33 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ A simple easy-to-use wrapper for the Flight Simulator 2020 SimConnect library. A
If, on the other hand, you just want to connect to Flight Simulator and read some information this may give you a quicker start.

FsConnect uses the _Microsoft.FlightSimulator.SimConnect_ .NET Framework library and the underlying native x64 _simconnect.dll_ library.
These files are distributed via the Flight Simulator 2020 SDK, currently version 0.5.1, but are included for easy use.
These files are distributed via the Flight Simulator 2020 SDK, currently version 0.6.1, but are included for easy use.

At the moment this project is intended as an easier to use wrapper than the current SimConnect for simple projects, creating a simpler C# programming model and reducing the need for repeated boiler plate code. Expect breaking changes.

## Current features
* Supports TCP connection, without use of SimConnect.cfg file
* Supports connections from API, without direct use of SimConnect.cfg file
* Supports registering and requesting simple simulation variables.
* Supports updating simulation variables.
* Does not require a Windows message pump.
Expand Down Expand Up @@ -59,6 +59,11 @@ namespace FsConnectTest
public static void Main()
{
FsConnect fsConnect = new FsConnect();

// Specify where the SimConnect.cfg should be written to
fsConnect.SimConnectFileLocation = SimConnectFileLocation.MyDocuments;

// Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
fsConnect.Connect("TestApp", "localhost", 500);
fsConnect.FsDataReceived += HandleReceivedFsData;

Expand Down
7 changes: 5 additions & 2 deletions src/CTrue.FsConnect.TestConsole/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ namespace CTrue.FsConnect.TestConsole
{
public class Options
{
[Option('h', "hostname", Required = true, HelpText = "Sets the hostname of the host that is running Flight Simulator.")]
[Option('h', "hostname", SetName = "connect", HelpText = "Sets the hostname of the host that is running Flight Simulator.")]
public string Hostname { get; set; }

[Option('p', "port", Required = true, HelpText = "Sets the TCP port that Flight Simulator is being hosting on.")]
[Option('p', "port", SetName = "connect", HelpText = "Sets the TCP port that Flight Simulator is being hosting on.")]
public uint Port { get; set; }

[Option('i', "index", SetName = "config", HelpText = "Specifies the config index in SimConnect.cfg to use.")]
public uint ConfigIndex { get; set; }
}
}
14 changes: 12 additions & 2 deletions src/CTrue.FsConnect.TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ private static void Run(Options commandLineOptions)
{
_fsConnect = new FsConnect();

Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
try
{
_fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port);
if (string.IsNullOrEmpty(commandLineOptions.Hostname))
{
Console.WriteLine($"Connecting to Flight Simulator using index {commandLineOptions.ConfigIndex}");
_fsConnect.Connect("FsConnectTestConsole", commandLineOptions.ConfigIndex);
}
else
{
Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
_fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port, SimConnectProtocol.Ipv4);
}


}
catch (Exception e)
{
Expand Down
10 changes: 9 additions & 1 deletion src/CTrue.FsConnect.TestConsole/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"profiles": {
"FsConnectTestConsole": {
"commandName": "Project",
"commandLineArgs": "-h 192.168.1.174 -p 57490"
"commandLineArgs": "-h 127.0.0.1 -p 500"
},
"FsConnectTestConsole1": {
"commandName": "Project",
"commandLineArgs": "-i 0"
},
"FsConnectTestConsole2": {
"commandName": "Project",
"commandLineArgs": ""
}
}
}
10 changes: 6 additions & 4 deletions src/CTrue.FsConnect/CTrue.FsConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
<Product>Flight Simulator Connect</Product>
<Copyright />
<Description>An easy to use wrapper for SimConnect, for connection to Flight Simulator 2020.
Contains SimConnect binaries, as distributed by the Flight Simulator 20202 SDK 0.5.1 release.</Description>
Contains SimConnect binaries, as distributed by the Flight Simulator 20202 SDK 0.6.1 release.</Description>
<Authors>C-True</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/c-true/FsConnect</RepositoryUrl>
<PackageTags>msfs flight-simulator simconnect</PackageTags>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>Simplified registering data definitions.
Added support for updating data structures.</PackageReleaseNotes>
<PackageReleaseNotes>Support for using predefined SimConnect.cfg or specifying location for where it should be written.
Verified that dependencies are the same for the SDK 0.6.1 release.</PackageReleaseNotes>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
53 changes: 42 additions & 11 deletions src/CTrue.FsConnect/FsConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CTrue.FsConnect
public class FsConnect : IFsConnect
{
private SimConnect _simConnect = null;

private EventWaitHandle _simConnectEventHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
private Thread _simConnectReceiveThread = null;
private bool _connected;
Expand All @@ -34,14 +34,17 @@ public bool Connected
get => _connected;
private set
{
if(_connected != value)
if (_connected != value)
{
_connected = value;
ConnectionChanged?.Invoke(this, EventArgs.Empty);
}
}
}

/// <inheritdoc />
public SimConnectFileLocation SimConnectFileLocation { get; set; } = SimConnectFileLocation.MyDocuments;

/// <inheritdoc />
public event EventHandler ConnectionChanged;

Expand All @@ -52,15 +55,11 @@ private set
public event EventHandler<FsErrorEventArgs> FsError;

/// <inheritdoc />
public void Connect(string applicationName, string hostName, uint port)
public void Connect(string applicationName, uint configIndex = 0)
{
if (applicationName == null) throw new ArgumentNullException(nameof(applicationName));

CreateSimConnectConfigFile(hostName, port);

try
{
_simConnect = new SimConnect(applicationName, IntPtr.Zero, 0, _simConnectEventHandle, 0);
_simConnect = new SimConnect(applicationName, IntPtr.Zero, 0, _simConnectEventHandle, configIndex);
}
catch (Exception e)
{
Expand All @@ -79,6 +78,16 @@ public void Connect(string applicationName, string hostName, uint port)
_simConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimobjectDataBytype);
}

/// <inheritdoc />
public void Connect(string applicationName, string hostName, uint port, SimConnectProtocol protocol)
{
if (applicationName == null) throw new ArgumentNullException(nameof(applicationName));

CreateSimConnectConfigFile(hostName, port, protocol);

Connect(applicationName);
}

/// <inheritdoc />
public void Disconnect()
{
Expand Down Expand Up @@ -188,18 +197,40 @@ private void SimConnect_MessageReceiveThreadHandler()
}
}

private void CreateSimConnectConfigFile(string hostName, uint port)
private void CreateSimConnectConfigFile(string hostName, uint port, SimConnectProtocol protocol)
{
try
{
StringBuilder sb = new StringBuilder();

string protocolString = "Ipv4";

switch (protocol)
{
case SimConnectProtocol.Pipe:
protocolString = "Pipe";
break;
case SimConnectProtocol.Ipv4:
protocolString = "Ipv4";
break;
case SimConnectProtocol.Ipv6:
protocolString = "Ipv6";
break;
}

sb.AppendLine("[SimConnect]");
sb.AppendLine("Protocol=IPv4");
sb.AppendLine("Protocol=" + protocolString);
sb.AppendLine($"Address={hostName}");
sb.AppendLine($"Port={port}");

string fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SimConnect.cfg");
string directory = "";
if (SimConnectFileLocation == SimConnectFileLocation.Local)
directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
else
directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

string fileName = Path.Combine(directory, "SimConnect.cfg");

File.WriteAllText(fileName, sb.ToString());
}
catch (Exception e)
Expand Down
33 changes: 23 additions & 10 deletions src/CTrue.FsConnect/IFsConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace CTrue.FsConnect
{
/// <summary>
/// A wrapper / helper class for connection to Flight Simulator.
/// A wrapper / helper class for connection to Microsoft Flight Simulator.
/// </summary>
/// <remarks>
/// This wrapper supports TCP IPv4 only, for the moment.
/// The <see cref="IFsConnect"/> wraps the SimConnect.dll and managed
/// </remarks>
public interface IFsConnect : IDisposable
{
Expand All @@ -33,17 +33,30 @@ public interface IFsConnect : IDisposable
bool Connected { get; }

/// <summary>
/// Connects to Flight Simulator on the specified host name and TCP port.
/// Gets or sets where to write the SimConnect.cfg file, that specifies how to connect to Flight Simulator.
/// </summary>
/// <param name="applicationName"></param>
/// <param name="hostName"></param>
/// <param name="port"></param>
SimConnectFileLocation SimConnectFileLocation { get; set; }

/// <summary>
/// Connects to Flight Simulator using an existing SimConnect.cfg.
/// </summary>
/// <param name="applicationName">A name identifying this client to Flight Simulator.</param>
/// <param name="configIndex">The index to a specified configuration in the SimConnect.cfg file. Default is index 0.</param>
void Connect(string applicationName, uint configIndex = 0);

/// <summary>
/// Connects to Flight Simulator on the specified host name and port.
/// </summary>
/// <param name="applicationName">A name identifying this client to Flight Simulator.</param>
/// <param name="hostName">A hostname or IP address.</param>
/// <param name="port">A TCP or pipe port number.</param>
/// <param name="protocol">The protocol to use to connect to Flight Simulator.</param>
/// <remarks>
/// A SimConnect.cfg file will be generated containing TCP connection information.
/// Flight Simulator must be configured for remote TCP connections by editing the SimConnect.xml file that are part of the installation.
/// A SimConnect.cfg file will be generated containing connection information.
/// Flight Simulator must be configured for remote connections by editing the SimConnect.xml file that are part of the installation.
/// </remarks>
void Connect(string applicationName, string hostName, uint port);

void Connect(string applicationName, string hostName, uint port, SimConnectProtocol protocol);
/// <summary>
/// Disconnects from Flight Simulator.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/CTrue.FsConnect/SimConnectFileLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CTrue.FsConnect
{
public enum SimConnectFileLocation
{
/// <summary>
/// The SimConnect.cfg file will be written to the MyDocuments location.
/// </summary>
MyDocuments,
/// <summary>
/// The SimConnect.cfg file will be written to the same location that the application was started from.
/// </summary>
/// <remarks>
/// The application must have write access to this folder.
/// </remarks>
Local
}
}
28 changes: 28 additions & 0 deletions src/CTrue.FsConnect/SimConnectProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CTrue.FsConnect
{
/// <summary>
/// Specifies the protocol to use to connect to Flight Simulator.
/// </summary>
public enum SimConnectProtocol
{
/// <summary>
/// Named pipes
/// </summary>
Pipe,

/// <summary>
/// TCP.IP v4
/// </summary>
Ipv4,

/// <summary>
/// TCP.IP v6
/// </summary>
Ipv6
}
}
2 changes: 1 addition & 1 deletion src/CTrue.FsConnect/SimProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SimProperty(string name, string unit, SIMCONNECT_DATATYPE dataType)
/// <summary>
/// Creates an initialized instance using enums for known values.
/// </summary>
/// <param name="name"></param>
/// <param name="simVar"></param>
/// <param name="unit"></param>
/// <param name="dataType"></param>
public SimProperty(FsSimVar simVar, FsUnit unit, SIMCONNECT_DATATYPE dataType)
Expand Down

0 comments on commit 45c8fce

Please sign in to comment.