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(); }
public void Load() { houses.Clear(); SaveFile saveFile = new SaveFile("data/houses"); saveFile.Load(); Dictionary<string, Node> nodes = saveFile.Nodes; if (nodes.ContainsKey("houses")) { Node houses2 = nodes["houses"]; foreach (KeyValuePair<string, Node> house in houses2.Nodes) { int x = int.Parse(house.Value.Nodes["x"].Value); int y = int.Parse(house.Value.Nodes["y"].Value); string type = house.Value.Nodes["type"].Value; House newhouse = new House(houseTypes[type], house.Key, x, y, houseTypes[type].Width, houseTypes[type].Height); if (house.Value.Nodes.ContainsKey("furniture")) // <furniture> { Node furniture = house.Value.Nodes["furniture"]; foreach (KeyValuePair<string, Node> furnitures in furniture.Nodes) { // <x|y> int furniturex = int.Parse(furnitures.Value.Nodes["x"].Value); int furniturey = int.Parse(furnitures.Value.Nodes["y"].Value); string furnituretype = furnitures.Value.Nodes["type"].Value; Furniture newFurniture = FurnitureManager.FurnitureTypes[furnituretype].FromNode(furnitures.Value); newhouse.Furniture.Add(new BlockPos(0, furniturex, furniturey), newFurniture); } } this.houses.Add(newhouse.builder, newhouse); DrawHouse(newhouse); } } }
public SaveFile Save(SaveFile saveFile) { //For each item in inventory foreach (KeyValuePair<string, Pair<IInventoryItem, int>> data in storedItems) { saveFile.AddNode(new NodePath("inventory." + data.Value.first.Name + ".amount"), new Node(data.Value.second.ToString())); //For each data entry in item foreach (KeyValuePair<string, object> entry in data.Value.first.GetData()) { saveFile.AddNode(new NodePath("inventory." + data.Value.first.Name + ".data." + entry.Key), new Node(entry.Value.ToString())); } } return saveFile; }
public void Load(SaveFile file) { Dictionary<string, Node> nodes = file.Nodes; if (nodes.ContainsKey("inventory")) { Node inventory = nodes["inventory"]; //For each item in inventory foreach (KeyValuePair<string, Node> item in inventory.Nodes) { IInventoryItem inventoryItem = new InventoryItem(item.Key); int amount = int.Parse(item.Value.Nodes["amount"].Value); //For each data entry in item foreach (KeyValuePair<string, Node> data in item.Value.Nodes) { if (data.Key.Equals("amount")) continue; inventoryItem.SetData(data.Key, data.Value.Value); } AddItem(inventoryItem, amount); } } }
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();*/ } }
public void Load() { lock (saveloadLockObject) { if (!Directory.Exists(@"data\)")) Directory.CreateDirectory(@"data\"); string dataPath = @"data\" + player.Name; if (!File.Exists(dataPath)) return; SaveFile saveFile = new SaveFile(dataPath); saveFile.Load(); if (saveFile.Nodes.ContainsKey("playerdata")) { Node playerData = saveFile.Nodes["playerdata"]; if (playerData.Nodes.ContainsKey("digxp")) xp = float.Parse(playerData.GetNode("digxp").Value); if (playerData.Nodes.ContainsKey("digmoney")) digMoney_ = int.Parse(playerData.GetNode("digmoney").Value); } inventory.Load(saveFile); /*digLevel_ = 0; IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); digXp = (int)formatter.Deserialize(stream); digMoney_ = (int)formatter.Deserialize(stream); stream.Close(); xpRequired = getXpRequired(digLevel);*/ } }