Skip to content

Commit aebca67

Browse files
Correctly handle empty mode, event, and variable name fields
1 parent 991d111 commit aebca67

File tree

10 files changed

+87
-41
lines changed

10 files changed

+87
-41
lines changed

Runtime/MediaController/Messages/Mode/ModeMonitor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public void Dispose()
5959

6060
private void OnModeStarted(object sender, ModeStartMessage msg)
6161
{
62-
if (msg.Name != _modeName)
63-
return;
64-
65-
IsModeActive = true;
62+
if (msg.Name != _modeName && !string.IsNullOrWhiteSpace(_modeName))
63+
IsModeActive = true;
6664
}
6765

6866
private void OnModeStopped(object sender, ModeStopMessage msg)

Runtime/MediaController/Messages/MpfVariableMonitorBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ protected MpfVariableMonitorBase(BcpInterface bcpInterface, string varName)
2828

2929
protected override bool MatchesMonitoringCriteria(TMessage msg)
3030
{
31-
return base.MatchesMonitoringCriteria(msg) && msg.Name == _varName;
31+
return base.MatchesMonitoringCriteria(msg) && !string.IsNullOrWhiteSpace(_varName) &&
32+
msg.Name == _varName;
3233
}
3334

3435
protected override TVar GetValueFromMessage(TMessage msg)

Runtime/MediaController/Messages/Trigger/MpfEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Dispose()
4646

4747
private void TriggerMessageHandler_Received(object sender, TriggerMessage msg)
4848
{
49-
if (msg.TriggerName == _eventName)
49+
if (msg.TriggerName == _eventName && !string.IsNullOrWhiteSpace(_eventName))
5050
Triggered?.Invoke(this, EventArgs.Empty);
5151
}
5252
}

Runtime/MediaController/ObjectToggle/EnableDuringMode.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using UnityEngine;
1313
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode;
14+
using Logger = NLog.Logger;
1415

1516
namespace VisualPinball.Engine.Mpf.Unity.MediaController.ObjectToggle
1617
{
@@ -25,8 +26,17 @@ public class EnableDuringMode : MonoBehaviour
2526

2627
private ModeMonitor _modeMonitor;
2728

29+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
30+
2831
private void Awake()
2932
{
33+
if (string.IsNullOrWhiteSpace(_mode))
34+
{
35+
Logger.Warn(
36+
"No MPF mode is specified. The component 'Enable During MPF Mode' on game object"
37+
+ $" '{gameObject.name}' won't do anything.");
38+
}
39+
3040
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface)) return;
3141

3242
_modeMonitor = new ModeMonitor(bcpInterface, _mode);
@@ -37,7 +47,8 @@ private void Start()
3747
{
3848
// This is done in Start to give other components like this one attached to children of this game object a
3949
// chance to run their Awake functions.
40-
gameObject.SetActive(false);
50+
if (_modeMonitor != null)
51+
gameObject.SetActive(false);
4152
}
4253

4354
private void OnDestroy()

Runtime/MediaController/ObjectToggle/ToggleOnEvent.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using UnityEngine;
1414
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Trigger;
15+
using Logger = NLog.Logger;
1516

1617
namespace VisualPinball.Engine.Mpf.Unity.MediaController.ObjectToggle
1718
{
@@ -29,9 +30,19 @@ public class ToggleOnEvent : MonoBehaviour
2930
private MpfEventListener _disableEventListener;
3031
private MpfEventListener _toggleEventListener;
3132

33+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
34+
3235

3336
private void Awake()
3437
{
38+
if (string.IsNullOrWhiteSpace(_enableEvent) && string.IsNullOrWhiteSpace(_disableEvent))
39+
{
40+
Logger.Warn(
41+
"Both 'Enable Event' and 'Disable Event' are unspecified. The component 'Toggle On MPF Event' on "
42+
+ $"game object '{gameObject.name}' won't do anything.");
43+
return;
44+
}
45+
3546
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface)) return;
3647

3748
if (_enableEvent == _disableEvent)
@@ -52,7 +63,8 @@ private void Start()
5263
{
5364
// This is done in Start to give other components like this one attached to children of this game object a
5465
// chance to run their Awake functions.
55-
gameObject.SetActive(_enabledOnStart);
66+
if (_enableEventListener != null || _disableEventListener != null || _toggleEventListener != null)
67+
gameObject.SetActive(_enabledOnStart);
5668
}
5769

5870
private void OnDestroy()

Runtime/MediaController/Sound/EventSound.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
using UnityEngine;
1414
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Trigger;
1515
using VisualPinball.Unity;
16+
using Logger = NLog.Logger;
1617

1718
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Sound
1819
{
1920
[AddComponentMenu("Pinball/Sound/MPF Event Sound")]
2021
public class EventSound : EventSoundComponent<MpfEventListener, EventArgs>
2122
{
22-
[SerializeField]
23-
private string _eventName;
23+
[SerializeField] private string _eventName;
24+
25+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
2426

2527
protected override void Subscribe(MpfEventListener eventSource)
2628
{
@@ -34,14 +36,18 @@ protected override void Unsubscribe(MpfEventListener eventSource)
3436

3537
protected override bool TryFindEventSource(out MpfEventListener eventSource)
3638
{
37-
if (MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
39+
eventSource = null;
40+
if (string.IsNullOrWhiteSpace(_eventName))
3841
{
39-
eventSource = new MpfEventListener(bcpInterface, _eventName);
40-
return true;
42+
Logger.Warn("No event name is specified. The component 'MPF Event Sound' on game object "
43+
+ $"'{gameObject.name}' will not do anything.");
44+
return false;
4145
}
4246

43-
eventSource = null;
44-
return false;
47+
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
48+
return false;
49+
eventSource = new MpfEventListener(bcpInterface, _eventName);
50+
return true;
4551
}
4652
}
47-
}
53+
}

Runtime/MediaController/Sound/ModeSound.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
using UnityEngine;
1313
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.Mode;
1414
using VisualPinball.Unity;
15+
using Logger = NLog.Logger;
1516

1617
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Sound
1718
{
1819
[AddComponentMenu("Pinball/Sound/MPF Mode Sound")]
1920
public class ModeSound : BinaryEventSoundComponent<ModeMonitor, bool>
2021
{
21-
[SerializeField]
22-
private string _modeName;
22+
[SerializeField] private string _modeName;
23+
24+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
2325

2426
protected override bool InterpretAsBinary(bool eventArgs) => eventArgs; // Big brain time
2527

@@ -35,14 +37,18 @@ protected override void Unsubscribe(ModeMonitor eventSource)
3537

3638
protected override bool TryFindEventSource(out ModeMonitor eventSource)
3739
{
38-
if (MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
40+
eventSource = null;
41+
if (string.IsNullOrWhiteSpace(_modeName))
3942
{
40-
eventSource = new ModeMonitor(bcpInterface, _modeName);
41-
return true;
43+
Logger.Warn("No mode name is specified. The component 'MPF Mode Sound' on game object "
44+
+ $"'{gameObject.name}' will not do anything.");
45+
return false;
4246
}
4347

44-
eventSource = null;
45-
return false;
48+
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
49+
return false;
50+
eventSource = new ModeMonitor(bcpInterface, _modeName);
51+
return true;
4652
}
4753
}
48-
}
54+
}

Runtime/MediaController/Text/MachineVariable/MachineVariableText.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,28 @@
1313
using UnityEngine;
1414
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages;
1515
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.MachineVariable;
16+
using Logger = NLog.Logger;
1617

1718
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Text
1819
{
1920
public abstract class MachineVariableText<T> : MonitoredVariableText<T, MachineVariableMessage>
2021
where T : IEquatable<T>, IConvertible
2122
{
22-
[SerializeField]
23-
private string _variableName;
23+
[SerializeField] private string _variableName;
24+
25+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
2426

2527
protected override MonitorBase<T, MachineVariableMessage> CreateMonitor(
2628
BcpInterface bcpInterface
2729
)
2830
{
31+
if (string.IsNullOrWhiteSpace(_variableName))
32+
{
33+
Logger.Warn("No MPF variable name is specified. The component 'MPF Machine Variable Text' on game "
34+
+ $"object '{gameObject.name}' will not do anything.");
35+
}
36+
2937
return new MachineVariableMonitor<T>(bcpInterface, _variableName);
3038
}
3139
}
32-
}
40+
}

Runtime/MediaController/Text/MonitoredVariableText.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ namespace VisualPinball.Engine.Mpf.Unity.MediaController.Text
1919
// This non-generic base class exists so there can be one inspector for all generic variations.
2020
public abstract class MonitoredVariableTextBase : MonoBehaviour
2121
{
22-
[SerializeField]
23-
protected TextMeshProUGUI _textField;
22+
[SerializeField] protected TextMeshProUGUI _textField;
2423

25-
[SerializeField]
26-
protected string _format = "{0}";
24+
[SerializeField] protected string _format = "{0}";
2725
}
2826

2927
public abstract class MonitoredVariableText<TVar, TMessage> : MonitoredVariableTextBase
@@ -36,12 +34,10 @@ public abstract class MonitoredVariableText<TVar, TMessage> : MonitoredVariableT
3634

3735
private void OnEnable()
3836
{
39-
if (MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface))
40-
{
41-
_monitor = CreateMonitor(bcpInterface);
42-
SetText(_monitor.VarValue);
43-
_monitor.ValueChanged += Monitor_ValueChanged;
44-
}
37+
if (!MpfGamelogicEngine.TryGetBcpInterface(this, out var bcpInterface)) return;
38+
_monitor = CreateMonitor(bcpInterface);
39+
SetText(_monitor.VarValue);
40+
_monitor.ValueChanged += Monitor_ValueChanged;
4541
}
4642

4743
private void OnDisable()
@@ -57,4 +53,4 @@ private void OnDisable()
5753

5854
private void SetText(TVar value) => _textField.text = string.Format(_format, value);
5955
}
60-
}
56+
}

Runtime/MediaController/Text/PlayerVariable/PlayerVariableText.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,28 @@
1313
using UnityEngine;
1414
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages;
1515
using VisualPinball.Engine.Mpf.Unity.MediaController.Messages.PlayerVariable;
16+
using Logger = NLog.Logger;
1617

1718
namespace VisualPinball.Engine.Mpf.Unity.MediaController.Text
1819
{
1920
public abstract class PlayerVariableText<T> : MonitoredVariableText<T, PlayerVariableMessage>
2021
where T : IEquatable<T>, IConvertible
2122
{
22-
[SerializeField]
23-
private string _variableName;
23+
[SerializeField] private string _variableName;
24+
25+
private static Logger Logger = NLog.LogManager.GetCurrentClassLogger();
2426

2527
protected override MonitorBase<T, PlayerVariableMessage> CreateMonitor(
2628
BcpInterface bcpInterface
2729
)
2830
{
31+
if (string.IsNullOrWhiteSpace(_variableName))
32+
{
33+
Logger.Warn("No MPF variable name is specified. The component 'MPF Player Variable Text' on game "
34+
+ $"object '{gameObject.name}' will not do anything.");
35+
}
36+
2937
return new PlayerVariableMonitor<T>(bcpInterface, _variableName);
3038
}
3139
}
32-
}
40+
}

0 commit comments

Comments
 (0)