public static void SaveChatCodes() { IEnumerable <XElement> xElements = AppViewModel.Instance.XChatCodes.Descendants().Elements("Code"); XElement[] enumerable = xElements as XElement[] ?? xElements.ToArray(); foreach (ChatCode chatCode in AppViewModel.Instance.ChatCodes) { XElement element = enumerable.FirstOrDefault(e => e.Attribute("Key")?.Value == chatCode.Code); string xKey = chatCode.Code; string xColor = chatCode.Color; string xDescription = chatCode.Description; List <KeyValuePair <string, string> > keyValuePairs = new List <KeyValuePair <string, string> >(); keyValuePairs.Add(new KeyValuePair <string, string>(xKey, xColor)); keyValuePairs.Add(new KeyValuePair <string, string>(xKey, xDescription)); if (element is null) { XMLHelper.SaveXMLNode(AppViewModel.Instance.XChatCodes, "Codes", "Code", xKey, keyValuePairs); } else { XElement xColorElement = element.Element("Color"); if (xColorElement != null) { xColorElement.Value = xColor; } else { element.Add(new XElement("Color", xColor)); } XElement xDescriptionElement = element.Element("Description"); if (xDescriptionElement != null) { xDescriptionElement.Value = xDescription; } else { element.Add(new XElement("Description", xDescription)); } } } AppViewModel.Instance.XChatCodes.Save(Path.Combine(AppViewModel.Instance.ConfigurationsPath, "ChatCodes.xml")); }
public static void SaveCurrentLog() { if (AppViewModel.Instance.ChatHistory.Any()) { try { // clear current builders foreach ((string _, StringBuilder builder) in _textLogBuilders) { builder.Clear(); } // setup full xml log file XDocument xChatHistory = ResourceHelper.LoadXML($"{Constants.AppPack}Resources/ChatHistory.xml"); foreach ((string playerName, List <ChatLogItem> chatLogItems) in AppViewModel.Instance.ChatHistory) { foreach (ChatLogItem chatLogItem in chatLogItems) { // process text logging try { if (_textLogBuilders.ContainsKey(chatLogItem.Code)) { string prefix = $"[{playerName}]"; if (Constants.ChatLS.Contains(chatLogItem.Code)) { prefix = $"{prefix}[LS{Array.IndexOf(Constants.ChatCWLS, chatLogItem.Code) + 1}] "; } if (Constants.ChatCWLS.Contains(chatLogItem.Code)) { prefix = $"{prefix}[CWLS{Array.IndexOf(Constants.ChatCWLS, chatLogItem.Code) + 1}] "; } _textLogBuilders[chatLogItem.Code].AppendLine($"{prefix} {chatLogItem.TimeStamp} {chatLogItem.Line}"); } } catch (Exception ex) { Logging.Log(Logger, new LogItem(ex)); } // process xml log try { string xTimeStamp = chatLogItem.TimeStamp.ToString("[HH:mm:ss]"); string xCode = chatLogItem.Code; string xBytes = chatLogItem.Bytes.Aggregate(string.Empty, (current, bytes) => current + bytes + " ").Trim(); string xLine = chatLogItem.Line; List <KeyValuePair <string, string> > keyPairList = new List <KeyValuePair <string, string> >(); keyPairList.Add(new KeyValuePair <string, string>("PlayerCharacterName", playerName)); keyPairList.Add(new KeyValuePair <string, string>("Bytes", xBytes)); keyPairList.Add(new KeyValuePair <string, string>("Line", xLine)); keyPairList.Add(new KeyValuePair <string, string>("TimeStamp", xTimeStamp)); XMLHelper.SaveXMLNode(xChatHistory, "History", "Entry", xCode, keyPairList); } catch (Exception ex) { Logging.Log(Logger, new LogItem(ex)); } } } // save text logs try { string textLogName = $"{DateTime.Now:yyyy_MM_dd_HH.mm.ss}.txt"; foreach ((string key, StringBuilder builder) in _textLogBuilders) { if (Constants.ChatSay.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "Say", textLogName), builder.ToString()); } if (Constants.ChatShout.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "Shout", textLogName), builder.ToString()); } if (Constants.ChatParty.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "Party", textLogName), builder.ToString()); } if (Constants.ChatTell.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "Tell", textLogName), builder.ToString()); } if (Constants.ChatLS.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "LS", textLogName), builder.ToString()); } if (Constants.ChatCWLS.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "CWLS", textLogName), builder.ToString()); } if (Constants.ChatFC.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "FC", textLogName), builder.ToString()); } if (Constants.ChatYell.Contains(key) && builder.Length > 0) { File.WriteAllText(Path.Combine(AppViewModel.Instance.LogsPath, "Yell", textLogName), builder.ToString()); } } } catch (Exception ex) { Logging.Log(Logger, new LogItem(ex)); } // save xml log try { xChatHistory.Save(Path.Combine(AppViewModel.Instance.LogsPath, $"{DateTime.Now:yyyy_MM_dd_HH.mm.ss}_ChatHistory.xml")); } catch (Exception ex) { Logging.Log(Logger, new LogItem(ex)); } } catch (Exception ex) { Logging.Log(Logger, new LogItem(ex)); } } }