private void OpenGame(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Open); var sr = new StreamReader(fs); var xml = XElement.Parse(sr.ReadToEnd()); sr.Close(); var mvm = new MainViewModel(); mvm.Location = fileName; //player.DataContext = mvm; mvm.CurrentGame = Game.FromXml(xml); Editor.App.Current.Resources["MainViewModelStatic"] = mvm; foreach (var room in mvm.CurrentGame.Rooms) { if (room.Value.RoomBase.StartingRoom) { mvm.CurrentGame.CurrentRoom = room.Value; } } if (mvm.CurrentGame.CurrentRoom == null) { MessageBox.Show("Error:\n\nNo starting room was found. Using the editor, please \nselect one room from this game to be the starting room."); } else { var player = new MainPlayer(); mvm.OutputCurrentRoomDescription(); player.Show(); } this.Close(); }
public static MainViewModel FromXML(XElement saveXml, string fileLocation) { string location = saveXml.Element("Location").Value; string gameLocation = Editor.MainViewModel.AbsolutePath(fileLocation, location); FileStream fs = new FileStream(gameLocation, FileMode.Open); var sr = new StreamReader(fs); var loadXml = XElement.Parse(sr.ReadToEnd()); sr.Close(); var mvm = new MainViewModel(); mvm.Location = gameLocation; //player.DataContext = mvm; mvm.CurrentGame = Game.FromXml(loadXml); var currentRoom = Guid.Parse(saveXml.Element("CurrentRoom").Value); Editor.App.Current.Resources["MainViewModelStatic"] = mvm; foreach (var room in mvm.CurrentGame.Rooms) { if (room.Value.RoomBase.RoomID == currentRoom) { mvm.CurrentGame.CurrentRoom = room.Value; } } mvm.CurrentGame.ActiveEvents = (from a in saveXml.Element("Events").Elements("Event") select ActiveEvent.FromXML(a)).ToList(); mvm.CurrentGame.PlayerInventory = new ObservableCollection <ItemInstance>(from a in saveXml.Element("Inventory").Elements() select ItemInstance.FromXML(a, mvm.CurrentGame)); foreach (var a in saveXml.Element("Equipment").Elements()) { mvm.CurrentGame.TryEquipItem(ItemInstance.FromXML(a, mvm.CurrentGame), true); } foreach (var a in saveXml.Element("Variables").Elements()) { var oldVar = mvm.CurrentGame.VarById[Guid.Parse(a.Element("ID").Value)]; var newVar = VariableWrapper.FromXML(a.Element("Variable"), mvm.CurrentGame, mvm.CurrentGame.VarById[Guid.Parse(a.Element("ID").Value)].VariableBase); oldVar.CurrentCommonEventValue = newVar.CurrentCommonEventValue; oldVar.CurrentItemValue = newVar.CurrentItemValue; oldVar.CurrentNumberValue = newVar.CurrentNumberValue; oldVar.CurrentStringValue = newVar.CurrentStringValue; } foreach (var a in saveXml.Element("Arrays").Elements()) { var arr = mvm.CurrentGame.ArraysById[Guid.Parse(a.Element("ID").Value)]; foreach (var b in a.Element("Values").Elements()) { switch (b.Element("Type").Value) { case "Number": arr.Add(Convert.ToInt32(b.Element("Value").Value)); break; case "String": arr.Add(b.Element("Value").Value); break; case "Item": arr.Add(ItemInstance.FromXML(b.Element("Value").Element("Item"), mvm.CurrentGame)); break; } } } mvm.CurrentGame.ActiveStatusEffects = (from a in saveXml.Element("StatusEffects").Elements("ActiveStatusEffect") select StatusEffectWrapper.FromXML(a, mvm.CurrentGame)).ToList(); mvm.CurrentGame.RefreshAll(); return(mvm); }