protected void AddNodeRecursively(NodePath pathToGoal, Node node, NodeContainer mother)
 {
     mother = (mother != null ? mother : this);
     string key = pathToGoal.Dequeue();
     if (pathToGoal.Path.Count > 0)
     {
         if (!mother.Nodes.ContainsKey(key))
             mother.Nodes.Add(key, new Node(key));
         Node newNode = mother.Nodes[key];
         AddNodeRecursively(pathToGoal, node, newNode);
         return;
     }
     if (!mother.Nodes.ContainsKey(key))
         mother.Nodes.Add(key, node);
     else
         mother.Nodes[key] = node;
 }
Beispiel #2
0
        protected Node LoadNode(StreamReader sr, string nodeName)
        {
            Node current = new Node(nodeName);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                line = line.Replace("\t", "");
                if (line.Length > 2 && line[0] == '<' && line[1] != '/')
                {
                    string currentNodeName = line.Substring(1, line.Length - 2);
                    Node node = LoadNode(sr, currentNodeName);
                    current.AddNode(currentNodeName, node);                }
                else if (line.Length > 2 && line.Substring(0, 2).Equals("</"))
                    return current;
                else
                {
                    //Actual data entries
                    string key = "";
                    string value = "";

                    for (int i = 0; i < line.Length; i++)
                    {
                        if (line[i].Equals(':'))
                        {
                            key = line.Substring(0, i);
                            value = line.Substring(i+2);
                            break;
                        }
                    }
                    current.AddNode(key, new Node(value));
                }
            }
            if (sr.ReadLine() == null)
                return current;
            throw new Exception("File \"" + _path + "\" is in incorrect format.");
        }
 public override Furniture FromNode(Node node)
 {
     return new FurnitureSwitch(int.Parse(node.Nodes["x"].Value), int.Parse(node.Nodes["y"].Value));
 }
 public void AddNode(string key, Node node)
 {
     AddNodeRecursively(new NodePath(key), node, null);
 }
 public void AddNode(NodePath path, Node node)
 {
     AddNodeRecursively(path, node, null);
 }
        public void Save()
        {
            SaveFile saveFile = new SaveFile("data/houses");
            Node houses = new Node("houses");
            foreach (KeyValuePair<string, House> house in this.houses)
            {
                Node playername = new Node(house.Key);
                playername.AddNode("x", new Node(house.Value.x.ToString()));
                playername.AddNode("y", new Node(house.Value.y.ToString()));
                playername.AddNode("type", new Node(house.Value.houseType.Name));

                if (house.Value.Furniture.Count > 0)
                {
                    Node furniture = new Node("furniture");
                    foreach (KeyValuePair<BlockPos, Furniture> f in house.Value.Furniture)
                    {
                        Node furnitureKey = new Node(f.Key.X + "|" + f.Key.Y);
                        furnitureKey.AddNode("x", new Node(f.Key.X.ToString()));
                        furnitureKey.AddNode("y", new Node(f.Key.Y.ToString()));
                        furnitureKey.AddNode("type", new Node(f.Value.Type));
                        furniture.AddNode(f.Key.X + "|" + f.Key.Y, furnitureKey);
                    }
                    playername.AddNode("furniture", furniture);
                }
                houses.AddNode(house.Key, playername);
            }
            saveFile.AddNode("houses", houses);
            saveFile.Save();
        }
Beispiel #7
0
        protected void SaveNode(string key, Node node, int currentlevel = 0)
        {
            if (node.HasNodes || node.Value.Equals(key))
            {
                for (int i = 0; i < currentlevel; i++)
                    _streamWriter.Write("\t"); //add tabs so it looks better!

                _streamWriter.WriteLine("<" + key + ">");
                foreach (KeyValuePair<string, Node> temp in node.Nodes)
                {
                    //for (int i = 0; i < currentlevel; i++)
                    //  _streamWriter.Write("\t"); //add tabs so it looks better!

                    SaveNode(temp.Key, temp.Value, currentlevel + 1);
                }
                for (int i = 0; i < currentlevel; i++)
                    _streamWriter.Write("\t"); //add tabs so it looks better!

                _streamWriter.WriteLine("</" + key + ">");
            }
            else
            {
                for (int i = 0; i < currentlevel; i++)
                    _streamWriter.Write("\t"); //add tabs so it looks better!
                _streamWriter.WriteLine(key + ": " + node.Value);
            }
        }
Beispiel #8
0
        public void Save()
        {
            lock (saveloadLockObject)
            {
                if (!Directory.Exists(@"data\)"))
                    Directory.CreateDirectory(@"data\");

                string dataPath = @"data\" + player.Name;
                SaveFile saveFile = new SaveFile(dataPath);
                inventory.Save(saveFile);

                Node playerData = new Node("playerdata");
                playerData.AddNode("digxp", new Node(xp.ToString()));
                playerData.AddNode("digmoney", new Node(digMoney_.ToString()));
                saveFile.AddNode("playerdata", playerData);

                saveFile.Save();

                /*IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(dataPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                formatter.Serialize(stream, digXp);
                formatter.Serialize(stream, digMoney);
                stream.Close();*/
            }
        }
Beispiel #9
0
 public abstract Furniture FromNode(Node node);