Пример #1
0
            public static unsafe Message Create(InputRemoting sender, IntPtr events, int eventCount)
            {
                // Find total size of event buffer we need.
                var totalSize = 0u;
                var eventPtr  = new InputEventPtr(events);

                for (var i = 0; i < eventCount; ++i, eventPtr = eventPtr.Next())
                {
                    totalSize += eventPtr.sizeInBytes;
                }

                // Copy event data to buffer. Would be nice if we didn't have to do that
                // but unfortunately we need a byte[] and can't just pass the 'events' IntPtr
                // directly.
                var data = new byte[totalSize];

                fixed(byte *dataPtr = data)
                {
                    UnsafeUtility.MemCpy(new IntPtr(dataPtr), events, totalSize);
                }

                // Done.
                return(new Message
                {
                    type = MessageType.NewEvents,
                    data = data
                });
            }
Пример #2
0
            public static Message?Create(InputRemoting sender, string templateName)
            {
                // Try to load the template. Ignore the template if it couldn't
                // be loaded.
                InputTemplate template;

                try
                {
                    template = sender.m_LocalManager.TryLoadTemplate(new InternedString(templateName));
                }
                catch (Exception exception)
                {
                    Debug.Log(string.Format(
                                  "Could not load template '{0}'; not sending to remote listeners (exception: {1})", templateName,
                                  exception));
                    return(null);
                }

                var json  = template.ToJson();
                var bytes = Encoding.UTF8.GetBytes(json);

                return(new Message
                {
                    type = MessageType.NewTemplate,
                    data = bytes
                });
            }
Пример #3
0
 public static Message Create(InputRemoting sender, InputDevice device)
 {
     return(new Message
     {
         type = MessageType.RemoveDevice,
         data = BitConverter.GetBytes(device.id)
     });
 }
Пример #4
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);
                var @namespace  = receiver.m_Senders[senderIndex].templateNamespace;

                var templateName = Encoding.UTF8.GetString(msg.data);

                receiver.m_LocalManager.RemoveTemplate(templateName, @namespace: @namespace);
            }
Пример #5
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var json        = Encoding.UTF8.GetString(msg.data);
                var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);
                var @namespace  = receiver.m_Senders[senderIndex].templateNamespace;

                receiver.m_LocalManager.RegisterTemplate(json, @namespace: @namespace);
                ArrayHelpers.Append(ref receiver.m_Senders[senderIndex].templates, json);
            }
Пример #6
0
            public static Message Create(InputRemoting sender, string templateName)
            {
                var bytes = Encoding.UTF8.GetBytes(templateName);

                return(new Message
                {
                    type = MessageType.RemoveTemplate,
                    data = bytes
                });
            }
Пример #7
0
 public static void Process(InputRemoting receiver, Message msg)
 {
     if (receiver.sending)
     {
         receiver.SendAllCurrentData(msg.participantId);
     }
     else if ((receiver.m_Flags & Flags.StartSendingOnConnect) == Flags.StartSendingOnConnect)
     {
         receiver.StartSending();
     }
 }
Пример #8
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var senderIndex    = receiver.FindOrCreateSenderRecord(msg.participantId);
                var remoteDeviceId = BitConverter.ToInt32(msg.data, 0);

                var device = receiver.TryGetDeviceByRemoteId(remoteDeviceId, senderIndex);

                if (device != null)
                {
                    receiver.m_LocalManager.RemoveDevice(device);
                }
            }
Пример #9
0
            public static Message Create(InputRemoting sender, InputDevice device)
            {
                var data = new Data
                {
                    deviceId = device.id,
                    usages   = device.usages.Select(x => x.ToString()).ToArray()
                };

                return(new Message
                {
                    type = MessageType.ChangeUsages,
                    data = SerializeData(data)
                });
            }
Пример #10
0
        private static void InitializeInPlayer()
        {
            // No domain reloads in the player so we don't need to look for existing
            // instances.
            s_Manager = new InputManager();
            s_Manager.Initialize();

            ////TODO: put this behind a switch so that it is off by default
            // Automatically enable remoting in development players.
            #if DEVELOPMENT_BUILD
            s_ConnectionToEditor = ScriptableObject.CreateInstance <RemoteInputPlayerConnection>();
            s_Remote             = new InputRemoting(s_Manager, startSendingOnConnect: true);
            s_Remote.Subscribe(s_ConnectionToEditor);
            s_ConnectionToEditor.Subscribe(s_Remote);
            s_ConnectionToEditor.Bind(PlayerConnection.instance, PlayerConnection.instance.isConnected);
            #endif
        }
Пример #11
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);
                var data        = DeserializeData <Data>(msg.data);

                var device = receiver.TryGetDeviceByRemoteId(data.deviceId, senderIndex);

                if (device != null)
                {
                    ////TODO: clearing usages and setting multiple usages

                    if (data.usages.Length == 1)
                    {
                        receiver.m_LocalManager.SetUsage(device, new InternedString(data.usages[0]));
                    }
                }
            }
Пример #12
0
        // For testing, we want the ability to push/pop system state even in the player.
        // However, we don't want it in release players.
#if DEVELOPMENT_BUILD || UNITY_EDITOR
        internal static void Reset()
        {
            #if UNITY_EDITOR
            if (s_SystemObject != null)
            {
                UnityEngine.Object.DestroyImmediate(s_SystemObject);
            }
            s_SystemObject = ScriptableObject.CreateInstance <InputSystemObject>();
            s_Manager      = s_SystemObject.manager;
            s_Remote       = s_SystemObject.remote;
            #else
            if (s_Manager != null)
            {
                s_Manager.Destroy();
            }
            ////TODO: reset remote
            InitializeInPlayer();
            #endif
        }
Пример #13
0
        private static void InitializeInEditor()
        {
            var existingSystemObjects = Resources.FindObjectsOfTypeAll <InputSystemObject>();

            if (existingSystemObjects != null && existingSystemObjects.Length > 0)
            {
                s_SystemObject = existingSystemObjects[0];
                s_SystemObject.ReviveAfterDomainReload();
                s_Manager = s_SystemObject.manager;
                s_Remote  = s_SystemObject.remote;
                InputDebuggerWindow.ReviveAfterDomainReload();
            }
            else
            {
                Reset();
            }

            EditorApplication.playModeStateChanged += OnPlayModeChange;
        }
Пример #14
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);

                // Remove devices added by remote.
                foreach (var remoteDevice in receiver.m_Senders[senderIndex].devices)
                {
                    var device = receiver.m_LocalManager.TryGetDeviceById(remoteDevice.localId);
                    if (device != null)
                    {
                        receiver.m_LocalManager.RemoveDevice(device);
                    }
                }

                ////TODO: remove templates added by remote

                ArrayHelpers.EraseAt(ref receiver.m_Senders, senderIndex);

                ////TODO: stop sending if last remote disconnects and StartSendingOnConnect is active
            }
Пример #15
0
        private void SetUpRemoting()
        {
            remote = new InputRemoting(manager);
            remote.RestoreState(m_RemotingState, manager);

            if (playerConnection == null)
            {
                playerConnection = CreateInstance <RemoteInputPlayerConnection>();
            }

            remote.Subscribe(playerConnection); // Feed messages from players into editor.
            playerConnection.Subscribe(remote); // Feed messages from editor into players.

            playerConnection.Bind(EditorConnection.instance, false);

            // We don't enable sending on the editor's remote by default.
            // By default, the editor acts as a receiver only.

            m_RemotingState = new InputRemoting.SerializedState();
        }
Пример #16
0
            public static Message Create(InputRemoting sender, InputDevice device)
            {
                Debug.Assert(!device.remote, "Device being sent to remotes should be a local device, not a remote one");

                var data = new Data
                {
                    name        = device.name,
                    template    = device.template,
                    deviceId    = device.id,
                    description = device.description
                };

                var json  = JsonUtility.ToJson(data);
                var bytes = Encoding.UTF8.GetBytes(json);

                return(new Message
                {
                    type = MessageType.NewDevice,
                    data = bytes
                });
            }
Пример #17
0
            public static unsafe void Process(InputRemoting receiver, Message msg)
            {
                // This isn't the best solution but should do for now. NativeInputSystem isn't
                // designed for having multiple InputManagers and we only have that scenario
                // for tests ATM. So, to make InputManagers that aren't really properly connected
                // still work for testing, we directly feed them the events we get here.
                var isConnectedToNative = NativeInputSystem.onUpdate == receiver.m_LocalManager.OnNativeUpdate;

                fixed(byte *dataPtr = msg.data)
                {
                    var dataEndPtr  = new IntPtr(dataPtr + msg.data.Length);
                    var eventCount  = 0;
                    var eventPtr    = new InputEventPtr((InputEvent *)dataPtr);
                    var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);

                    while (eventPtr.data.ToInt64() < dataEndPtr.ToInt64())
                    {
                        // Patch up device ID to refer to local device and send event.
                        var remoteDeviceId = eventPtr.deviceId;
                        var localDeviceId  = receiver.FindLocalDeviceId(remoteDeviceId, senderIndex);
                        eventPtr.deviceId = localDeviceId;

                        if (localDeviceId != InputDevice.kInvalidDeviceId && isConnectedToNative)
                        {
                            ////TODO: add API to send events in bulk rather than one by one
                            InputSystem.QueueEvent(eventPtr);
                        }

                        ++eventCount;
                        eventPtr = eventPtr.Next();
                    }

                    if (!isConnectedToNative)
                    {
                        receiver.m_LocalManager.OnNativeUpdate(NativeInputUpdateType.Dynamic, eventCount, new IntPtr(dataPtr));
                    }
                }
            }
Пример #18
0
            public static void Process(InputRemoting receiver, Message msg)
            {
                var senderIndex = receiver.FindOrCreateSenderRecord(msg.participantId);
                var data        = DeserializeData <Data>(msg.data);

                // Create device.
                var template = string.Format("{0}::{1}", receiver.m_Senders[senderIndex].templateNamespace,
                                             data.template);
                InputDevice device;

                try
                {
                    device = receiver.m_LocalManager.AddDevice(template,
                                                               string.Format("Remote{0}::{1}", msg.participantId, data.name));
                }
                catch (Exception exception)
                {
                    Debug.Log(
                        string.Format(
                            "Could not create remote device '{0}' with template '{1}' locally (exception: {2})",
                            data.description, data.template, exception));
                    return;
                }
                device.m_Description = data.description;
                device.m_Flags      |= InputDevice.Flags.Remote;

                // Remember it.
                var record = new RemoteInputDevice
                {
                    remoteId     = data.deviceId,
                    localId      = device.id,
                    description  = data.description,
                    templateName = template
                };

                ArrayHelpers.Append(ref receiver.m_Senders[senderIndex].devices, record);
            }