Skip to content

Commit 0df299e

Browse files
MakeEscapedJsonString now null-checks inputs. Empty strings also now bypass an allocation.
This matches older behaviour on null-checking descriptor fields which is needed for certain device implementations.
1 parent 82ec624 commit 0df299e

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,13 +4162,40 @@ public void Devices_RemovingAndReaddingDevice_DoesNotAllocateMemory()
41624162
recorder.CollectFromAllThreads();
41634163
#endif
41644164

4165-
// We expect a single allocation for each call to ReportNewInputDevice when there is one disconnected device
4166-
//
4167-
int numberOfRepeats = 2;
4168-
int numberOfDisconnectedDevices = 1;
4169-
int numberOfCallsToReportNewInputDevicePerRun = 2;
4170-
int expectedAllocations = numberOfRepeats * numberOfDisconnectedDevices * numberOfCallsToReportNewInputDevicePerRun;
4171-
Assert.AreEqual(expectedAllocations, recorder.sampleBlockCount);
4165+
// No allocations are expected.
4166+
Assert.AreEqual(0, recorder.sampleBlockCount);
4167+
}
4168+
4169+
// Regression test to cover having null descriptor fields for a device. Some non-desktop gamepad device types do this.
4170+
[Test]
4171+
[Category("Devices")]
4172+
public void Devices_RemovingAndReaddingDeviceWithNullDescriptorFields_DoesNotThrow()
4173+
{
4174+
// InputDeviceDescription.ToJson writes empty string fields and not null values, whereas reporting a device via an incomplete description string will fully omit the fields.
4175+
string description = @"{
4176+
""type"": ""Gamepad"",
4177+
""product"": ""TestProduct""
4178+
}";
4179+
4180+
var deviceId = runtime.ReportNewInputDevice(description);
4181+
InputSystem.Update();
4182+
4183+
// "Unplug" device.
4184+
var removeEvent1 = DeviceRemoveEvent.Create(deviceId);
4185+
InputSystem.QueueEvent(ref removeEvent1);
4186+
InputSystem.Update();
4187+
4188+
// "Plug" it back in.
4189+
deviceId = runtime.ReportNewInputDevice(description);
4190+
InputSystem.Update();
4191+
4192+
// Repeat that sequence.
4193+
var removeEvent2 = DeviceRemoveEvent.Create(deviceId);
4194+
InputSystem.QueueEvent(ref removeEvent2);
4195+
InputSystem.Update();
4196+
4197+
runtime.ReportNewInputDevice(description);
4198+
InputSystem.Update();
41724199
}
41734200

41744201
[Test]

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,16 @@ private JsonParser.JsonString MakeEscapedJsonString(string theString)
25622562
// To avoid a very costly escape-skipping character-by-character string comparison in JsonParser.Json.Equals() we
25632563
// reconstruct an escaped string and make an escaped JsonParser.JsonString and use that for the comparison instead.
25642564
//
2565+
2566+
if (string.IsNullOrEmpty(theString))
2567+
{
2568+
return new JsonParser.JsonString
2569+
{
2570+
text = string.Empty, // text should be an empty string and not null for consistency on property comparisons
2571+
hasEscapes = false
2572+
};
2573+
}
2574+
25652575
var builder = new StringBuilder();
25662576
var length = theString.Length;
25672577
var hasEscapes = false;

0 commit comments

Comments
 (0)