@@ -845,69 +845,75 @@ public InputControlLayout TryLoadControlLayout(InternedString name)
845
845
////FIXME: allowing the description to be modified as part of this is surprising; find a better way
846
846
public InternedString TryFindMatchingControlLayout ( ref InputDeviceDescription deviceDescription , int deviceId = InputDevice . InvalidDeviceId )
847
847
{
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
854
850
{
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 ( ) )
862
857
{
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
+ }
867
871
}
868
- }
869
872
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 )
891
877
{
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 )
893
894
{
894
- var newLayout = m_DeviceFindLayoutCallbacks [ i ] ( ref deviceDescription , layoutName , m_DeviceFindExecuteCommandDelegate ) ;
895
- if ( ! string . IsNullOrEmpty ( newLayout ) && ! haveOverriddenLayoutName )
895
+ try
896
896
{
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 ) ;
899
908
}
900
909
}
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 ( ) ;
906
911
}
907
- m_DeviceFindLayoutCallbacks . UnlockForChanges ( ) ;
908
912
}
909
-
910
- Profiler . EndSample ( ) ;
913
+ finally
914
+ {
915
+ Profiler . EndSample ( ) ;
916
+ }
911
917
return layoutName ;
912
918
}
913
919
@@ -1259,7 +1265,10 @@ public InputDevice AddDevice(InputDeviceDescription description, bool throwIfNoL
1259
1265
if ( layout . IsEmpty ( ) )
1260
1266
{
1261
1267
if ( throwIfNoLayoutFound )
1268
+ {
1269
+ Profiler . EndSample ( ) ;
1262
1270
throw new ArgumentException ( $ "Cannot find layout matching device description '{ description } '", nameof ( description ) ) ;
1271
+ }
1263
1272
1264
1273
// If it's a device coming from the runtime, disable it.
1265
1274
if ( deviceId != InputDevice . InvalidDeviceId )
@@ -2844,7 +2853,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
2844
2853
Profiler . BeginSample ( "InputUpdate" ) ;
2845
2854
2846
2855
if ( m_InputEventStream . isOpen )
2856
+ {
2857
+ Profiler . EndSample ( ) ;
2847
2858
throw new InvalidOperationException ( "Already have an event buffer set! Was OnUpdate() called recursively?" ) ;
2859
+ }
2848
2860
2849
2861
// Restore devices before checking update mask. See InputSystem.RunInitialUpdate().
2850
2862
RestoreDevicesAfterDomainReloadIfNecessary ( ) ;
@@ -3205,7 +3217,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
3205
3217
var shouldProcess = ( ( IEventPreProcessor ) device ) . PreProcessEvent ( currentEventReadPtr ) ;
3206
3218
#if UNITY_EDITOR
3207
3219
if ( currentEventReadPtr ->sizeInBytes > eventSizeBeforePreProcessor )
3220
+ {
3221
+ Profiler . EndSample ( ) ;
3208
3222
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
+ }
3209
3224
#endif
3210
3225
if ( ! shouldProcess )
3211
3226
{
@@ -3386,6 +3401,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
3386
3401
{
3387
3402
// We need to restore m_InputEventStream to a sound state
3388
3403
// to avoid failing recursive OnUpdate check next frame.
3404
+ Profiler . EndSample ( ) ;
3389
3405
m_InputEventStream . CleanUpAfterException ( ) ;
3390
3406
throw ;
3391
3407
}
0 commit comments