public static Game Loadgame(String filename) { Game loadGame = new Game(); while (true) { try { FileStream fs = new FileStream("../../" + filename, FileMode.Open); BinaryFormatter binaryForm = new BinaryFormatter(); SavedGame ToLoadGame = (SavedGame)binaryForm.Deserialize(fs); loadGame.SetupRoom(); foreach (Room room in ToLoadGame.Setup) { if (room.Current) { Console.WriteLine(room.Description);//TODO:remove print statement loadGame.currentRoom = TransferRoom(room); } else if (room.Name == "Kitchen") { loadGame.kitchen = TransferRoom(room); } else if (room.Name == "Outside") { loadGame.outside = TransferRoom(room); } else if (room.Name == "Painting Room") { loadGame.paintingRoom = TransferRoom(room); } else if (room.Name == "Library") { loadGame.library = TransferRoom(room); } else if (room.Name == "Laboratory") { loadGame.laboratory = TransferRoom(room); } else if (room.Name == "Living Room") { loadGame.livingRoom = TransferRoom(room); } else if (room.Name == "Skeleton Room") { loadGame.skeletonRoom = TransferRoom(room); } else if (room.Name == "Fancy Bedroom") { loadGame.fancyBedroom = TransferRoom(room); } else if (room.Name == "Cellar") { loadGame.cellar = TransferRoom(room); } else if (room.Name == "Entrance Hall") { loadGame.entranceHall = TransferRoom(room); } } loadGame.bagpack = ToLoadGame.UserBagpack; loadGame.enemy = ToLoadGame.enemy; fs.Close(); return(loadGame); } catch (Exception e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); Console.WriteLine("Enter the correct File Name:"); filename = Console.ReadLine() + ".bin"; } } }
public void SaveGame() { currentRoom.Current = true; SavedGame save = new SavedGame(); save.enemy = enemy; save.UserBagpack = bagpack; save.Setup = new List <Room>(); save.Setup.Add(outside); save.Setup.Add(entranceHall); save.Setup.Add(livingRoom); save.Setup.Add(paintingRoom); save.Setup.Add(kitchen); save.Setup.Add(fancyBedroom); save.Setup.Add(cellar); save.Setup.Add(library); save.Setup.Add(laboratory); save.Setup.Add(skeletonRoom); save.Setup.Add(currentRoom); Console.WriteLine("File Name: "); String FileName = "../../" + Console.ReadLine() + ".bin"; Stream SaveFileStream = File.Create(FileName); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(SaveFileStream, save); SaveFileStream.Close(); /*System.Xml.Serialization.XmlSerializer writer = * new System.Xml.Serialization.XmlSerializer(typeof(SavedGame)); * * var path = "../../bagpack.xml"; * System.IO.FileStream File = System.IO.File.Create(path); * * writer.Serialize(File, save); * * titleReader.Serialize(File, "Bagpack"); * foreach (Item item in bagpack.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, entranceHall.Name); * foreach (Item item in entranceHall.Items) * { * writer.Serialize(File, item); * } * * titleReader.Serialize(File, outside.Name); * foreach (Item item in outside.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, livingRoom.Name); * foreach (Item item in livingRoom.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, paintingRoom.Name); * foreach (Item item in paintingRoom.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, kitchen.Name); * foreach (Item item in kitchen.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, fancyBedroom.Name); * foreach (Item item in fancyBedroom.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, cellar.Name); * foreach (Item item in cellar.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, library.Name); * foreach (Item item in library.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, laboratory.Name); * foreach (Item item in laboratory.Items) * writer.Serialize(File, item); * * titleReader.Serialize(File, skeletonRoom.Name); * foreach (Item item in skeletonRoom.Items) * writer.Serialize(File, item); * */ }