/// <summary> /// Load a Character from a file and return it (thread-safe). /// </summary> /// <param name="strFileName">File to load.</param> /// <param name="blnIncludeInMRU">Whether or not the file should appear in the MRU list.</param> /// <param name="strNewName">New name for the character.</param> /// <param name="blnClearFileName">Whether or not the name of the save file should be cleared.</param> public Character LoadCharacter(string strFileName, string strNewName = "", bool blnClearFileName = false, bool blnShowErrors = true) { Character objCharacter = null; if (File.Exists(strFileName) && strFileName.EndsWith("chum5")) { Timekeeper.Start("loading"); bool blnLoaded = false; objCharacter = new Character { FileName = strFileName }; XmlDocument objXmlDocument = new XmlDocument(); //StreamReader is used to prevent encoding errors using (StreamReader sr = new StreamReader(strFileName, true)) { try { objXmlDocument.Load(sr); } catch (XmlException ex) { if (blnShowErrors) { MessageBox.Show(LanguageManager.GetString("Message_FailedLoad").Replace("{0}", ex.Message), LanguageManager.GetString("MessageTitle_FailedLoad"), MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); } } XmlNode objXmlCharacter = objXmlDocument.SelectSingleNode("/character"); if (!string.IsNullOrEmpty(objXmlCharacter?["appversion"]?.InnerText)) { string strVersion = objXmlCharacter["appversion"].InnerText; if (strVersion.StartsWith("0.")) { strVersion = strVersion.Substring(2); } Version.TryParse(strVersion, out Version verSavedVersion); Version.TryParse("5.188.34", out Version verCorrectedVersion); if (verCorrectedVersion != null && verSavedVersion != null) { int intResult = verSavedVersion.CompareTo(verCorrectedVersion); //Check for typo in Corrupter quality and correct it if (intResult == -1) { File.WriteAllText(strFileName, Regex.Replace(File.ReadAllText(strFileName), "Corruptor", "Corrupter")); } } } OpenCharacters.Add(objCharacter); Timekeeper.Start("load_file"); blnLoaded = objCharacter.Load(); Timekeeper.Finish("load_file"); if (!blnLoaded) { objCharacter.Dispose(); OpenCharacters.Remove(objCharacter); return(null); } // If a new name is given, set the character's name to match (used in cloning). if (!string.IsNullOrEmpty(strNewName)) { objCharacter.Name = strNewName; } // Clear the File Name field so that this does not accidentally overwrite the original save file (used in cloning). if (blnClearFileName) { objCharacter.FileName = string.Empty; } } else if (blnShowErrors) { MessageBox.Show(LanguageManager.GetString("Message_FileNotFound").Replace("{0}", strFileName), LanguageManager.GetString("MessageTitle_FileNotFound"), MessageBoxButtons.OK, MessageBoxIcon.Error); } return(objCharacter); }
/// <summary> /// Create a new character and show the Create Form. /// </summary> private void ShowNewForm(object sender, EventArgs e) { string strFilePath = Path.Combine(Application.StartupPath, "settings", "default.xml"); if (!File.Exists(strFilePath)) { if (MessageBox.Show(LanguageManager.Instance.GetString("Message_CharacterOptions_OpenOptions"), LanguageManager.Instance.GetString("MessageTitle_CharacterOptions_OpenOptions"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { frmOptions frmOptions = new frmOptions(); frmOptions.ShowDialog(); } } Character objCharacter = new Character(); string settingsPath = Path.Combine(Application.StartupPath, "settings"); string[] settingsFiles = Directory.GetFiles(settingsPath, "*.xml"); if (settingsFiles.Length > 1) { frmSelectSetting frmPickSetting = new frmSelectSetting(); frmPickSetting.ShowDialog(this); if (frmPickSetting.DialogResult == DialogResult.Cancel) { return; } objCharacter.SettingsFile = frmPickSetting.SettingsFile; } else { string strSettingsFile = settingsFiles[0]; objCharacter.SettingsFile = Path.GetFileName(strSettingsFile); } // Show the BP selection window. frmSelectBuildMethod frmBP = new frmSelectBuildMethod(objCharacter); frmBP.ShowDialog(); if (frmBP.DialogResult == DialogResult.Cancel) { return; } if (objCharacter.BuildMethod == CharacterBuildMethod.Karma || objCharacter.BuildMethod == CharacterBuildMethod.LifeModule) { frmKarmaMetatype frmSelectMetatype = new frmKarmaMetatype(objCharacter); frmSelectMetatype.ShowDialog(); if (frmSelectMetatype.DialogResult == DialogResult.Cancel) { return; } } // Show the Metatype selection window. else if (objCharacter.BuildMethod == CharacterBuildMethod.Priority || objCharacter.BuildMethod == CharacterBuildMethod.SumtoTen) { frmPriorityMetatype frmSelectMetatype = new frmPriorityMetatype(objCharacter); frmSelectMetatype.ShowDialog(); if (frmSelectMetatype.DialogResult == DialogResult.Cancel) { return; } } // Add the Unarmed Attack Weapon to the character. try { XmlDocument objXmlDocument = XmlManager.Instance.Load("weapons.xml"); XmlNode objXmlWeapon = objXmlDocument.SelectSingleNode("/chummer/weapons/weapon[name = \"Unarmed Attack\"]"); TreeNode objDummy = new TreeNode(); Weapon objWeapon = new Weapon(objCharacter); objWeapon.Create(objXmlWeapon, objCharacter, objDummy, null, null); objCharacter.Weapons.Add(objWeapon); } catch { } frmCreate frmNewCharacter = new frmCreate(objCharacter); frmNewCharacter.MdiParent = this; frmNewCharacter.WindowState = FormWindowState.Maximized; frmNewCharacter.Show(); OpenCharacters.Add(objCharacter); objCharacter.CharacterNameChanged += objCharacter_CharacterNameChanged; }