Skip to content

Commit 9c476d1

Browse files
authored
NEW: Added InputDeviceMatcher.WithManufacturerContains(string noRegexMatch) to improve DualShockSupport initialization performance (#1990)
* [InputSystem] Added WithManufacturerContains.WithManufacturerContains(string) - improve DualShockSupport initialization performance o WithManufacturerContains() provides for simple keyword (no regex) matching in the manufacturer string o Changes DualShock4Gamepad / DualShock3Gamepad matchers to avoid regex creation on initialization These changes save 25ms in DualShockSupport.Initialize during player start-up (26ms -> 1ms) * [InputSystem] Improve parameter name: WithManufacturerContains(string noRegexMatch), added chanelog entry (ISX-1411) * [InputSystem] Fixed typo in CHANGELOG * [InputSystem] Fix string check in MatchSinglePropertyContains() o Was looking for manufacturer string in pattern not pattern in manufacturer string (doh!) * [InputSystem] Fix formatting of modified files (used unity-meta format tool)
1 parent 41a977c commit 9c476d1

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ however, it has to be formatted properly to pass verification tests.
1919

2020
### Added
2121
- Added Hinge Angle sensor support for foldable devices.
22+
- Added InputDeviceMatcher.WithManufacturerContains(string noRegexMatch) API to improve DualShockSupport.Initialize performance (ISX-1411)
2223

2324
### Changed
2425
- Use `ProfilerMarker` instead of `Profiler.BeginSample` and `Profiler.EndSample` when appropriate to enable recording of profiling data.

Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ public InputDeviceMatcher WithManufacturer(string pattern, bool supportRegex = t
142142
return With(kManufacturerKey, pattern, supportRegex);
143143
}
144144

145+
/// <summary>
146+
/// Add a pattern (simple string) to <see cref="patterns"/> to match a <see cref="InputDeviceDescription.manufacturer"/>.
147+
/// </summary>
148+
/// <param name="noRegExPattern">String to match - simple keyword search in a device manufacturer string (eg "SONY").</param>
149+
/// <returns>The modified device matcher with the added pattern.</returns>
150+
/// <seealso cref="InputDeviceDescription.manufacturer"/>
151+
public InputDeviceMatcher WithManufacturerContains(string noRegExPattern)
152+
{
153+
return With(kManufacturerContainsKey, noRegExPattern, supportRegex: false);
154+
}
155+
145156
/// <summary>
146157
/// Add a pattern to <see cref="patterns"/> to match a <see cref="InputDeviceDescription.product"/>.
147158
/// </summary>
@@ -309,6 +320,12 @@ public float MatchPercentage(InputDeviceDescription deviceDescription)
309320
|| !MatchSingleProperty(pattern, deviceDescription.manufacturer))
310321
return 0;
311322
}
323+
else if (key == kManufacturerContainsKey)
324+
{
325+
if (string.IsNullOrEmpty(deviceDescription.manufacturer)
326+
|| !MatchSinglePropertyContains(pattern, deviceDescription.manufacturer))
327+
return 0;
328+
}
312329
else if (key == kProductKey)
313330
{
314331
if (string.IsNullOrEmpty(deviceDescription.product)
@@ -357,6 +374,15 @@ private static bool MatchSingleProperty(object pattern, string value)
357374
return false;
358375
}
359376

377+
private static bool MatchSinglePropertyContains(object pattern, string value)
378+
{
379+
// String match.
380+
if (pattern is string str)
381+
return value.Contains(str, StringComparison.OrdinalIgnoreCase);
382+
383+
return false;
384+
}
385+
360386
private static int GetNumPropertiesIn(InputDeviceDescription description)
361387
{
362388
var count = 0;
@@ -518,6 +544,7 @@ public override int GetHashCode()
518544
private static readonly InternedString kInterfaceKey = new InternedString("interface");
519545
private static readonly InternedString kDeviceClassKey = new InternedString("deviceClass");
520546
private static readonly InternedString kManufacturerKey = new InternedString("manufacturer");
547+
private static readonly InternedString kManufacturerContainsKey = new InternedString("manufacturerContains");
521548
private static readonly InternedString kProductKey = new InternedString("product");
522549
private static readonly InternedString kVersionKey = new InternedString("version");
523550

@@ -529,6 +556,7 @@ internal struct MatcherJson
529556
public string deviceClass;
530557
public string[] deviceClasses;
531558
public string manufacturer;
559+
public string manufacturerContains;
532560
public string[] manufacturers;
533561
public string product;
534562
public string[] products;
@@ -617,13 +645,17 @@ public InputDeviceMatcher ToMatcher()
617645
foreach (var value in deviceClasses)
618646
matcher = matcher.WithDeviceClass(value);
619647

620-
// Manufacturer.
648+
// Manufacturer (string or regex)
621649
if (!string.IsNullOrEmpty(manufacturer))
622650
matcher = matcher.WithManufacturer(manufacturer);
623651
if (manufacturers != null)
624652
foreach (var value in manufacturers)
625653
matcher = matcher.WithManufacturer(value);
626654

655+
// ManufacturerContains (simple string, can occur anywhere in the reported manufacturer string)
656+
if (!string.IsNullOrEmpty(manufacturerContains))
657+
matcher = matcher.WithManufacturerContains(manufacturerContains);
658+
627659
// Product.
628660
if (!string.IsNullOrEmpty(product))
629661
matcher = matcher.WithProduct(product);

Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static void Initialize()
5353
InputSystem.RegisterLayoutMatcher<DualShock4GamepadHID>(
5454
new InputDeviceMatcher()
5555
.WithInterface("HID")
56-
.WithManufacturer("Sony.+Entertainment")
56+
.WithManufacturerContains("Sony")
57+
.WithManufacturerContains("Entertainment")
5758
.WithProduct("Wireless Controller"));
5859

5960
InputSystem.RegisterLayout<DualShock3GamepadHID>(
@@ -65,8 +66,9 @@ public static void Initialize()
6566
InputSystem.RegisterLayoutMatcher<DualShock3GamepadHID>(
6667
new InputDeviceMatcher()
6768
.WithInterface("HID")
68-
.WithManufacturer("Sony.+Entertainment")
69-
.WithProduct("PLAYSTATION(R)3 Controller"));
69+
.WithManufacturerContains("Sony")
70+
.WithManufacturerContains("Entertainment")
71+
.WithProduct("PLAYSTATION(R)3 Controller", supportRegex: false));
7072
#endif
7173
}
7274
}

0 commit comments

Comments
 (0)