Skip to content

Commit 7767170

Browse files
FIX: JSON Binding overrides are set to empty string when null (#1644)
* (De)serialize null strings with string "null" * Call BindingOverrideJson.ToBinding() and FromBinding() * Add tests * Update changelog
1 parent b1d5788 commit 7767170

File tree

4 files changed

+76
-23
lines changed

4 files changed

+76
-23
lines changed

Assets/Tests/InputSystem/CoreTests_Actions_Rebinding.cs

+57
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,63 @@ public void Actions_CanSaveAndLoadRebinds()
13461346
Assert.That(secondActionInAsset.bindings[0].overrideProcessors, Is.EqualTo("invert"));
13471347
}
13481348

1349+
[Test]
1350+
[Category("Actions")]
1351+
public void Actions_CanSaveAndLoadRebindOnlyForOverridesSet()
1352+
{
1353+
var action = new InputAction();
1354+
action.AddBinding("<Gamepad>/buttonSouth", processors: "invert");
1355+
action.AddBinding("<Keyboard>/space", interactions: "tap");
1356+
1357+
// Check that the effective paths are correct
1358+
Assert.That(action.bindings[0].effectivePath, Is.EqualTo("<Gamepad>/buttonSouth"));
1359+
Assert.That(action.bindings[0].effectiveInteractions, Is.EqualTo(null));
1360+
Assert.That(action.bindings[0].effectiveProcessors, Is.EqualTo("invert"));
1361+
1362+
Assert.That(action.bindings[1].effectivePath, Is.EqualTo("<Keyboard>/space"));
1363+
Assert.That(action.bindings[1].effectiveInteractions, Is.EqualTo("tap"));
1364+
Assert.That(action.bindings[1].effectiveProcessors, Is.EqualTo(null));
1365+
1366+
action.ApplyBindingOverride(0, "<Gamepad>/buttonWest");
1367+
action.ApplyBindingOverride(1, new InputBinding
1368+
{
1369+
overridePath = "<Keyboard>/a",
1370+
overrideInteractions = "",
1371+
overrideProcessors = "multiply",
1372+
});
1373+
1374+
var actionRebindsJson = action.SaveBindingOverridesAsJson();
1375+
Debug.Log(actionRebindsJson.ToString());
1376+
1377+
action.LoadBindingOverridesFromJson(actionRebindsJson);
1378+
1379+
// Check that effective binding path changed
1380+
Assert.That(action.bindings[0].effectivePath, Is.EqualTo("<Gamepad>/buttonWest"));
1381+
Assert.That(action.bindings[0].overridePath, Is.EqualTo("<Gamepad>/buttonWest"));
1382+
Assert.That(action.bindings[0].path, Is.EqualTo("<Gamepad>/buttonSouth"));
1383+
// Check that effective interaction maintained it's null value
1384+
Assert.That(action.bindings[0].effectiveInteractions, Is.EqualTo(null));
1385+
Assert.That(action.bindings[0].overrideInteractions, Is.EqualTo(null));
1386+
Assert.That(action.bindings[0].interactions, Is.EqualTo(null));
1387+
// Check that effective interaction maintained it's previously set value
1388+
Assert.That(action.bindings[0].effectiveProcessors, Is.EqualTo("invert"));
1389+
Assert.That(action.bindings[0].overrideProcessors, Is.EqualTo(null));
1390+
Assert.That(action.bindings[0].processors, Is.EqualTo("invert"));
1391+
1392+
// Check that effective binding path changed
1393+
Assert.That(action.bindings[1].effectivePath, Is.EqualTo("<Keyboard>/a"));
1394+
Assert.That(action.bindings[1].overridePath, Is.EqualTo("<Keyboard>/a"));
1395+
Assert.That(action.bindings[1].path, Is.EqualTo("<Keyboard>/space"));
1396+
// Check that effective binding was disabled
1397+
Assert.That(action.bindings[1].effectiveInteractions, Is.EqualTo(""));
1398+
Assert.That(action.bindings[1].overrideInteractions, Is.EqualTo(""));
1399+
Assert.That(action.bindings[1].interactions, Is.EqualTo("tap"));
1400+
// Check that effective binding which was null before, now has changed
1401+
Assert.That(action.bindings[1].effectiveProcessors, Is.EqualTo("multiply"));
1402+
Assert.That(action.bindings[1].overrideProcessors, Is.EqualTo("multiply"));
1403+
Assert.That(action.bindings[1].processors, Is.EqualTo(null));
1404+
}
1405+
13491406
[Test]
13501407
[Category("Actions")]
13511408
public void Actions_ActionMapFindBinding_ShouldReturnNegativeOne_IfEmpty()

Packages/com.unity.inputsystem/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ however, it has to be formatted properly to pass verification tests.
1515
- Fixed InputAction.bindings.count not getting correctly updated after removing bindings with Erase().
1616
- Fixed an issue where connecting a gamepad in the editor with certain settings will cause memory and performance to degrade ([case UUM-19480](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-19480)).
1717
- Fixed issue leading to a stack overflow crash during device initialization in `InsertControlBitRangeNode` (case ISXB-405).
18+
- Fixed the issue where saving and loading override bindings to JSON would set unassigned overrides (that were `null`) to assigned overrides (as an empty string `""`).
1819

1920
## [1.5.0] - 2023-01-24
2021

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionMap.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1463,15 +1463,25 @@ internal struct BindingOverrideJson
14631463
public string interactions;
14641464
public string processors;
14651465

1466-
public static BindingOverrideJson FromBinding(InputBinding binding)
1466+
public static BindingOverrideJson FromBinding(InputBinding binding, InputAction bindingAction = null)
14671467
{
14681468
return new BindingOverrideJson
14691469
{
1470-
action = binding.action,
1471-
id = binding.m_Id,
1472-
path = binding.overridePath,
1473-
interactions = binding.overrideInteractions,
1474-
processors = binding.overrideProcessors,
1470+
action = bindingAction != null && !bindingAction.isSingletonAction ? $"{bindingAction.actionMap.name}/{bindingAction.name}" : "",
1471+
id = binding.id.ToString() ,
1472+
path = binding.overridePath ?? "null",
1473+
interactions = binding.overrideInteractions ?? "null",
1474+
processors = binding.overrideProcessors ?? "null"
1475+
};
1476+
}
1477+
1478+
public static InputBinding ToBinding(BindingOverrideJson bindingOverride)
1479+
{
1480+
return new InputBinding
1481+
{
1482+
overridePath = bindingOverride.path != "null" ? bindingOverride.path : null,
1483+
overrideInteractions = bindingOverride.interactions != "null" ? bindingOverride.interactions : null,
1484+
overrideProcessors = bindingOverride.processors != "null" ? bindingOverride.processors : null,
14751485
};
14761486
}
14771487
}

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionRebindingExtensions.cs

+2-17
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,6 @@ public static void ApplyBindingOverrides(this InputActionMap actionMap, IEnumera
891891
if (overrides == null)
892892
throw new ArgumentNullException(nameof(overrides));
893893

894-
895894
foreach (var binding in overrides)
896895
ApplyBindingOverride(actionMap, binding);
897896
}
@@ -903,7 +902,6 @@ public static void RemoveBindingOverrides(this InputActionMap actionMap, IEnumer
903902
if (overrides == null)
904903
throw new ArgumentNullException(nameof(overrides));
905904

906-
907905
foreach (var binding in overrides)
908906
RemoveBindingOverride(actionMap, binding);
909907
}
@@ -1145,14 +1143,7 @@ private static void AddBindingOverrideJsonTo(this IInputActionCollection2 action
11451143
if (action == null)
11461144
action = actions.FindAction(binding.action);
11471145

1148-
var @override = new InputActionMap.BindingOverrideJson
1149-
{
1150-
action = action != null && !action.isSingletonAction ? $"{action.actionMap.name}/{action.name}" : null,
1151-
id = binding.id.ToString(),
1152-
path = binding.overridePath,
1153-
interactions = binding.overrideInteractions,
1154-
processors = binding.overrideProcessors
1155-
};
1146+
var @override = InputActionMap.BindingOverrideJson.FromBinding(binding, action);
11561147

11571148
list.Add(@override);
11581149
}
@@ -1262,16 +1253,10 @@ private static void LoadBindingOverridesFromJsonInternal(this IInputActionCollec
12621253
var bindingIndex = actions.FindBinding(new InputBinding { m_Id = entry.id }, out var action);
12631254
if (bindingIndex != -1)
12641255
{
1265-
action.ApplyBindingOverride(bindingIndex, new InputBinding
1266-
{
1267-
overridePath = entry.path,
1268-
overrideInteractions = entry.interactions,
1269-
overrideProcessors = entry.processors,
1270-
});
1256+
action.ApplyBindingOverride(bindingIndex, InputActionMap.BindingOverrideJson.ToBinding(entry));
12711257
continue;
12721258
}
12731259
}
1274-
12751260
Debug.LogWarning("Could not override binding as no existing binding was found with the id: " + entry.id);
12761261
}
12771262
}

0 commit comments

Comments
 (0)