public static void SendEntityCreated(MyObjectBuilder_EntityBase entity)
        {
            var msg = new CreateCompressedMsg();

            MemoryStream stream = new MemoryStream();

            MyObjectBuilderSerializer.SerializeXML(stream, (MyObjectBuilder_Base)entity, MyObjectBuilderSerializer.XmlCompression.Gzip, typeof(MyObjectBuilder_EntityBase));

            Debug.Assert(stream.Length <= int.MaxValue);
            if (stream.Length > int.MaxValue)
            {
                MySandboxGame.Log.WriteLine("Cannot synchronize created entity: number of bytes when serialized is larger than int.MaxValue!");
                return;
            }

            msg.ObjectBuilders    = stream.ToArray();
            msg.BuilderLengths    = new int[1];
            msg.BuilderLengths[0] = (int)stream.Length;

            MySession.Static.SyncLayer.SendMessageToAll(ref msg);
        }
        static void OnMessageCompressed(ref CreateCompressedMsg msg, MyNetworkClient sender)
        {
            MySandboxGame.Log.WriteLine("CreateCompressedMsg received");

            Debug.Assert(msg.BuilderLengths != null);
            Debug.Assert(msg.ObjectBuilders != null);

            if (msg.BuilderLengths == null)
            {
                return;
            }
            if (msg.ObjectBuilders == null)
            {
                return;
            }

            int bytesOffset = 0;

            for (int i = 0; i < msg.BuilderLengths.Length; ++i)
            {
                MemoryStream stream = new MemoryStream(msg.ObjectBuilders, bytesOffset, msg.BuilderLengths[i]);

                MyObjectBuilder_EntityBase entity;
                if (Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.DeserializeGZippedXML(stream, out entity))
                {
                    Debug.Assert(entity != null);
                    if (entity != null)
                    {
                        MySandboxGame.Log.WriteLine("CreateCompressedMsg: " + msg.ObjectBuilders.GetType().Name.ToString() + " EntityID: " + entity.EntityId.ToString("X8"));
                        MyEntities.CreateFromObjectBuilderAndAdd(entity);
                        MySandboxGame.Log.WriteLine("Status: Exists(" + MyEntities.EntityExists(entity.EntityId) + ") InScene(" + ((entity.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene) + ")");
                    }
                }

                bytesOffset += msg.BuilderLengths[i];
            }
        }
Beispiel #3
0
        private static MyEntity OnMessageCompressedInternal(ref CreateCompressedMsg msg)
        {
            Debug.Fail("Use replication instead of this!");
            MyEntity firstEntity = null;

            int bytesOffset = 0;
            for (int i = 0; i < msg.BuilderLengths.Length; ++i)
            {
                MemoryStream stream = new MemoryStream(msg.ObjectBuilders, bytesOffset, msg.BuilderLengths[i]);

                MyObjectBuilder_EntityBase entity;
                if (MyObjectBuilderSerializer.DeserializeGZippedXML(stream, out entity))
                {
                    MySandboxGame.Log.WriteLine("CreateCompressedMsg: Type: " + entity.GetType().Name.ToString() + "  Name: " + entity.Name + "  EntityID: " + entity.EntityId.ToString("X8"));
                    if (i == 0)
                        firstEntity = MyEntities.CreateFromObjectBuilderAndAdd(entity);
                    else
                        MyEntities.CreateFromObjectBuilderAndAdd(entity);
                    MySandboxGame.Log.WriteLine("Status: Exists(" + MyEntities.EntityExists(entity.EntityId) + ") InScene(" + ((entity.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene) + ")");
                }

                bytesOffset += msg.BuilderLengths[i];
            }

            return firstEntity;
        }
Beispiel #4
0
        private static bool BuildCompressedMessage(List<MyObjectBuilder_CubeGrid> entities, out CreateCompressedMsg msg)
        {
            msg = new CreateCompressedMsg();

            MemoryStream stream = new MemoryStream();
            List<int> lengths = new List<int>(8);

            long byteOffset = 0;
            foreach (var entity in entities)
            {
                MyObjectBuilderSerializer.SerializeXML(stream, (MyObjectBuilder_Base)entity, MyObjectBuilderSerializer.XmlCompression.Gzip, typeof(MyObjectBuilder_EntityBase));

                Debug.Assert(stream.Length <= int.MaxValue);
                if (stream.Length > int.MaxValue)
                {
                    MySandboxGame.Log.WriteLine("Cannot synchronize created entity: number of bytes when serialized is larger than int.MaxValue!");
                    return false;
                }

                lengths.Add((int)(stream.Length - byteOffset));
                byteOffset = stream.Length;
            }

            msg.ObjectBuilders = stream.ToArray();
            msg.BuilderLengths = lengths.ToArray();
            return true;
        }
Beispiel #5
0
        public static void SendEntityCreated(MyObjectBuilder_EntityBase entity)
        {
            Debug.Fail("Use replication instead of this!");
            var msg = new CreateCompressedMsg();

            MemoryStream stream = new MemoryStream();
            MyObjectBuilderSerializer.SerializeXML(stream, (MyObjectBuilder_Base)entity, MyObjectBuilderSerializer.XmlCompression.Gzip, typeof(MyObjectBuilder_EntityBase));

            Debug.Assert(stream.Length <= int.MaxValue);
            if (stream.Length > int.MaxValue)
            {
                MySandboxGame.Log.WriteLine("Cannot synchronize created entity: number of bytes when serialized is larger than int.MaxValue!");
                return;
            }

            msg.ObjectBuilders = stream.ToArray();
            msg.BuilderLengths = new int[1];
            msg.BuilderLengths[0] = (int)stream.Length;

            if (Sync.IsServer)
            {
                MySession.Static.SyncLayer.SendMessageToAll(ref msg);
            }
            else
            {
                MySession.Static.SyncLayer.SendMessageToServer(ref msg);
            }
        }
Beispiel #6
0
        static void OnMessageCompressed(ref CreateCompressedMsg msg, MyNetworkClient sender)
        {
            MySandboxGame.Log.WriteLine("CreateCompressedMsg received");

            Debug.Assert(msg.BuilderLengths != null);
            Debug.Assert(msg.ObjectBuilders != null);

            if (msg.BuilderLengths == null)
                return;
            if (msg.ObjectBuilders == null)
                return;

            OnMessageCompressedInternal(ref msg);
          
        }
        private static bool BuildCompressedMessage(List <MyObjectBuilder_EntityBase> entities, out CreateCompressedMsg msg)
        {
            msg = new CreateCompressedMsg();

            MemoryStream stream  = new MemoryStream();
            List <int>   lengths = new List <int>(8);

            long byteOffset = 0;

            foreach (var entity in entities)
            {
                Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.SerializeXML(stream, (MyObjectBuilder_Base)entity, Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.XmlCompression.Gzip, typeof(MyObjectBuilder_EntityBase));

                Debug.Assert(stream.Length <= int.MaxValue);
                if (stream.Length > int.MaxValue)
                {
                    MySandboxGame.Log.WriteLine("Cannot synchronize created entity: number of bytes when serialized is larger than int.MaxValue!");
                    return(false);
                }

                lengths.Add((int)(stream.Length - byteOffset));
                byteOffset = stream.Length;
            }

            msg.ObjectBuilders = stream.ToArray();
            msg.BuilderLengths = lengths.ToArray();
            return(true);
        }
        static void OnMessageCompressed(ref CreateCompressedMsg msg, MyNetworkClient sender)
        {
            MySandboxGame.Log.WriteLine("CreateCompressedMsg received");

            Debug.Assert(msg.BuilderLengths != null);
            Debug.Assert(msg.ObjectBuilders != null);
            
            if (msg.BuilderLengths == null)
                return;
            if (msg.ObjectBuilders == null)
                return;

            int bytesOffset = 0;
            for (int i = 0; i < msg.BuilderLengths.Length; ++i)
            {
                MemoryStream stream = new MemoryStream(msg.ObjectBuilders, bytesOffset, msg.BuilderLengths[i]);

                MyObjectBuilder_EntityBase entity;
                if (Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.DeserializeGZippedXML(stream, out entity))
                {
                    Debug.Assert(entity != null);
                    if (entity != null)
                    {
                        MySandboxGame.Log.WriteLine("CreateCompressedMsg: " + msg.ObjectBuilders.GetType().Name.ToString() + " EntityID: " + entity.EntityId.ToString("X8"));
                        MyEntities.CreateFromObjectBuilderAndAdd(entity);
                        MySandboxGame.Log.WriteLine("Status: Exists(" + MyEntities.EntityExists(entity.EntityId) + ") InScene(" + ((entity.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene) + ")");
                    }
                }

                bytesOffset += msg.BuilderLengths[i];
            }
        }