Skip to content

Commit 5cb4617

Browse files
committed
Changed TypeTable to be tightly coupled to an InputManager instance. Removed property that could be internal to InputManager and be returned as method result instead.
1 parent 4b229a2 commit 5cb4617

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,7 @@ internal void Initialize(IInputRuntime runtime, InputSettings settings)
18581858
Debug.Assert(settings != null);
18591859

18601860
m_Settings = settings;
1861+
m_LazyLoadCustomTypes = () => RegisterCustomTypes(); // Cached delegate
18611862

18621863
InitializeActions();
18631864
InitializeData();
@@ -1913,9 +1914,9 @@ private void InitializeActions()
19131914
internal void InitializeData()
19141915
{
19151916
m_Layouts.Allocate();
1916-
m_Processors.Initialize();
1917-
m_Interactions.Initialize();
1918-
m_Composites.Initialize();
1917+
m_Processors.Initialize(this);
1918+
m_Interactions.Initialize(this);
1919+
m_Composites.Initialize(this);
19191920
m_DevicesById = new Dictionary<int, InputDevice>();
19201921

19211922
// Determine our default set of enabled update types. By
@@ -2030,7 +2031,7 @@ internal void InitializeData()
20302031
// .inputaction JSON assets. This is managed via TypeTable.cs.
20312032
}
20322033

2033-
void RegisterCustomTypes(Type[] types)
2034+
static void RegisterCustomTypes(Type[] types)
20342035
{
20352036
foreach (Type type in types)
20362037
{
@@ -2053,13 +2054,17 @@ void RegisterCustomTypes(Type[] types)
20532054
}
20542055
}
20552056

2056-
internal bool hasCustomTypesBeenRegistered { get; private set; }
2057+
private bool m_CustomTypesRegistered;
20572058

2058-
internal void RegisterCustomTypes()
2059+
internal bool RegisterCustomTypes()
20592060
{
2060-
if (hasCustomTypesBeenRegistered)
2061-
return;
2062-
hasCustomTypesBeenRegistered = true;
2061+
// If we have already attempted to register custom types, there is no need to reattempt since we
2062+
// would end up with the same result again. Only with a domain reload would the resulting types
2063+
// be different, and hence it is sufficient to use a static flag that we do not reset.
2064+
if (!m_CustomTypesRegistered)
2065+
return false;
2066+
2067+
m_CustomTypesRegistered = true;
20632068

20642069
k_InputRegisterCustomTypesMarker.Begin();
20652070

@@ -2085,11 +2090,13 @@ internal void RegisterCustomTypes()
20852090
}
20862091
catch (ReflectionTypeLoadException)
20872092
{
2088-
continue;
2093+
// Ignore exception
20892094
}
20902095
}
20912096

20922097
k_InputRegisterCustomTypesMarker.End();
2098+
2099+
return true;
20932100
}
20942101

20952102
internal void InstallRuntime(IInputRuntime runtime)
@@ -2270,6 +2277,7 @@ internal struct AvailableDevice
22702277
internal IInputRuntime m_Runtime;
22712278
internal InputMetrics m_Metrics;
22722279
internal InputSettings m_Settings;
2280+
private Func<bool> m_LazyLoadCustomTypes;
22732281

22742282
// Extract as booleans (from m_Settings) because feature check is in the hot path
22752283

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ internal struct TypeTable
2525
public HashSet<InternedString> aliases;
2626
#endif
2727

28-
public void Initialize()
28+
// Strong coupling to Input Manager which is always the owner
29+
private InputManager m_Manager;
30+
31+
public void Initialize(InputManager manager)
2932
{
3033
table = new Dictionary<InternedString, Type>();
3134
#if UNITY_EDITOR
3235
aliases = new HashSet<InternedString>();
3336
#endif
37+
m_Manager = manager;
3438
}
3539

3640
public InternedString FindNameForType(Type type)
@@ -80,18 +84,24 @@ public Type LookupTypeRegistration(string name)
8084
if (table == null)
8185
throw new InvalidOperationException("Input System not yet initialized");
8286

83-
var internedName = new InternedString(name);
87+
return TryLookupTypeRegistration(new InternedString(name));
88+
}
89+
90+
private Type TryLookupTypeRegistration(InternedString internedName)
91+
{
8492
if (table.TryGetValue(internedName, out var type))
8593
return type;
8694

87-
// Failed to look-up type, either type do not exist or it is a custom type.
95+
// Failed to look-up type, either type do not exist or it is a custom type that has not been registered.
8896
// Check whether we have attempted to load custom types and otherwise lazily load
8997
// types only when referenced and reattempt looking up type by name. (ISXB-1766)
90-
if (InputSystem.s_Manager == null || InputSystem.s_Manager.hasCustomTypesBeenRegistered)
91-
return null;
98+
if (m_Manager != null)
99+
{
100+
if (m_Manager.RegisterCustomTypes())
101+
return TryLookupTypeRegistration(internedName); // Recursive call
102+
}
92103

93-
InputSystem.s_Manager.RegisterCustomTypes();
94-
return LookupTypeRegistration(name);
104+
return null;
95105
}
96106

97107
#if UNITY_EDITOR

0 commit comments

Comments
 (0)