// Returns a SerializationData a Serializer can use to save the state // of the object to an Xml file public SerializationData GetSerializationData(Serializer Serializer, XmlWriter Writer) { // Create a new SerializationData SerializationData data = new SerializationData(Serializer, Writer); // Add the basic Component values data.AddData("Component.DrawOrder", DrawOrder); data.AddData("Component.ParentScreen", Parent.Name); data.AddData("Component.Visible", Visible); data.AddData("Component.Name", this.Name); // Tell serializer that it will need to know the type of component data.AddDependency(this.GetType()); // Construct a ServiceData ServiceData sd = new ServiceData(); // If this object is a service, find out what the provider type is // (the type used to look up the services) Type serviceType; if (Engine.Services.IsService(this, out serviceType)) { // Tell serializer about provider type data.AddDependency(serviceType); // Set data to ServiceData sd.IsService = true; sd.Type = serviceType.FullName; } // Add the ServiceData to the SerializationData data.AddData("Component.ServiceData", sd); // Call the overridable function that allows components to provide data SaveSerializationData(data); return data; }
// Save the current state of the engine to file public static void SerializeState(string Filename) { // Get the start time DateTime startTime = DateTime.Now; // Create an XmlWriter XmlWriterSettings set = new XmlWriterSettings(); set.Indent = true; XmlWriter writer = XmlWriter.Create(new FileStream(Filename, FileMode.Create), set); // Create Serializer Serializer s = new Serializer(); // Write start of document including root node and save time writer.WriteStartDocument(); writer.WriteStartElement("EngineState"); writer.WriteAttributeString("Time", startTime.ToString()); s.WriteGameScreens(writer); writer.WriteStartElement("Components"); // Serialize components foreach (GameScreen gameScreen in GameScreens) foreach (Component component in gameScreen.Components) if (component.Serialize) { writer.WriteStartElement(component.GetType().FullName); s.Serialize(writer, component.GetSerializationData(s, writer)); writer.WriteEndElement(); } writer.WriteEndElement(); s.WriteDependencies(writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); DateTime stopTime = DateTime.Now; TimeSpan elapsedTime = stopTime - startTime; }