Client LuaCsForBarotrauma
TextFile.cs
1 using System.Collections.Immutable;
2 using System.Linq;
3 using System.Xml.Linq;
4 
5 namespace Barotrauma
6 {
7  [NotSyncedInMultiplayer]
8  public sealed class TextFile : ContentFile
9  {
10  public TextFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
11 
12  public override void LoadFile()
13  {
14  XDocument doc = XMLExtensions.TryLoadXml(Path);
15  var mainElement = doc.Root.FromPackage(ContentPackage);
16 
17  var languageName = mainElement.GetAttributeIdentifier("language", TextManager.DefaultLanguage.Value);
18 
19  LanguageIdentifier language = languageName.ToLanguageIdentifier();
20  if (!TextManager.TextPacks.ContainsKey(language))
21  {
22  TextManager.TextPacks.TryAdd(language, ImmutableList<TextPack>.Empty);
23  }
24  var newPack = new TextPack(this, mainElement, language);
25  var newList = TextManager.TextPacks[language].Add(newPack);
26  TextManager.TextPacks.TryRemove(language, out _);
27  TextManager.TextPacks.TryAdd(language, newList);
28  TextManager.IncrementLanguageVersion();
29  }
30 
31  public override void UnloadFile()
32  {
33  foreach (var kvp in TextManager.TextPacks.ToArray())
34  {
35  var newList = kvp.Value.Where(p => p.ContentFile != this).ToImmutableList();
36  TextManager.TextPacks.TryRemove(kvp.Key, out _);
37  if (newList.Count != 0) { TextManager.TextPacks.TryAdd(kvp.Key, newList); }
38  }
39  TextManager.IncrementLanguageVersion();
40  if (!TextManager.TextPacks.ContainsKey(GameSettings.CurrentConfig.Language) &&
41  GameSettings.CurrentConfig.Language != TextManager.DefaultLanguage)
42  {
43  DebugConsole.AddWarning($"The language {GameSettings.CurrentConfig.Language} is no longer available. Switching to {TextManager.DefaultLanguage}...");
44  var config = GameSettings.CurrentConfig;
45  config.Language = TextManager.DefaultLanguage;
46  GameSettings.SetCurrentConfig(config);
47  }
48  }
49 
50  public override void Sort()
51  {
52  foreach (var language in TextManager.TextPacks.Keys.ToList())
53  {
54  TextManager.TextPacks[language] =
55  TextManager.TextPacks[language].Sort((t1, t2) => (t1.ContentFile.ContentPackage?.Index ?? int.MaxValue) - (t2.ContentFile.ContentPackage?.Index ?? int.MaxValue));
56  }
57  }
58  }
59 }
Base class for content file types, which are loaded from filelist.xml via reflection....
Definition: ContentFile.cs:23
TextFile(ContentPackage contentPackage, ContentPath path)
Definition: TextFile.cs:10
override void Sort()
Definition: TextFile.cs:50
override void UnloadFile()
Definition: TextFile.cs:31
override void LoadFile()
Definition: TextFile.cs:12