Ejemplo n.º 1
0
        public override void WriteField(XmlWriter xmlWriter, object fieldToWrite)
        {
            object list = fieldToWrite;

            int listCount = (int)list.GetType().GetProperty("Count").GetValue(list, null);

            for (int index = 0; index < listCount; index++)
            {
                object item = list.GetType().GetMethod("get_Item").Invoke(list, new object[] { index });
                if (item is GameObject)
                {
                    xmlWriter.WriteStartElement("Item");

                    GameDataAttribute.WriteTypeAttributes(xmlWriter, item);

                    ((GameObject)item).WriteGameObjectData(xmlWriter);
                    xmlWriter.WriteEndElement();
                }
                else
                {
                    xmlWriter.WriteValue(item);
                    xmlWriter.WriteValue(" ");
                }
            }
        }
Ejemplo n.º 2
0
        public override object ReadField(XmlReader xmlReader)
        {
            object list = GameDataAttribute.ReadTypeAttributes(xmlReader);

            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    if (xmlReader.Name == "Item")
                    {
                        object listItem = GameDataAttribute.ReadTypeAttributes(xmlReader);
                        if (listItem is GameObject)
                        {
                            GameObject listGameObject = (GameObject)listItem;
                            listGameObject.LoadGameObjectData(xmlReader);
                            MethodInfo addMethod = list.GetType().GetMethod("Add");
                            addMethod.Invoke(list, new object[] { listGameObject });
                        }
                        else
                        {
                            throw new NotImplementedException("List of non-GameObjects not deserializable");
                        }
                    }
                }
                else if (xmlReader.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }
            }

            return(list);
        }
Ejemplo n.º 3
0
        public override object ReadField(XmlReader xmlReader)
        {
            Vector2 newVector2D = (Vector2)GameDataAttribute.ReadTypeAttributes(xmlReader);

            string xString = xmlReader.GetAttribute("x");
            string yString = xmlReader.GetAttribute("y");

            newVector2D = new Vector2(Convert.ToDouble(xString), Convert.ToDouble(yString));

            return(newVector2D);
        }
Ejemplo n.º 4
0
        public virtual void WriteGameObjectData(XmlWriter xmlWriter)
        {
            Type gameObjectType = GetType();

            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            FieldInfo[] fieldsOfGameObject = gameObjectType.GetFields(bindingFlags);
            foreach (FieldInfo fieldOfGameObject in fieldsOfGameObject)
            {
                object[] gameDataAttributes = fieldOfGameObject.GetCustomAttributes(typeof(GameDataAttribute), false);
                if (gameDataAttributes.Length > 0)
                {
                    if (gameDataAttributes.Length > 1)
                    {
                        throw new Exception("You can only have one GameDataAttribute on any given Field.");
                    }

                    object objectWithAttribute = fieldOfGameObject.GetValue(this);
                    if (objectWithAttribute != null)
                    {
                        var    singleGameDataAttribute = (GameDataAttribute)gameDataAttributes[0];
                        string name = singleGameDataAttribute.Name;

                        if (name.Contains(" "))
                        {
                            throw new Exception(ToString() + " : '" + name + "' has a space. Attribute names con not contain spaces.");
                        }

                        xmlWriter.WriteStartElement(name);
                        GameDataAttribute.WriteTypeAttributes(xmlWriter, objectWithAttribute);
                        xmlWriter.WriteEndAttribute();

                        if (objectWithAttribute == null)
                        {
                            throw new Exception(ToString() + " : " + fieldOfGameObject.ToString() + " must have a default value.\n"
                                                + "\n"
                                                + "All data marked as [GameData] must be a primitive or a struct or if a class have a DEFALT value or filed initializer.");
                        }

                        if (objectWithAttribute is GameObject)
                        {
                            ((GameObject)objectWithAttribute).WriteGameObjectData(xmlWriter);
                        }
                        else
                        {
                            singleGameDataAttribute.WriteField(xmlWriter, objectWithAttribute);
                        }

                        xmlWriter.WriteEndElement();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public virtual void LoadGameObjectData(XmlReader xmlReader)
        {
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    string attributeNameForElement = xmlReader.Name;

                    BindingFlags bindingFlags       = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                    FieldInfo[]  fieldsOfGameObject = GetType().GetFields(bindingFlags);
                    foreach (FieldInfo fieldOfGameObject in fieldsOfGameObject)
                    {
                        object[] gameDataAttributes = fieldOfGameObject.GetCustomAttributes(typeof(GameDataAttribute), false);
                        if (gameDataAttributes.Length > 0)
                        {
                            var    singleGameDataAttribute = (GameDataAttribute)gameDataAttributes[0];
                            string AttributeNameForField   = singleGameDataAttribute.Name;
                            if (AttributeNameForField == attributeNameForElement)
                            {
                                if (fieldOfGameObject.FieldType.IsSubclassOf(typeof(GameObject)))
                                {
                                    var newGameObject = (GameObject)GameDataAttribute.ReadTypeAttributes(xmlReader);
                                    newGameObject.LoadGameObjectData(xmlReader);

                                    fieldOfGameObject.SetValue(this, newGameObject);
                                }
                                else
                                {
                                    object objectReadByAttribute = singleGameDataAttribute.ReadField(xmlReader);
                                    fieldOfGameObject.SetValue(this, objectReadByAttribute);
                                }

                                break;
                            }
                        }
                    }
                }
                else if (xmlReader.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }
            }
        }