@@ -127,7 +127,7 @@ internal unsafe class InputActionState : IInputStateChangeMonitor, ICloneable, I
127127 public void Initialize ( InputBindingResolver resolver )
128128 {
129129 ClaimDataFrom ( resolver ) ;
130- AddToGlobaList ( ) ;
130+ AddToGlobalList ( ) ;
131131 }
132132
133133 internal void ClaimDataFrom ( InputBindingResolver resolver )
@@ -390,7 +390,7 @@ public void RestoreActionStates(UnmanagedMemory oldState)
390390 HookOnBeforeUpdate ( ) ;
391391
392392 // Fire notifications.
393- if ( s_OnActionChange . length > 0 )
393+ if ( s_GlobalState . onActionChange . length > 0 )
394394 {
395395 for ( var i = 0 ; i < totalMapCount ; ++ i )
396396 {
@@ -1955,7 +1955,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
19551955 {
19561956 // If there's no listeners, don't bother with anything else.
19571957 var callbacksOnMap = actionMap . m_ActionCallbacks ;
1958- if ( listeners . length == 0 && callbacksOnMap . length == 0 && s_OnActionChange . length == 0 )
1958+ if ( listeners . length == 0 && callbacksOnMap . length == 0 && s_GlobalState . onActionChange . length == 0 )
19591959 return ;
19601960
19611961 var context = new InputAction . CallbackContext
@@ -1968,7 +1968,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
19681968
19691969 // Global callback goes first.
19701970 var action = context . action ;
1971- if ( s_OnActionChange . length > 0 )
1971+ if ( s_GlobalState . onActionChange . length > 0 )
19721972 {
19731973 InputActionChange change ;
19741974 switch ( phase )
@@ -1987,7 +1987,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
19871987 return ;
19881988 }
19891989
1990- DelegateHelpers . InvokeCallbacksSafe ( ref s_OnActionChange , action , change , "InputSystem.onActionChange" ) ;
1990+ DelegateHelpers . InvokeCallbacksSafe ( ref s_GlobalState . onActionChange , action , change , "InputSystem.onActionChange" ) ;
19911991 }
19921992
19931993 // Run callbacks (if any) directly on action.
@@ -3607,7 +3607,7 @@ public UnmanagedMemory Clone()
36073607 #region Global State
36083608
36093609 /// <summary>
3610- /// List of weak references to all action map states currently in the system.
3610+ /// Global state containing a list of weak references to all action map states currently in the system.
36113611 /// </summary>
36123612 /// <remarks>
36133613 /// When the control setup in the system changes, we need a way for control resolution that
@@ -3616,25 +3616,44 @@ public UnmanagedMemory Clone()
36163616 ///
36173617 /// Both of these needs are served by this global list.
36183618 /// </remarks>
3619- internal static InlinedArray < GCHandle > s_GlobalList ;
3620- internal static CallbackArray < Action < object , InputActionChange > > s_OnActionChange ;
3621- internal static CallbackArray < Action < object > > s_OnActionControlsChanged ;
3619+ internal struct GlobalState
3620+ {
3621+ internal InlinedArray < GCHandle > globalList ;
3622+ internal CallbackArray < Action < object , InputActionChange > > onActionChange ;
3623+ internal CallbackArray < Action < object > > onActionControlsChanged ;
3624+ }
3625+
3626+ internal static GlobalState s_GlobalState ;
3627+
3628+ internal static ISavedState SaveAndResetState ( )
3629+ {
3630+ // Save current state
3631+ var savedState = new SavedStructState < GlobalState > (
3632+ ref s_GlobalState ,
3633+ ( ref GlobalState state ) => s_GlobalState = state , // restore
3634+ ( ) => ResetGlobals ( ) ) ; // static dispose
3635+
3636+ // Reset global state
3637+ s_GlobalState = default ;
3638+
3639+ return savedState ;
3640+ }
36223641
3623- private void AddToGlobaList ( )
3642+ private void AddToGlobalList ( )
36243643 {
36253644 CompactGlobalList ( ) ;
36263645 var handle = GCHandle . Alloc ( this , GCHandleType . Weak ) ;
3627- s_GlobalList . AppendWithCapacity ( handle ) ;
3646+ s_GlobalState . globalList . AppendWithCapacity ( handle ) ;
36283647 }
36293648
36303649 private void RemoveMapFromGlobalList ( )
36313650 {
3632- var count = s_GlobalList . length ;
3651+ var count = s_GlobalState . globalList . length ;
36333652 for ( var i = 0 ; i < count ; ++ i )
3634- if ( s_GlobalList [ i ] . Target == this )
3653+ if ( s_GlobalState . globalList [ i ] . Target == this )
36353654 {
3636- s_GlobalList [ i ] . Free ( ) ;
3637- s_GlobalList . RemoveAtByMovingTailWithCapacity ( i ) ;
3655+ s_GlobalState . globalList [ i ] . Free ( ) ;
3656+ s_GlobalState . globalList . RemoveAtByMovingTailWithCapacity ( i ) ;
36383657 break ;
36393658 }
36403659 }
@@ -3644,25 +3663,25 @@ private void RemoveMapFromGlobalList()
36443663 /// </summary>
36453664 private static void CompactGlobalList ( )
36463665 {
3647- var length = s_GlobalList . length ;
3666+ var length = s_GlobalState . globalList . length ;
36483667 var head = 0 ;
36493668 for ( var i = 0 ; i < length ; ++ i )
36503669 {
3651- var handle = s_GlobalList [ i ] ;
3670+ var handle = s_GlobalState . globalList [ i ] ;
36523671 if ( handle . IsAllocated && handle . Target != null )
36533672 {
36543673 if ( head != i )
3655- s_GlobalList [ head ] = handle ;
3674+ s_GlobalState . globalList [ head ] = handle ;
36563675 ++ head ;
36573676 }
36583677 else
36593678 {
36603679 if ( handle . IsAllocated )
3661- s_GlobalList [ i ] . Free ( ) ;
3662- s_GlobalList [ i ] = default ;
3680+ s_GlobalState . globalList [ i ] . Free ( ) ;
3681+ s_GlobalState . globalList [ i ] = default ;
36633682 }
36643683 }
3665- s_GlobalList . length = head ;
3684+ s_GlobalState . globalList . length = head ;
36663685 }
36673686
36683687 internal static void NotifyListenersOfActionChange ( InputActionChange change , object actionOrMapOrAsset )
@@ -3671,33 +3690,33 @@ internal static void NotifyListenersOfActionChange(InputActionChange change, obj
36713690 Debug . Assert ( actionOrMapOrAsset is InputAction || ( actionOrMapOrAsset as InputActionMap ) ? . m_SingletonAction == null ,
36723691 "Must not send notifications for changes made to hidden action maps of singleton actions" ) ;
36733692
3674- DelegateHelpers . InvokeCallbacksSafe ( ref s_OnActionChange , actionOrMapOrAsset , change , "onActionChange" ) ;
3693+ DelegateHelpers . InvokeCallbacksSafe ( ref s_GlobalState . onActionChange , actionOrMapOrAsset , change , "onActionChange" ) ;
36753694 if ( change == InputActionChange . BoundControlsChanged )
3676- DelegateHelpers . InvokeCallbacksSafe ( ref s_OnActionControlsChanged , actionOrMapOrAsset , "onActionControlsChange" ) ;
3695+ DelegateHelpers . InvokeCallbacksSafe ( ref s_GlobalState . onActionControlsChanged , actionOrMapOrAsset , "onActionControlsChange" ) ;
36773696 }
36783697
36793698 /// <summary>
36803699 /// Nuke global state we have to keep track of action map states.
36813700 /// </summary>
3682- internal static void ResetGlobals ( )
3701+ private static void ResetGlobals ( )
36833702 {
36843703 DestroyAllActionMapStates ( ) ;
3685- for ( var i = 0 ; i < s_GlobalList . length ; ++ i )
3686- if ( s_GlobalList [ i ] . IsAllocated )
3687- s_GlobalList [ i ] . Free ( ) ;
3688- s_GlobalList . length = 0 ;
3689- s_OnActionChange . Clear ( ) ;
3690- s_OnActionControlsChanged . Clear ( ) ;
3704+ for ( var i = 0 ; i < s_GlobalState . globalList . length ; ++ i )
3705+ if ( s_GlobalState . globalList [ i ] . IsAllocated )
3706+ s_GlobalState . globalList [ i ] . Free ( ) ;
3707+ s_GlobalState . globalList . length = 0 ;
3708+ s_GlobalState . onActionChange . Clear ( ) ;
3709+ s_GlobalState . onActionControlsChanged . Clear ( ) ;
36913710 }
36923711
36933712 // Walk all maps with enabled actions and add all enabled actions to the given list.
36943713 internal static int FindAllEnabledActions ( List < InputAction > result )
36953714 {
36963715 var numFound = 0 ;
3697- var stateCount = s_GlobalList . length ;
3716+ var stateCount = s_GlobalState . globalList . length ;
36983717 for ( var i = 0 ; i < stateCount ; ++ i )
36993718 {
3700- var handle = s_GlobalList [ i ] ;
3719+ var handle = s_GlobalState . globalList [ i ] ;
37013720 if ( ! handle . IsAllocated )
37023721 continue ;
37033722 var state = ( InputActionState ) handle . Target ;
@@ -3761,15 +3780,15 @@ internal static void OnDeviceChange(InputDevice device, InputDeviceChange change
37613780 change == InputDeviceChange . SoftReset || change == InputDeviceChange . HardReset ,
37623781 "Should only be called for relevant changes" ) ;
37633782
3764- for ( var i = 0 ; i < s_GlobalList . length ; ++ i )
3783+ for ( var i = 0 ; i < s_GlobalState . globalList . length ; ++ i )
37653784 {
3766- var handle = s_GlobalList [ i ] ;
3785+ var handle = s_GlobalState . globalList [ i ] ;
37673786 if ( ! handle . IsAllocated || handle . Target == null )
37683787 {
37693788 // Stale entry in the list. State has already been reclaimed by GC. Remove it.
37703789 if ( handle . IsAllocated )
3771- s_GlobalList [ i ] . Free ( ) ;
3772- s_GlobalList . RemoveAtWithCapacity ( i ) ;
3790+ s_GlobalState . globalList [ i ] . Free ( ) ;
3791+ s_GlobalState . globalList . RemoveAtWithCapacity ( i ) ;
37733792 -- i ;
37743793 continue ;
37753794 }
@@ -3833,15 +3852,15 @@ internal static void DeferredResolutionOfBindings()
38333852 ++ InputActionMap . s_DeferBindingResolution ;
38343853 try
38353854 {
3836- for ( var i = 0 ; i < s_GlobalList . length ; ++ i )
3855+ for ( var i = 0 ; i < s_GlobalState . globalList . length ; ++ i )
38373856 {
3838- var handle = s_GlobalList [ i ] ;
3857+ var handle = s_GlobalState . globalList [ i ] ;
38393858 if ( ! handle . IsAllocated || handle . Target == null )
38403859 {
38413860 // Stale entry in the list. State has already been reclaimed by GC. Remove it.
38423861 if ( handle . IsAllocated )
3843- s_GlobalList [ i ] . Free ( ) ;
3844- s_GlobalList . RemoveAtWithCapacity ( i ) ;
3862+ s_GlobalState . globalList [ i ] . Free ( ) ;
3863+ s_GlobalState . globalList . RemoveAtWithCapacity ( i ) ;
38453864 -- i ;
38463865 continue ;
38473866 }
@@ -3859,9 +3878,9 @@ internal static void DeferredResolutionOfBindings()
38593878
38603879 internal static void DisableAllActions ( )
38613880 {
3862- for ( var i = 0 ; i < s_GlobalList . length ; ++ i )
3881+ for ( var i = 0 ; i < s_GlobalState . globalList . length ; ++ i )
38633882 {
3864- var handle = s_GlobalList [ i ] ;
3883+ var handle = s_GlobalState . globalList [ i ] ;
38653884 if ( ! handle . IsAllocated || handle . Target == null )
38663885 continue ;
38673886 var state = ( InputActionState ) handle . Target ;
@@ -3885,16 +3904,16 @@ internal static void DisableAllActions()
38853904 /// </remarks>
38863905 internal static void DestroyAllActionMapStates ( )
38873906 {
3888- while ( s_GlobalList . length > 0 )
3907+ while ( s_GlobalState . globalList . length > 0 )
38893908 {
3890- var index = s_GlobalList . length - 1 ;
3891- var handle = s_GlobalList [ index ] ;
3909+ var index = s_GlobalState . globalList . length - 1 ;
3910+ var handle = s_GlobalState . globalList [ index ] ;
38923911 if ( ! handle . IsAllocated || handle . Target == null )
38933912 {
38943913 // Already destroyed.
38953914 if ( handle . IsAllocated )
3896- s_GlobalList [ index ] . Free ( ) ;
3897- s_GlobalList . RemoveAtWithCapacity ( index ) ;
3915+ s_GlobalState . globalList [ index ] . Free ( ) ;
3916+ s_GlobalState . globalList . RemoveAtWithCapacity ( index ) ;
38983917 continue ;
38993918 }
39003919
0 commit comments