@@ -14,6 +14,7 @@ namespace AssettoCorsaSharedMemory
14
14
public delegate void PhysicsUpdatedHandler ( object sender , PhysicsEventArgs e ) ;
15
15
public delegate void GraphicsUpdatedHandler ( object sender , GraphicsEventArgs e ) ;
16
16
public delegate void StaticInfoUpdatedHandler ( object sender , StaticInfoEventArgs e ) ;
17
+ public delegate void GameStatusChangedHandler ( object sender , GameStatusEventArgs e ) ;
17
18
18
19
public class AssettoCorsaNotStartedException : Exception
19
20
{
@@ -31,6 +32,25 @@ public class AssettoCorsa
31
32
private AC_MEMORY_STATUS memoryStatus = AC_MEMORY_STATUS . DISCONNECTED ;
32
33
public bool IsRunning { get { return ( memoryStatus == AC_MEMORY_STATUS . CONNECTED ) ; } }
33
34
35
+ private AC_STATUS gameStatus = AC_STATUS . AC_OFF ;
36
+
37
+ public event GameStatusChangedHandler GameStatusChanged ;
38
+ public virtual void OnGameStatusChanged ( GameStatusEventArgs e )
39
+ {
40
+ if ( GameStatusChanged != null )
41
+ {
42
+ GameStatusChanged ( this , e ) ;
43
+ }
44
+ }
45
+
46
+ public static readonly Dictionary < AC_STATUS , string > StatusNameLookup = new Dictionary < AC_STATUS , string >
47
+ {
48
+ { AC_STATUS . AC_OFF , "Off" } ,
49
+ { AC_STATUS . AC_LIVE , "Live" } ,
50
+ { AC_STATUS . AC_PAUSE , "Pause" } ,
51
+ { AC_STATUS . AC_REPLAY , "Replay" } ,
52
+ } ;
53
+
34
54
public AssettoCorsa ( )
35
55
{
36
56
sharedMemoryRetryTimer = new Timer ( 2000 ) ;
@@ -78,22 +98,15 @@ private bool ConnectToSharedMemory()
78
98
graphicsMMF = MemoryMappedFile . OpenExisting ( "Local\\ acpmf_graphics" ) ;
79
99
staticInfoMMF = MemoryMappedFile . OpenExisting ( "Local\\ acpmf_static" ) ;
80
100
81
- // Start the timers if listeners are available
82
- if ( staticInfoUpdated != null && staticInfoUpdated . GetInvocationList ( ) . Length > 0 )
83
- {
84
- staticInfoTimer . Start ( ) ;
85
- ProcessStaticInfo ( ) ;
86
- }
87
- if ( graphicsUpdated != null && graphicsUpdated . GetInvocationList ( ) . Length > 0 )
88
- {
89
- graphicsTimer . Start ( ) ;
90
- ProcessGraphics ( ) ;
91
- }
92
- if ( physicsUpdated != null && physicsUpdated . GetInvocationList ( ) . Length > 0 )
93
- {
94
- physicsTimer . Start ( ) ;
95
- ProcessPhysics ( ) ;
96
- }
101
+ // Start the timers
102
+ staticInfoTimer . Start ( ) ;
103
+ ProcessStaticInfo ( ) ;
104
+
105
+ graphicsTimer . Start ( ) ;
106
+ ProcessGraphics ( ) ;
107
+
108
+ physicsTimer . Start ( ) ;
109
+ ProcessPhysics ( ) ;
97
110
98
111
// Stop retry timer
99
112
sharedMemoryRetryTimer . Stop ( ) ;
@@ -176,94 +189,47 @@ public double StaticInfoInterval
176
189
Timer graphicsTimer ;
177
190
Timer staticInfoTimer ;
178
191
179
- private event PhysicsUpdatedHandler physicsUpdated ;
180
- private event GraphicsUpdatedHandler graphicsUpdated ;
181
- private event StaticInfoUpdatedHandler staticInfoUpdated ;
182
-
183
192
/// <summary>
184
193
/// Represents the method that will handle the physics update events
185
194
/// </summary>
186
- public event PhysicsUpdatedHandler PhysicsUpdated
187
- {
188
- add
189
- {
190
- physicsUpdated += value ;
191
- physicsTimer . Start ( ) ;
192
- ProcessPhysics ( ) ; // Start with a new tick
193
- }
194
- remove
195
- {
196
- physicsUpdated -= value ;
197
- if ( physicsUpdated . GetInvocationList ( ) . Length == 0 )
198
- {
199
- physicsTimer . Stop ( ) ;
200
- }
201
- }
202
- }
195
+ public event PhysicsUpdatedHandler PhysicsUpdated ;
203
196
204
197
/// <summary>
205
198
/// Represents the method that will handle the graphics update events
206
199
/// </summary>
207
- public event GraphicsUpdatedHandler GraphicsUpdated
208
- {
209
- add
210
- {
211
- graphicsUpdated += value ;
212
- graphicsTimer . Start ( ) ;
213
- ProcessGraphics ( ) ; // Start with a new tick
214
- }
215
- remove
216
- {
217
- graphicsUpdated -= value ;
218
- if ( graphicsUpdated . GetInvocationList ( ) . Length == 0 )
219
- {
220
- graphicsTimer . Stop ( ) ;
221
- }
222
- }
223
- }
200
+ public event GraphicsUpdatedHandler GraphicsUpdated ;
224
201
225
202
/// <summary>
226
203
/// Represents the method that will handle the static info update events
227
204
/// </summary>
228
- public event StaticInfoUpdatedHandler StaticInfoUpdated
229
- {
230
- add
231
- {
232
- staticInfoUpdated += value ;
233
- staticInfoTimer . Start ( ) ;
234
- ProcessStaticInfo ( ) ; // Start with a new tick
235
- }
236
- remove
237
- {
238
- staticInfoUpdated -= value ;
239
- if ( staticInfoUpdated . GetInvocationList ( ) . Length == 0 )
240
- {
241
- staticInfoTimer . Stop ( ) ;
242
- }
243
- }
244
- }
205
+ public event StaticInfoUpdatedHandler StaticInfoUpdated ;
245
206
246
207
public virtual void OnPhysicsUpdated ( PhysicsEventArgs e )
247
208
{
248
- if ( physicsUpdated != null )
209
+ if ( PhysicsUpdated != null )
249
210
{
250
- physicsUpdated ( this , e ) ;
211
+ PhysicsUpdated ( this , e ) ;
251
212
}
252
213
}
253
214
254
215
public virtual void OnGraphicsUpdated ( GraphicsEventArgs e )
255
216
{
256
- if ( graphicsUpdated != null )
217
+ if ( GraphicsUpdated != null )
257
218
{
258
- graphicsUpdated ( this , e ) ;
219
+ GraphicsUpdated ( this , e ) ;
220
+ if ( gameStatus != e . Graphics . Status )
221
+ {
222
+ gameStatus = e . Graphics . Status ;
223
+ GameStatusChanged ( this , new GameStatusEventArgs ( gameStatus ) ) ;
224
+ }
259
225
}
260
226
}
261
227
262
228
public virtual void OnStaticInfoUpdated ( StaticInfoEventArgs e )
263
229
{
264
- if ( staticInfoUpdated != null )
230
+ if ( StaticInfoUpdated != null )
265
231
{
266
- staticInfoUpdated ( this , e ) ;
232
+ StaticInfoUpdated ( this , e ) ;
267
233
}
268
234
}
269
235
0 commit comments