private void treCharacterList_DoubleClick(object sender, EventArgs e) { CharacterCache objCache = lstCharacterCache[Convert.ToInt32(treCharacterList.SelectedNode.Tag)]; GlobalOptions.Instance.MainForm.LoadCharacter(objCache.FilePath); }
/// <summary> /// Update the labels and images based on the selected treenode. /// </summary> /// <param name="objCache"></param> private void UpdateCharacter(CharacterCache objCache) { if (objCache != null) { string strUnknown = LanguageManager.GetString("String_Unknown", GlobalOptions.Language); string strNone = LanguageManager.GetString("String_None", GlobalOptions.Language); txtCharacterBio.Text = objCache.Description; txtCharacterBackground.Text = objCache.Background; txtCharacterNotes.Text = objCache.CharacterNotes; txtGameNotes.Text = objCache.GameNotes; txtCharacterConcept.Text = objCache.Concept; lblCareerKarma.Text = objCache.Karma; if (string.IsNullOrEmpty(lblCareerKarma.Text) || lblCareerKarma.Text == "0") { lblCareerKarma.Text = strNone; } lblPlayerName.Text = objCache.PlayerName; if (string.IsNullOrEmpty(lblPlayerName.Text)) { lblPlayerName.Text = strUnknown; } lblCharacterName.Text = objCache.CharacterName; if (string.IsNullOrEmpty(lblCharacterName.Text)) { lblCharacterName.Text = strUnknown; } lblCharacterAlias.Text = objCache.CharacterAlias; if (string.IsNullOrEmpty(lblCharacterAlias.Text)) { lblCharacterAlias.Text = strUnknown; } lblEssence.Text = objCache.Essence; if (string.IsNullOrEmpty(lblEssence.Text)) { lblEssence.Text = strUnknown; } lblFilePath.Text = objCache.FileName; if (string.IsNullOrEmpty(lblFilePath.Text)) { lblFilePath.Text = LanguageManager.GetString("MessageTitle_FileNotFound", GlobalOptions.Language); } lblSettings.Text = objCache.SettingsFile; if (string.IsNullOrEmpty(lblSettings.Text)) { lblSettings.Text = strUnknown; } lblFilePath.SetToolTip(objCache.FilePath.CheapReplace(Application.StartupPath, () => '<' + Application.ProductName + '>')); picMugshot.Image = objCache.Mugshot; // Populate character information fields. XmlDocument objMetatypeDoc = XmlManager.Load("metatypes.xml"); XmlNode objMetatypeNode = objMetatypeDoc.SelectSingleNode("/chummer/metatypes/metatype[name = " + objCache.Metatype.CleanXPath() + "]"); if (objMetatypeNode == null) { objMetatypeDoc = XmlManager.Load("critters.xml"); objMetatypeNode = objMetatypeDoc.SelectSingleNode("/chummer/metatypes/metatype[name = " + objCache.Metatype.CleanXPath() + "]"); } string strMetatype = objMetatypeNode?["translate"]?.InnerText ?? objCache.Metatype; if (!string.IsNullOrEmpty(objCache.Metavariant) && objCache.Metavariant != "None") { objMetatypeNode = objMetatypeNode?.SelectSingleNode("metavariants/metavariant[name = " + objCache.Metavariant.CleanXPath() + "]"); strMetatype += LanguageManager.GetString("String_Space", GlobalOptions.Language) + '(' + (objMetatypeNode?["translate"]?.InnerText ?? objCache.Metavariant) + ')'; } lblMetatype.Text = strMetatype; tabCharacterText.Visible = true; } else { tabCharacterText.Visible = false; txtCharacterBio.Text = string.Empty; txtCharacterBackground.Text = string.Empty; txtCharacterNotes.Text = string.Empty; txtGameNotes.Text = string.Empty; txtCharacterConcept.Text = string.Empty; lblCareerKarma.Text = string.Empty; lblMetatype.Text = string.Empty; lblPlayerName.Text = string.Empty; lblCharacterName.Text = string.Empty; lblCharacterAlias.Text = string.Empty; lblEssence.Text = string.Empty; lblFilePath.Text = string.Empty; lblFilePath.SetToolTip(string.Empty); lblSettings.Text = string.Empty; picMugshot.Image = null; } lblCareerKarmaLabel.Visible = !string.IsNullOrEmpty(lblCareerKarma.Text); lblMetatypeLabel.Visible = !string.IsNullOrEmpty(lblMetatype.Text); lblPlayerNameLabel.Visible = !string.IsNullOrEmpty(lblPlayerName.Text); lblCharacterNameLabel.Visible = !string.IsNullOrEmpty(lblCharacterName.Text); lblCharacterAliasLabel.Visible = !string.IsNullOrEmpty(lblCharacterAlias.Text); lblEssenceLabel.Visible = !string.IsNullOrEmpty(lblEssence.Text); lblFilePathLabel.Visible = !string.IsNullOrEmpty(lblFilePath.Text); lblSettingsLabel.Visible = !string.IsNullOrEmpty(lblSettings.Text); ProcessMugshotSizeMode(); }
private void treCharacterList_AfterSelect(object sender, TreeViewEventArgs e) { CharacterCache objCache = lstCharacterCache[Convert.ToInt32(treCharacterList.SelectedNode.Tag)]; UpdateCharacter(objCache); }
/// <summary> /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character. /// </summary> /// <param name="strFile"></param> /// <param name="objParentNode"></param> private TreeNode CacheCharacter(string strFile) { if (!File.Exists(strFile)) { return(null); } XmlDocument objXmlSource = new XmlDocument(); // If we run into any problems loading the character cache, fail out early. try { using (StreamReader sr = new StreamReader(strFile, true)) { objXmlSource.Load(sr); } } catch (IOException) { return(null); } catch (XmlException) { return(null); } CharacterCache objCache = new CharacterCache(); XmlNode objXmlSourceNode = objXmlSource.SelectSingleNode("/character"); if (objXmlSourceNode != null) { objCache.Description = objXmlSourceNode["description"]?.InnerText; objCache.BuildMethod = objXmlSourceNode["buildmethod"]?.InnerText; objCache.Background = objXmlSourceNode["background"]?.InnerText; objCache.CharacterNotes = objXmlSourceNode["notes"]?.InnerText; objCache.GameNotes = objXmlSourceNode["gamenotes"]?.InnerText; objCache.Concept = objXmlSourceNode["concept"]?.InnerText; objCache.Karma = objXmlSourceNode["totalkarma"]?.InnerText; objCache.Metatype = objXmlSourceNode["metatype"]?.InnerText; objCache.PlayerName = objXmlSourceNode["playername"]?.InnerText; objCache.CharacterName = objXmlSourceNode["name"]?.InnerText; objCache.CharacterAlias = objXmlSourceNode["alias"]?.InnerText; objCache.Created = objXmlSourceNode["created"]?.InnerText == System.Boolean.TrueString; objCache.Essence = objXmlSourceNode["totaless"]?.InnerText; var s = objXmlSourceNode["settings"]?.InnerText; objCache.SettingsFile = !File.Exists(Path.Combine(Application.StartupPath, "settings", s)) ? LanguageManager.GetString("MessageTitle_FileNotFound", GlobalOptions.Language) : s; string strMugshotBase64 = objXmlSourceNode["mugshot"]?.InnerText; if (!string.IsNullOrEmpty(strMugshotBase64)) { objCache.Mugshot = strMugshotBase64.ToImage(); } else { int intMainMugshotIndex = -1; objXmlSourceNode.TryGetInt32FieldQuickly("mainmugshotindex", ref intMainMugshotIndex); XmlNodeList objXmlMugshotsList = objXmlSourceNode.SelectNodes("mugshots/mugshot"); if (intMainMugshotIndex >= 0 && intMainMugshotIndex < objXmlMugshotsList.Count) { objCache.Mugshot = objXmlMugshotsList[intMainMugshotIndex].InnerText?.ToImage(); } else { objCache.Mugshot = null; } } } objCache.FilePath = strFile; objCache.FileName = strFile.Substring(strFile.LastIndexOf(Path.DirectorySeparatorChar) + 1); TreeNode objNode = new TreeNode { ContextMenuStrip = cmsRoster, Text = CalculatedName(objCache), ToolTipText = objCache.FilePath.CheapReplace(Application.StartupPath, () => "<" + Application.ProductName + ">") }; lock (_lstCharacterCacheLock) { _lstCharacterCache.Add(objCache); objNode.Tag = _lstCharacterCache.IndexOf(objCache); } return(objNode); }
/// <summary> /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character. /// </summary> /// <param name="strFile"></param> private TreeNode CacheCharacter(string strFile) { CharacterCache objCache = new CharacterCache(); string strErrorText = string.Empty; XPathNavigator xmlSourceNode; if (!File.Exists(strFile)) { xmlSourceNode = null; strErrorText = LanguageManager.GetString("MessageTitle_FileNotFound", GlobalOptions.Language); } else { // If we run into any problems loading the character cache, fail out early. try { using (StreamReader objStreamReader = new StreamReader(strFile, Encoding.UTF8, true)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(objStreamReader); xmlSourceNode = xmlDoc.CreateNavigator().SelectSingleNode("/character"); } } catch (Exception ex) { xmlSourceNode = null; strErrorText = ex.ToString(); } } if (xmlSourceNode != null) { objCache.Description = xmlSourceNode.SelectSingleNode("description")?.Value; objCache.BuildMethod = xmlSourceNode.SelectSingleNode("buildmethod")?.Value; objCache.Background = xmlSourceNode.SelectSingleNode("background")?.Value; objCache.CharacterNotes = xmlSourceNode.SelectSingleNode("notes")?.Value; objCache.GameNotes = xmlSourceNode.SelectSingleNode("gamenotes")?.Value; objCache.Concept = xmlSourceNode.SelectSingleNode("concept")?.Value; objCache.Karma = xmlSourceNode.SelectSingleNode("totalkarma")?.Value; objCache.Metatype = xmlSourceNode.SelectSingleNode("metatype")?.Value; objCache.Metavariant = xmlSourceNode.SelectSingleNode("metavariant")?.Value; objCache.PlayerName = xmlSourceNode.SelectSingleNode("playername")?.Value; objCache.CharacterName = xmlSourceNode.SelectSingleNode("name")?.Value; objCache.CharacterAlias = xmlSourceNode.SelectSingleNode("alias")?.Value; objCache.Created = xmlSourceNode.SelectSingleNode("created")?.Value == bool.TrueString; objCache.Essence = xmlSourceNode.SelectSingleNode("totaless")?.Value; string strSettings = xmlSourceNode.SelectSingleNode("settings")?.Value ?? string.Empty; objCache.SettingsFile = !File.Exists(Path.Combine(Application.StartupPath, "settings", strSettings)) ? LanguageManager.GetString("MessageTitle_FileNotFound", GlobalOptions.Language) : strSettings; string strMugshotBase64 = xmlSourceNode.SelectSingleNode("mugshot")?.Value; if (!string.IsNullOrEmpty(strMugshotBase64)) { objCache.Mugshot = strMugshotBase64.ToImage(); } else { XPathNavigator xmlMainMugshotIndex = xmlSourceNode.SelectSingleNode("mainmugshotindex"); if (xmlMainMugshotIndex != null && int.TryParse(xmlMainMugshotIndex.Value, out int intMainMugshotIndex) && intMainMugshotIndex >= 0) { XPathNodeIterator xmlMugshotList = xmlSourceNode.Select("mugshots/mugshot"); if (xmlMugshotList.Count > intMainMugshotIndex) { int intIndex = 0; foreach (XPathNavigator xmlMugshot in xmlMugshotList) { if (intMainMugshotIndex == intIndex) { objCache.Mugshot = xmlMugshot.Value.ToImage(); break; } intIndex += 1; } } } } } else { objCache.ErrorText = strErrorText; } objCache.FilePath = strFile; objCache.FileName = strFile.Substring(strFile.LastIndexOf(Path.DirectorySeparatorChar) + 1); if (!_lstCharacterCache.TryAdd(strFile, objCache)) { _lstCharacterCache[strFile] = objCache; } TreeNode objNode = new TreeNode { ContextMenuStrip = cmsRoster, Text = CalculatedName(objCache), ToolTipText = objCache.FilePath.CheapReplace(Application.StartupPath, () => '<' + Application.ProductName + '>'), Tag = strFile }; if (!string.IsNullOrEmpty(objCache.ErrorText)) { objNode.ForeColor = Color.Red; objNode.ToolTipText += Environment.NewLine + Environment.NewLine + LanguageManager.GetString("String_Error", GlobalOptions.Language) + LanguageManager.GetString("String_Colon", GlobalOptions.Language) + Environment.NewLine + objCache.ErrorText; } return(objNode); }
public CharacterCache(CharacterCache objExistingCache) : this() { CopyFrom(objExistingCache); }
/// <summary> /// Syntactic sugar to call LoadFromFile() asynchronously immediately after the constructor. /// </summary> /// <param name="strFile"></param> public static Task <CharacterCache> CreateFromFileAsync(string strFile) { CharacterCache objReturn = new CharacterCache(); return(objReturn.LoadFromFileAsync(strFile).ContinueWith(x => objReturn)); }
/// <summary> /// Update the labels and images based on the selected treenode. /// </summary> /// <param name="objCache"></param> public void UpdateCharacter(CharacterCache objCache) { if (IsDisposed) // Safety check for external calls { return; } if (objCache != null) { string strUnknown = LanguageManager.GetString("String_Unknown"); string strNone = LanguageManager.GetString("String_None"); if (objCache.Description.IsRtf()) { rtbCharacterBio.Rtf = objCache.Description; } else { rtbCharacterBio.Text = objCache.Description; } if (objCache.Background.IsRtf()) { rtbCharacterBackground.Rtf = objCache.Background; } else { rtbCharacterBackground.Text = objCache.Background; } if (objCache.CharacterNotes.IsRtf()) { rtbCharacterNotes.Rtf = objCache.CharacterNotes; } else { rtbCharacterNotes.Text = objCache.CharacterNotes; } if (objCache.GameNotes.IsRtf()) { rtbGameNotes.Rtf = objCache.GameNotes; } else { rtbGameNotes.Text = objCache.GameNotes; } if (objCache.Concept.IsRtf()) { rtbCharacterConcept.Rtf = objCache.Concept; } else { rtbCharacterConcept.Text = objCache.Concept; } lblCareerKarma.Text = objCache.Karma; if (string.IsNullOrEmpty(lblCareerKarma.Text) || lblCareerKarma.Text == 0.ToString(GlobalOptions.CultureInfo)) { lblCareerKarma.Text = strNone; } lblPlayerName.Text = objCache.PlayerName; if (string.IsNullOrEmpty(lblPlayerName.Text)) { lblPlayerName.Text = strUnknown; } lblCharacterName.Text = objCache.CharacterName; if (string.IsNullOrEmpty(lblCharacterName.Text)) { lblCharacterName.Text = strUnknown; } lblCharacterAlias.Text = objCache.CharacterAlias; if (string.IsNullOrEmpty(lblCharacterAlias.Text)) { lblCharacterAlias.Text = strUnknown; } lblEssence.Text = objCache.Essence; if (string.IsNullOrEmpty(lblEssence.Text)) { lblEssence.Text = strUnknown; } lblFilePath.Text = objCache.FileName; if (string.IsNullOrEmpty(lblFilePath.Text)) { lblFilePath.Text = LanguageManager.GetString("MessageTitle_FileNotFound"); } lblSettings.Text = objCache.SettingsFile; if (string.IsNullOrEmpty(lblSettings.Text)) { lblSettings.Text = strUnknown; } lblFilePath.SetToolTip(objCache.FilePath.CheapReplace(Utils.GetStartupPath, () => '<' + Application.ProductName + '>')); picMugshot.Image?.Dispose(); picMugshot.Image = objCache.Mugshot; // Populate character information fields. XmlDocument objMetatypeDoc = XmlManager.Load("metatypes.xml"); if (objCache.Metatype != null) { XmlNode objMetatypeNode = objMetatypeDoc.SelectSingleNode("/chummer/metatypes/metatype[name = " + objCache.Metatype?.CleanXPath() + "]"); if (objMetatypeNode == null) { objMetatypeDoc = XmlManager.Load("critters.xml"); objMetatypeNode = objMetatypeDoc.SelectSingleNode("/chummer/metatypes/metatype[name = " + objCache.Metatype?.CleanXPath() + "]"); } string strMetatype = objMetatypeNode?["translate"]?.InnerText ?? objCache.Metatype; if (!string.IsNullOrEmpty(objCache.Metavariant) && objCache.Metavariant != "None") { objMetatypeNode = objMetatypeNode?.SelectSingleNode("metavariants/metavariant[name = " + objCache.Metavariant.CleanXPath() + "]"); strMetatype += LanguageManager.GetString("String_Space") + '(' + (objMetatypeNode?["translate"]?.InnerText ?? objCache.Metavariant) + ')'; } lblMetatype.Text = strMetatype; } else { lblMetatype.Text = LanguageManager.GetString("String_MetatypeLoadError"); } tabCharacterText.Visible = true; if (!string.IsNullOrEmpty(objCache.ErrorText)) { rtbCharacterBio.Text = objCache.ErrorText; rtbCharacterBio.ForeColor = Color.Red; rtbCharacterBio.BringToFront(); } else { rtbCharacterBio.ForeColor = SystemColors.WindowText; } } else { tabCharacterText.Visible = false; rtbCharacterBio.Clear(); rtbCharacterBackground.Clear(); rtbCharacterNotes.Clear(); rtbGameNotes.Clear(); rtbCharacterConcept.Clear(); lblCareerKarma.Text = string.Empty; lblMetatype.Text = string.Empty; lblPlayerName.Text = string.Empty; lblCharacterName.Text = string.Empty; lblCharacterAlias.Text = string.Empty; lblEssence.Text = string.Empty; lblFilePath.Text = string.Empty; lblFilePath.SetToolTip(string.Empty); lblSettings.Text = string.Empty; picMugshot.Image = null; } lblCareerKarmaLabel.Visible = !string.IsNullOrEmpty(lblCareerKarma.Text); lblMetatypeLabel.Visible = !string.IsNullOrEmpty(lblMetatype.Text); lblPlayerNameLabel.Visible = !string.IsNullOrEmpty(lblPlayerName.Text); lblCharacterNameLabel.Visible = !string.IsNullOrEmpty(lblCharacterName.Text); lblCharacterAliasLabel.Visible = !string.IsNullOrEmpty(lblCharacterAlias.Text); lblEssenceLabel.Visible = !string.IsNullOrEmpty(lblEssence.Text); lblFilePathLabel.Visible = !string.IsNullOrEmpty(lblFilePath.Text); lblSettingsLabel.Visible = !string.IsNullOrEmpty(lblSettings.Text); ProcessMugshotSizeMode(); }
private void treCharacterList_DragDrop(object sender, DragEventArgs e) { // Do not allow the root element to be moved. if (treCharacterList.SelectedNode == null || treCharacterList.SelectedNode.Level == 0 || treCharacterList.SelectedNode.Parent.Tag.ToString() == "Watch") { return; } if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { TreeView treSenderView = sender as TreeView; if (treSenderView == null) { return; } Point pt = treSenderView.PointToClient(new Point(e.X, e.Y)); TreeNode nodDestinationNode = treSenderView.GetNodeAt(pt); if (nodDestinationNode.Level > 0) { nodDestinationNode = nodDestinationNode.Parent; } if (nodDestinationNode.Tag.ToString() != "Watch") { TreeNode nodNewNode = e.Data.GetData("System.Windows.Forms.TreeNode") as TreeNode; int intCharacterIndex; CharacterCache objCache = null; if (nodNewNode == null) { return; } if (int.TryParse(nodNewNode.Tag.ToString(), out intCharacterIndex) && intCharacterIndex >= 0 && intCharacterIndex < _lstCharacterCache.Count) { objCache = _lstCharacterCache[intCharacterIndex]; } if (objCache == null) { return; } switch (nodDestinationNode.Tag.ToString()) { case "Recent": GlobalOptions.RemoveFromMRUList(objCache.FilePath, "stickymru"); GlobalOptions.AddToMRUList(objCache.FilePath); break; case "Favourite": GlobalOptions.RemoveFromMRUList(objCache.FilePath); GlobalOptions.AddToMRUList(objCache.FilePath, "stickymru"); break; default: return; } TreeNode nodClonedNewNode = nodNewNode.Clone() as TreeNode; if (nodClonedNewNode != null) { nodDestinationNode.Nodes.Add(nodClonedNewNode); nodDestinationNode.Expand(); } nodNewNode.Remove(); } } }
private void LoadCharacters() { List <string> lstFavorites = GlobalOptions.ReadMRUList("stickymru"); TreeNode objFavouriteNode = null; if (lstFavorites.Count > 0) { objFavouriteNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_FavouriteCharacters")) { Tag = "Favourite" }; } List <string> lstRecents = GlobalOptions.ReadMRUList(); TreeNode objRecentNode = null; if (lstRecents.Count > 0) { objRecentNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_RecentCharacters")) { Tag = "Recent" }; } List <string> lstWatch = new List <string>(); TreeNode objWatchNode = null; if (!string.IsNullOrEmpty(GlobalOptions.CharacterRosterPath) && Directory.Exists(GlobalOptions.CharacterRosterPath)) { string[] objFiles = Directory.GetFiles(GlobalOptions.CharacterRosterPath); //Make sure we're not loading a character that was already loaded by the MRU list. if (objFiles.Length > 0) { foreach (string strFile in objFiles.Where(strFile => strFile.EndsWith(".chum5"))) { CharacterCache objCachedCharacter = _lstCharacterCache.FirstOrDefault(x => x.FilePath == strFile); if (objCachedCharacter != null) { foreach (TreeNode rootNode in treCharacterList.Nodes) { foreach (TreeNode objChildNode in rootNode.Nodes) { if (Convert.ToInt32(objChildNode.Tag) == _lstCharacterCache.IndexOf(objCachedCharacter)) { goto CharacterAlreadyLoaded; } } } } lstWatch.Add(strFile); CharacterAlreadyLoaded :; } } } if (lstWatch.Count > 0) { objWatchNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_WatchFolder")) { Tag = "Watch" }; } TreeNode[] lstFavoritesNodes = new TreeNode[lstFavorites.Count]; object lstFavoritesNodesLock = new object(); TreeNode[] lstRecentsNodes = new TreeNode[lstRecents.Count]; object lstRecentsNodesLock = new object(); TreeNode[] lstWatchNodes = new TreeNode[lstWatch.Count]; object lstWatchNodesLock = new object(); Parallel.Invoke( () => { if (objFavouriteNode != null) { Parallel.For(0, lstFavorites.Count, i => { string strFile = lstFavorites[i]; TreeNode objNode = CacheCharacter(strFile); lock (lstFavoritesNodesLock) lstFavoritesNodes[i] = objNode; }); } for (int i = 0; i < lstFavoritesNodes.Length; i++) { TreeNode objNode = lstFavoritesNodes[i]; if (objNode != null) { objFavouriteNode.Nodes.Add(objNode); } } }, () => { if (objRecentNode != null) { Parallel.For(0, lstRecents.Count, i => { string strFile = lstRecents[i]; TreeNode objNode = CacheCharacter(strFile); lock (lstRecentsNodesLock) lstRecentsNodes[i] = objNode; }); } for (int i = 0; i < lstRecentsNodes.Length; i++) { TreeNode objNode = lstRecentsNodes[i]; if (objNode != null) { objRecentNode.Nodes.Add(objNode); } } }, () => { if (objWatchNode != null) { Parallel.For(0, lstWatch.Count, i => { string strFile = lstWatch[i]; TreeNode objNode = CacheCharacter(strFile); lock (lstWatchNodesLock) lstWatchNodes[i] = objNode; }); } for (int i = 0; i < lstWatchNodes.Length; i++) { TreeNode objNode = lstWatchNodes[i]; if (objNode != null) { objWatchNode.Nodes.Add(objNode); } } }); if (objFavouriteNode != null) { treCharacterList.Nodes.Add(objFavouriteNode); } if (objRecentNode != null) { treCharacterList.Nodes.Add(objRecentNode); } if (objWatchNode != null) { treCharacterList.Nodes.Add(objWatchNode); } treCharacterList.ExpandAll(); }
/// <summary> /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character. /// </summary> /// <param name="strFile"></param> /// <param name="objParentNode"></param> private TreeNode CacheCharacter(string strFile) { if (!File.Exists(strFile)) { return(null); } XmlDocument objXmlSource = new XmlDocument(); // If we run into any problems loading the character cache, fail out early. try { using (StreamReader sr = new StreamReader(strFile, true)) { objXmlSource.Load(sr); } } catch (IOException) { return(null); } catch (XmlException) { return(null); } CharacterCache objCache = new CharacterCache(); XmlNode objXmlSourceNode = objXmlSource.SelectSingleNode("/character"); if (objXmlSourceNode != null) { objCache.Description = objXmlSourceNode["description"]?.InnerText; objCache.BuildMethod = objXmlSourceNode["buildmethod"]?.InnerText; objCache.Background = objXmlSourceNode["background"]?.InnerText; objCache.CharacterNotes = objXmlSourceNode["notes"]?.InnerText; objCache.GameNotes = objXmlSourceNode["gamenotes"]?.InnerText; objCache.Concept = objXmlSourceNode["concept"]?.InnerText; objCache.Karma = objXmlSourceNode["totalkarma"]?.InnerText; objCache.Metatype = objXmlSourceNode["metatype"]?.InnerText; objCache.PlayerName = objXmlSourceNode["player"]?.InnerText; objCache.CharacterName = objXmlSourceNode["name"]?.InnerText; objCache.CharacterAlias = objXmlSourceNode["alias"]?.InnerText; objCache.Created = Convert.ToBoolean(objXmlSourceNode["created"]?.InnerText); objCache.Essence = objXmlSourceNode["totaless"]?.InnerText; string strMugshotBase64 = objXmlSourceNode["mugshot"]?.InnerText; if (!string.IsNullOrEmpty(strMugshotBase64)) { objCache.Mugshot = strMugshotBase64.ToImage(); } else { int intMainMugshotIndex = 0; objXmlSourceNode.TryGetInt32FieldQuickly("mainmugshotindex", ref intMainMugshotIndex); XmlNodeList objXmlMugshotsList = objXmlSourceNode.SelectNodes("mugshots/mugshot"); int intMugshotCount = objXmlMugshotsList.Count; if (intMainMugshotIndex >= intMugshotCount) { intMainMugshotIndex = 0; } else if (intMainMugshotIndex < 0) { intMainMugshotIndex = intMugshotCount - 1; } if (intMugshotCount > 0 && intMainMugshotIndex < intMugshotCount) { objCache.Mugshot = objXmlMugshotsList[intMainMugshotIndex].InnerText?.ToImage(); } else { objCache.Mugshot = null; } } } objCache.FilePath = strFile; objCache.FileName = strFile.Substring(strFile.LastIndexOf('\\') + 1); TreeNode objNode = new TreeNode(); objNode.Text = CalculatedName(objCache); objNode.ToolTipText = objCache.FilePath; lock (_lstCharacterCacheLock) { _lstCharacterCache.Add(objCache); objNode.Tag = _lstCharacterCache.IndexOf(objCache); } return(objNode); }
/// <summary> /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character. /// </summary> /// <param name="strFile"></param> /// <param name="objParentNode"></param> private void CacheCharacter(string strFile, TreeNode objParentNode) { if (!File.Exists(strFile)) { return; } XmlDocument objXmlSource = new XmlDocument(); // If we run into any problems loading the character cache, fail out early. try { using (StreamReader sr = new StreamReader(strFile, true)) { objXmlSource.Load(sr); } } catch (IOException) { return; } catch (XmlException) { return; } CharacterCache objCache = new CharacterCache(); XmlNode objXmlSourceNode = objXmlSource.SelectSingleNode("/character"); if (objXmlSourceNode != null) { objCache.Description = objXmlSourceNode["description"]?.InnerText; objCache.BuildMethod = objXmlSourceNode["buildmethod"]?.InnerText; objCache.Background = objXmlSourceNode["background"]?.InnerText; objCache.CharacterNotes = objXmlSourceNode["notes"]?.InnerText; objCache.GameNotes = objXmlSourceNode["gamenotes"]?.InnerText; objCache.Concept = objXmlSourceNode["concept"]?.InnerText; objCache.Karma = objXmlSourceNode["totalkarma"]?.InnerText; objCache.Metatype = objXmlSourceNode["metatype"]?.InnerText; objCache.PlayerName = objXmlSourceNode["player"]?.InnerText; objCache.CharacterName = objXmlSourceNode["name"]?.InnerText; objCache.CharacterAlias = objXmlSourceNode["alias"]?.InnerText; objCache.Created = Convert.ToBoolean(objXmlSourceNode["created"]?.InnerText); objCache.Essence = objXmlSourceNode["totaless"]?.InnerText; if (!string.IsNullOrEmpty(objXmlSourceNode["mugshot"]?.InnerText)) { byte[] bytImage = Convert.FromBase64String(objXmlSourceNode["mugshot"]?.InnerText); MemoryStream objStream = new MemoryStream(bytImage, 0, bytImage.Length); objStream.Write(bytImage, 0, bytImage.Length); Image imgMugshot = Image.FromStream(objStream, true); objCache.Mugshot = imgMugshot; } else { int intMainMugshotIndex = 0; objXmlSourceNode.TryGetInt32FieldQuickly("mainmugshotindex", ref intMainMugshotIndex); XmlNodeList objXmlMugshotsList = objXmlSourceNode.SelectNodes("mugshots/mugshot"); List <string> lstMugshots = (from XmlNode objXmlMugshot in objXmlMugshotsList where !string.IsNullOrWhiteSpace(objXmlMugshot.InnerText) select objXmlMugshot.InnerText).ToList(); if (intMainMugshotIndex >= lstMugshots.Count) { intMainMugshotIndex = 0; } else if (intMainMugshotIndex < 0) { if (lstMugshots.Count > 0) { intMainMugshotIndex = lstMugshots.Count - 1; } else { intMainMugshotIndex = 0; } } if (lstMugshots.Count > 0) { byte[] bytImage = Convert.FromBase64String(lstMugshots.ElementAt(intMainMugshotIndex)); MemoryStream objStream = new MemoryStream(bytImage, 0, bytImage.Length); objStream.Write(bytImage, 0, bytImage.Length); Image imgMugshot = Image.FromStream(objStream, true); objCache.Mugshot = imgMugshot; } else { objCache.Mugshot = null; } } } objCache.FilePath = strFile; objCache.FileName = strFile.Split('\\').ToArray().Last(); lstCharacterCache.Add(objCache); TreeNode objNode = new TreeNode(); objNode.Tag = lstCharacterCache.IndexOf(objCache); objNode.Text = CalculatedName(objCache); objNode.ToolTipText = objCache.FilePath; objParentNode.Nodes.Add(objNode); }