@@ -845,69 +845,75 @@ public InputControlLayout TryLoadControlLayout(InternedString name)
845845 ////FIXME: allowing the description to be modified as part of this is surprising; find a better way
846846 public InternedString TryFindMatchingControlLayout ( ref InputDeviceDescription deviceDescription , int deviceId = InputDevice . InvalidDeviceId )
847847 {
848- Profiler . BeginSample ( "InputSystem.TryFindMatchingControlLayout" ) ;
849- ////TODO: this will want to take overrides into account
850-
851- // See if we can match by description.
852- var layoutName = m_Layouts . TryFindMatchingLayout ( deviceDescription ) ;
853- if ( layoutName . IsEmpty ( ) )
848+ InternedString layoutName = new InternedString ( string . Empty ) ;
849+ try
854850 {
855- // No, so try to match by device class. If we have a "Gamepad" layout,
856- // for example, a device that classifies itself as a "Gamepad" will match
857- // that layout.
858- //
859- // NOTE: Have to make sure here that we get a device layout and not a
860- // control layout.
861- if ( ! string . IsNullOrEmpty ( deviceDescription . deviceClass ) )
851+ Profiler . BeginSample ( "InputSystem.TryFindMatchingControlLayout" ) ;
852+ ////TODO: this will want to take overrides into account
853+
854+ // See if we can match by description.
855+ layoutName = m_Layouts . TryFindMatchingLayout ( deviceDescription ) ;
856+ if ( layoutName . IsEmpty ( ) )
862857 {
863- var deviceClassLowerCase = new InternedString ( deviceDescription . deviceClass ) ;
864- var type = m_Layouts . GetControlTypeForLayout ( deviceClassLowerCase ) ;
865- if ( type != null && typeof ( InputDevice ) . IsAssignableFrom ( type ) )
866- layoutName = new InternedString ( deviceDescription . deviceClass ) ;
858+ // No, so try to match by device class. If we have a "Gamepad" layout,
859+ // for example, a device that classifies itself as a "Gamepad" will match
860+ // that layout.
861+ //
862+ // NOTE: Have to make sure here that we get a device layout and not a
863+ // control layout.
864+ if ( ! string . IsNullOrEmpty ( deviceDescription . deviceClass ) )
865+ {
866+ var deviceClassLowerCase = new InternedString ( deviceDescription . deviceClass ) ;
867+ var type = m_Layouts . GetControlTypeForLayout ( deviceClassLowerCase ) ;
868+ if ( type != null && typeof ( InputDevice ) . IsAssignableFrom ( type ) )
869+ layoutName = new InternedString ( deviceDescription . deviceClass ) ;
870+ }
867871 }
868- }
869872
870- ////REVIEW: listeners registering new layouts from in here may potentially lead to the creation of devices; should we disallow that?
871- ////REVIEW: if a callback picks a layout, should we re-run through the list of callbacks? or should we just remove haveOverridenLayoutName?
872- // Give listeners a shot to select/create a layout.
873- if ( m_DeviceFindLayoutCallbacks . length > 0 )
874- {
875- // First time we get here, put our delegate for executing device commands
876- // in place. We wrap the call to IInputRuntime.DeviceCommand so that we don't
877- // need to expose the runtime to the onFindLayoutForDevice callbacks.
878- if ( m_DeviceFindExecuteCommandDelegate == null )
879- m_DeviceFindExecuteCommandDelegate =
880- ( ref InputDeviceCommand commandRef ) =>
881- {
882- if ( m_DeviceFindExecuteCommandDeviceId == InputDevice . InvalidDeviceId )
883- return InputDeviceCommand . GenericFailure ;
884- return m_Runtime . DeviceCommand ( m_DeviceFindExecuteCommandDeviceId , ref commandRef ) ;
885- } ;
886- m_DeviceFindExecuteCommandDeviceId = deviceId ;
887-
888- var haveOverriddenLayoutName = false ;
889- m_DeviceFindLayoutCallbacks . LockForChanges ( ) ;
890- for ( var i = 0 ; i < m_DeviceFindLayoutCallbacks . length ; ++ i )
873+ ////REVIEW: listeners registering new layouts from in here may potentially lead to the creation of devices; should we disallow that?
874+ ////REVIEW: if a callback picks a layout, should we re-run through the list of callbacks? or should we just remove haveOverridenLayoutName?
875+ // Give listeners a shot to select/create a layout.
876+ if ( m_DeviceFindLayoutCallbacks . length > 0 )
891877 {
892- try
878+ // First time we get here, put our delegate for executing device commands
879+ // in place. We wrap the call to IInputRuntime.DeviceCommand so that we don't
880+ // need to expose the runtime to the onFindLayoutForDevice callbacks.
881+ if ( m_DeviceFindExecuteCommandDelegate == null )
882+ m_DeviceFindExecuteCommandDelegate =
883+ ( ref InputDeviceCommand commandRef ) =>
884+ {
885+ if ( m_DeviceFindExecuteCommandDeviceId == InputDevice . InvalidDeviceId )
886+ return InputDeviceCommand . GenericFailure ;
887+ return m_Runtime . DeviceCommand ( m_DeviceFindExecuteCommandDeviceId , ref commandRef ) ;
888+ } ;
889+ m_DeviceFindExecuteCommandDeviceId = deviceId ;
890+
891+ var haveOverriddenLayoutName = false ;
892+ m_DeviceFindLayoutCallbacks . LockForChanges ( ) ;
893+ for ( var i = 0 ; i < m_DeviceFindLayoutCallbacks . length ; ++ i )
893894 {
894- var newLayout = m_DeviceFindLayoutCallbacks [ i ] ( ref deviceDescription , layoutName , m_DeviceFindExecuteCommandDelegate ) ;
895- if ( ! string . IsNullOrEmpty ( newLayout ) && ! haveOverriddenLayoutName )
895+ try
896896 {
897- layoutName = new InternedString ( newLayout ) ;
898- haveOverriddenLayoutName = true ;
897+ var newLayout = m_DeviceFindLayoutCallbacks [ i ] ( ref deviceDescription , layoutName , m_DeviceFindExecuteCommandDelegate ) ;
898+ if ( ! string . IsNullOrEmpty ( newLayout ) && ! haveOverriddenLayoutName )
899+ {
900+ layoutName = new InternedString ( newLayout ) ;
901+ haveOverriddenLayoutName = true ;
902+ }
903+ }
904+ catch ( Exception exception )
905+ {
906+ Debug . LogError ( $ "{ exception . GetType ( ) . Name } while executing 'InputSystem.onFindLayoutForDevice' callbacks") ;
907+ Debug . LogException ( exception ) ;
899908 }
900909 }
901- catch ( Exception exception )
902- {
903- Debug . LogError ( $ "{ exception . GetType ( ) . Name } while executing 'InputSystem.onFindLayoutForDevice' callbacks") ;
904- Debug . LogException ( exception ) ;
905- }
910+ m_DeviceFindLayoutCallbacks . UnlockForChanges ( ) ;
906911 }
907- m_DeviceFindLayoutCallbacks . UnlockForChanges ( ) ;
908912 }
909-
910- Profiler . EndSample ( ) ;
913+ finally
914+ {
915+ Profiler . EndSample ( ) ;
916+ }
911917 return layoutName ;
912918 }
913919
@@ -1259,7 +1265,10 @@ public InputDevice AddDevice(InputDeviceDescription description, bool throwIfNoL
12591265 if ( layout . IsEmpty ( ) )
12601266 {
12611267 if ( throwIfNoLayoutFound )
1268+ {
1269+ Profiler . EndSample ( ) ;
12621270 throw new ArgumentException ( $ "Cannot find layout matching device description '{ description } '", nameof ( description ) ) ;
1271+ }
12631272
12641273 // If it's a device coming from the runtime, disable it.
12651274 if ( deviceId != InputDevice . InvalidDeviceId )
@@ -2844,7 +2853,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
28442853 Profiler . BeginSample ( "InputUpdate" ) ;
28452854
28462855 if ( m_InputEventStream . isOpen )
2856+ {
2857+ Profiler . EndSample ( ) ;
28472858 throw new InvalidOperationException ( "Already have an event buffer set! Was OnUpdate() called recursively?" ) ;
2859+ }
28482860
28492861 // Restore devices before checking update mask. See InputSystem.RunInitialUpdate().
28502862 RestoreDevicesAfterDomainReloadIfNecessary ( ) ;
@@ -3205,7 +3217,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
32053217 var shouldProcess = ( ( IEventPreProcessor ) device ) . PreProcessEvent ( currentEventReadPtr ) ;
32063218#if UNITY_EDITOR
32073219 if ( currentEventReadPtr ->sizeInBytes > eventSizeBeforePreProcessor )
3220+ {
3221+ Profiler . EndSample ( ) ;
32083222 throw new AccessViolationException ( $ "'{ device } '.PreProcessEvent tries to grow an event from { eventSizeBeforePreProcessor } bytes to { currentEventReadPtr ->sizeInBytes } bytes, this will potentially corrupt events after the current event and/or cause out-of-bounds memory access.") ;
3223+ }
32093224#endif
32103225 if ( ! shouldProcess )
32113226 {
@@ -3386,6 +3401,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
33863401 {
33873402 // We need to restore m_InputEventStream to a sound state
33883403 // to avoid failing recursive OnUpdate check next frame.
3404+ Profiler . EndSample ( ) ;
33893405 m_InputEventStream . CleanUpAfterException ( ) ;
33903406 throw ;
33913407 }
0 commit comments