Client LuaCsForBarotrauma
SplitLString.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 #nullable enable
7 namespace Barotrauma
8 {
9  public class LStringSplitter
10  {
11  public IReadOnlyList<LocalizedString> Substrings => substrings;
12 
13  private class SubstringList : IReadOnlyList<LocalizedString>
14  {
15  public SubstringList(LStringSplitter splitter) { this.splitter = splitter; }
16 
17  private LStringSplitter splitter;
18  private readonly List<LocalizedString> underlyingList = new List<LocalizedString>();
19 
20  public List<LocalizedString> UnderlyingList
21  {
22  get
23  {
24  splitter.UpdateSubstrings();
25  return underlyingList;
26  }
27  }
28 
29  public IEnumerator<LocalizedString> GetEnumerator() => UnderlyingList.GetEnumerator();
30 
31  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
32 
33  public int Count => UnderlyingList.Count;
34 
35  public LocalizedString this[int index] => UnderlyingList[index];
36  }
37 
38  private readonly SubstringList substrings;
39  private readonly char[] separators;
40  private readonly LocalizedString originalString;
41  private string[] substrValues;
42 
43  private string cachedOriginal;
44 
45  public bool Loaded => originalString.Loaded;
46 
47  public LStringSplitter(LocalizedString input, params char[] separators)
48  {
49  originalString = input;
50  substrings = new SubstringList(this);
51  substrValues = Array.Empty<string>();
52  this.separators = separators;
53  cachedOriginal = "";
54  }
55 
56  private void UpdateSubstrings()
57  {
58  if (originalString.Value != cachedOriginal)
59  {
60  cachedOriginal = originalString.Value;
61  substrValues = cachedOriginal.Split(separators);
62  substrings.UnderlyingList.Clear();
63  substrings.UnderlyingList.AddRange(Enumerable.Range(0, substrValues.Length).Select(i => new SplitLString(this, i) as LocalizedString));
64  }
65  }
66 
67  public string GetValue(int index)
68  {
69  UpdateSubstrings();
70  return substrValues[index];
71  }
72  }
73 
75  {
76  private bool loaded = false;
77  private readonly LStringSplitter splitter;
78  private readonly int index;
79 
80  public SplitLString(LStringSplitter splitter, int index)
81  {
82  this.splitter = splitter; this.index = index;
83  }
84 
85  public override bool Loaded => loaded && splitter.Loaded;
86  public override void RetrieveValue()
87  {
88  loaded = true;
89  cachedValue = splitter.GetValue(index);
91  }
92  }
93 }
LStringSplitter(LocalizedString input, params char[] separators)
Definition: SplitLString.cs:47
string GetValue(int index)
Definition: SplitLString.cs:67
IReadOnlyList< LocalizedString > Substrings
Definition: SplitLString.cs:11
override void RetrieveValue()
Definition: SplitLString.cs:86
SplitLString(LStringSplitter splitter, int index)
Definition: SplitLString.cs:80
override bool Loaded
Definition: SplitLString.cs:85