2 using System.Collections.Generic;
3 using System.Collections.Immutable;
14 public readonly Identifier
Value;
18 public override bool Equals(
object obj)
21 return base.Equals(obj);
32 public static class LanguageIdentifierExtensions
39 public static LanguageIdentifier ToLanguageIdentifier(
this string str)
41 return str.ToIdentifier().ToLanguageIdentifier();
52 public readonly record
struct Text(
57 public readonly ImmutableDictionary<Identifier, ImmutableArray<Text>>
Texts;
66 if (languageName.IsEmpty)
68 DebugConsole.AddWarning($
"Language not defined in text file \"{file.Path}\". Setting the language as {TextManager.DefaultLanguage}.",
70 languageName = TextManager.DefaultLanguage.Value;
76 Dictionary<Identifier, List<Text>> texts =
new Dictionary<Identifier, List<Text>>();
77 LoadElements(mainElement, isOverride: mainElement.
IsOverride());
79 void LoadElements(XElement parentElement,
bool isOverride)
81 foreach (var element
in parentElement.Elements())
83 Identifier elemName = element.NameAsIdentifier();
85 if (element.IsOverride())
87 LoadElements(element, isOverride:
true);
91 if (!texts.ContainsKey(elemName)) { texts.Add(elemName,
new List<Text>()); }
93 string str = element.ElementInnerText()
95 .Replace(
"&",
"&")
98 .Replace(
""",
"\"")
99 .Replace(
"'",
"'");
101 texts[elemName].Add(
new Text(str, isOverride,
this));
106 Texts = texts.Select(kvp => (kvp.Key, kvp.Value.ToImmutableArray())).ToImmutableDictionary();
110 public void CheckForDuplicates(
int index)
112 Dictionary<Identifier, int> tagCounts =
new Dictionary<Identifier, int>();
113 Dictionary<string, int> contentCounts =
new Dictionary<string, int>();
116 if (doc ==
null) {
return; }
118 foreach (var subElement
in doc.Root.Elements())
120 Identifier infoName = subElement.NameAsIdentifier();
121 if (!tagCounts.ContainsKey(infoName))
123 tagCounts.Add(infoName, 1);
127 tagCounts[infoName] += 1;
130 string infoContent = subElement.Value;
131 if (
string.IsNullOrEmpty(infoContent)) {
continue; }
132 if (!contentCounts.ContainsKey(infoContent))
134 contentCounts.Add(infoContent, 1);
138 contentCounts[infoContent] += 1;
142 StringBuilder sb =
new StringBuilder();
145 sb.Append(
"Duplicate tags:");
149 List<string> lines =
new List<string>();
150 for (
int i = 0; i < tagCounts.Keys.Count; i++)
152 if (tagCounts[
Texts.Keys.ElementAt(i)] > 1)
154 lines.Add(
Texts.Keys.ElementAt(i) +
" | Count: " + tagCounts[
Texts.Keys.ElementAt(i)]);
157 foreach (
string line
in lines.OrderBy(l => l))
163 sb.Append(
"Duplicate content:");
167 for (
int i = 0; i < contentCounts.Keys.Count; i++)
169 if (contentCounts[contentCounts.Keys.ElementAt(i)] > 1)
171 sb.Append(contentCounts.Keys.ElementAt(i) +
" | Count: " + contentCounts[contentCounts.Keys.ElementAt(i)]);
176 Barotrauma.
IO.
File.WriteAllText($
"duplicate_{Language.ToString().ToLower()}_{index}.txt", sb.ToString());
179 public void WriteToCSV(
int index)
181 StringBuilder sb =
new StringBuilder();
184 if (doc ==
null) {
return; }
186 List<(
string key,
string value)> texts =
new List<(
string key,
string value)>();
188 foreach (var element
in doc.Root.Elements())
190 string text = element.ElementInnerText()
191 .Replace(
"&",
"&")
192 .Replace(
"<",
"<")
193 .Replace(
">",
">")
194 .Replace(
""",
"\"")
195 .Replace(
"'",
"'");
197 texts.Add((element.Name.ToString(), text));
200 foreach ((
string key,
string value) in texts)
213 Barotrauma.
IO.
File.WriteAllText($
"csv_{Language.ToString().ToLower()}_{index}.csv", sb.ToString());
Base class for content file types, which are loaded from filelist.xml via reflection....
readonly ContentPath Path
string? GetAttributeString(string key, string? def)
ContentPackage? ContentPackage
bool GetAttributeBool(string key, bool def)
Identifier GetAttributeIdentifier(string key, string def)
readonly record struct Text(string String, bool IsOverride, TextPack TextPack)
readonly TextFile ContentFile
readonly ImmutableDictionary< Identifier, ImmutableArray< Text > > Texts
readonly bool NoWhitespace
readonly LanguageIdentifier Language
readonly string TranslatedName
TextPack(TextFile file, ContentXElement mainElement, LanguageIdentifier language)
static bool operator!=(LanguageIdentifier a, LanguageIdentifier b)
LanguageIdentifier(Identifier value)
override int GetHashCode()
override bool Equals(object obj)
override string ToString()
static bool operator==(LanguageIdentifier a, LanguageIdentifier b)
static readonly LanguageIdentifier None
readonly Identifier Value