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

Commit

Permalink
Made a Settings File. Added check to see if game and DSX are running.
Browse files Browse the repository at this point in the history
  • Loading branch information
patmagauran committed Dec 28, 2021
1 parent 7d28df5 commit 5093948
Show file tree
Hide file tree
Showing 30 changed files with 770 additions and 30 deletions.
8 changes: 8 additions & 0 deletions ForzaDualSense/ForzaDualSense.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="appsettings.ini">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

Expand Down
76 changes: 53 additions & 23 deletions ForzaDualSense/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Diagnostics;
using Microsoft.Extensions.Configuration;

namespace ForzaDualSense
{
class Program
{
private const float GRIP_LOSS_VAL = 0.5f; //The point at which the brake will begin to become choppy
private const int MAX_BRAKE_VIBRATION = 35; //The maximum brake frequency in Hz (avoid over 40). COrrelates to better grip
private const float TURN_ACCEL_MOD = 0.5f; //How to scale turning acceleration in determining throttle stiffness.
private const float FORWARD_ACCEL_MOD = 1.0f;//How to scale Forward acceleration in determining throttle stiffness.
private const int MIN_BRAKE_STIFFNESS = 200; //On a scale of 1-200 with 1 being most stiff
private const int MAX_BRAKE_STIFFNESS = 1; //On a scale of 1-200 with 1 being most stiff
private const int BRAKE_VIBRATION_START = 20; //The position (0-255) at which the brake should feel engaged with low grip surfaces
private const int MAX_THROTTLE_RESISTANCE = 6; //The Maximum resistance on the throttle (0-7)
private const int MAX_BRAKE_RESISTANCE = 6;//The Maximum resistance on the Brake (0-7)
private const int MIN_THROTTLE_RESISTANCE = 1;//The Minimum resistance on the throttle (0-7)
private const int MIN_BRAKE_RESISTANCE = 1;//The Minimum resistance on the Brake (0-7)
private const int ACCELRATION_LIMIT = 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.
static Settings settings = new Settings();

//This sends the data to DualSenseX based on the input parsed data from Forza.
//See DataPacket.cs for more details about what forza parameters can be accessed.
Expand All @@ -39,16 +30,16 @@ static void SendData(DataPacket data)
//Set the updates for the right Trigger(Throttle)
p.instructions[2].type = InstructionType.TriggerUpdate;
//It should probably always be uniformly stiff
float avgAccel = (float)Math.Sqrt((TURN_ACCEL_MOD * (data.AccelerationX * data.AccelerationX)) + (FORWARD_ACCEL_MOD * (data.AccelerationZ * data.AccelerationZ)));
resistance = (int)Math.Floor(Map(avgAccel, 0, ACCELRATION_LIMIT, MIN_THROTTLE_RESISTANCE, MAX_THROTTLE_RESISTANCE));
float avgAccel = (float)Math.Sqrt((settings.TURN_ACCEL_MOD * (data.AccelerationX * data.AccelerationX)) + (settings.FORWARD_ACCEL_MOD * (data.AccelerationZ * data.AccelerationZ)));
resistance = (int)Math.Floor(Map(avgAccel, 0, settings.ACCELRATION_LIMIT, settings.MIN_THROTTLE_RESISTANCE, settings.MAX_THROTTLE_RESISTANCE));

p.instructions[2].parameters = new object[] { controllerIndex, Trigger.Right, TriggerMode.Resistance, 0, resistance };


//Update the left(Brake) trigger
p.instructions[0].type = InstructionType.TriggerUpdate;
//By default, Increasingly resistant to force
resistance = (int)Math.Floor(Map(data.Brake, 0, 1, MIN_BRAKE_RESISTANCE, MAX_BRAKE_RESISTANCE));
resistance = (int)Math.Floor(Map(data.Brake, 0, 1, settings.MIN_BRAKE_RESISTANCE, settings.MAX_BRAKE_RESISTANCE));

p.instructions[0].parameters = new object[] { controllerIndex, Trigger.Left, TriggerMode.Resistance, 0, resistance };

Expand All @@ -63,13 +54,13 @@ static void SendData(DataPacket data)

}
//Some grip lost, begin to vibrate according to the amount of grip lost
else if (combinedTireSlip > GRIP_LOSS_VAL)
else if (combinedTireSlip > settings.GRIP_LOSS_VAL)
{
int freq = MAX_BRAKE_VIBRATION - (int)Math.Floor(Map(combinedTireSlip, GRIP_LOSS_VAL, 1, 0, MAX_BRAKE_VIBRATION));
resistance = MIN_BRAKE_STIFFNESS - (int)Math.Floor(Map(data.Brake, 0, 1, MAX_BRAKE_STIFFNESS, MIN_BRAKE_STIFFNESS));
int freq = settings.MAX_BRAKE_VIBRATION - (int)Math.Floor(Map(combinedTireSlip, settings.GRIP_LOSS_VAL, 1, 0, settings.MAX_BRAKE_VIBRATION));
resistance = settings.MIN_BRAKE_STIFFNESS - (int)Math.Floor(Map(data.Brake, 0, 1, settings.MAX_BRAKE_STIFFNESS, settings.MIN_BRAKE_STIFFNESS));

//Set left trigger to the custom mode VibrateResitance with values of Frequency = freq, Stiffness = 104, startPostion = 76.
p.instructions[0].parameters = new object[] { controllerIndex, Trigger.Left, TriggerMode.CustomTriggerValue, CustomTriggerValueMode.VibrateResistance, freq, resistance, BRAKE_VIBRATION_START, 0, 0, 0, 0 };
p.instructions[0].parameters = new object[] { controllerIndex, Trigger.Left, TriggerMode.CustomTriggerValue, CustomTriggerValueMode.VibrateResistance, freq, resistance, settings.BRAKE_VIBRATION_START, 0, 0, 0, 0 };

}

Expand Down Expand Up @@ -221,7 +212,7 @@ public static float Map(float x, float in_min, float in_max, float out_min, floa
static void Connect()
{
senderClient = new UdpClient();
endPoint = new IPEndPoint(Triggers.localhost, 6750);
endPoint = new IPEndPoint(Triggers.localhost, 6969);
}
//Send Data to DualSenseX
static void Send(Packet data)
Expand All @@ -233,15 +224,55 @@ static void Send(Packet data)
//Main running thread of program.
static async Task Main(string[] args)
{
#region udp stuff
// 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>();
}
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)
{
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");
}
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);

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 6750");
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");

//Main loop, go until killed
while (true)
Expand All @@ -264,7 +295,6 @@ await client.ReceiveAsync().ContinueWith(receive =>
}


#endregion

}

Expand Down
19 changes: 19 additions & 0 deletions ForzaDualSense/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace ForzaDualSense
{
public class Settings
{
public float GRIP_LOSS_VAL { get; set; } = 0.5f; //The point at which the brake will begin to become choppy
public int MAX_BRAKE_VIBRATION { get; set; } = 35; //The maximum brake frequency in Hz (avoid over 40). COrrelates to better grip
public float TURN_ACCEL_MOD { get; set; } = 0.5f; //How to scale turning acceleration in determining throttle stiffness.
public float FORWARD_ACCEL_MOD { get; set; } = 1.0f;//How to scale Forward acceleration in determining throttle stiffness.
public int MIN_BRAKE_STIFFNESS { get; set; } = 200; //On a scale of 1-200 with 1 being most stiff
public int MAX_BRAKE_STIFFNESS { get; set; } = 1; //On a scale of 1-200 with 1 being most stiff
public int BRAKE_VIBRATION_START { get; set; } = 20; //The position (0-255) at which the brake should feel engaged with low grip surfaces
public int MAX_THROTTLE_RESISTANCE { get; set; } = 6; //The Maximum resistance on the throttle (0-7)
public int MAX_BRAKE_RESISTANCE { get; set; } = 6;//The Maximum resistance on the Brake (0-7)
public int MIN_THROTTLE_RESISTANCE { get; set; } = 1;//The Minimum resistance on the throttle (0-7)
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?
}
}
25 changes: 25 additions & 0 deletions ForzaDualSense/appsettings.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
;The point at which the brake will begin to become choppy
GRIP_LOSS_VAL=0.5
;The maximum brake frequency in Hz (avoid over 40). COrrelates to better grip
MAX_BRAKE_VIBRATION=35
;How to scale turning acceleration in determining throttle stiffness.
TURN_ACCEL_MOD=0.5
;How to scale Forward acceleration in determining throttle stiffness.
FORWARD_ACCEL_MOD=1.0
;On a scale of 1-200 with 1 being most stiff
MIN_BRAKE_STIFFNESS=200
;On a scale of 1-200 with 1 being most stiff
MAX_BRAKE_STIFFNESS=1
;The position (0-255) at which the brake should feel engaged with low grip surfaces
BRAKE_VIBRATION_START=20
;The Maximum resistance on the throttle (0-7)
MAX_THROTTLE_RESISTANCE=6
;The Maximum resistance on the Brake (0-7)
MAX_BRAKE_RESISTANCE=6
;The Minimum resistance on the throttle (0-7)
MIN_THROTTLE_RESISTANCE=1
;The Minimum resistance on the Brake (0-7)
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
Loading

0 comments on commit 5093948

Please sign in to comment.