Skip to content

Commit 6c13179

Browse files
Add ModeListMonitor
1 parent 11775a3 commit 6c13179

File tree

6 files changed

+106
-15
lines changed

6 files changed

+106
-15
lines changed

Runtime/MediaController/Messages/Mode/Mode.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1010
// SOFTWARE.
1111

12+
using System;
13+
1214
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode
1315
{
14-
public readonly struct Mode
16+
public readonly struct Mode : IEquatable<Mode>
1517
{
1618
public readonly string Name;
1719
public readonly int Priority;
@@ -21,5 +23,20 @@ public Mode(string name, int priority)
2123
Name = name;
2224
Priority = priority;
2325
}
26+
27+
public bool Equals(Mode other)
28+
{
29+
return Name == other.Name && Priority == other.Priority;
30+
}
31+
32+
public override bool Equals(object obj)
33+
{
34+
return obj is Mode other && Equals(other);
35+
}
36+
37+
public override int GetHashCode()
38+
{
39+
return HashCode.Combine(Name, Priority);
40+
}
2441
}
2542
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2025 freezy and VPE Team
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
10+
// SOFTWARE.
11+
12+
using System;
13+
using System.Collections.ObjectModel;
14+
using System.Linq;
15+
16+
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode
17+
{
18+
public class ModeList : IEquatable<ModeList>
19+
{
20+
private readonly ReadOnlyCollection<Mode> _list;
21+
22+
public ModeList(ReadOnlyCollection<Mode> list)
23+
{
24+
_list = list;
25+
}
26+
27+
public bool Equals(ModeList other)
28+
{
29+
if (other is null) return false;
30+
if (ReferenceEquals(this, other)) return true;
31+
if (Equals(_list, other._list)) return true;
32+
if (_list == null || other._list == null) return false;
33+
return _list.Count == other._list.Count && _list.All(other._list.Contains);
34+
}
35+
36+
public override bool Equals(object obj)
37+
{
38+
if (obj is null) return false;
39+
if (ReferenceEquals(this, obj)) return true;
40+
if (obj.GetType() != GetType()) return false;
41+
return Equals((ModeList)obj);
42+
}
43+
44+
public override int GetHashCode()
45+
{
46+
return _list != null ? _list.GetHashCode() : 0;
47+
}
48+
}
49+
}

Runtime/MediaController/Messages/Mode/ModeList.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/MediaController/Messages/Mode/ModeListMessage.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// SOFTWARE.
1111

1212
using System;
13-
using System.Collections.ObjectModel;
1413
using Newtonsoft.Json;
1514
using Newtonsoft.Json.Linq;
1615

@@ -20,39 +19,35 @@ public class ModeListMessage : EventArgs
2019
{
2120
public const string Command = "mode_list";
2221
public const string RunningModesParamName = "running_modes";
23-
public ReadOnlyCollection<Mode> RunningModes => Array.AsReadOnly(_runningModes);
24-
private readonly Mode[] _runningModes;
22+
public readonly ModeList RunningModes;
2523

26-
public ModeListMessage(Mode[] runningModes)
24+
public ModeListMessage(ModeList runningModes)
2725
{
28-
_runningModes = runningModes;
26+
RunningModes = runningModes;
2927
}
3028

3129
public static ModeListMessage FromGenericMessage(BcpMessage bcpMessage)
3230
{
3331
try
3432
{
3533
var jArr = bcpMessage.GetParamValue<JArray>(RunningModesParamName);
36-
Mode[] runningModes = new Mode[jArr.Count];
34+
var runningModes = new Mode[jArr.Count];
3735

38-
for (int i = 0; i < jArr.Count; i++)
36+
for (var i = 0; i < jArr.Count; i++)
3937
{
4038
var modeJArr = (JArray)jArr[i];
4139
var modeName = (string)modeJArr[0];
4240
var modePrio = (int)modeJArr[1];
4341
runningModes[i] = new Mode(modeName, modePrio);
4442
}
4543

46-
return new ModeListMessage(runningModes);
44+
return new ModeListMessage(new ModeList(Array.AsReadOnly(runningModes)));
4745
}
4846
catch (Exception e)
49-
when (e is JsonException
50-
|| e is InvalidCastException
51-
|| e is IndexOutOfRangeException
52-
)
47+
when (e is JsonException or InvalidCastException or IndexOutOfRangeException)
5348
{
54-
throw new ParameterException(RunningModesParamName, null, e);
49+
throw new ParameterException(RunningModesParamName, bcpMessage, e);
5550
}
5651
}
5752
}
58-
}
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2025 freezy and VPE Team
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
10+
// SOFTWARE.
11+
12+
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode
13+
{
14+
public class ModeListMonitor : MonitorBase<ModeList, ModeListMessage>
15+
{
16+
public ModeListMonitor(BcpInterface bcpInterface) : base(bcpInterface)
17+
{
18+
}
19+
20+
protected override string BcpCommand => ModeListMessage.Command;
21+
22+
protected override ModeList GetValueFromMessage(ModeListMessage msg) => msg.RunningModes;
23+
}
24+
}

Runtime/MediaController/Messages/Mode/ModeListMonitor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)