Unstable 1.2.4.0
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Barotrauma.Extensions;
|
||||
|
||||
namespace Barotrauma
|
||||
@@ -32,7 +33,7 @@ namespace Barotrauma
|
||||
|
||||
(string value, bool loaded) tryLoad(LanguageIdentifier lang)
|
||||
{
|
||||
IReadOnlyList<string> candidates = Array.Empty<string>();
|
||||
IReadOnlyList<TextPack.Text> candidates = Array.Empty<TextPack.Text>();
|
||||
int tagIndex = 0;
|
||||
|
||||
if (TextManager.TextPacks.TryGetValue(lang, out var packs))
|
||||
@@ -50,8 +51,17 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
bool loaded = candidates.Count > 0;
|
||||
return (loaded ? candidates.GetRandomUnsynced() : "", loaded);
|
||||
if (candidates.Count == 0) { return (string.Empty, loaded: false); }
|
||||
var firstOverride = candidates.FirstOrDefault(c => c.IsOverride);
|
||||
if (firstOverride != default)
|
||||
{
|
||||
//if there's overrides defined, choose from the first pack that defines overrides
|
||||
return (candidates.Where(static c => c.IsOverride).Where(c => c.TextPack == firstOverride.TextPack).GetRandomUnsynced().String, loaded: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (candidates.GetRandomUnsynced().String, loaded: true);
|
||||
}
|
||||
}
|
||||
|
||||
var (value, loaded) = tryLoad(Language);
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Barotrauma
|
||||
{
|
||||
protected bool loaded = false;
|
||||
protected LanguageIdentifier language = LanguageIdentifier.None;
|
||||
private int languageVersion = 0;
|
||||
|
||||
protected string cachedSanitizedValue = "";
|
||||
public string SanitizedValue
|
||||
@@ -32,9 +33,9 @@ namespace Barotrauma
|
||||
#if CLIENT
|
||||
private readonly GUIFont? font;
|
||||
private readonly GUIComponentStyle? componentStyle;
|
||||
private readonly bool forceUpperCase = false;
|
||||
private bool forceUpperCase = false;
|
||||
|
||||
private bool fontOrStyleForceUpperCase
|
||||
private bool FontOrStyleForceUpperCase
|
||||
=> font is { ForceUpperCase: true } || componentStyle is { ForceUpperCase: true };
|
||||
#endif
|
||||
|
||||
@@ -91,8 +92,9 @@ namespace Barotrauma
|
||||
{
|
||||
return NestedStr.Loaded != loaded
|
||||
|| language != GameSettings.CurrentConfig.Language
|
||||
|| languageVersion != TextManager.LanguageVersion
|
||||
#if CLIENT
|
||||
|| (fontOrStyleForceUpperCase != forceUpperCase)
|
||||
|| (FontOrStyleForceUpperCase != forceUpperCase)
|
||||
#endif
|
||||
;
|
||||
}
|
||||
@@ -100,9 +102,9 @@ namespace Barotrauma
|
||||
public void RetrieveValue()
|
||||
{
|
||||
#if CLIENT
|
||||
NestedStr = fontOrStyleForceUpperCase ? originalStr.ToUpper() : originalStr;
|
||||
NestedStr = FontOrStyleForceUpperCase ? originalStr.ToUpper() : originalStr;
|
||||
forceUpperCase = FontOrStyleForceUpperCase;
|
||||
#endif
|
||||
|
||||
if (shouldParseRichTextData)
|
||||
{
|
||||
RichTextData = Barotrauma.RichTextData.GetRichTextData(NestedStr.Value, out cachedSanitizedValue);
|
||||
@@ -113,6 +115,7 @@ namespace Barotrauma
|
||||
}
|
||||
if (postProcess != null) { cachedSanitizedValue = postProcess(cachedSanitizedValue); }
|
||||
language = GameSettings.CurrentConfig.Language;
|
||||
languageVersion = TextManager.LanguageVersion;
|
||||
loaded = NestedStr.Loaded;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Barotrauma
|
||||
public static bool DebugDraw;
|
||||
|
||||
public readonly static LanguageIdentifier DefaultLanguage = "English".ToLanguageIdentifier();
|
||||
public readonly static ConcurrentDictionary<LanguageIdentifier, ImmutableHashSet<TextPack>> TextPacks = new ConcurrentDictionary<LanguageIdentifier, ImmutableHashSet<TextPack>>();
|
||||
public readonly static ConcurrentDictionary<LanguageIdentifier, ImmutableList<TextPack>> TextPacks = new ConcurrentDictionary<LanguageIdentifier, ImmutableList<TextPack>>();
|
||||
public static IEnumerable<LanguageIdentifier> AvailableLanguages => TextPacks.Keys;
|
||||
|
||||
private readonly static Dictionary<Identifier, WeakReference<TagLString>> cachedStrings =
|
||||
@@ -160,22 +160,48 @@ namespace Barotrauma
|
||||
return TextPacks[GameSettings.CurrentConfig.Language].Any(p => p.Texts.ContainsKey(tag));
|
||||
}
|
||||
|
||||
public static bool ContainsTag(Identifier tag, LanguageIdentifier language)
|
||||
{
|
||||
return TextPacks[language].Any(p => p.Texts.ContainsKey(tag));
|
||||
}
|
||||
public static IEnumerable<string> GetAll(string tag)
|
||||
=> GetAll(tag.ToIdentifier());
|
||||
|
||||
public static IEnumerable<string> GetAll(Identifier tag)
|
||||
{
|
||||
return TextPacks[GameSettings.CurrentConfig.Language]
|
||||
var allTexts = TextPacks[GameSettings.CurrentConfig.Language]
|
||||
.SelectMany(p => p.Texts.TryGetValue(tag, out var value)
|
||||
? (IEnumerable<string>)value
|
||||
: Array.Empty<string>());
|
||||
? (IEnumerable<TextPack.Text>)value
|
||||
: Array.Empty<TextPack.Text>());
|
||||
|
||||
var firstOverride = allTexts.FirstOrDefault(t => t.IsOverride);
|
||||
if (firstOverride != default)
|
||||
{
|
||||
return allTexts.Where(t => t.IsOverride && t.TextPack == firstOverride.TextPack).Select(t => t.String);
|
||||
}
|
||||
else
|
||||
{
|
||||
return allTexts.Select(t => t.String);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<KeyValuePair<Identifier, string>> GetAllTagTextPairs()
|
||||
{
|
||||
return TextPacks[GameSettings.CurrentConfig.Language]
|
||||
.SelectMany(p => p.Texts)
|
||||
.SelectMany(kvp => kvp.Value.Select(v => new KeyValuePair<Identifier, string>(kvp.Key, v)));
|
||||
var allTexts = TextPacks[GameSettings.CurrentConfig.Language]
|
||||
.SelectMany(p => p.Texts);
|
||||
|
||||
var firstOverride = allTexts.SelectMany(kvp => kvp.Value).FirstOrDefault(t => t.IsOverride);
|
||||
if (firstOverride != default)
|
||||
{
|
||||
return allTexts
|
||||
.Where(kvp => kvp.Value.Any(t => t.IsOverride && t.TextPack == firstOverride.TextPack))
|
||||
.SelectMany(kvp => kvp.Value.Select(v => new KeyValuePair<Identifier, string>(kvp.Key, v.String)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return allTexts
|
||||
.SelectMany(kvp => kvp.Value.Select(v => new KeyValuePair<Identifier, string>(kvp.Key, v.String)));
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetTextFiles()
|
||||
@@ -213,13 +239,21 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
public static LocalizedString Get(params Identifier[] tags)
|
||||
{
|
||||
if (tags.Length == 1)
|
||||
{
|
||||
return Get(tags[0]);
|
||||
}
|
||||
return new TagLString(tags);
|
||||
}
|
||||
|
||||
public static LocalizedString Get(Identifier tag)
|
||||
{
|
||||
TagLString? str = null;
|
||||
lock (cachedStrings)
|
||||
{
|
||||
if (tags.Length == 1 && !nonCacheableTags.Contains(tags[0]))
|
||||
if (!nonCacheableTags.Contains(tag))
|
||||
{
|
||||
var tag = tags[0];
|
||||
if (cachedStrings.TryGetValue(tag, out var strRef))
|
||||
{
|
||||
if (!strRef.TryGetTarget(out str))
|
||||
@@ -246,15 +280,18 @@ namespace Barotrauma
|
||||
}
|
||||
else
|
||||
{
|
||||
str = new TagLString(tags);
|
||||
str = new TagLString(tag);
|
||||
cachedStrings.Add(tag, new WeakReference<TagLString>(str));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return str ?? new TagLString(tags);
|
||||
return str ?? new TagLString(tag);
|
||||
}
|
||||
|
||||
|
||||
public static LocalizedString Get(string tag)
|
||||
=> Get(tag.ToIdentifier());
|
||||
|
||||
public static LocalizedString Get(params string[] tags)
|
||||
=> Get(tags.ToIdentifiers());
|
||||
|
||||
|
||||
@@ -48,7 +48,13 @@ namespace Barotrauma
|
||||
|
||||
public readonly LanguageIdentifier Language;
|
||||
|
||||
public readonly ImmutableDictionary<Identifier, ImmutableArray<string>> Texts;
|
||||
|
||||
public readonly record struct Text(
|
||||
string String,
|
||||
bool IsOverride,
|
||||
TextPack TextPack);
|
||||
|
||||
public readonly ImmutableDictionary<Identifier, ImmutableArray<Text>> Texts;
|
||||
public readonly string TranslatedName;
|
||||
public readonly bool NoWhitespace;
|
||||
|
||||
@@ -56,24 +62,47 @@ namespace Barotrauma
|
||||
{
|
||||
ContentFile = file;
|
||||
|
||||
var languageName = mainElement.GetAttributeIdentifier("language", TextManager.DefaultLanguage.Value);
|
||||
var languageName = mainElement.GetAttributeIdentifier("language", Identifier.Empty);
|
||||
if (languageName.IsEmpty)
|
||||
{
|
||||
DebugConsole.AddWarning($"Language not defined in text file \"{file.Path}\". Setting the language as {TextManager.DefaultLanguage}.",
|
||||
mainElement.ContentPackage);
|
||||
languageName = TextManager.DefaultLanguage.Value;
|
||||
}
|
||||
Language = language;
|
||||
TranslatedName = mainElement.GetAttributeString("translatedname", languageName.Value);
|
||||
NoWhitespace = mainElement.GetAttributeBool("nowhitespace", false);
|
||||
|
||||
Dictionary<Identifier, List<string>> texts = new Dictionary<Identifier, List<string>>();
|
||||
foreach (var element in mainElement.Elements())
|
||||
Dictionary<Identifier, List<Text>> texts = new Dictionary<Identifier, List<Text>>();
|
||||
LoadElements(mainElement, isOverride: mainElement.IsOverride());
|
||||
|
||||
void LoadElements(XElement parentElement, bool isOverride)
|
||||
{
|
||||
Identifier elemName = element.NameAsIdentifier();
|
||||
if (!texts.ContainsKey(elemName)) { texts.Add(elemName, new List<string>()); }
|
||||
texts[elemName].Add(element.ElementInnerText()
|
||||
.Replace(@"\n", "\n")
|
||||
.Replace("&", "&")
|
||||
.Replace("<", "<")
|
||||
.Replace(">", ">")
|
||||
.Replace(""", "\"")
|
||||
.Replace("'", "'"));
|
||||
foreach (var element in parentElement.Elements())
|
||||
{
|
||||
Identifier elemName = element.NameAsIdentifier();
|
||||
|
||||
if (element.IsOverride())
|
||||
{
|
||||
LoadElements(element, isOverride: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!texts.ContainsKey(elemName)) { texts.Add(elemName, new List<Text>()); }
|
||||
|
||||
string str = element.ElementInnerText()
|
||||
.Replace(@"\n", "\n")
|
||||
.Replace("&", "&")
|
||||
.Replace("<", "<")
|
||||
.Replace(">", ">")
|
||||
.Replace(""", "\"")
|
||||
.Replace("'", "'");
|
||||
|
||||
texts[elemName].Add(new Text(str, isOverride, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Texts = texts.Select(kvp => (kvp.Key, kvp.Value.ToImmutableArray())).ToImmutableDictionary();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user