public static void SendEntityCreatedRelative(MyObjectBuilder_EntityBase entity, MyEntity baseEntity, Vector3 relativeVelocity) { var msg = new CreateRelativeCompressedMsg(); MemoryStream stream = new MemoryStream(); Matrix relativeMatrix = entity.PositionAndOrientation.Value.GetMatrix() * baseEntity.PositionComp.WorldMatrixNormalizedInv; entity.PositionAndOrientation = new MyPositionAndOrientation(relativeMatrix); 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; } msg.CreateMessage.ObjectBuilders = stream.ToArray(); msg.CreateMessage.BuilderLengths = new int[1]; msg.CreateMessage.BuilderLengths[0] = (int)stream.Length; msg.BaseEntity = baseEntity.EntityId; msg.RelativeVelocity = relativeVelocity; MySession.Static.SyncLayer.SendMessageToAll(ref msg); }
static void OnMessageRelativeCompressed(ref CreateRelativeCompressedMsg msg, MyNetworkClient sender) { MySandboxGame.Log.WriteLine("CreateRelativeCompressedMsg received"); int bytesOffset = 0; for (int i = 0; i < msg.CreateMessage.BuilderLengths.Length; ++i) { MemoryStream stream = new MemoryStream(msg.CreateMessage.ObjectBuilders, bytesOffset, msg.CreateMessage.BuilderLengths[i]); MyObjectBuilder_EntityBase entity; if (Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.DeserializeGZippedXML(stream, out entity)) { MySandboxGame.Log.WriteLine("CreateRelativeCompressedMsg: " + msg.CreateMessage.ObjectBuilders.GetType().Name.ToString() + " EntityID: " + entity.EntityId.ToString("X8")); MyEntity baseEntity; if (MyEntities.TryGetEntityById(msg.BaseEntity, out baseEntity)) { Matrix worldMatrix = entity.PositionAndOrientation.Value.GetMatrix() * baseEntity.WorldMatrix; entity.PositionAndOrientation = new MyPositionAndOrientation(worldMatrix); var newEntity = MyEntities.CreateFromObjectBuilderAndAdd(entity); Vector3 velocity = Vector3.Transform(msg.RelativeVelocity, baseEntity.WorldMatrix.GetOrientation()); newEntity.Physics.LinearVelocity = velocity; MySandboxGame.Log.WriteLine("Status: Exists(" + MyEntities.EntityExists(entity.EntityId) + ") InScene(" + ((entity.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene) + ")"); } } bytesOffset += msg.CreateMessage.BuilderLengths[i]; } }
public static void SendEntityCreatedRelative(MyObjectBuilder_EntityBase entity, MyEntity baseEntity, Vector3 relativeVelocity) { Debug.Fail("Use replication instead of this!"); var msg = new CreateRelativeCompressedMsg(); MemoryStream stream = new MemoryStream(); Matrix relativeMatrix = entity.PositionAndOrientation.Value.GetMatrix() * baseEntity.PositionComp.WorldMatrixNormalizedInv; entity.PositionAndOrientation = new MyPositionAndOrientation(relativeMatrix); 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.CreateMessage.ObjectBuilders = stream.ToArray(); msg.CreateMessage.BuilderLengths = new int[1]; msg.CreateMessage.BuilderLengths[0] = (int)stream.Length; msg.BaseEntity = baseEntity.EntityId; msg.RelativeVelocity = relativeVelocity; if (Sync.IsServer) { MySession.Static.SyncLayer.SendMessageToAll(ref msg); } else { MySession.Static.SyncLayer.SendMessageToServer(ref msg); } }
static void OnMessageRelativeCompressed(ref CreateRelativeCompressedMsg msg, MyNetworkClient sender) { Debug.Fail("Use replication instead of this!"); MySandboxGame.Log.WriteLine("CreateRelativeCompressedMsg received"); int bytesOffset = 0; for (int i = 0; i < msg.CreateMessage.BuilderLengths.Length; ++i) { MemoryStream stream = new MemoryStream(msg.CreateMessage.ObjectBuilders, bytesOffset, msg.CreateMessage.BuilderLengths[i]); MyObjectBuilder_EntityBase entity; if (MyObjectBuilderSerializer.DeserializeGZippedXML(stream, out entity)) { MyEntity baseEntity; if (MyEntities.TryGetEntityById(msg.BaseEntity, out baseEntity)) { MySandboxGame.Log.WriteLine("CreateRelativeCompressedMsg: Type: " + baseEntity.GetType().Name.ToString() + " Name: " + baseEntity.Name + " EntityID: " + baseEntity.EntityId.ToString("X8")); Matrix worldMatrix = entity.PositionAndOrientation.Value.GetMatrix() * baseEntity.WorldMatrix; entity.PositionAndOrientation = new MyPositionAndOrientation(worldMatrix); var newEntity = MyEntities.CreateFromObjectBuilderAndAdd(entity); Vector3 velocity = Vector3.Transform(msg.RelativeVelocity, baseEntity.WorldMatrix.GetOrientation()); newEntity.Physics.LinearVelocity = velocity; MySandboxGame.Log.WriteLine("Status: Exists(" + MyEntities.EntityExists(entity.EntityId) + ") InScene(" + ((entity.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene) + ")"); } } bytesOffset += msg.CreateMessage.BuilderLengths[i]; } if (Sync.IsServer) { MySession.Static.SyncLayer.SendMessageToAllButOne(ref msg, sender.SteamUserId); } }