public XElement Serialize(string name, object rootObject, Serializer serializer) { XElement root = new XElement(name); //Обрабатываем кольцевые ссылки if (serializer.CheckCircularReferences(root, rootObject)) { return(root); } //Сериализуем метаданные Serializer.SerializeMetadata(root, rootObject); //Сериализуем имя типа Serializer.SerializeTypeName(root, typeof(Body)); Body body = (Body)rootObject; root.Add( new XAttribute("BodyType", DefaultConverters.IntToString((int)body.BodyType)), new XAttribute("Active", DefaultConverters.BoolToString(body.Enabled)), new XAttribute("AllowSleep", DefaultConverters.BoolToString(body.SleepingAllowed)), new XAttribute("Angle", DefaultConverters.FloatToString(body.Rotation)), new XAttribute("AngularDamping", DefaultConverters.FloatToString(body.AngularDamping)), new XAttribute("AngularVelocity", DefaultConverters.FloatToString(body.AngularVelocity)), new XAttribute("Awake", DefaultConverters.BoolToString(body.Awake)), new XAttribute("Bullet", DefaultConverters.BoolToString(body.IsBullet)), new XAttribute("FixedRotation", DefaultConverters.BoolToString(body.FixedRotation)), new XAttribute("LinearDamping", DefaultConverters.FloatToString(body.LinearDamping)), new XAttribute("LinearVelocity", DefaultConverters.Vector2ToString(body.LinearVelocity)), new XAttribute("Position", DefaultConverters.Vector2ToString(body.Position)), serializer.SerializeObject("World", SerializerHelpers.GetFieldValue(body, "World"))); XElement fixtures = new XElement("Fixtures"); for (int i = 0; i < body.FixtureList.Count; i++) { fixtures.Add(serializer.SerializeObject("Fixture", body.FixtureList[i])); } root.Add(fixtures); return(root); }
public XElement Serialize(string name, object rootObject, Serializer serializer) { XElement root = new XElement(name); //Обрабатываем кольцевые ссылки if (serializer.CheckCircularReferences(root, rootObject)) { return(root); } //Сериализуем метаданные Serializer.SerializeMetadata(root, rootObject); //Сериализуем имя типа Serializer.SerializeTypeName(root, typeof(World)); //Сериализуем гравитацию World world = (World)rootObject; root.Add( new XAttribute("Gravity", DefaultConverters.Vector2ToString(world.Gravity))); //Сериализуем bodies XElement bodies = new XElement("Bodies"); for (int i = 0; i < world.BodyList.Count; i++) { bodies.Add(serializer.SerializeObject("Body", world.BodyList[i])); } //Сериализуем joint'ы XElement joints = new XElement("Joints"); foreach (Joint joint in world.JointList) { joints.Add(serializer.SerializeObject("Joint", joint)); } root.Add(bodies); root.Add(joints); return(root); }