Skip to content

Commit a7aa6af

Browse files
committed
nsgp - fixed telemetry issues
1 parent f6877f7 commit a7aa6af

4 files changed

Lines changed: 161 additions & 85 deletions

File tree

Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
3-
<ModId>13244593144621035959</ModId>
3+
<ModId>6001643931841748123</ModId>
44
<GameDir>$(APPDATA)\raicuparta\rai-pal\data\installed-mods\$(ModId)\</GameDir>
55
</PropertyGroup>
66

NewStarGPTelemetryMod.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
<RootNamespace>com.drowhunter.NewStarGPTelemetryMod</RootNamespace>
1616
</PropertyGroup>
1717

18-
<ItemGroup>
19-
<Compile Remove="com.drowhunter.sharedlib\**" />
20-
<Compile Remove="lib\**" />
21-
<EmbeddedResource Remove="com.drowhunter.sharedlib\**" />
22-
<EmbeddedResource Remove="lib\**" />
23-
<None Remove="com.drowhunter.sharedlib\**" />
18+
<ItemGroup>
19+
<Compile Remove="lib\**" />
20+
<EmbeddedResource Remove="lib\**" />
2421
<None Remove="lib\**" />
2522
</ItemGroup>
2623

2724
<ItemGroup>
2825
<None Remove=".git" />
2926
<None Remove=".gitignore" />
30-
<None Remove="Directory.Build.targets" />
3127
<None Remove="objectinspector.png" />
28+
<None Remove="README.md" />
3229
</ItemGroup>
3330

3431
<ItemGroup>
@@ -46,6 +43,9 @@
4643
<Reference Include="Assembly-CSharp">
4744
<HintPath>lib\Assembly-CSharp_publicized.dll</HintPath>
4845
</Reference>
46+
<Reference Include="netstandard">
47+
<HintPath>lib\netstandard.dll</HintPath>
48+
</Reference>
4949
</ItemGroup>
5050

5151

NewStarTelemetryData.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace com.drowhunter.NewStarGPTelemetryMod
4+
{
5+
[StructLayout(LayoutKind.Sequential)]
6+
internal struct NewStarTelemetryData
7+
{
8+
//public Quaternion Orientation;
9+
10+
public float Pitch;
11+
public float Yaw;
12+
public float Roll;
13+
14+
public float AngularVelocityX;
15+
public float AngularVelocityY;
16+
public float AngularVelocityZ;
17+
18+
public float cForce;
19+
20+
public float VelocityX;
21+
public float VelocityY;
22+
public float VelocityZ;
23+
24+
public float AccelX;
25+
public float AccelY;
26+
public float AccelZ;
27+
28+
/// <summary>
29+
/// Speed in Meter per Second
30+
/// </summary>
31+
public float Speed;
32+
public float RPM;
33+
public int CurrentGear;
34+
35+
public float TireFL;
36+
public float TireFR;
37+
public float TireBL;
38+
public float TireBR;
39+
40+
public int WheelsOnTrack;
41+
public bool IsBoosting;
42+
43+
internal bool AllowDriving;
44+
internal bool IsRacing;
45+
internal bool IsEventOver;
46+
47+
//public float WheelFL;
48+
//public float WheelFR;
49+
//public float WheelBL;
50+
//public float WheelBR;
51+
//public bool GamePaused;
52+
//public bool IsRacing;
53+
//public bool Boost;
54+
55+
56+
57+
58+
}
59+
60+
61+
}

NewStarTelemetryPlugin.cs

Lines changed: 92 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
using TelemetryLib;
1212

1313
using UnityEngine;
14+
using System.Collections.Generic;
15+
using System.Linq;
16+
using HarmonyLib;
17+
using UnityEngine.UIElements;
18+
using BepInEx.Configuration;
1419

1520
namespace com.drowhunter.NewStarGPTelemetryMod
1621
{
@@ -31,93 +36,129 @@ public class NewStarTelemetryPlugin : BaseUnityPlugin
3136

3237
RacingContextManager _racingContextManager;
3338

39+
private ConfigEntry<float> configAccelSmoothing;
3440

3541
CarControl _carControl
3642
{
3743
get
3844
{
39-
if (!_racingContextManager)
40-
{
45+
if (!_racingContextManager)
46+
{
4147
_racingContextManager = RacingContextManager.inst;
4248
}
4349

4450
return _racingContextManager?.SinglePlayer?.Control;
4551
}
4652
}
4753

54+
static bool paused = false;
4855

4956

57+
//[HarmonyPatch(typeof(PauseMenu), nameof(PauseMenu.Update))]
58+
//class Patch
59+
//{
60+
// static void Postfix(PauseMenu __instance)
61+
// {
62+
// paused = __instance.paused;
63+
64+
65+
66+
// }
67+
//}
68+
5069

5170
private void Awake()
5271
{
72+
//Harmony.CreateAndPatchAll(typeof(NewStarTelemetryPlugin));
73+
//var harmony = new Harmony("com.drowhunter.NewStarTelemetryPlugin");
74+
//harmony.PatchAll();
5375

5476

5577
// Plugin startup logic
5678
Logger = base.Logger;
57-
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
79+
80+
81+
82+
configAccelSmoothing = Config.Bind("Telemetry", "AccelSmoothing", 0.1f, "Smoothing factor for AccelZ");
5883

59-
//gameManager = NSGP.GameManager.inst;
6084

6185

6286
_udp = new UdpTelemetry<NewStarTelemetryData>(new UdpTelemetryConfig
6387
{
6488
SendAddress = new IPEndPoint(IPAddress.Loopback, 12345)
6589
});
6690

91+
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
6792
}
6893

94+
95+
6996
private void Start()
7097
{
7198
telemetryExtractor = new TelemetryExtractor();
7299
}
73100

74-
private GameObject fl, fr, rl, rr, chassis;
75101

76-
void GetWheels()
102+
103+
104+
private Delta[] deltas = Enumerable.Range(0, 4).Select(_ => new Delta()).ToArray();
105+
106+
enum wheel { rr, fr, rl, fl };
107+
void GetWheels(Vehicle vehicle)
77108
{
78-
if(fl != null && fr != null)
109+
var localwheels = vehicle.wheels.Select(w => w.transform.position.y).ToArray();
110+
111+
var avg = localwheels.Average();
112+
for (var i = 0; i < localwheels.Length; i++)
79113
{
80-
return;
114+
deltas[i].Update(localwheels[i] - avg);
81115
}
116+
}
82117

83-
fl = GameObject.Find("RacingContexts/SinglePlayerRacingContext/PlayerCarTemplate/Wheels/frontleft");
84-
fr = GameObject.Find("RacingContexts/SinglePlayerRacingContext/PlayerCarTemplate/Wheels/frontright");
85-
rl = GameObject.Find("RacingContexts/SinglePlayerRacingContext/PlayerCarTemplate/Wheels/rearleft");
86-
rr = GameObject.Find("RacingContexts/SinglePlayerRacingContext/PlayerCarTemplate/Wheels/rearright");
87-
chassis = GameObject.Find("RacingContexts/SinglePlayerRacingContext/PlayerCarTemplate/Car_chasis/Car_FrontWing");
118+
119+
120+
88121

89-
Logger.LogInfo($"Found Wheels: {fl?.name ?? "nope"} {fr?.name ?? "nope"}");
90-
}
91122
private void FixedUpdate()
92123
{
93-
if (_carControl == null)
124+
bool doTelemetry = true;
125+
126+
if (!_carControl)
94127
{
95128
return;
96129
}
97-
130+
var isRacing = TWK.RaceState == "Racing";
131+
var isRaceOver = TWK.ChallengeEventOver == 1;
98132

99133
var vehicle = _carControl.vehicle;
134+
var allowDriving = vehicle.allowDriving;
135+
100136

101-
var gearbox = _carControl.vehicle.gearbox;
137+
doTelemetry = (allowDriving && !isRaceOver && isRacing);
102138

103-
var cRigidbody = vehicle.rigid;// _carControl.rb;
139+
if(!doTelemetry)
140+
{
141+
return;
142+
}
143+
144+
var data = new NewStarTelemetryData();
145+
146+
147+
var cRigidbody = vehicle.rigid;
104148
telemetryExtractor.Update(cRigidbody);
105149

106-
if (cRigidbody == null)
150+
if (!cRigidbody)
107151
{
108152
Logger.LogInfo("Rigidbody is null");
109153
return;
110154
}
111-
//_carControl.GetAcceleration();
112-
113-
114155

115156
var basic = telemetryExtractor.ExtractTelemetry();
116-
GetWheels();
117157

118-
var data = new NewStarTelemetryData
158+
GetWheels(vehicle);
159+
160+
data = new NewStarTelemetryData
119161
{
120-
//Orientation = basic.Rotation,
121162
Pitch = basic.EulerAngles.x,
122163
Yaw = basic.EulerAngles.y,
123164
Roll = basic.EulerAngles.z,
@@ -131,69 +172,43 @@ private void FixedUpdate()
131172
AccelX = basic.Accel.x,
132173
AccelY = basic.Accel.y,
133174
AccelZ = basic.Accel.z,
175+
134176
Speed = vehicle.speed,
135-
RPM = vehicle.engine.maxRPM != 0 ? vehicle.engine.RPM / vehicle.engine.maxRPM: 0,
136-
CurrentGear = gearbox.targetGear,
137-
WheelsOnTrack = vehicle.wheelsOnTrack,
138-
Boosting = vehicle.slipstream.boosting,
139-
TireFL = fl.transform.position.y,
140-
TireFR = fr.transform.position.y,
141-
TireBL = rl.transform.position.y,
142-
TireBR = rr.transform.position.y
177+
RPM = vehicle.engine.maxRPM != 0 ? vehicle.engine.RPM / vehicle.engine.maxRPM : 0,
178+
CurrentGear = _carControl.vehicle.gearbox.targetGear,
143179

180+
TireFL = deltas[(int)wheel.fl] / Time.fixedDeltaTime,
181+
TireFR = deltas[(int)wheel.fr] / Time.fixedDeltaTime,
182+
TireBL = deltas[(int)wheel.rl] / Time.fixedDeltaTime,
183+
TireBR = deltas[(int)wheel.rr] / Time.fixedDeltaTime,
184+
144185

145-
};
186+
WheelsOnTrack = vehicle.wheelsOnTrack,
187+
IsBoosting = vehicle.slipstream.boosting,
188+
AllowDriving = allowDriving,
189+
IsRacing = isRacing,
190+
IsEventOver = isRaceOver,
146191

192+
};
193+
147194
_udp.Send(data);
148195

149196
}
150197
}
151198

152-
[StructLayout(LayoutKind.Sequential)]
153-
public struct NewStarTelemetryData
199+
public class Delta : MonoBehaviour
154200
{
155-
//public Quaternion Orientation;
156-
157-
public float Pitch;
158-
public float Yaw;
159-
public float Roll;
160-
161-
public float AngularVelocityX;
162-
public float AngularVelocityY;
163-
public float AngularVelocityZ;
164-
165-
public float cForce;
166-
167-
public float VelocityX;
168-
public float VelocityY;
169-
public float VelocityZ;
170-
171-
public float AccelX;
172-
public float AccelY;
173-
public float AccelZ;
174-
175-
/// <summary>
176-
/// Speed in Meter per Second
177-
/// </summary>
178-
public float Speed;
179-
180-
public float RPM;
181-
182-
public int CurrentGear;
183-
public int WheelsOnTrack;
184-
public bool Boosting;
185-
public float TireFL;
186-
public float TireFR;
187-
public float TireBL;
188-
public float TireBR;
189-
//public bool GamePaused;
190-
//public bool IsRacing;
191-
//public bool Boost;
192-
201+
private float _lastValue;
193202

203+
public float Value { get; private set; }
194204

205+
public float Update(float currentValue)
206+
{
207+
Value = currentValue - _lastValue;
208+
_lastValue = currentValue;
209+
return Value;
210+
}
195211

212+
public static implicit operator float(Delta d) => d.Value;
196213
}
197-
198-
199214
}

0 commit comments

Comments
 (0)