Skip to content

Commit caf32e4

Browse files
committed
Update Plugin classes to fix static usages (ISX-1840)
- Add a flag to guard against re-initializing Plugins within PerformDefaultPluginInitialization() - Refactor SteamSupport to fix Init/Shutdown flows and improve state management - Refactor PlayerInput's static fields into a single GlobalState struct (matching other classes) - Move EnhancedTouch static fields into Touch's GlobalState struct - Minor refactoring of SteamSupport to improve init/cleanp-up flows (especially with tests)
1 parent f7fbc7f commit caf32e4

File tree

10 files changed

+175
-118
lines changed

10 files changed

+175
-118
lines changed

Assets/Tests/InputSystem/Plugins/SteamTests.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ public override void Setup()
3636
public override void TearDown()
3737
{
3838
base.TearDown();
39-
m_SteamAPI = null;
40-
41-
SteamSupport.s_API = null;
42-
SteamSupport.s_InputDevices = null;
43-
SteamSupport.s_ConnectedControllers = null;
44-
SteamSupport.s_InputDeviceCount = 0;
45-
SteamSupport.s_HooksInstalled = false;
39+
SteamSupport.Shutdown();
4640
}
4741

4842
[Test]

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

+16
Original file line numberDiff line numberDiff line change
@@ -3802,8 +3802,24 @@ private static void RunInitialUpdate()
38023802
}
38033803

38043804
#if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
3805+
3806+
#if UNITY_EDITOR
3807+
// Plug-ins must only be initialized once, since many of them use static fields.
3808+
// When Domain Reloads are disabled, we must guard against this method being called a second time.
3809+
private static bool s_PluginsInitialized = false;
3810+
#endif
3811+
38053812
internal static void PerformDefaultPluginInitialization()
38063813
{
3814+
#if UNITY_EDITOR
3815+
if (s_PluginsInitialized)
3816+
{
3817+
Debug.Assert(false, "Attempted to re-initialize InputSystem Plugins!");
3818+
return;
3819+
}
3820+
s_PluginsInitialized = true;
3821+
#endif
3822+
38073823
UISupport.Initialize();
38083824

38093825
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_ANDROID || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS

Packages/com.unity.inputsystem/InputSystem/InputSystemTestHooks.cs

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ internal static void TestHook_InitializeForPlayModeTests(bool enableRemoting, II
2828
InputSystem.SetUpRemoting();
2929

3030
#if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
31+
// Reset the flag so can re-initialize Plugins between tests.
32+
InputSystem.s_PluginsInitialized = false;
3133
InputSystem.PerformDefaultPluginInitialization();
3234
#endif
3335
}
@@ -42,6 +44,7 @@ internal static void TestHook_SimulateDomainReload(IInputRuntime runtime)
4244
InputSystem.s_DomainStateManager.OnBeforeSerialize();
4345
InputSystem.s_DomainStateManager = null;
4446
InputSystem.s_Manager = null; // Do NOT Dispose()! The native memory cannot be freed as it's reference by saved state
47+
InputSystem.s_PluginsInitialized = false;
4548
InputSystem.InitializeInEditor(true, runtime);
4649
}
4750
#endif // UNITY_EDITOR

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ public DualSenseHIDInputReport ToHIDInputReport()
675675
[StructLayout(LayoutKind.Explicit)]
676676
internal struct DualSenseHIDMinimalInputReport
677677
{
678-
public static int ExpectedSize1 = 10;
679-
public static int ExpectedSize2 = 78;
678+
public const int ExpectedSize1 = 10;
679+
public const int ExpectedSize2 = 78;
680680

681681
[FieldOffset(0)] public byte reportId;
682682
[FieldOffset(1)] public byte leftStickX;

Packages/com.unity.inputsystem/InputSystem/Plugins/EnhancedTouch/EnhancedTouchSupport.cs

+8-11
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public static class EnhancedTouchSupport
6363
/// Whether enhanced touch support is currently enabled.
6464
/// </summary>
6565
/// <value>True if EnhancedTouch support has been enabled.</value>
66-
public static bool enabled => s_Enabled > 0;
67-
68-
private static int s_Enabled;
69-
private static InputSettings.UpdateMode s_UpdateMode;
66+
public static bool enabled => Touch.s_GlobalState.enhancedTouchEnabled > 0;
7067

7168
/// <summary>
7269
/// Enable enhanced touch support.
@@ -82,8 +79,8 @@ public static class EnhancedTouchSupport
8279
/// </remarks>
8380
public static void Enable()
8481
{
85-
++s_Enabled;
86-
if (s_Enabled > 1)
82+
++Touch.s_GlobalState.enhancedTouchEnabled;
83+
if (Touch.s_GlobalState.enhancedTouchEnabled > 1)
8784
return;
8885

8986
InputSystem.onDeviceChange += OnDeviceChange;
@@ -107,8 +104,8 @@ public static void Disable()
107104
{
108105
if (!enabled)
109106
return;
110-
--s_Enabled;
111-
if (s_Enabled > 0)
107+
--Touch.s_GlobalState.enhancedTouchEnabled;
108+
if (Touch.s_GlobalState.enhancedTouchEnabled > 0)
112109
return;
113110

114111
InputSystem.onDeviceChange -= OnDeviceChange;
@@ -131,7 +128,7 @@ internal static void Reset()
131128
Touch.s_GlobalState.editorState.Destroy();
132129
Touch.s_GlobalState.editorState = default;
133130
#endif
134-
s_Enabled = 0;
131+
Touch.s_GlobalState.enhancedTouchEnabled = 0;
135132
}
136133

137134
private static void SetUpState()
@@ -141,7 +138,7 @@ private static void SetUpState()
141138
Touch.s_GlobalState.editorState.updateMask = InputUpdateType.Editor;
142139
#endif
143140

144-
s_UpdateMode = InputSystem.settings.updateMode;
141+
Touch.s_GlobalState.enhancedTouchUpdateMode = InputSystem.settings.updateMode;
145142

146143
foreach (var device in InputSystem.devices)
147144
OnDeviceChange(device, InputDeviceChange.Added);
@@ -186,7 +183,7 @@ private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
186183
private static void OnSettingsChange()
187184
{
188185
var currentUpdateMode = InputSystem.settings.updateMode;
189-
if (s_UpdateMode == currentUpdateMode)
186+
if (Touch.s_GlobalState.enhancedTouchUpdateMode == currentUpdateMode)
190187
return;
191188
TearDownState();
192189
SetUpState();

Packages/com.unity.inputsystem/InputSystem/Plugins/EnhancedTouch/Touch.cs

+4
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ internal struct GlobalState
587587
internal CallbackArray<Action<Finger>> onFingerMove;
588588
internal CallbackArray<Action<Finger>> onFingerUp;
589589

590+
// Used by EnhancedTouchSupport but placed here to consolidate static fields
591+
internal int enhancedTouchEnabled;
592+
internal InputSettings.UpdateMode enhancedTouchUpdateMode;
593+
590594
internal FingerAndTouchState playerState;
591595
#if UNITY_EDITOR
592596
internal FingerAndTouchState editorState;

0 commit comments

Comments
 (0)