Skip to content

Commit

Permalink
Update Plugin classes to fix static usages (ISX-1840)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
timkeo committed May 14, 2024
1 parent da58df8 commit 1bcabff
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 118 deletions.
8 changes: 1 addition & 7 deletions Assets/Tests/InputSystem/Plugins/SteamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ public override void Setup()
public override void TearDown()
{
base.TearDown();
m_SteamAPI = null;

SteamSupport.s_API = null;
SteamSupport.s_InputDevices = null;
SteamSupport.s_ConnectedControllers = null;
SteamSupport.s_InputDeviceCount = 0;
SteamSupport.s_HooksInstalled = false;
SteamSupport.Shutdown();
}

[Test]
Expand Down
16 changes: 16 additions & 0 deletions Packages/com.unity.inputsystem/InputSystem/InputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3802,8 +3802,24 @@ private static void RunInitialUpdate()
}

#if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION

#if UNITY_EDITOR
// Plug-ins must only be initialized once, since many of them use static fields.
// When Domain Reloads are disabled, we must guard against this method being called a second time.
private static bool s_PluginsInitialized = false;
#endif

internal static void PerformDefaultPluginInitialization()
{
#if UNITY_EDITOR
if (s_PluginsInitialized)
{
Debug.Assert(false, "Attempted to re-initialize InputSystem Plugins!");
return;
}
s_PluginsInitialized = true;
#endif

UISupport.Initialize();

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_ANDROID || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal static void TestHook_InitializeForPlayModeTests(bool enableRemoting, II
InputSystem.SetUpRemoting();

#if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
// Reset the flag so can re-initialize Plugins between tests.
InputSystem.s_PluginsInitialized = false;
InputSystem.PerformDefaultPluginInitialization();
#endif
}
Expand All @@ -42,6 +44,7 @@ internal static void TestHook_SimulateDomainReload(IInputRuntime runtime)
InputSystem.s_DomainStateManager.OnBeforeSerialize();
InputSystem.s_DomainStateManager = null;
InputSystem.s_Manager = null; // Do NOT Dispose()! The native memory cannot be freed as it's reference by saved state
InputSystem.s_PluginsInitialized = false;
InputSystem.InitializeInEditor(true, runtime);
}
#endif // UNITY_EDITOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ public DualSenseHIDInputReport ToHIDInputReport()
[StructLayout(LayoutKind.Explicit)]
internal struct DualSenseHIDMinimalInputReport
{
public static int ExpectedSize1 = 10;
public static int ExpectedSize2 = 78;
public const int ExpectedSize1 = 10;
public const int ExpectedSize2 = 78;

[FieldOffset(0)] public byte reportId;
[FieldOffset(1)] public byte leftStickX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public static class EnhancedTouchSupport
/// Whether enhanced touch support is currently enabled.
/// </summary>
/// <value>True if EnhancedTouch support has been enabled.</value>
public static bool enabled => s_Enabled > 0;

private static int s_Enabled;
private static InputSettings.UpdateMode s_UpdateMode;
public static bool enabled => Touch.s_GlobalState.enhancedTouchEnabled > 0;

/// <summary>
/// Enable enhanced touch support.
Expand All @@ -82,8 +79,8 @@ public static class EnhancedTouchSupport
/// </remarks>
public static void Enable()
{
++s_Enabled;
if (s_Enabled > 1)
++Touch.s_GlobalState.enhancedTouchEnabled;
if (Touch.s_GlobalState.enhancedTouchEnabled > 1)
return;

InputSystem.onDeviceChange += OnDeviceChange;
Expand All @@ -107,8 +104,8 @@ public static void Disable()
{
if (!enabled)
return;
--s_Enabled;
if (s_Enabled > 0)
--Touch.s_GlobalState.enhancedTouchEnabled;
if (Touch.s_GlobalState.enhancedTouchEnabled > 0)
return;

InputSystem.onDeviceChange -= OnDeviceChange;
Expand All @@ -131,7 +128,7 @@ internal static void Reset()
Touch.s_GlobalState.editorState.Destroy();
Touch.s_GlobalState.editorState = default;
#endif
s_Enabled = 0;
Touch.s_GlobalState.enhancedTouchEnabled = 0;
}

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

s_UpdateMode = InputSystem.settings.updateMode;
Touch.s_GlobalState.enhancedTouchUpdateMode = InputSystem.settings.updateMode;

foreach (var device in InputSystem.devices)
OnDeviceChange(device, InputDeviceChange.Added);
Expand Down Expand Up @@ -186,7 +183,7 @@ private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
private static void OnSettingsChange()
{
var currentUpdateMode = InputSystem.settings.updateMode;
if (s_UpdateMode == currentUpdateMode)
if (Touch.s_GlobalState.enhancedTouchUpdateMode == currentUpdateMode)
return;
TearDownState();
SetUpState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ internal struct GlobalState
internal CallbackArray<Action<Finger>> onFingerMove;
internal CallbackArray<Action<Finger>> onFingerUp;

// Used by EnhancedTouchSupport but placed here to consolidate static fields
internal int enhancedTouchEnabled;
internal InputSettings.UpdateMode enhancedTouchUpdateMode;

internal FingerAndTouchState playerState;
#if UNITY_EDITOR
internal FingerAndTouchState editorState;
Expand Down
Loading

0 comments on commit 1bcabff

Please sign in to comment.