File size: 16,093 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#if MLA_INPUT_SYSTEM
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Unity.MLAgents.Extensions.Input
{
    /// <summary>
    /// Component class that handles the parsing of the <see cref="InputActionAsset"/> and translates that into
    /// <see cref="InputActionActuator"/>s.
    /// </summary>
    [RequireComponent(typeof(PlayerInput), typeof(IInputActionAssetProvider))]
    [AddComponentMenu("ML Agents/Input Actuator", (int)MenuGroup.Actuators)]
    public class InputActuatorComponent : ActuatorComponent
    {
        InputActionAsset m_InputAsset;
        IInputActionCollection2 m_AssetCollection;
        PlayerInput m_PlayerInput;
        BehaviorParameters m_BehaviorParameters;
        IActuator[] m_Actuators;
        InputDevice m_Device;

        /// <summary>
        /// Mapping of <see cref="InputControl"/> types to types of <see cref="IRLActionInputAdaptor"/> concrete classes.
        /// </summary>
        public static Dictionary<Type, Type> controlTypeToAdaptorType = new Dictionary<Type, Type>
        {
            { typeof(Vector2Control), typeof(Vector2InputActionAdaptor) },
            { typeof(ButtonControl), typeof(ButtonInputActionAdaptor) },
            { typeof(IntegerControl), typeof(IntegerInputActionAdaptor) },
            { typeof(AxisControl), typeof(FloatInputActionAdaptor) },
            { typeof(DoubleControl), typeof(DoubleInputActionAdaptor) }
        };

        string m_LayoutName;
        [SerializeField]
        ActionSpec m_ActionSpec;
        InputControlScheme m_ControlScheme;

        public const string mlAgentsLayoutFormat = "MLAT";
        public const string mlAgentsLayoutName = "MLAgentsLayout";
        public const string mlAgentsControlSchemeName = "ml-agents";

        /// <inheritdoc cref="IActuator.ActionSpec"/>
        public override ActionSpec ActionSpec
        {
            get
            {
#if UNITY_EDITOR
                if (!EditorApplication.isPlaying && m_ActionSpec.NumContinuousActions == 0
                    && m_ActionSpec.BranchSizes == null
                    || m_ActionSpec.BranchSizes.Length == 0)
                {
                    FindNeededComponents();
                    var actuators = CreateActuatorsFromMap(m_InputAsset.FindActionMap(m_PlayerInput.defaultActionMap),
                        m_BehaviorParameters,
                        null,
                        InputActuatorEventContext.s_EditorContext);
                    m_ActionSpec = CombineActuatorActionSpecs(actuators);
                }
#endif
                return m_ActionSpec;
            }
        }

        void OnDisable()
        {
            CleanupActionAsset();
        }

        /// <summary>
        /// This method is where the <see cref="InputActionAsset"/> gets parsed and translated into
        /// <see cref="InputActionActuator"/>s that communicate with the <see cref="InputSystem"/> via a
        /// virtual <see cref="InputDevice"/>.
        /// <remarks>
        /// The flow of this method is as follows:
        /// <list type="number">
        ///     <item>
        ///     <description>Ensure that our custom <see cref="InputBindingComposite{TValue}"/>s are registered with
        ///     the InputSystem.</description>
        ///     </item>
        ///     <item>
        ///     <description>Look for the components that are needed by this class in order to retrieve the
        ///     <see cref="InputActionAsset"/>.  It first looks for <see cref="IInputActionAssetProvider"/>, if that
        ///     is not found, it will get the asset from the <see cref="PlayerInput"/> component.</description>
        ///     </item>
        ///     <item>
        ///     <description>Create the list <see cref="InputActionActuator"/>s, one for each action in the default
        ///     <see cref="InputActionMap"/> as set by the <see cref="PlayerInput"/> component.  Within the method
        ///     where the actuators are being created, an <see cref="InputControlLayout"/> is also being built based
        ///     on the number and types of <see cref="InputAction"/>s.  This will be used to create a virtual
        ///     <see cref="InputDevice"/> with a <see cref="InputControlLayout"/> that is specific to the
        ///     <see cref="InputActionMap"/> specified by <see cref="PlayerInput"/></description>
        ///     </item>
        ///     <item>
        ///     <description>Create our device based on the layout that was generated and registered during
        ///     actuator creation.</description>
        ///     </item>
        ///     <item>
        ///     <description>Create an ml-agents control scheme and add it to the <see cref="InputActionAsset"/> so
        ///     our virtual devices can be used.</description>
        ///     </item>
        ///     <item>
        ///     <description>Add our virtual <see cref="InputDevice"/> to the input system.</description>
        ///     </item>
        /// </list>
        /// </remarks>
        /// </summary>
        /// <returns>A list of </returns>
        public override IActuator[] CreateActuators()
        {
            FindNeededComponents();
            var collection = m_AssetCollection ?? m_InputAsset;
            collection.Disable();
            var inputActionMap = m_InputAsset.FindActionMap(m_PlayerInput.defaultActionMap);

            RegisterLayoutBuilder(inputActionMap, m_LayoutName);
            m_Device = InputSystem.AddDevice(m_LayoutName);

            var context = new InputActuatorEventContext(inputActionMap.actions.Count, m_Device);
            m_Actuators = CreateActuatorsFromMap(inputActionMap, m_BehaviorParameters, m_Device, context);

            UpdateDeviceBinding(m_BehaviorParameters.IsInHeuristicMode());
            inputActionMap.Enable();

            m_ActionSpec = CombineActuatorActionSpecs(m_Actuators);
            collection.Enable();
            return m_Actuators;
        }

        static ActionSpec CombineActuatorActionSpecs(IActuator[] actuators)
        {
            var specs = new ActionSpec[actuators.Length];
            for (var i = 0; i < actuators.Length; i++)
            {
                specs[i] = actuators[i].ActionSpec;
            }
            return ActionSpec.Combine(specs);
        }

        internal static IActuator[] CreateActuatorsFromMap(InputActionMap inputActionMap,
            BehaviorParameters behaviorParameters,
            InputDevice inputDevice,
            InputActuatorEventContext context)
        {
            var actuators = new IActuator[inputActionMap.actions.Count];
            for (var i = 0; i < inputActionMap.actions.Count; i++)
            {
                var action = inputActionMap.actions[i];
                var actionLayout = InputSystem.LoadLayout(action.expectedControlType);
                var adaptor = (IRLActionInputAdaptor)Activator.CreateInstance(controlTypeToAdaptorType[actionLayout.type]);
                actuators[i] = new InputActionActuator(inputDevice, behaviorParameters, action, adaptor, context);

                // Reasonably, the input system starts adding numbers after the first none numbered name
                // is added.  So for device ID of 0, we use the empty string in the path.
                var path = $"{inputDevice?.path}{InputControlPath.Separator}{action.name}";
                action.AddBinding(path,
                    action.interactions,
                    action.processors,
                    mlAgentsControlSchemeName);
                action.bindingMask = InputBinding.MaskByGroup(mlAgentsControlSchemeName);
            }
            return actuators;
        }

        /// <summary>
        /// Set up bindings based on whether or not the BehaviorParameters are working in Heuristic mode or not.
        /// If we are working in Heuristic mode, we want the input system to handle everything.  If not, we
        /// want the neural network to send input from virtual devices.
        /// </summary>
        /// <param name="isInHeuristicMode">true if the Agent connected to this GameObject is working in
        /// Heuristic mode.</param>
        /// <seealso cref="BehaviorParameters.IsInHeuristicMode"/>
        internal void UpdateDeviceBinding(bool isInHeuristicMode)
        {
            if (ReferenceEquals(m_Device, null))
            {
                return;
            }
            var collection = m_AssetCollection ?? m_InputAsset;
            m_ControlScheme = CreateControlScheme(m_Device, isInHeuristicMode, m_InputAsset);
            if (m_InputAsset.FindControlSchemeIndex(m_ControlScheme.name) != -1)
            {
                m_InputAsset.RemoveControlScheme(m_ControlScheme.name);
            }

            if (!isInHeuristicMode)
            {
                var inputActionMap = m_InputAsset.FindActionMap(m_PlayerInput.defaultActionMap);
                m_InputAsset.AddControlScheme(m_ControlScheme);
                collection.bindingMask = InputBinding.MaskByGroup(m_ControlScheme.bindingGroup);
                collection.devices = new ReadOnlyArray<InputDevice>(new[] { m_Device });
                inputActionMap.bindingMask = collection.bindingMask;
                inputActionMap.devices = collection.devices;
            }
            else
            {
                var inputActionMap = m_InputAsset.FindActionMap(m_PlayerInput.defaultActionMap);
                collection.bindingMask = null;
                collection.devices = InputSystem.devices;
                inputActionMap.devices = InputSystem.devices;
                inputActionMap.bindingMask = null;
            }
            collection.Enable();
        }

        /// <summary>
        /// This method creates a control scheme and adds it to the <see cref="InputActionAsset"/> passed in so
        /// we can add our device to in order for it to be discovered by the <see cref="InputSystem"/>.
        /// </summary>
        /// <param name="device">The virtual device to add to our custom control scheme.</param>
        /// <param name="isInHeuristicMode">if we are in heuristic mode, we need to add other other device requirements.</param>
        /// <param name="asset">The InputActionAsset to get the device requirements from</param>
        internal static InputControlScheme CreateControlScheme(InputControl device,
            bool isInHeuristicMode,
            InputActionAsset asset)
        {
            var deviceRequirements = new List<InputControlScheme.DeviceRequirement>
            {
                new InputControlScheme.DeviceRequirement
                {
                    controlPath = InputBinding.Separator + mlAgentsLayoutName
                }
            };

            if (isInHeuristicMode)
            {
                for (var i = 0; i < asset.controlSchemes.Count; i++)
                {
                    var scheme = asset.controlSchemes[i];
                    for (var ii = 0; ii < scheme.deviceRequirements.Count; ii++)
                    {
                        deviceRequirements.Add(scheme.deviceRequirements[ii]);
                    }
                }
            }

            var inputControlScheme = new InputControlScheme(
                mlAgentsControlSchemeName,
                deviceRequirements);

            return inputControlScheme;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="defaultMap"></param>
        /// <param name="layoutName"></param>
        /// <returns></returns>
        internal static void RegisterLayoutBuilder(InputActionMap defaultMap, string layoutName)
        {
            if (InputSystem.LoadLayout(layoutName) == null)
            {
                InputSystem.RegisterLayoutBuilder(() =>
                {
                    // TODO does this need to change based on the action map we use?
                    var builder = new InputControlLayout.Builder()
                        .WithName(layoutName)
                        .WithFormat(mlAgentsLayoutFormat);
                    for (var i = 0; i < defaultMap.actions.Count; i++)
                    {
                        var action = defaultMap.actions[i];
                        builder.AddControl(action.name)
                            .WithLayout(action.expectedControlType);
                    }
                    return builder.Build();
                }, layoutName);
            }
        }

        internal void FindNeededComponents()
        {
            if (m_InputAsset == null)
            {
                var assetProvider = GetComponent<IInputActionAssetProvider>();
                Assert.IsNotNull(assetProvider);
                (m_InputAsset, m_AssetCollection) = assetProvider.GetInputActionAsset();
                Assert.IsNotNull(m_InputAsset, "An InputActionAsset could not be found on IInputActionAssetProvider or PlayerInput.");
            }
            if (m_PlayerInput == null)
            {
                m_PlayerInput = GetComponent<PlayerInput>();
                Assert.IsNotNull(m_PlayerInput, "PlayerInput component could not be found on this GameObject.");
            }

            if (m_BehaviorParameters == null)
            {
                m_BehaviorParameters = GetComponent<BehaviorParameters>();
                Assert.IsNotNull(m_BehaviorParameters, "BehaviorParameters were not on the current GameObject.");
                m_BehaviorParameters.OnPolicyUpdated += UpdateDeviceBinding;
                m_LayoutName = mlAgentsLayoutName + m_BehaviorParameters.BehaviorName;
            }
        }

        internal void CleanupActionAsset()
        {
            InputSystem.RemoveLayout(mlAgentsLayoutName);
            if (!ReferenceEquals(m_Device, null))
            {
                InputSystem.RemoveDevice(m_Device);
            }

            if (!ReferenceEquals(m_InputAsset, null)
                && m_InputAsset.FindControlSchemeIndex(mlAgentsControlSchemeName) != -1)
            {
                m_InputAsset.RemoveControlScheme(mlAgentsControlSchemeName);
            }

            if (m_Actuators != null)
            {
                Array.Clear(m_Actuators, 0, m_Actuators.Length);
            }

            if (!ReferenceEquals(m_BehaviorParameters, null))
            {
                m_BehaviorParameters.OnPolicyUpdated -= UpdateDeviceBinding;
            }

            m_InputAsset = null;
            m_PlayerInput = null;
            m_BehaviorParameters = null;
            m_Device = null;
        }

        int m_ActuatorsWrittenToEvent;
        NativeArray<byte> m_InputBufferForFrame;
        InputEventPtr m_InputEventPtrForFrame;
        public InputEventPtr GetEventForFrame()
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                return new InputEventPtr();
            }
#endif
            if (m_ActuatorsWrittenToEvent % m_Actuators.Length == 0 || !m_InputEventPtrForFrame.valid)
            {
                m_ActuatorsWrittenToEvent = 0;
                m_InputEventPtrForFrame = new InputEventPtr();
                m_InputBufferForFrame = StateEvent.From(m_Device, out m_InputEventPtrForFrame);
            }

            return m_InputEventPtrForFrame;
        }

        public void EventProcessedInFrame()
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                return;
            }
#endif
            m_ActuatorsWrittenToEvent++;
            if (m_ActuatorsWrittenToEvent == m_Actuators.Length && m_InputEventPtrForFrame.valid)
            {
                InputSystem.QueueEvent(m_InputEventPtrForFrame);
                m_InputBufferForFrame.Dispose();
            }
        }
    }
}
#endif // MLA_INPUT_SYSTEM