Skip to content

Commit 472fb5e

Browse files
committed
Added GameStatus event that triggers when switching game states and some random refactoring
1 parent 3723947 commit 472fb5e

File tree

3 files changed

+61
-77
lines changed

3 files changed

+61
-77
lines changed

AssettoCorsa.cs

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace AssettoCorsaSharedMemory
1414
public delegate void PhysicsUpdatedHandler(object sender, PhysicsEventArgs e);
1515
public delegate void GraphicsUpdatedHandler(object sender, GraphicsEventArgs e);
1616
public delegate void StaticInfoUpdatedHandler(object sender, StaticInfoEventArgs e);
17+
public delegate void GameStatusChangedHandler(object sender, GameStatusEventArgs e);
1718

1819
public class AssettoCorsaNotStartedException : Exception
1920
{
@@ -31,6 +32,25 @@ public class AssettoCorsa
3132
private AC_MEMORY_STATUS memoryStatus = AC_MEMORY_STATUS.DISCONNECTED;
3233
public bool IsRunning { get { return (memoryStatus == AC_MEMORY_STATUS.CONNECTED); } }
3334

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+
3454
public AssettoCorsa()
3555
{
3656
sharedMemoryRetryTimer = new Timer(2000);
@@ -78,22 +98,15 @@ private bool ConnectToSharedMemory()
7898
graphicsMMF = MemoryMappedFile.OpenExisting("Local\\acpmf_graphics");
7999
staticInfoMMF = MemoryMappedFile.OpenExisting("Local\\acpmf_static");
80100

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();
97110

98111
// Stop retry timer
99112
sharedMemoryRetryTimer.Stop();
@@ -176,94 +189,47 @@ public double StaticInfoInterval
176189
Timer graphicsTimer;
177190
Timer staticInfoTimer;
178191

179-
private event PhysicsUpdatedHandler physicsUpdated;
180-
private event GraphicsUpdatedHandler graphicsUpdated;
181-
private event StaticInfoUpdatedHandler staticInfoUpdated;
182-
183192
/// <summary>
184193
/// Represents the method that will handle the physics update events
185194
/// </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;
203196

204197
/// <summary>
205198
/// Represents the method that will handle the graphics update events
206199
/// </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;
224201

225202
/// <summary>
226203
/// Represents the method that will handle the static info update events
227204
/// </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;
245206

246207
public virtual void OnPhysicsUpdated(PhysicsEventArgs e)
247208
{
248-
if (physicsUpdated != null)
209+
if (PhysicsUpdated != null)
249210
{
250-
physicsUpdated(this, e);
211+
PhysicsUpdated(this, e);
251212
}
252213
}
253214

254215
public virtual void OnGraphicsUpdated(GraphicsEventArgs e)
255216
{
256-
if (graphicsUpdated != null)
217+
if (GraphicsUpdated != null)
257218
{
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+
}
259225
}
260226
}
261227

262228
public virtual void OnStaticInfoUpdated(StaticInfoEventArgs e)
263229
{
264-
if (staticInfoUpdated != null)
230+
if (StaticInfoUpdated != null)
265231
{
266-
staticInfoUpdated(this, e);
232+
StaticInfoUpdated(this, e);
267233
}
268234
}
269235

AssettoCorsaSharedMemory.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
</ItemGroup>
4141
<ItemGroup>
4242
<Compile Include="AssettoCorsa.cs" />
43+
<Compile Include="GameStatusEventArgs.cs" />
4344
<Compile Include="Graphics.cs" />
4445
<Compile Include="Physics.cs" />
4546
<Compile Include="Properties\AssemblyInfo.cs" />

GameStatusEventArgs.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace AssettoCorsaSharedMemory
7+
{
8+
public class GameStatusEventArgs : EventArgs
9+
{
10+
public AC_STATUS GameStatus {get; private set;}
11+
12+
public GameStatusEventArgs(AC_STATUS status)
13+
{
14+
GameStatus = status;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)