Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
Ports are configurable.
Browse files Browse the repository at this point in the history
Reads DSX port by default.
Some VSCodeLaunch Fixes.
Added Exception Handling.
  • Loading branch information
patmagauran committed Dec 28, 2021
1 parent 887d09d commit 3658716
Show file tree
Hide file tree
Showing 58 changed files with 148 additions and 598 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/ForzaDualSense/bin/Debug/net6.0/ForzaDualSense.exe",
"program": "${workspaceFolder}/ForzaDualSense/bin/Debug/net6.0/win-x64/ForzaDualSense.exe",
"args": [],
"cwd": "${workspaceFolder}/ForzaDualSense",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
3 changes: 2 additions & 1 deletion ForzaDualSense/ForzaDualSense.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<PublishSingleFile Condition="'$(Configuration)' == 'Release'">true</PublishSingleFile>

<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
Expand Down
191 changes: 140 additions & 51 deletions ForzaDualSense/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace ForzaDualSense
{
Expand Down Expand Up @@ -205,83 +206,152 @@ public static float Map(float x, float in_min, float in_max, float out_min, floa
}

private static DataPacket data = new DataPacket();
private const int FORZA_DATA_OUT_PORT = 5300;
static UdpClient senderClient;
static IPEndPoint endPoint;
//Connect to DualSenseX
static void Connect()
{
senderClient = new UdpClient();
endPoint = new IPEndPoint(Triggers.localhost, 6969);
var portNumber = File.ReadAllText(@"C:\Temp\DualSenseX\DualSenseX_PortNumber.txt");
Console.WriteLine("DSX is using port " + portNumber + ". Attempting to connect..");
int portNum = settings.DSX_PORT;
if (portNumber != null)
{
try
{
portNum = Convert.ToInt32(portNumber);
}
catch (FormatException e)
{
Console.WriteLine($"DSX provided a non numerical Port! Using configured default({settings.DSX_PORT}).");
portNum = settings.DSX_PORT;
}
}
else
{
Console.WriteLine($"DSX did not provided a port value. Using configured default({settings.DSX_PORT})");
}

endPoint = new IPEndPoint(Triggers.localhost, Convert.ToInt32(portNumber));
try
{
senderClient.Connect(endPoint);
}
catch (Exception e)
{
Console.Write("Error Connecting: ");

if (e is SocketException)
{
Console.WriteLine("Couldn't Access Port. " + e.Message);
}
else if (e is ObjectDisposedException)
{
Console.WriteLine("Connection Object Closed. Restart the Application.");
}
else
{
Console.WriteLine("Unknown Error: " + e.Message);
}
throw e;
}
}
//Send Data to DualSenseX
static void Send(Packet data)
{
var RequestData = Encoding.ASCII.GetBytes(Triggers.PacketToJson(data));
senderClient.Send(RequestData, RequestData.Length, endPoint);
}

//Main running thread of program.
static async Task Main(string[] args)
{
// Build a config object, using env vars and JSON providers.
IConfiguration config = new ConfigurationBuilder()
.AddIniFile("appsettings.ini")
.Build();
try
{
// Get values from the config given their key and their target type.
settings = config.Get<Settings>();
senderClient.Send(RequestData, RequestData.Length);
}
catch (Exception e)
{
Console.WriteLine("Invalid Configuration File!");
Console.WriteLine(e.Message);
return;
Console.Write("Error Sending Message: ");

if (e is SocketException)
{
Console.WriteLine("Couldn't Access Port. " + e.Message);
throw e;
}
else if (e is ObjectDisposedException)
{
Console.WriteLine("Connection closed. Restarting...");
Connect();
}
else
{
Console.WriteLine("Unknown Error: " + e.Message);

}

}
if (!settings.DISABLE_APP_CHECK)
}

//Main running thread of program.
static async Task Main(string[] args)
{
IPEndPoint ipEndPoint = null;
UdpClient client = null;
try
{
int forzaProcesses = Process.GetProcessesByName("ForzaHorizon 5").Length;
forzaProcesses += Process.GetProcessesByName("ForzaHorizon4").Length;
forzaProcesses += Process.GetProcessesByName("ForzaMotorsport7").Length;
Process[] DSX = Process.GetProcessesByName("DualSenseX");
Process[] cur = Process.GetProcesses();
while (forzaProcesses == 0 || DSX.Length == 0)
// Build a config object, using env vars and JSON providers.
IConfiguration config = new ConfigurationBuilder()
.AddIniFile("appsettings.ini")
.Build();
try
{
if (forzaProcesses == 0)
{
Console.WriteLine("No Running Instances of Forza found. Waiting... ");

}
else if (DSX.Length == 0)
// Get values from the config given their key and their target type.
settings = config.Get<Settings>();
}
catch (Exception e)
{
Console.WriteLine("Invalid Configuration File!");
Console.WriteLine(e.Message);
return;
}
if (!settings.DISABLE_APP_CHECK)
{
int forzaProcesses = Process.GetProcessesByName("ForzaHorizon 5").Length;
forzaProcesses += Process.GetProcessesByName("ForzaHorizon4").Length;
forzaProcesses += Process.GetProcessesByName("ForzaMotorsport7").Length;
Process[] DSX = Process.GetProcessesByName("DualSenseX");
Process[] cur = Process.GetProcesses();
while (forzaProcesses == 0 || DSX.Length == 0)
{
Console.WriteLine("No Running Instances of DualSenseX found. Waiting... ");
if (forzaProcesses == 0)
{
Console.WriteLine("No Running Instances of Forza found. Waiting... ");

}
else if (DSX.Length == 0)
{
Console.WriteLine("No Running Instances of DualSenseX found. Waiting... ");
}
System.Threading.Thread.Sleep(1000);
forzaProcesses += Process.GetProcessesByName("ForzaHorizon5").Length;
forzaProcesses += Process.GetProcessesByName("ForzaHorizon4").Length; //Guess at name
forzaProcesses += Process.GetProcessesByName("ForzaMotorsport7").Length; //Guess at name
DSX = Process.GetProcessesByName("DualSenseX");
}
System.Threading.Thread.Sleep(1000);
forzaProcesses += Process.GetProcessesByName("ForzaHorizon5").Length;
forzaProcesses += Process.GetProcessesByName("ForzaHorizon4").Length; //Guess at name
forzaProcesses += Process.GetProcessesByName("ForzaMotorsport7").Length; //Guess at name
DSX = Process.GetProcessesByName("DualSenseX");
Console.WriteLine("Forza and DSX are running. Let's Go!");
}
Console.WriteLine("Forza and DSX are running. Let's Go!");
}
//Connect to DualSenseX
Connect();

//Connect to Forza
var ipEndPoint = new IPEndPoint(IPAddress.Loopback, FORZA_DATA_OUT_PORT);
var client = new UdpClient(FORZA_DATA_OUT_PORT);
//Connect to DualSenseX
Connect();

Console.WriteLine("The Program is running. Please set the Forza data out to 127.0.0.1, port 5300 and the DualSenseX UDP Port to 6969");
//Connect to Forza
ipEndPoint = new IPEndPoint(IPAddress.Loopback, settings.FORZA_PORT);
client = new UdpClient(settings.FORZA_PORT);

//Main loop, go until killed
while (true)
{
//If Forza sends an update
await client.ReceiveAsync().ContinueWith(receive =>
Console.WriteLine($"The Program is running. Please set the Forza data out to 127.0.0.1, port {settings.FORZA_PORT} and verify the DualSenseX UDP Port is set to {settings.DSX_PORT}");
UdpReceiveResult receive;
//Main loop, go until killed
while (true)
{
//If Forza sends an update
receive = await client.ReceiveAsync();
//parse data
var resultBuffer = receive.Result.Buffer;
var resultBuffer = receive.Buffer;
if (!AdjustToBufferType(resultBuffer.Length))
{
// return;
Expand All @@ -291,10 +361,29 @@ await client.ReceiveAsync().ContinueWith(receive =>
//Process and send data to DualSenseX
SendData(data);

});
}

}
catch (Exception e)
{
Console.WriteLine("Application encountered an exception: " + e.Message);
}
finally
{
if (client != null)
{
client.Close();
client.Dispose();
}
if (senderClient != null)
{
senderClient.Close();
senderClient.Dispose();
}


}
return;

}

Expand Down
2 changes: 2 additions & 0 deletions ForzaDualSense/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class Settings
public int MIN_BRAKE_RESISTANCE { get; set; } = 1;//The Minimum resistance on the Brake (0-7)
public int ACCELRATION_LIMIT { get; set; } = 10; //The upper end acceleration when calculating the throttle resistance. Any acceleration above this will be counted as this value when determining the throttle resistance.
public bool DISABLE_APP_CHECK { get; set; } = false; //Should we disable the check for running applications?
public int DSX_PORT { get; set; } = 6969; //Port for DSX Port Listener
public int FORZA_PORT { get; set; } = 5300; //Port for Forza UDP server
}
}
4 changes: 3 additions & 1 deletion ForzaDualSense/appsettings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ MIN_THROTTLE_RESISTANCE=1
MIN_BRAKE_RESISTANCE=1
;The upper end acceleration when calculating the throttle resistance. Any acceleration above this will be counted as this value when determining the throttle resistance.
ACCELRATION_LIMIT=10
DISABLE_APP_CHECK=true
DISABLE_APP_CHECK=true
DSX_PORT=6969
FORZA_PORT=5300
41 changes: 0 additions & 41 deletions ForzaDualSense/bin/Debug/net6.0/ForzaCore.deps.json

This file was deleted.

Binary file removed ForzaDualSense/bin/Debug/net6.0/ForzaCore.dll
Binary file not shown.
Binary file removed ForzaDualSense/bin/Debug/net6.0/ForzaCore.exe
Binary file not shown.
Binary file removed ForzaDualSense/bin/Debug/net6.0/ForzaCore.pdb
Binary file not shown.
9 changes: 0 additions & 9 deletions ForzaDualSense/bin/Debug/net6.0/ForzaCore.runtimeconfig.json

This file was deleted.

Loading

0 comments on commit 3658716

Please sign in to comment.