コード例 #1
0
ファイル: Serializer.cs プロジェクト: JllyGrnGiant/game_demo
        // Deserialize component defined in the ComponentNode
        public Component Deserialize(XmlNode ComponentNode)
        {
            // Find type from component nodes name
            Assembly a = Assembly.Load(DependencyMap[ComponentNode.LocalName]);
            Type t = a.GetType(ComponentNode.LocalName);

            // Create an instance of the type and a SerializationData object
            Component component = (Component)Activator.CreateInstance(t);
            SerializationData data = new SerializationData(this, null);

            // For each field defined, get its value and add the field to SerializationData
            foreach (XmlNode child in ComponentNode.ChildNodes)
            {
                // Make name and object values, and get
                // the name from the 0 attribute.
                string name = child.Attributes[0].Value;
                object value = null;

                // If the field node contains text only, it's a value type
                // and we can set the object directly
                if (child.ChildNodes[0].NodeType == XmlNodeType.Text)
                    value = parse(child);
                // Otherwise we need to recreate a more complex object from the data
                else if (child.ChildNodes[0].NodeType == XmlNodeType.Element)
                    value = parseTree(child.FirstChild);

                // Save the field to the SerializationData
                data.AddData(name, value);
            }

            // Tell the component to load from the data
            component.RecieveSerializationData(data);

            return component;
        }
コード例 #2
0
ファイル: Component.cs プロジェクト: JllyGrnGiant/game_demo
        // 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;
        }