1111using TelemetryLib ;
1212
1313using UnityEngine ;
14+ using System . Collections . Generic ;
15+ using System . Linq ;
16+ using HarmonyLib ;
17+ using UnityEngine . UIElements ;
18+ using BepInEx . Configuration ;
1419
1520namespace 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