From 340ddc27c4c77a778dbba3fe9e6c6b65ced5c92c Mon Sep 17 00:00:00 2001 From: Alex Tyrer Date: Fri, 23 Aug 2024 14:56:18 +0100 Subject: [PATCH 1/5] [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/Devices/InputDeviceMatcher.cs | 34 ++++++++++++++++++- .../Plugins/DualShock/DualShockSupport.cs | 8 +++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs index 496273f5a5..e633140609 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs @@ -142,6 +142,17 @@ public InputDeviceMatcher WithManufacturer(string pattern, bool supportRegex = t return With(kManufacturerKey, pattern, supportRegex); } + /// + /// Add a pattern (simple string) to to match a . + /// + /// String to match - simple keyword search in a device manufacturer string (eg "SONY"). + /// The modified device matcher with the added pattern. + /// + public InputDeviceMatcher WithManufacturerContains(string pattern) + { + return With(kManufacturerContainsKey, pattern, supportRegex:false); + } + /// /// Add a pattern to to match a . /// @@ -309,6 +320,12 @@ public float MatchPercentage(InputDeviceDescription deviceDescription) || !MatchSingleProperty(pattern, deviceDescription.manufacturer)) return 0; } + else if (key == kManufacturerContainsKey) + { + if (string.IsNullOrEmpty(deviceDescription.manufacturer) + || !MatchSinglePropertyContains(pattern, deviceDescription.manufacturer)) + return 0; + } else if (key == kProductKey) { if (string.IsNullOrEmpty(deviceDescription.product) @@ -357,6 +374,15 @@ private static bool MatchSingleProperty(object pattern, string value) return false; } + private static bool MatchSinglePropertyContains(object pattern, string value) + { + // String match. + if (pattern is string str) + return str.Contains(value, StringComparison.OrdinalIgnoreCase); + + return false; + } + private static int GetNumPropertiesIn(InputDeviceDescription description) { var count = 0; @@ -518,6 +544,7 @@ public override int GetHashCode() private static readonly InternedString kInterfaceKey = new InternedString("interface"); private static readonly InternedString kDeviceClassKey = new InternedString("deviceClass"); private static readonly InternedString kManufacturerKey = new InternedString("manufacturer"); + private static readonly InternedString kManufacturerContainsKey = new InternedString("manufacturerContains"); private static readonly InternedString kProductKey = new InternedString("product"); private static readonly InternedString kVersionKey = new InternedString("version"); @@ -529,6 +556,7 @@ internal struct MatcherJson public string deviceClass; public string[] deviceClasses; public string manufacturer; + public string manufacturerContains; public string[] manufacturers; public string product; public string[] products; @@ -617,13 +645,17 @@ public InputDeviceMatcher ToMatcher() foreach (var value in deviceClasses) matcher = matcher.WithDeviceClass(value); - // Manufacturer. + // Manufacturer (string or regex) if (!string.IsNullOrEmpty(manufacturer)) matcher = matcher.WithManufacturer(manufacturer); if (manufacturers != null) foreach (var value in manufacturers) matcher = matcher.WithManufacturer(value); + // ManufacturerContains (simple string, can occur anywhere in the reported manufacturer string) + if (!string.IsNullOrEmpty(manufacturerContains)) + matcher = matcher.WithManufacturerContains(manufacturerContains); + // Product. if (!string.IsNullOrEmpty(product)) matcher = matcher.WithProduct(product); diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs index cd8f2cd3d9..f217f38d02 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs @@ -53,7 +53,8 @@ public static void Initialize() InputSystem.RegisterLayoutMatcher( new InputDeviceMatcher() .WithInterface("HID") - .WithManufacturer("Sony.+Entertainment") + .WithManufacturerContains("Sony") + .WithManufacturerContains("Entertainment") .WithProduct("Wireless Controller")); InputSystem.RegisterLayout( @@ -65,8 +66,9 @@ public static void Initialize() InputSystem.RegisterLayoutMatcher( new InputDeviceMatcher() .WithInterface("HID") - .WithManufacturer("Sony.+Entertainment") - .WithProduct("PLAYSTATION(R)3 Controller")); + .WithManufacturerContains("Sony") + .WithManufacturerContains("Entertainment") + .WithProduct("PLAYSTATION(R)3 Controller", supportRegex:false)); #endif } } From 6c5d159b9d4d9d66bdff59fc95d9bce308fd589f Mon Sep 17 00:00:00 2001 From: Alex Tyrer Date: Fri, 23 Aug 2024 15:17:10 +0100 Subject: [PATCH 2/5] [InputSystem] Improve parameter name: WithManufacturerContains(string noRegexMatch), added chanelog entry (ISX-1411) --- Packages/com.unity.inputsystem/CHANGELOG.md | 1 + .../InputSystem/Devices/InputDeviceMatcher.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 5c1a6882d3..0fd3874353 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -19,6 +19,7 @@ however, it has to be formatted properly to pass verification tests. ### Added - Added Hinge Angle sensor support for foldable devices. +- Added InputDeviceMatcher.WithManufacturerContains(string noRegexMatch) API to improved DualShockSupport.Initialize performance (ISX-1411) ### Changed - Use `ProfilerMarker` instead of `Profiler.BeginSample` and `Profiler.EndSample` when appropriate to enable recording of profiling data. diff --git a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs index e633140609..cb1dbfb675 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs @@ -145,12 +145,12 @@ public InputDeviceMatcher WithManufacturer(string pattern, bool supportRegex = t /// /// Add a pattern (simple string) to to match a . /// - /// String to match - simple keyword search in a device manufacturer string (eg "SONY"). + /// String to match - simple keyword search in a device manufacturer string (eg "SONY"). /// The modified device matcher with the added pattern. /// - public InputDeviceMatcher WithManufacturerContains(string pattern) + public InputDeviceMatcher WithManufacturerContains(string noRegExPattern) { - return With(kManufacturerContainsKey, pattern, supportRegex:false); + return With(kManufacturerContainsKey, noRegExPattern, supportRegex:false); } /// From c697d5b1d562889294f52ab3c09245c3c42dd098 Mon Sep 17 00:00:00 2001 From: Alex Tyrer Date: Fri, 23 Aug 2024 15:32:44 +0100 Subject: [PATCH 3/5] [InputSystem] Fixed typo in CHANGELOG --- Packages/com.unity.inputsystem/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 0fd3874353..d5f36cabfe 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -19,7 +19,7 @@ however, it has to be formatted properly to pass verification tests. ### Added - Added Hinge Angle sensor support for foldable devices. -- Added InputDeviceMatcher.WithManufacturerContains(string noRegexMatch) API to improved DualShockSupport.Initialize performance (ISX-1411) +- Added InputDeviceMatcher.WithManufacturerContains(string noRegexMatch) API to improve DualShockSupport.Initialize performance (ISX-1411) ### Changed - Use `ProfilerMarker` instead of `Profiler.BeginSample` and `Profiler.EndSample` when appropriate to enable recording of profiling data. From 849e96dfcd7db03c93cef23dfae64f71b610bbc8 Mon Sep 17 00:00:00 2001 From: Alex Tyrer Date: Fri, 23 Aug 2024 17:27:37 +0100 Subject: [PATCH 4/5] [InputSystem] Fix string check in MatchSinglePropertyContains() o Was looking for manufacturer string in pattern not pattern in manufacturer string (doh!) --- .../InputSystem/Devices/InputDeviceMatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs index cb1dbfb675..18237eaf9f 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs @@ -378,7 +378,7 @@ private static bool MatchSinglePropertyContains(object pattern, string value) { // String match. if (pattern is string str) - return str.Contains(value, StringComparison.OrdinalIgnoreCase); + return value.Contains(str, StringComparison.OrdinalIgnoreCase); return false; } From 58be2b9b2062046bdbf6d4657210043d4a852d23 Mon Sep 17 00:00:00 2001 From: Alex Tyrer Date: Fri, 23 Aug 2024 17:41:16 +0100 Subject: [PATCH 5/5] [InputSystem] Fix formatting of modified files (used unity-meta format tool) --- .../InputSystem/Devices/InputDeviceMatcher.cs | 4 ++-- .../InputSystem/Plugins/DualShock/DualShockSupport.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs index 18237eaf9f..2292ad5058 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Devices/InputDeviceMatcher.cs @@ -150,7 +150,7 @@ public InputDeviceMatcher WithManufacturer(string pattern, bool supportRegex = t /// public InputDeviceMatcher WithManufacturerContains(string noRegExPattern) { - return With(kManufacturerContainsKey, noRegExPattern, supportRegex:false); + return With(kManufacturerContainsKey, noRegExPattern, supportRegex: false); } /// @@ -323,7 +323,7 @@ public float MatchPercentage(InputDeviceDescription deviceDescription) else if (key == kManufacturerContainsKey) { if (string.IsNullOrEmpty(deviceDescription.manufacturer) - || !MatchSinglePropertyContains(pattern, deviceDescription.manufacturer)) + || !MatchSinglePropertyContains(pattern, deviceDescription.manufacturer)) return 0; } else if (key == kProductKey) diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs index f217f38d02..851b3bb734 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/DualShock/DualShockSupport.cs @@ -68,7 +68,7 @@ public static void Initialize() .WithInterface("HID") .WithManufacturerContains("Sony") .WithManufacturerContains("Entertainment") - .WithProduct("PLAYSTATION(R)3 Controller", supportRegex:false)); + .WithProduct("PLAYSTATION(R)3 Controller", supportRegex: false)); #endif } }