Skip to content

Commit d3baa87

Browse files
author
Rene Damm
authored
FIX: LayoutNotFoundExceptions from ToHumanReadableString (#946).
1 parent a5cc644 commit d3baa87

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

Assets/Tests/InputSystem/CoreTests_Controls.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using UnityEngine.InputSystem.LowLevel;
1010
using UnityEngine.InputSystem.Processors;
1111
using UnityEngine.InputSystem.Utilities;
12+
using UnityEngine.Scripting;
1213
using UnityEngine.TestTools.Constraints;
1314
using Is = UnityEngine.TestTools.Constraints.Is;
1415

@@ -1021,6 +1022,22 @@ public void Controls_CanTurnControlPathIntoHumanReadableText()
10211022
InputControlPath.HumanReadableStringOptions.OmitDevice), Is.EqualTo("PrimaryAction"));
10221023
}
10231024

1025+
[Preserve]
1026+
private class DeviceWithoutAnyControls : InputDevice
1027+
{
1028+
}
1029+
1030+
[Test]
1031+
[Category("Controls")]
1032+
public void Controls_CanTurnControlPathIntoHumanReadableText_EvenIfLayoutCannotBeFoundOrHasErrors()
1033+
{
1034+
// This one will throw as the layout will result in a zero-size memory block.
1035+
InputSystem.RegisterLayout<DeviceWithoutAnyControls>();
1036+
1037+
Assert.That(InputControlPath.ToHumanReadableString("<UnknownGamepad>/leftStick"), Is.EqualTo("leftStick [UnknownGamepad]"));
1038+
Assert.That(InputControlPath.ToHumanReadableString("<DeviceWithoutAnyControls>/control"), Is.EqualTo("control [DeviceWithoutAnyControls]"));
1039+
}
1040+
10241041
[Test]
10251042
[Category("Controls")]
10261043
public void Controls_CanCheckIfControlMatchesGivenPath()

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
Due to package verification, the latest version below is the unpublished version and the date is meaningless.
88
however, it has to be formatted properly to pass verification tests.
99

10+
## [1.0.0-preview.3] - 2019-12-12
11+
12+
### Fixed
13+
14+
- Fixed `LayoutNotFoundException` being thrown when `InputControlPath.ToHumanReadableString` referenced a layout that could not be found.
15+
1016
## [1.0.0-preview.2] - 2019-11-4
1117

1218
### Changed

Packages/com.unity.inputsystem/Documentation~/Components.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The Input System provides two `MonoBehaviour` components that simplify setting u
1313

1414
![PlayerInput](Images/PlayerInput.png)
1515

16-
Each [`PlayerInput`](../api/UnityEngine.InputSystem.PlayerInput.html) instance represents a separate player in the game. Multiple [`PlayerInput`](../api/UnityEngine.InputSystem.PlayerInput.html) instances can coexist at the same time (though not on the same `GameObject`) to represent local multiplayer setups. The Input System pairs each player to a unique set of Devices that the player uses excplusively, but you can also manually pair Devices in a way that enables two or more players to share a Device (for example, left/right keyboard splits or hot seat use).
16+
Each [`PlayerInput`](../api/UnityEngine.InputSystem.PlayerInput.html) instance represents a separate player in the game. Multiple [`PlayerInput`](../api/UnityEngine.InputSystem.PlayerInput.html) instances can coexist at the same time (though not on the same `GameObject`) to represent local multiplayer setups. The Input System pairs each player to a unique set of Devices that the player uses exclusively, but you can also manually pair Devices in a way that enables two or more players to share a Device (for example, left/right keyboard splits or hot seat use).
1717

1818
Each [`PlayerInput`](../api/UnityEngine.InputSystem.PlayerInput.html) corresponds to one [`InputUser`](UserManagement.md). You can query the [`InputUser`](UserManagement.md) from the component using [`PlayerInput.user`](../api/UnityEngine.InputSystem.PlayerInput.html#UnityEngine_InputSystem_PlayerInput_user).
1919

Packages/com.unity.inputsystem/InputSystem/Controls/InputControlLayout.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ public void Clear()
20522052
table = null;
20532053
}
20542054

2055-
public InputControlLayout FindOrLoadLayout(string name)
2055+
public InputControlLayout FindOrLoadLayout(string name, bool throwIfNotFound = true)
20562056
{
20572057
var internedName = new InternedString(name);
20582058

@@ -2064,7 +2064,9 @@ public InputControlLayout FindOrLoadLayout(string name)
20642064
return layout;
20652065

20662066
// Nothing.
2067-
throw new LayoutNotFoundException(name);
2067+
if (throwIfNotFound)
2068+
throw new LayoutNotFoundException(name);
2069+
return null;
20682070
}
20692071
}
20702072

Packages/com.unity.inputsystem/InputSystem/Controls/InputControlPath.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private static string FindControlLayoutRecursive(ref PathParser parser, string l
325325
using (InputControlLayout.CacheRef())
326326
{
327327
// Load layout.
328-
var layout = InputControlLayout.cache.FindOrLoadLayout(new InternedString(layoutName));
328+
var layout = InputControlLayout.cache.FindOrLoadLayout(new InternedString(layoutName), throwIfNotFound: false);
329329
if (layout == null)
330330
return null;
331331

@@ -1062,7 +1062,7 @@ public string ToHumanReadableString(string parentLayoutName, out string referenc
10621062
// Where possible, use the displayName of the given layout rather than
10631063
// just the internal layout name.
10641064
string layoutString;
1065-
var referencedLayout = InputControlLayout.cache.FindOrLoadLayout(referencedLayoutName);
1065+
var referencedLayout = InputControlLayout.cache.FindOrLoadLayout(referencedLayoutName, throwIfNotFound: false);
10661066
if (referencedLayout != null && !string.IsNullOrEmpty(referencedLayout.m_DisplayName))
10671067
layoutString = referencedLayout.m_DisplayName;
10681068
else
@@ -1084,7 +1084,8 @@ public string ToHumanReadableString(string parentLayoutName, out string referenc
10841084
{
10851085
// NOTE: This produces a fully merged layout. We should thus pick up display names
10861086
// from base layouts automatically wherever applicable.
1087-
var parentLayout = InputControlLayout.cache.FindOrLoadLayout(new InternedString(parentLayoutName));
1087+
var parentLayout =
1088+
InputControlLayout.cache.FindOrLoadLayout(new InternedString(parentLayoutName), throwIfNotFound: false);
10881089
if (parentLayout != null)
10891090
{
10901091
var controlName = new InternedString(name.ToString());

Packages/com.unity.inputsystem/InputSystem/Editor/EditorInputControlLayoutCache.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static InputControlLayout TryGetLayout(string layoutName)
8080
throw new ArgumentException("Layout name cannot be null or empty", nameof(layoutName));
8181

8282
Refresh();
83-
return InputControlLayout.cache.FindOrLoadLayout(layoutName);
83+
return InputControlLayout.cache.FindOrLoadLayout(layoutName, throwIfNotFound: false);
8484
}
8585

8686
public static Type GetValueType(string layoutName)
@@ -232,7 +232,10 @@ private static void Refresh()
232232
// Load and store all layouts.
233233
foreach (var layoutName in layoutNames)
234234
{
235-
var layout = InputControlLayout.cache.FindOrLoadLayout(layoutName);
235+
var layout = InputControlLayout.cache.FindOrLoadLayout(layoutName, throwIfNotFound: false);
236+
if (layout == null)
237+
continue;
238+
236239
ScanLayout(layout);
237240

238241
if (layout.isOverride)
@@ -264,7 +267,9 @@ private static void Refresh()
264267
break;
265268
}
266269

267-
var baseLayout = InputControlLayout.cache.FindOrLoadLayout(baseLayoutName);
270+
var baseLayout = InputControlLayout.cache.FindOrLoadLayout(baseLayoutName, throwIfNotFound: false);
271+
if (baseLayout == null)
272+
continue;
268273
if (baseLayout.m_BaseLayouts.length > 1)
269274
throw new NotImplementedException();
270275
baseLayoutName = baseLayout.baseLayouts.FirstOrDefault();

Packages/com.unity.inputsystem/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.unity.inputsystem",
33
"displayName": "Input System",
4-
"version": "1.0.0-preview.2",
4+
"version": "1.0.0-preview.3",
55
"unity": "2019.1",
66
"repository": {
77
"type": "git",

0 commit comments

Comments
 (0)