Exemple #1
0
        private Node LoadNode(XmlReader reader, Node node = null)
        {
            if (reader == null) {
                throw new ArgumentNullException();
            }

            if (node == null) {
                if (!reader.HasAttributes) {
                    throw new ArgumentException();
                }

                string name = null;
                Node.DataType type = Node.DataType.Empty;
                NodeData data = new NodeData();

                while (reader.MoveToNextAttribute()) {
                    switch (reader.Name) {
                        case "name":
                            name = reader.Value;
                            break;

                        case "type":
                            byte btype = 0;
                            if (!byte.TryParse(reader.Value, out btype)) {
                                switch (reader.Value.ToLower()) {
                                    case "int":
                                    case "long":
                                    case "integer":
                                        btype = 1;
                                        break;

                                    case "float":
                                    case "double":
                                    case "decimal":
                                        btype = 2;
                                        break;

                                    case "str":
                                    case "text":
                                    case "string":
                                        btype = 3;
                                        break;

                                    case "vec":
                                    case "point":
                                    case "position":
                                    case "pos":
                                    case "location":
                                    case "loc":
                                    case "coord":
                                    case "coordinate":
                                    case "vector":
                                        btype = 4;
                                        break;

                                    case "png":
                                    case "img":
                                    case "image":
                                    case "canvas":
                                    case "bitmap":
                                        btype = 5;
                                        break;

                                    case "mp3":
                                    case "music":
                                    case "sound":
                                    case "track":
                                    case "audio":
                                        btype = 6;
                                        break;

                                    case "none":
                                    case "empty":
                                    default:
                                        btype = 0;
                                        break;
                                }
                            }

                            type = (Node.DataType)btype;
                            break;

                        case "value":
                            switch (type) {
                                case Node.DataType.Integer:
                                    data.Integer = long.Parse(reader.Value);
                                    break;

                                case Node.DataType.Decimal:
                                    if (!long.TryParse(reader.Value, out data.Integer)) {
                                        data.Decimal = double.Parse(reader.Value);
                                    }
                                    break;

                                case Node.DataType.String:
                                    if (!long.TryParse(reader.Value, out data.Integer)) {
                                        data.String = uint.Parse(reader.Value);
                                    }
                                    break;

                                case Node.DataType.Vector:
                                    if (!long.TryParse(reader.Value, out data.Integer)) {
                                        data.x = int.Parse(reader.Value.Split(',')[0]);
                                        data.y = int.Parse(reader.Value.Split(',')[1]);
                                    }
                                    break;

                                case Node.DataType.Bitmap:
                                    if (!long.TryParse(reader.Value, out data.Integer)) {
                                        data.id = uint.Parse(reader.Value.Split(',')[0]);
                                        data.width = ushort.Parse(reader.Value.Split(',')[1]);
                                        data.height = ushort.Parse(reader.Value.Split(',')[2]);
                                    }
                                    break;

                                case Node.DataType.Audio:
                                    if (!long.TryParse(reader.Value, out data.Integer)) {
                                        data.id = uint.Parse(reader.Value.Split(',')[0]);
                                        data.length = uint.Parse(reader.Value.Split(',')[1]);
                                    }
                                    break;
                            }
                            break;
                    }
                }

                reader.MoveToElement();

                return new Node(name, type, data);
            } else {
                if (reader.IsEmptyElement) {
                    return null;
                }

                while (reader.Read()) {
                    switch (reader.NodeType) {
                        case XmlNodeType.Element:
                            Node child = LoadNode(reader, null);
                            node[child.Name] = child;
                            LoadNode(reader, child);
                            break;
                        case XmlNodeType.EndElement:
                            return null;
                        default:
                            break;
                    }
                }
            }

            return null;
        }
Exemple #2
0
        private void SaveNode(XmlWriter writer, Node node)
        {
            writer.WriteStartElement("node");

            writer.WriteAttributeString("name", node.Name);

            if (node.Type != Node.DataType.Empty) {
                if (node != Root) {
                    writer.WriteAttributeString("type", ((byte)node.Type).ToString());
                }

                if (!secret) {
                    switch (node.Type) {
                        case Node.DataType.Integer:
                            writer.WriteAttributeString("value", node.Data.Integer.ToString());
                            break;
                        case Node.DataType.Decimal:
                            writer.WriteAttributeString("value", node.Data.Decimal.ToString());
                            break;
                        case Node.DataType.String:
                            writer.WriteAttributeString("value", node.Data.String.ToString());
                            break;
                        case Node.DataType.Vector:
                            writer.WriteAttributeString("value", node.Data.x + "," + node.Data.y);
                            break;
                        case Node.DataType.Bitmap:
                            writer.WriteAttributeString("value", node.Data.id + "," + node.Data.width + "," + node.Data.height);
                            break;
                        case Node.DataType.Audio:
                            writer.WriteAttributeString("value", node.Data.id + "," + node.Data.length);
                            break;

                        default:
                            break;
                    }
                } else {
                    writer.WriteAttributeString("value", node.Data.Integer.ToString());
                }
            }

            foreach (Node n in node.Children.Values) {
                SaveNode(writer, n);
            }

            writer.WriteEndElement();
        }