using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Xml.Linq; using Barotrauma.IO; namespace Barotrauma { class TextPack { public readonly string Language; /// /// The name of the language in the language this pack is written in /// public readonly string TranslatedName; private readonly Dictionary> texts; public readonly string FilePath; public TextPack(string filePath) { this.FilePath = filePath; texts = new Dictionary>(); XDocument doc = null; for (int i = 0; i < 3; i++) { doc = XMLExtensions.TryLoadXml(filePath); if (doc != null) { break; } if (filePath.Equals("content/texts/englishvanilla.xml", StringComparison.OrdinalIgnoreCase)) { //try fixing legacy EnglishVanilla path string newPath = "Content/Texts/English/EnglishVanilla.xml"; if (Barotrauma.IO.File.Exists(newPath)) { DebugConsole.NewMessage("Content package is using the obsolete text file path \"" + filePath + "\". Attempting to load from \"" + newPath + "\"..."); this.FilePath = filePath = newPath; } } Thread.Sleep(1000); } if (doc == null) { Language = "Unknown"; return; } Language = doc.Root.GetAttributeString("language", "Unknown"); TranslatedName = doc.Root.GetAttributeString("translatedname", Language); foreach (XElement subElement in doc.Root.Elements()) { string infoName = subElement.Name.ToString().ToLowerInvariant(); if (!texts.TryGetValue(infoName, out List infoList)) { infoList = new List(); texts.Add(infoName, infoList); } string text = subElement.ElementInnerText(); text = text.Replace("&", "&"); text = text.Replace("<", "<"); text = text.Replace(">", ">"); text = text.Replace(""", "\""); infoList.Add(text); } } public string Get(string textTag) { if (string.IsNullOrEmpty(textTag)) { return null; } if (!texts.TryGetValue(textTag.ToLowerInvariant(), out List textList) || !textList.Any()) { return null; } string text = textList[Rand.Int(textList.Count)].Replace(@"\n", "\n"); return text; } public List GetAll(string textTag) { if (textTag is null) { return null; } if (!texts.TryGetValue(textTag.ToLowerInvariant(), out List textList) || !textList.Any()) { return null; } return textList; } public List> GetAllTagTextPairs() { var pairs = new List>(); foreach (KeyValuePair> kvp in texts) { foreach (string line in kvp.Value) { pairs.Add(new KeyValuePair(kvp.Key, line)); } } return pairs; } #if DEBUG public void CheckForDuplicates(int index) { Dictionary tagCounts = new Dictionary(); Dictionary contentCounts = new Dictionary(); XDocument doc = XMLExtensions.TryLoadXml(FilePath); if (doc == null) { return; } foreach (XElement subElement in doc.Root.Elements()) { string infoName = subElement.Name.ToString().ToLowerInvariant(); if (!tagCounts.ContainsKey(infoName)) { tagCounts.Add(infoName, 1); } else { tagCounts[infoName] += 1; } string infoContent = subElement.Value; if (string.IsNullOrEmpty(infoContent)) continue; if (!contentCounts.ContainsKey(infoContent)) { contentCounts.Add(infoContent, 1); } else { contentCounts[infoContent] += 1; } } StringBuilder sb = new StringBuilder(); sb.Append("Language: " + Language); sb.AppendLine(); sb.Append("Duplicate tags:"); sb.AppendLine(); sb.AppendLine(); for (int i = 0; i < tagCounts.Keys.Count; i++) { if (tagCounts[texts.Keys.ElementAt(i)] > 1) { sb.Append(texts.Keys.ElementAt(i) + " | Count: " + tagCounts[texts.Keys.ElementAt(i)]); sb.AppendLine(); } } sb.AppendLine(); sb.AppendLine(); sb.Append("Duplicate content:"); sb.AppendLine(); sb.AppendLine(); for (int i = 0; i < contentCounts.Keys.Count; i++) { if (contentCounts[contentCounts.Keys.ElementAt(i)] > 1) { sb.Append(contentCounts.Keys.ElementAt(i) + " | Count: " + contentCounts[contentCounts.Keys.ElementAt(i)]); sb.AppendLine(); } } File.WriteAllText(@"duplicate_" + Language.ToLower() + "_" + index + ".txt", sb.ToString()); } public void WriteToCSV(int index) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < texts.Count; i++) { string key = texts.Keys.ElementAt(i); texts.TryGetValue(key, out List infoList); for (int j = 0; j < infoList.Count; j++) { sb.Append(key); // ID sb.Append('*'); sb.Append(infoList[j]); // Original sb.Append('*'); // Translated sb.Append('*'); // Comments sb.AppendLine(); } } File.WriteAllText(@"csv_" + Language.ToLower() + "_" + index + ".csv", sb.ToString()); } #endif } }