Beispiel #1
0
        // Send messages to all clients.
        public bool SendMessageToClients <TMessage>(TMessage msg) where TMessage : struct, MyExternalDebugStructures.IExternalDebugMsg
        {
            int messageDataSize = Marshal.SizeOf(typeof(TMessage));

            MyExternalDebugStructures.CommonMsgHeader msgHeader = MyExternalDebugStructures.CommonMsgHeader.Create(msg.GetTypeStr(), messageDataSize);
            Marshal.StructureToPtr(msgHeader, m_tempBuffer, true);
            Marshal.Copy(m_tempBuffer, m_arrayBuffer, 0, MyExternalDebugStructures.MsgHeaderSize);
            Marshal.StructureToPtr(msg, m_tempBuffer, true);
            Marshal.Copy(m_tempBuffer, m_arrayBuffer, MyExternalDebugStructures.MsgHeaderSize, messageDataSize);

            foreach (var clientInfo in m_clients)
            {
                try
                {
                    if (clientInfo.TcpClient.Client != null)
                    {
                        clientInfo.TcpClient.Client.Send(m_arrayBuffer, 0,
                                                         MyExternalDebugStructures.MsgHeaderSize + messageDataSize, SocketFlags.None);
                    }
                }
                catch (SocketException)
                {
                    clientInfo.TcpClient.Close();
                }
            }
            return(true);
        }
Beispiel #2
0
        private void ClientThreadProc()
        {
            while (!m_finished)
            {
                if (m_client == null || !m_client.Connected)
                {
                    var result = MyTryConnectHelper.TryConnect(IPAddress.Loopback.ToString(), GameDebugPort);
                    if (!result)
                    {
                        Thread.Sleep(2500);
                        continue;
                    }

                    try
                    {
                        m_client = new TcpClient();
                        m_client.Connect(IPAddress.Loopback, GameDebugPort);
                    }
                    catch (Exception)
                    {
                        // just try to connect all the time
                    }

                    if (m_client == null || m_client.Client == null || !m_client.Connected)
                    {
                        Thread.Sleep(2500);
                        continue;
                    }
                }

                try
                {
                    m_client.Client.Receive(m_arrayBuffer, 0, MyExternalDebugStructures.MsgHeaderSize, SocketFlags.None);
                    Marshal.Copy(m_arrayBuffer, 0, m_tempBuffer, MyExternalDebugStructures.MsgHeaderSize);
                    MyExternalDebugStructures.CommonMsgHeader header = (MyExternalDebugStructures.CommonMsgHeader)
                                                                       Marshal.PtrToStructure(m_tempBuffer, typeof(MyExternalDebugStructures.CommonMsgHeader));
                    if (header.IsValid)
                    {
                        m_client.Client.Receive(m_arrayBuffer, header.MsgSize, SocketFlags.None);
                        if (m_receivedMsgHandlers != null)
                        {
                            Marshal.Copy(m_arrayBuffer, 0, m_tempBuffer, header.MsgSize);
                            // callback
                            foreach (var handler in m_receivedMsgHandlers)
                            {
                                if (handler != null)
                                {
                                    handler(header, m_tempBuffer);
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // ignore invalid message - TODO: do something smarter
                }
            }
        }
        private void ReceivedMessageHandler(MyExternalDebugStructures.CommonMsgHeader messageHeader, IntPtr messageData)
        {
            MyExternalDebugStructures.ACReloadInGameMsg msgReload;
            if (MyExternalDebugStructures.ReadMessageFromPtr(ref messageHeader, messageData, out msgReload))
            {
                try
                {
                    string acAddress = msgReload.ACAddress;
                    string acName    = msgReload.ACName;

                    MyObjectBuilder_Definitions allDefinitions; // = null;
                    // load animation controller definition from SBC file
                    if (MyObjectBuilderSerializer.DeserializeXML(acAddress, out allDefinitions) &&
                        allDefinitions.Definitions != null &&
                        allDefinitions.Definitions.Length > 0)
                    {
                        var          firstDef = allDefinitions.Definitions[0];
                        MyModContext context  = new MyModContext();
                        context.Init("AnimationControllerDefinition", Path.GetFileName(acAddress));
                        MyAnimationControllerDefinition animationControllerDefinition = new MyAnimationControllerDefinition();
                        animationControllerDefinition.Init(firstDef, context);

                        // swap animation controller for each entity
                        foreach (MyEntity entity in MyEntities.GetEntities())
                        {
                            MyCharacter character = entity as MyCharacter;
                            if (character != null && character.Definition.AnimationController == acName)
                            {
                                character.AnimationController.InitFromDefinition(animationControllerDefinition);
                                character.ObtainBones();
                            }
                        }

                        // update in def. manager
                        MyStringHash animSubtypeNameHash = MyStringHash.GetOrCompute(acName);
                        MyAnimationControllerDefinition animControllerDefInManager =
                            MyDefinitionManager.Static.GetDefinition <MyAnimationControllerDefinition>(animSubtypeNameHash);
                        animControllerDefInManager.Init(firstDef, context);
                    }
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(e);
                }
            }
        }
Beispiel #4
0
        public bool SendMessageToGame <TMessage>(TMessage msg) where TMessage : MyExternalDebugStructures.IExternalDebugMsg
        {
            if (m_client == null || m_client.Client == null || !m_client.Connected)
            {
                return(false);
            }

            int messageDataSize = Marshal.SizeOf(typeof(TMessage));

            MyExternalDebugStructures.CommonMsgHeader msgHeader = MyExternalDebugStructures.CommonMsgHeader.Create(msg.GetTypeStr(), messageDataSize);
            Marshal.StructureToPtr(msgHeader, m_tempBuffer, true);
            Marshal.Copy(m_tempBuffer, m_arrayBuffer, 0, MyExternalDebugStructures.MsgHeaderSize);
            Marshal.StructureToPtr(msg, m_tempBuffer, true);
            Marshal.Copy(m_tempBuffer, m_arrayBuffer, MyExternalDebugStructures.MsgHeaderSize, messageDataSize);
            try
            {
                m_client.Client.Send(m_arrayBuffer, 0, MyExternalDebugStructures.MsgHeaderSize + messageDataSize, SocketFlags.None);
            }
            catch (SocketException)
            {
                return(false);
            }
            return(true);
        }
Beispiel #5
0
        // receiving messages
        private void LiveDebugging_ReceivedMessageHandler(MyExternalDebugStructures.CommonMsgHeader messageHeader, IntPtr messageData)
        {
            MyExternalDebugStructures.ACReloadInGameMsg msgReload;
            if (MyExternalDebugStructures.ReadMessageFromPtr(ref messageHeader, messageData, out msgReload))
            {
                try
                {
                    string acContentPath = msgReload.ACContentAddress;
                    string acAddress     = msgReload.ACAddress;
                    string acName        = msgReload.ACName;

                    MyObjectBuilder_Definitions allDefinitions; // = null;
                    // load animation controller definition from SBC file
                    if (MyObjectBuilderSerializer.DeserializeXML(acAddress, out allDefinitions) &&
                        allDefinitions.Definitions != null &&
                        allDefinitions.Definitions.Length > 0)
                    {
                        var          firstDef = allDefinitions.Definitions[0];
                        MyModContext context  = new MyModContext();
                        context.Init("AnimationControllerDefinition", acAddress, acContentPath);
                        MyAnimationControllerDefinition animationControllerDefinition = new MyAnimationControllerDefinition();
                        animationControllerDefinition.Init(firstDef, context);
                        MyStringHash animSubtypeNameHash = MyStringHash.GetOrCompute(acName);

                        // post process and update in def. manager
                        MyAnimationControllerDefinition originalAnimationControllerDefinition =
                            MyDefinitionManagerBase.Static.GetDefinition <MyAnimationControllerDefinition>(
                                animSubtypeNameHash);

                        var postprocessor = MyDefinitionManagerBase.GetPostProcessor(typeof(MyObjectBuilder_AnimationControllerDefinition));
                        if (postprocessor != null)
                        {
                            MyDefinitionPostprocessor.Bundle originalBundle = new MyDefinitionPostprocessor.Bundle
                            {
                                Context     = MyModContext.BaseGame,
                                Definitions = new Dictionary <MyStringHash, MyDefinitionBase>
                                {
                                    { animSubtypeNameHash, originalAnimationControllerDefinition }
                                },
                                Set = new MyDefinitionSet()
                            };
                            originalBundle.Set.AddDefinition(originalAnimationControllerDefinition);

                            MyDefinitionPostprocessor.Bundle overridingBundle = new MyDefinitionPostprocessor.Bundle
                            {
                                Context     = context,
                                Definitions = new Dictionary <MyStringHash, MyDefinitionBase>
                                {
                                    { animSubtypeNameHash, animationControllerDefinition }
                                },
                                Set = new MyDefinitionSet()
                            };
                            overridingBundle.Set.AddDefinition(animationControllerDefinition);

                            // postprocess -> override existing definition in memory
                            postprocessor.AfterLoaded(ref overridingBundle);
                            postprocessor.OverrideBy(ref originalBundle, ref overridingBundle);
                        }

                        // swap animation controller for each entity
                        foreach (var component in m_skinnedEntityComponents)
                        {
                            if (component != null && component.SourceId.SubtypeName == acName)
                            {
                                component.Clear();
                                component.InitFromDefinition(originalAnimationControllerDefinition, forceReloadMwm: true); // reload from original def that was modified by postprocessor
                                if (component.ReloadBonesNeeded != null)
                                {
                                    component.ReloadBonesNeeded();
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(e);
                }
            }
        }