Skip to content

Commit b44537b

Browse files
committed
Refactor various InputSystem members to improved separation of concerns (ISX-1842)
- Make InputSystem static fields private with read-only accessors - Add InputSystemTestHooks.cs for "Test Hook" operations that need direct access to fields - Factor out InputSystemObject (and related State) and rename to InputSystemStateManager - Factor out "dirty asset tracking" functionality to a separate Utility class Despite touching a bunch of files, this is a low-risk set of changes; functions are moved to new files and types renamed, but no real changes to functionality. Validated changes with Edit/Play/Player tests.
1 parent 36edb18 commit b44537b

37 files changed

+353
-303
lines changed

Assets/Tests/InputSystem/CoreTests_Actions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6040,12 +6040,12 @@ public void Actions_AddingSameProcessorTwice_DoesntImpactUIHideState()
60406040
InputSystem.RegisterProcessor<ConstantFloat1TestProcessor>();
60416041
Assert.That(InputSystem.TryGetProcessor("ConstantFloat1Test"), Is.Not.EqualTo(null));
60426042

6043-
bool hide = InputSystem.s_Manager.processors.ShouldHideInUI("ConstantFloat1Test");
6043+
bool hide = InputSystem.manager.processors.ShouldHideInUI("ConstantFloat1Test");
60446044
Assert.That(hide, Is.EqualTo(false));
60456045

60466046
InputSystem.RegisterProcessor<ConstantFloat1TestProcessor>();
60476047
// Check we haven't caused this to alias with itself and cause it to be hidden in the UI
6048-
hide = InputSystem.s_Manager.processors.ShouldHideInUI("ConstantFloat1Test");
6048+
hide = InputSystem.manager.processors.ShouldHideInUI("ConstantFloat1Test");
60496049
Assert.That(hide, Is.EqualTo(false));
60506050
}
60516051

@@ -6691,7 +6691,7 @@ public void Actions_RegisteringExistingInteractionUnderNewName_CreatesAlias()
66916691
{
66926692
InputSystem.RegisterInteraction<HoldInteraction>("TestTest");
66936693

6694-
Assert.That(InputSystem.s_Manager.interactions.aliases.Contains(new InternedString("TestTest")));
6694+
Assert.That(InputSystem.manager.interactions.aliases.Contains(new InternedString("TestTest")));
66956695
}
66966696

66976697
#endif // UNITY_EDITOR
@@ -8970,7 +8970,7 @@ public void Actions_RegisteringExistingCompositeUnderNewName_CreatesAlias()
89708970
{
89718971
InputSystem.RegisterBindingComposite<Vector2Composite>("TestTest");
89728972

8973-
Assert.That(InputSystem.s_Manager.composites.aliases.Contains(new InternedString("TestTest")));
8973+
Assert.That(InputSystem.manager.composites.aliases.Contains(new InternedString("TestTest")));
89748974
}
89758975

89768976
#endif // UNITY_EDITOR
@@ -11050,7 +11050,7 @@ public void Actions_DisablingAllActions_RemovesAllTheirStateMonitors()
1105011050

1105111051
// Not the most elegant test as we reach into internals here but with the
1105211052
// current API, it's not possible to enumerate monitors from outside.
11053-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors,
11053+
Assert.That(InputSystem.manager.m_StateChangeMonitors,
1105411054
Has.All.Matches(
1105511055
(InputManager.StateChangeMonitorsForDevice x) => x.memoryRegions.All(r => r.sizeInBits == 0)));
1105611056
}

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,11 @@ public void Devices_AddingDeviceThatUsesBeforeRenderUpdates_CausesBeforeRenderUp
572572

573573
InputSystem.RegisterLayout(deviceJson);
574574

575-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo((InputUpdateType)0));
575+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo((InputUpdateType)0));
576576

577577
InputSystem.AddDevice("CustomGamepad");
578578

579-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
579+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
580580
}
581581

582582
[Test]
@@ -596,15 +596,15 @@ public void Devices_RemovingLastDeviceThatUsesBeforeRenderUpdates_CausesBeforeRe
596596
var device1 = InputSystem.AddDevice("CustomGamepad");
597597
var device2 = InputSystem.AddDevice("CustomGamepad");
598598

599-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
599+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
600600

601601
InputSystem.RemoveDevice(device1);
602602

603-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
603+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo(InputUpdateType.BeforeRender));
604604

605605
InputSystem.RemoveDevice(device2);
606606

607-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo((InputUpdateType)0));
607+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.BeforeRender, Is.EqualTo((InputUpdateType)0));
608608
}
609609

610610
private class TestDeviceReceivingAddAndRemoveNotification : Mouse

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,6 @@ public static Version ReadVersion()
7171
}
7272
}
7373

74-
private void SimulateDomainReload()
75-
{
76-
// This quite invasively goes into InputSystem internals. Unfortunately, we
77-
// have no proper way of simulating domain reloads ATM. So we directly call various
78-
// internal methods here in a sequence similar to what we'd get during a domain reload.
79-
// Since we're faking it, pass 'true' for calledFromCtor param.
80-
81-
InputSystem.s_SystemObject.OnBeforeSerialize();
82-
InputSystem.s_SystemObject = null;
83-
InputSystem.s_Manager = null; // Do NOT Dispose()! The native memory cannot be freed as it's reference by saved state
84-
InputSystem.InitializeInEditor(true, runtime);
85-
}
86-
8774
[Test]
8875
[Category("Editor")]
8976
public void Editor_PackageVersionAndAssemblyVersionAreTheSame()
@@ -213,7 +200,7 @@ public void Editor_DomainReload_CanRestoreDevicesBuiltWithDynamicallyGeneratedLa
213200
Assert.That(InputSystem.devices, Is.Empty);
214201

215202
var state = m_StateManager.GetSavedState();
216-
var manager = InputSystem.s_Manager;
203+
var manager = InputSystem.manager;
217204

218205
manager.m_SavedAvailableDevices = state.managerState.availableDevices;
219206
manager.m_SavedDeviceStates = state.managerState.devices;
@@ -232,7 +219,7 @@ public void Editor_DomainReload_PreservesUsagesOnDevices()
232219
var device = InputSystem.AddDevice<Gamepad>();
233220
InputSystem.SetDeviceUsage(device, CommonUsages.LeftHand);
234221

235-
SimulateDomainReload();
222+
InputSystem.TestHook_SimulateDomainReload(runtime);
236223

237224
var newDevice = InputSystem.devices[0];
238225

@@ -252,7 +239,7 @@ public void Editor_DomainReload_PreservesEnabledState()
252239

253240
Assert.That(device.enabled, Is.False);
254241

255-
SimulateDomainReload();
242+
InputSystem.TestHook_SimulateDomainReload(runtime);
256243

257244
var newDevice = InputSystem.devices[0];
258245

@@ -265,7 +252,7 @@ public void Editor_DomainReload_InputSystemInitializationCausesDevicesToBeRecrea
265252
{
266253
InputSystem.AddDevice<Gamepad>();
267254

268-
SimulateDomainReload();
255+
InputSystem.TestHook_SimulateDomainReload(runtime);
269256

270257
Assert.That(InputSystem.devices, Has.Count.EqualTo(1));
271258
Assert.That(InputSystem.devices[0], Is.TypeOf<Gamepad>());
@@ -302,7 +289,7 @@ public void Editor_DomainReload_CustomDevicesAreRestoredAsLayoutsBecomeAvailable
302289
InputSystem.RegisterLayout(kLayout);
303290
InputSystem.AddDevice("CustomDevice");
304291

305-
SimulateDomainReload();
292+
InputSystem.TestHook_SimulateDomainReload(runtime);
306293

307294
Assert.That(InputSystem.devices, Is.Empty);
308295

@@ -323,7 +310,7 @@ public void Editor_DomainReload_RetainsUnsupportedDevices()
323310
});
324311
InputSystem.Update();
325312

326-
SimulateDomainReload();
313+
InputSystem.TestHook_SimulateDomainReload(runtime);
327314

328315
Assert.That(InputSystem.GetUnsupportedDevices(), Has.Count.EqualTo(1));
329316
Assert.That(InputSystem.GetUnsupportedDevices()[0].interfaceName, Is.EqualTo("SomethingUnknown"));
@@ -2517,7 +2504,7 @@ public void TODO_Editor_SettingsModifiedInPlayMode_AreRestoredWhenReEnteringEdit
25172504
[Category("Editor")]
25182505
public void Editor_AlwaysKeepsEditorUpdatesEnabled()
25192506
{
2520-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.Editor, Is.EqualTo(InputUpdateType.Editor));
2507+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.Editor, Is.EqualTo(InputUpdateType.Editor));
25212508
}
25222509

25232510
[Test]
@@ -2939,15 +2926,15 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29392926
action.Enable();
29402927

29412928
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.EqualTo(1));
2942-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors.Length, Is.GreaterThan(0));
2943-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].count, Is.EqualTo(1));
2929+
Assert.That(InputSystem.manager.m_StateChangeMonitors.Length, Is.GreaterThan(0));
2930+
Assert.That(InputSystem.manager.m_StateChangeMonitors[0].count, Is.EqualTo(1));
29442931

29452932
// Exit play mode.
29462933
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingPlayMode);
29472934
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
29482935

29492936
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.Zero);
2950-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null); // Won't get removed, just cleared.
2937+
Assert.That(InputSystem.manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null); // Won't get removed, just cleared.
29512938
}
29522939

29532940
[Test]

Assets/Tests/InputSystem/CoreTests_Events.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public void Events_CanSwitchToFullyManualUpdates()
431431

432432
#if UNITY_EDITOR
433433
// Edit mode updates shouldn't have been disabled in editor.
434-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.Editor, Is.Not.Zero);
434+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.Editor, Is.Not.Zero);
435435
#endif
436436

437437
InputSystem.QueueStateEvent(mouse, new MouseState().WithButton(MouseButton.Left));
@@ -458,8 +458,8 @@ public void Events_CanSwitchToProcessingInFixedUpdates()
458458

459459
Assert.That(InputSystem.settings.updateMode, Is.EqualTo(InputSettings.UpdateMode.ProcessEventsInFixedUpdate));
460460
Assert.That(receivedOnChange, Is.True);
461-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.Fixed, Is.EqualTo(InputUpdateType.Fixed));
462-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.Dynamic, Is.EqualTo(InputUpdateType.None));
461+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.Fixed, Is.EqualTo(InputUpdateType.Fixed));
462+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.Dynamic, Is.EqualTo(InputUpdateType.None));
463463

464464
InputSystem.QueueStateEvent(mouse, new MouseState().WithButton(MouseButton.Left));
465465
runtime.currentTimeForFixedUpdate += Time.fixedDeltaTime;
@@ -475,19 +475,19 @@ public void Events_CanSwitchToProcessingInFixedUpdates()
475475
[Category("Events")]
476476
public void Events_ShouldRunUpdate_AppliesUpdateMask()
477477
{
478-
InputSystem.s_Manager.updateMask = InputUpdateType.Dynamic;
478+
InputSystem.manager.updateMask = InputUpdateType.Dynamic;
479479

480480
Assert.That(runtime.onShouldRunUpdate.Invoke(InputUpdateType.Dynamic));
481481
Assert.That(!runtime.onShouldRunUpdate.Invoke(InputUpdateType.Fixed));
482482
Assert.That(!runtime.onShouldRunUpdate.Invoke(InputUpdateType.Manual));
483483

484-
InputSystem.s_Manager.updateMask = InputUpdateType.Manual;
484+
InputSystem.manager.updateMask = InputUpdateType.Manual;
485485

486486
Assert.That(!runtime.onShouldRunUpdate.Invoke(InputUpdateType.Dynamic));
487487
Assert.That(!runtime.onShouldRunUpdate.Invoke(InputUpdateType.Fixed));
488488
Assert.That(runtime.onShouldRunUpdate.Invoke(InputUpdateType.Manual));
489489

490-
InputSystem.s_Manager.updateMask = InputUpdateType.Default;
490+
InputSystem.manager.updateMask = InputUpdateType.Default;
491491

492492
Assert.That(runtime.onShouldRunUpdate.Invoke(InputUpdateType.Dynamic));
493493
Assert.That(runtime.onShouldRunUpdate.Invoke(InputUpdateType.Fixed));

Assets/Tests/InputSystem/CoreTests_Remoting.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public void Remote_CanConnectInputSystemsOverEditorPlayerConnection()
297297
connectionToPlayer.Bind(fakeEditorConnection, true);
298298

299299
// Bind a local remote on the player side.
300-
var local = new InputRemoting(InputSystem.s_Manager);
300+
var local = new InputRemoting(InputSystem.manager);
301301
local.Subscribe(connectionToEditor);
302302

303303
connectionToEditor.Subscribe(local);
@@ -479,11 +479,11 @@ public FakeRemote()
479479
runtime = new InputTestRuntime();
480480
var manager = InputManager.CreateAndInitialize(runtime, null, true);
481481

482-
local = new InputRemoting(InputSystem.s_Manager);
482+
local = new InputRemoting(InputSystem.manager);
483483
remote = new InputRemoting(manager);
484484

485485
var remoteInstaller = new GlobalsInstallerObserver(manager);
486-
var localInstaller = new GlobalsInstallerObserver(InputSystem.s_Manager);
486+
var localInstaller = new GlobalsInstallerObserver(InputSystem.manager);
487487

488488
// The installers will ensure the globals environment is prepared right before
489489
// the receiver processes the message. There are some static fields, such as
@@ -501,14 +501,12 @@ public FakeRemote()
501501

502502
public void SwitchToRemoteState()
503503
{
504-
InputSystem.s_Manager = remoteManager;
505-
InputStateBuffers.SwitchTo(remoteManager.m_StateBuffers, remoteManager.defaultUpdateType);
504+
InputSystem.TestHook_SwitchToDifferentInputManager(remoteManager);
506505
}
507506

508507
public void SwitchToLocalState()
509508
{
510-
InputSystem.s_Manager = localManager;
511-
InputStateBuffers.SwitchTo(localManager.m_StateBuffers, localManager.defaultUpdateType);
509+
InputSystem.TestHook_SwitchToDifferentInputManager(localManager);
512510
}
513511

514512
public void Dispose()

Assets/Tests/InputSystem/CoreTests_State.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ public void State_FixedUpdatesAreDisabledByDefault()
11961196
{
11971197
Assert.That(InputSystem.settings.updateMode, Is.EqualTo(InputSettings.UpdateMode.ProcessEventsInDynamicUpdate));
11981198
Assert.That(runtime.onShouldRunUpdate(InputUpdateType.Fixed), Is.False);
1199-
Assert.That(InputSystem.s_Manager.updateMask & InputUpdateType.Fixed, Is.EqualTo(InputUpdateType.None));
1199+
Assert.That(InputSystem.manager.updateMask & InputUpdateType.Fixed, Is.EqualTo(InputUpdateType.None));
12001200
}
12011201

12021202
[Test]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ IEnumerator IEnumerable.GetEnumerator()
867867
internal void MarkAsDirty()
868868
{
869869
#if UNITY_EDITOR
870-
InputSystem.TrackDirtyInputActionAsset(this);
870+
DirtyAssetTracker.TrackDirtyInputActionAsset(this);
871871
#endif
872872
}
873873

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ private void EnableControls(int mapIndex, int controlStartIndex, int numControls
11201120
"Control start index out of range");
11211121
Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds");
11221122

1123-
var manager = InputSystem.s_Manager;
1123+
var manager = InputSystem.manager;
11241124
for (var i = 0; i < numControls; ++i)
11251125
{
11261126
var controlIndex = controlStartIndex + i;
@@ -1149,7 +1149,7 @@ private void DisableControls(int mapIndex, int controlStartIndex, int numControl
11491149
"Control start index out of range");
11501150
Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds");
11511151

1152-
var manager = InputSystem.s_Manager;
1152+
var manager = InputSystem.manager;
11531153
for (var i = 0; i < numControls; ++i)
11541154
{
11551155
var controlIndex = controlStartIndex + i;
@@ -1222,7 +1222,7 @@ private void HookOnBeforeUpdate()
12221222

12231223
if (m_OnBeforeUpdateDelegate == null)
12241224
m_OnBeforeUpdateDelegate = OnBeforeInitialUpdate;
1225-
InputSystem.s_Manager.onBeforeUpdate += m_OnBeforeUpdateDelegate;
1225+
InputSystem.manager.onBeforeUpdate += m_OnBeforeUpdateDelegate;
12261226
m_OnBeforeUpdateHooked = true;
12271227
}
12281228

@@ -1231,7 +1231,7 @@ private void UnhookOnBeforeUpdate()
12311231
if (!m_OnBeforeUpdateHooked)
12321232
return;
12331233

1234-
InputSystem.s_Manager.onBeforeUpdate -= m_OnBeforeUpdateDelegate;
1234+
InputSystem.manager.onBeforeUpdate -= m_OnBeforeUpdateDelegate;
12351235
m_OnBeforeUpdateHooked = false;
12361236
}
12371237

@@ -1264,7 +1264,7 @@ private void OnBeforeInitialUpdate()
12641264
// Go through all binding states and for every binding that needs an initial state check,
12651265
// go through all bound controls and for each one that isn't in its default state, pretend
12661266
// that the control just got actuated.
1267-
var manager = InputSystem.s_Manager;
1267+
var manager = InputSystem.manager;
12681268
for (var bindingIndex = 0; bindingIndex < totalBindingCount; ++bindingIndex)
12691269
{
12701270
ref var bindingState = ref bindingStates[bindingIndex];
@@ -2099,7 +2099,7 @@ internal void StartTimeout(float seconds, ref TriggerState trigger)
20992099
Debug.Assert(trigger.controlIndex >= 0 && trigger.controlIndex < totalControlCount, "Control index out of range");
21002100
Debug.Assert(trigger.interactionIndex >= 0 && trigger.interactionIndex < totalInteractionCount, "Interaction index out of range");
21012101

2102-
var manager = InputSystem.s_Manager;
2102+
var manager = InputSystem.manager;
21032103
var currentTime = trigger.time;
21042104
var control = controls[trigger.controlIndex];
21052105
var interactionIndex = trigger.interactionIndex;
@@ -2128,7 +2128,7 @@ private void StopTimeout(int interactionIndex)
21282128

21292129
ref var interactionState = ref interactionStates[interactionIndex];
21302130

2131-
var manager = InputSystem.s_Manager;
2131+
var manager = InputSystem.manager;
21322132
manager.RemoveStateChangeMonitorTimeout(this, interactionState.timerMonitorIndex, interactionIndex);
21332133

21342134
// Update state.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public unsafe long ExecuteCommand<TCommand>(ref TCommand command)
557557
var commandPtr = (InputDeviceCommand*)UnsafeUtility.AddressOf(ref command);
558558

559559
// Give callbacks first shot.
560-
var manager = InputSystem.s_Manager;
560+
var manager = InputSystem.manager;
561561
manager.m_DeviceCommandCallbacks.LockForChanges();
562562
for (var i = 0; i < manager.m_DeviceCommandCallbacks.length; ++i)
563563
{

Packages/com.unity.inputsystem/InputSystem/Editor/AssetEditor/InputActionPropertiesView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override void DrawGeneralProperties()
8181
private void BuildControlTypeList()
8282
{
8383
var types = new List<string>();
84-
var allLayouts = InputSystem.s_Manager.m_Layouts;
84+
var allLayouts = InputSystem.manager.m_Layouts;
8585
foreach (var layoutName in allLayouts.layoutTypes.Keys)
8686
{
8787
if (EditorInputControlLayoutCache.TryGetLayout(layoutName).hideInUI)

0 commit comments

Comments
 (0)