Avoid exceptions in `AxisValue` implementations (resolves #4057)

see #4056
only in Release config, because cores really shouldn't be using
undeclared axes and I'd like to be able to catch that
This commit is contained in:
YoshiRulz 2024-09-28 17:49:48 +10:00
parent e3cfcdaeb9
commit 444d1b2182
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 31 additions and 7 deletions

View File

@ -41,7 +41,12 @@ namespace BizHawk.Client.Common
_buttonStarts.Clear();
}
public int AxisValue(string name) => Definition.Axes[name].Neutral;
public int AxisValue(string name)
#if DEBUG
=> Definition.Axes[name].Neutral; // throw if no such axis
#else
=> Definition.Axes.TryGetValue(name, out var axisSpec) ? axisSpec.Neutral : default;
#endif
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => Array.Empty<(string, int)>();

View File

@ -25,9 +25,13 @@ namespace BizHawk.Client.Common
}
public int AxisValue(string name)
{
return _axisHolds.TryGetValue(name, out int axisValue) ? axisValue : Definition.Axes[name].Neutral;
}
=> _axisHolds.TryGetValue(name, out int axisValue)
? axisValue
#if DEBUG
: Definition.Axes[name].Neutral; // throw if no such axis
#else
: Definition.Axes.TryGetValue(name, out var axisSpec) ? axisSpec.Neutral : default;
#endif
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => throw new NotSupportedException();
public void SetHapticChannelStrength(string name, int strength) => throw new NotSupportedException();
@ -113,7 +117,11 @@ namespace BizHawk.Client.Common
public int AxisValue(string name)
=> _axisPatterns.TryGetValue(name, out var pattern)
? pattern.PeekNextValue()
: Definition.Axes[name].Neutral;
#if DEBUG
: Definition.Axes[name].Neutral; // throw if no such axis
#else
: Definition.Axes.TryGetValue(name, out var axisSpec) ? axisSpec.Neutral : default;
#endif
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => throw new NotSupportedException();
public void SetHapticChannelStrength(string name, int strength) => throw new NotSupportedException();

View File

@ -51,7 +51,11 @@ namespace BizHawk.Client.Common
{
int sourceAxisValue = Source.AxisValue(name);
int sourceXorAxisValue = SourceXor.AxisValue(name);
int neutral = Definition.Axes[name].Neutral;
#if DEBUG
var neutral = Definition.Axes[name].Neutral; // throw if no such axis
#else
var neutral = Definition.Axes.TryGetValue(name, out var axisSpec) ? axisSpec.Neutral : default;
#endif
if (sourceAxisValue == neutral)
{
@ -82,7 +86,14 @@ namespace BizHawk.Client.Common
public int AxisValue(string name)
{
int sourceValue = Source.AxisValue(name);
return sourceValue != Source.Definition.Axes[name].Neutral ? sourceValue : SourceOr.AxisValue(name);
#if DEBUG
var neutralValue = Source.Definition.Axes[name].Neutral; // throw if no such axis
#else
var neutralValue = Source.Definition.Axes.TryGetValue(name, out var axisSpec) ? axisSpec.Neutral : default;
#endif
return sourceValue != neutralValue
? sourceValue
: SourceOr.AxisValue(name);
}
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => Source.GetHapticsSnapshot();